📖 Scenario: Imagine you have a map of cities connected by roads. You want to explore all cities starting from one city by traveling along the roads. This is like exploring a graph using Depth First Search (DFS).
🎯 Goal: You will build a simple graph using an adjacency list and write a DFS function to visit all connected cities starting from a given city.
📋 What You'll Learn
Create a graph as an adjacency list with exact cities and connections
Create a visited set to keep track of visited cities
Write a DFS function that visits cities recursively
Print the order of cities visited starting from 'A'
💡 Why This Matters
🌍 Real World
Graph traversal like DFS is used in maps, social networks, and solving puzzles by exploring connected points.
💼 Career
Understanding DFS is essential for software engineers working on algorithms, network analysis, and game development.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a variable called graph as an object with these exact entries: 'A': ['B', 'C'], 'B': ['D'], 'C': ['E'], 'D': [], 'E': []
DSA Typescript
Hint
Use an object where keys are city names and values are arrays of connected cities.
2
Create a visited set to track visited cities
Create a variable called visited as a new empty Set<string>()
DSA Typescript
Hint
Use new Set<string>() to create an empty set for visited cities.
3
Write the DFS function to visit cities recursively
Write a function called dfs that takes a city: string. Inside, add city to visited. Then use for loop with neighbor over graph[city]. If neighbor is not in visited, call dfs(neighbor) recursively.
DSA Typescript
Hint
Use recursion to visit neighbors that are not yet visited.
4
Print the order of cities visited starting from 'A'
Call dfs('A') to start DFS from city 'A'. Then print the visited cities as an array using console.log(Array.from(visited))
DSA Typescript
Hint
Call dfs('A') and then print the visited cities as an array.
Practice
(1/5)
1. What is the main idea behind Depth First Search (DFS) on a graph?
easy
A. Explore as far as possible along each branch before backtracking
B. Visit all neighbors of a node before moving deeper
C. Visit nodes in order of their distance from the start node
D. Randomly visit nodes until all are visited
Solution
Step 1: Understand DFS traversal strategy
DFS explores nodes by going deep into one branch before backtracking to explore other branches.
Step 2: Compare with other traversal methods
BFS visits neighbors first, but DFS goes deep first, matching Explore as far as possible along each branch before backtracking.
Final Answer:
Explore as far as possible along each branch before backtracking -> Option A
Quick Check:
DFS = deep exploration first [OK]
Hint: DFS goes deep first, BFS goes wide first [OK]
Common Mistakes:
Confusing DFS with BFS
Thinking DFS visits neighbors first
Assuming DFS visits nodes by distance
2. Which of the following is the correct TypeScript function signature for a DFS function on a graph represented as an adjacency list?
easy
A. function dfs(graph: Map, start: number): void
B. function dfs(graph: number[], start: number): void
C. function dfs(graph: number[][], start: number): void
D. function dfs(graph: string[][], start: string): void
Solution
Step 1: Identify graph representation type
An adjacency list for a graph with numbered nodes is usually number[][], where each index holds neighbors.
Step 2: Check parameter types
Start node is a number, so function signature with graph: number[][] and start: number is correct.
Final Answer:
function dfs(graph: number[][], start: number): void -> Option C
Quick Check:
Adjacency list = number[][], start node = number [OK]
Hint: Adjacency list uses array of arrays for neighbors [OK]
Common Mistakes:
Using number[] instead of number[][] for adjacency list
Using Map with incorrect value type
Using string types for numeric graphs
3. What is the output of the following DFS traversal starting from node 0?