Write a Python function that visits every node reachable from a specified starting node in an undirected graph. The graph is provided as an adjacency list, and your implementation may use either breadth-first search (BFS) or depth-first search (DFS).
Return the visited nodes as a list. Do not call graph-traversal utilities from Python's standard library.
Use this function signature:
def traverse_graph(adj_list, start):
pass
Input:
[[1, 2], [0, 3], [0, 4], [1], [2]]0[0, 1, 2, 3, 4]0 in a valid traversal order.Input:
[[1, 3], [0, 2], [1], [0]]0[0, 1, 3, 2]Input:
{"adj_list": [[1, 2], [0, 3], [0, 4], [1], [2]], "start": 0}
Output:
[0, 1, 2, 3, 4]
adj_list = [[1,2], [0, 3], [0, 4], [1], [2]] start = 0
[0,1, 2, 3, 4]
We are given an undirected graph with 5 nodes (0 through 4) and a starting node 0. The adjacency list shows each node's neighbors.
The traversal begins at node 0 and reaches all five nodes in the graph.
adj_list = [[1,2], [0, 3], [0, 4], [1], [2]] start = 0
[0,1, 2, 3, 4]
We are given an undirected graph with 5 nodes (0 through 4) and a starting node 0. The adjacency list shows each node's neighbors.