Shortest Path Finder

Graph Traversal Bfs Algorithms Coding Interview

Problem Description

You are given an undirected graph represented as an adjacency list. The graph is non-weighted, and your task is to find the shortest path (measured in number of edges) between two specified nodes using a breadth-first search (BFS) traversal.

Input:

  • An adjacency list representing the graph. For example, a dictionary or map where the keys are node identifiers and the values are lists of neighboring node identifiers.
  • A start node and an end node.

Output:

  • An array (or list) representing the nodes in the shortest path from start to end (including both start and end). If there is no valid path between the two nodes, return an empty array.

Example:

Suppose the graph is represented as:

{
  'A': ['B', 'C'],
  'B': ['A', 'D'],
  'C': ['A', 'D'],
  'D': ['B', 'C', 'E'],
  'E': ['D']
}

Calling your function with start = 'A' and end = 'E' should return: ['A', 'B', 'D', 'E'] or ['A', 'C', 'D', 'E'] depending on your traversal order.

Note: April 13 marks several historical events; today, let it also mark a day for solving a classic graph traversal problem to celebrate progress in algorithmic thinking!

Instructions

Implement a function findShortestPath(graph, start, end) that solves the problem as described.

Starter Code

Below is some starter code in various languages to help you get started. You can use any language of your choice.