iterates over graph correctly