📖 Scenario: You are exploring a small map of connected cities. Each city is linked to some other cities by roads. You want to visit all cities starting from a specific city, going as deep as possible before backtracking. This is like exploring a maze by always going forward until you cannot go further, then stepping back.
🎯 Goal: Build a program that uses Depth First Search (DFS) to visit all cities in the map starting from city 0. You will create the graph, set up a visited list, write the DFS function, and print the order of cities visited.
📋 What You'll Learn
Create a graph as an adjacency list using a vector of vectors of usize
Create a vector called visited to track visited cities
Write a recursive DFS function named dfs that takes the graph, visited vector, and current city index
Print the order of cities visited during DFS
💡 Why This Matters
🌍 Real World
DFS is used in many real-world problems like maze solving, network traversal, and finding connected components.
💼 Career
Understanding DFS is essential for software engineers working on algorithms, game development, and systems programming.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a variable called graph as a vector of vectors of usize with these exact connections: city 0 connects to cities 1 and 2, city 1 connects to city 2, city 2 connects to city 0 and 3, city 3 connects to city 3.
DSA Rust
Hint
Think of graph as a list where each city has a list of cities it connects to.
2
Create the visited vector
Create a mutable vector called visited of type Vec with length 4, initialized with false values.
DSA Rust
Hint
This vector keeps track of which cities you have already visited.
3
Write the DFS function
Write a recursive function called dfs that takes graph: &Vec>, visited: &mut Vec, and city: usize. Inside, mark visited[city] as true. Then, for each neighbor in graph[city], if it is not visited, call dfs recursively on that neighbor. Print the current city inside the function after marking it visited.
DSA Rust
Hint
Mark the city visited, print it, then visit all neighbors recursively if not visited.
4
Call DFS and print the visit order
Call the dfs function with &graph, &mut visited, and starting city 0. This will print the order of cities visited.
DSA Rust
Hint
Just call dfs(&graph, &mut visited, 0); to start visiting from city 0.
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 nodes at the current depth before moving deeper
C. Visit nodes randomly without any order
D. Only visit nodes with the highest value first
Solution
Step 1: Understand DFS traversal
DFS explores a graph by going deep into one branch before exploring others.
Step 2: Compare with other traversal methods
BFS visits nodes level by level, but DFS goes deep first.
Final Answer:
Explore as far as possible along each branch before backtracking -> Option A
Quick Check:
DFS = deep exploration first [OK]
Hint: DFS means go deep first, then backtrack [OK]
Common Mistakes:
Confusing DFS with BFS
Thinking DFS visits nodes level-wise
Assuming DFS visits nodes randomly
2. Which of the following is the correct Rust function signature for a recursive DFS function on a graph represented as adjacency list?
easy
A. fn dfs(graph: &mut Vec, node: usize, visited: Vec<bool>)
B. fn dfs(graph: Vec, node: usize, visited: Vec<bool>)
C. fn dfs(graph: &Vec, node: usize, visited: &mut Vec<bool>)
D. fn dfs(graph: Vec, node: usize, visited: &mut Vec<bool>)
Solution
Step 1: Understand parameter passing in Rust for DFS
The graph should be passed by reference to avoid copying, and visited vector should be mutable reference to mark nodes visited.
Step 2: Check each option
fn dfs(graph: &Vec, node: usize, visited: &mut Vec<bool>) passes graph by reference and visited as mutable reference, which is correct for DFS recursion.