changed BFS to a more understandable code and O(V+E)