using a deque instead of modifyng the graph