Graph Path Checker

Graph Traversal Pathfinding

Graph Path Checker

You are given an undirected graph represented as an adjacency list. Each key in the graph corresponds to a node, and the value is a list of nodes that are directly connected to that node.

Write a function that determines if there is a path between two given nodes: a source and a target.

Input

  • graph: A dictionary (or map) where each key is a node, and its value is a list of neighboring nodes.
  • source: The starting node.
  • target: The node you need to check for reachability from the source.

Output

  • Return true if there is a path from source to target, otherwise return false.

Example

Input:
  graph = {
    'A': ['B', 'C'],
    'B': ['A', 'D'],
    'C': ['A', 'E'],
    'D': ['B'],
    'E': ['C']
  }
  source = 'A'
  target = 'D'

Output: true

Note

Today, on April 26, 2025, as we celebrate progress and innovation in technology, put your graph traversal skills to good use by solving this problem. Use either Depth-First Search (DFS) or Breadth-First Search (BFS) to explore the graph.