📖 Scenario: You are working on a navigation app that helps users find the shortest route between places in a city. The city map is represented as a graph where intersections are nodes and roads are edges. Since all roads take the same time to cross, you want to find the shortest path from one intersection to another using a simple method.
🎯 Goal: Build a program that finds the shortest path between two intersections in an unweighted graph using Breadth-First Search (BFS). You will create the graph, set the start and end points, run BFS to find the shortest path, and then print the path.
📋 What You'll Learn
Create a graph as an adjacency list with exact nodes and edges
Set start and end nodes for the path search
Implement BFS to find the shortest path
Print the shortest path as a list of nodes
💡 Why This Matters
🌍 Real World
Finding shortest routes in maps, social networks, or any system where connections have equal cost.
💼 Career
Graph traversal and shortest path algorithms are fundamental in software engineering, especially in navigation, networking, and recommendation systems.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a variable called graph as an object representing the city map with these exact connections: 'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E']
DSA Javascript
Hint
Use an object where keys are node names and values are arrays of connected nodes.
2
Set the start and end nodes
Create two variables called startNode and endNode and set them to 'A' and 'F' respectively.
DSA Javascript
Hint
Just create two constants with the exact names and values.
3
Implement BFS to find the shortest path
Write a function called findShortestPath that takes graph, startNode, and endNode as parameters and returns an array of nodes representing the shortest path from startNode to endNode using Breadth-First Search (BFS). Use a queue, a visited set, and a predecessors object to track the path.
DSA Javascript
Hint
Use a queue to explore nodes level by level, track visited nodes to avoid repeats, and store predecessors to build the path back.
4
Print the shortest path
Call findShortestPath(graph, startNode, endNode) and print the returned array using console.log.
DSA Javascript
Hint
Call the function with the graph, startNode, and endNode, then print the result.
Practice
(1/5)
1. What does BFS (Breadth-First Search) help find in an unweighted graph?
easy
A. The longest path between two nodes
B. The node with the highest degree
C. The total number of nodes in the graph
D. The shortest path from a start node to all other nodes
Solution
Step 1: Understand BFS traversal
BFS explores nodes level by level starting from the source node, visiting all neighbors before moving deeper.
Step 2: BFS finds shortest paths in unweighted graphs
Because BFS visits nodes in order of increasing distance, the first time it reaches a node is via the shortest path.
Final Answer:
The shortest path from a start node to all other nodes -> Option D
Quick Check:
BFS = shortest path in unweighted graph [OK]
Hint: BFS visits nodes by distance layers to find shortest paths [OK]
Common Mistakes:
Confusing BFS with DFS for shortest path
Thinking BFS finds longest path
Assuming BFS counts total nodes only
2. Which JavaScript data structure is best to implement the BFS queue for shortest path?
easy
A. Map to store node distances
B. Array with push and shift methods
C. Set to store unique nodes
D. Object with key-value pairs
Solution
Step 1: Identify BFS queue operations
BFS requires adding nodes at the end and removing from the front, which is a queue behavior.
Step 2: Use Array methods for queue
JavaScript arrays support push (add end) and shift (remove front), perfect for BFS queue.
Final Answer:
Array with push and shift methods -> Option B
Quick Check:
Queue = Array push/shift [OK]
Hint: Use array push/shift for BFS queue in JavaScript [OK]
Common Mistakes:
Using Set as queue (no order guaranteed)
Using Map for queue operations
Using Object which has no order
3. What is the output distance array after running BFS from node 0 on this graph?