What is the output of the DFS traversal starting from node 0?
use std::collections::HashMap;
fn dfs(graph: &HashMap<i32, Vec<i32>>, start: i32, visited: &mut Vec<i32>, result: &mut Vec<i32>) {
if visited.contains(&start) {
return;
}
visited.push(start);
result.push(start);
if let Some(neighbors) = graph.get(&start) {
for &node in neighbors {
dfs(graph, node, visited, result);
}
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1, 2]);
graph.insert(1, vec![3]);
graph.insert(2, vec![3]);
graph.insert(3, vec![]);
let mut visited = Vec::new();
let mut result = Vec::new();
dfs(&graph, 0, &mut visited, &mut result);
println!("{:?}", result);
}DFS explores as far as possible along each branch before backtracking.
Starting at 0, DFS visits 1 first, then 3 from 1, then backtracks to 0 and visits 2, which also leads to 3 but 3 is already visited.
Why is it important to track visited nodes in DFS on a graph?
Think about what happens if the graph has cycles.
Tracking visited nodes prevents revisiting the same node multiple times, which can cause infinite loops in graphs with cycles.
What is the output of DFS starting from node 0 on this disconnected graph?
use std::collections::HashMap;
fn dfs(graph: &HashMap<i32, Vec<i32>>, start: i32, visited: &mut Vec<i32>, result: &mut Vec<i32>) {
if visited.contains(&start) {
return;
}
visited.push(start);
result.push(start);
if let Some(neighbors) = graph.get(&start) {
for &node in neighbors {
dfs(graph, node, visited, result);
}
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1]);
graph.insert(1, vec![]);
graph.insert(2, vec![3]);
graph.insert(3, vec![]);
let mut visited = Vec::new();
let mut result = Vec::new();
dfs(&graph, 0, &mut visited, &mut result);
println!("{:?}", result);
}DFS only visits nodes reachable from the start node.
Nodes 2 and 3 are disconnected from 0, so DFS starting at 0 visits only 0 and 1.
What error will this DFS code produce when run?
use std::collections::HashMap;
fn dfs(graph: &HashMap<i32, Vec<i32>>, start: i32, visited: &mut Vec<i32>, result: &mut Vec<i32>) {
if visited.contains(&start) {
return;
}
visited.push(start);
result.push(start);
for node in graph.get(&start).unwrap() {
dfs(graph, *node, visited, result);
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1]);
graph.insert(1, vec![2]);
// Node 2 is missing from graph keys
let mut visited = Vec::new();
let mut result = Vec::new();
dfs(&graph, 0, &mut visited, &mut result);
println!("{:?}", result);
}Check what happens when a node is not present in the graph keys.
Calling unwrap() on None when node 2 is missing causes a runtime panic.
Given an undirected graph represented as adjacency list, what is the number of connected components?
Graph edges: 0-1, 1-2, 3-4
use std::collections::{HashMap, HashSet};
fn dfs(node: i32, graph: &HashMap<i32, Vec<i32>>, visited: &mut HashSet<i32>) {
if visited.contains(&node) {
return;
}
visited.insert(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
dfs(neighbor, graph, visited);
}
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1]);
graph.insert(1, vec![0, 2]);
graph.insert(2, vec![1]);
graph.insert(3, vec![4]);
graph.insert(4, vec![3]);
let mut visited = HashSet::new();
let mut count = 0;
for &node in graph.keys() {
if !visited.contains(&node) {
dfs(node, &graph, &mut visited);
count += 1;
}
}
println!("{}", count);
}Count how many times DFS starts on an unvisited node.
There are two connected components: {0,1,2} and {3,4}.
