Reachable Nodes

Graph Traversal Dfs Bfs Algorithm Coding Problem

Problem: Reachable Nodes

On March 15th, the Ides of March, we are reminded of the importance of seeing all possible outcomes. In a graph, overlooking a node might lead to missing vital information. Your task is to implement a function that, given an undirected graph and a starting node, returns all nodes that are reachable from the starting node.

Details

  • The graph is represented as an adjacency list. For example, a graph could be represented as:
    {
      'A': ['B', 'C'],
      'B': ['A', 'D'],
      'C': ['A'],
      'D': ['B']
    }
    
  • The graph is undirected, meaning if node A appears in node B's neighbor list, B will appear in A's list as well.
  • The function should return an array/list of nodes that are reachable from the starting node. The order of the nodes in the result is not important.
  • You can use either Breadth-First Search (BFS) or Depth-First Search (DFS) to traverse the graph.

Function Signature

Implement the following function:

  • Input:
    • graph: The graph represented as an adjacency list (e.g., a dictionary or map).
    • start: The starting node.
  • Output: A collection (list/array) of nodes reachable from the start node, including the start node.

Good luck, and remember: just as in historical events, every node has its role in the network!