5 of 7
Write a function find_path(num_nodes, edges, start, end) that:
start to end as a list of nodes[]The graph is directed β you can only follow edges in their given direction.
print(find_path(5, [[0,1], [0,2], [1,3], [2,3], [3,4]], 0, 4)) # Output: [0, 1, 3, 4] # (or [0, 2, 3, 4] β any valid path is accepted) print(find_path(4, [[0,1], [1,2], [2,3]], 0, 3)) # Output: [0, 1, 2, 3] print(find_path(4, [[0,1], [2,3]], 0, 3)) # Output: [] # (no path from 0 to 3) print(find_path(3, [[0,1], [1,2], [2,0]], 0, 2)) # Output: [0, 1, 2] print(find_path(4, [[0,1], [0,2], [1,3], [2,3]], 1, 0)) # Output: [] # (no path from 1 back to 0 β directed graph)
| Complexity | |
|---|---|
| Time | O(V + E) |
| Space | O(V + E) |
Where V is the number of nodes and E is the number of edges.
start, keeping track of the current pathend, return the pathvisited set to avoid revisiting nodes[]def find_path(num_nodes, edges, start, end): # Your code here pass # Test your function: print(find_path(5, [[0,1], [0,2], [1,3], [2,3], [3,4]], 0, 4)) # Output: [0, 1, 3, 4] or [0, 2, 3, 4] print(find_path(4, [[0,1], [1,2], [2,3]], 0, 3)) # Output: [0, 1, 2, 3] print(find_path(4, [[0,1], [2,3]], 0, 3)) # Output: [] print(find_path(3, [[0,1], [1,2], [2,0]], 0, 2)) # Output: [0, 1, 2] print(find_path(4, [[0,1], [0,2], [1,3], [2,3]], 1, 0)) # Output: []