use std::collections::{HashMap, HashSet};
fn dfs(node: usize, graph: &HashMap<usize, Vec<usize>>, visited: &mut HashSet<usize>, stack: &mut Vec<usize>) {
if visited.contains(&node) {
return;
}
visited.insert(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
dfs(neighbor, graph, visited, stack);
}
}
stack.push(node);
}
fn topological_sort(graph: &HashMap<usize, Vec<usize>>) -> Vec<usize> {
let mut visited = HashSet::new();
let mut stack = Vec::new();
for &node in graph.keys() {
dfs(node, graph, &mut visited, &mut stack);
}
stack.reverse();
stack
}
fn main() {
let mut graph = HashMap::new();
graph.insert(5, vec![2, 0]);
graph.insert(4, vec![0, 1]);
graph.insert(2, vec![3]);
graph.insert(3, vec![1]);
graph.insert(0, vec![]);
graph.insert(1, vec![]);
let order = topological_sort(&graph);
println!("{:?}", order);
}The DFS visits nodes starting from 5 and 4. Node 5 leads to 2 and 0, and 2 leads to 3, which leads to 1. Node 4 leads to 0 and 1. The final reversed stack gives the order [5, 4, 2, 3, 1, 0].
use std::collections::{HashMap, HashSet};
fn dfs(node: usize, graph: &HashMap<usize, Vec<usize>>, visited: &mut HashSet<usize>, stack: &mut Vec<usize>, rec_stack: &mut HashSet<usize>) -> bool {
if rec_stack.contains(&node) {
return true; // cycle detected
}
if visited.contains(&node) {
return false;
}
visited.insert(node);
rec_stack.insert(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
if dfs(neighbor, graph, visited, stack, rec_stack) {
return true;
}
}
}
rec_stack.remove(&node);
stack.push(node);
false
}
fn topological_sort(graph: &HashMap<usize, Vec<usize>>) -> Result<Vec<usize>, &'static str> {
let mut visited = HashSet::new();
let mut stack = Vec::new();
let mut rec_stack = HashSet::new();
for &node in graph.keys() {
if dfs(node, graph, &mut visited, &mut stack, &mut rec_stack) {
return Err("Cycle detected, topological sort not possible");
}
}
stack.reverse();
Ok(stack)
}
fn main() {
let mut graph = HashMap::new();
graph.insert(1, vec![2]);
graph.insert(2, vec![3]);
graph.insert(3, vec![1]);
match topological_sort(&graph) {
Ok(order) => println!("{:?}", order),
Err(e) => println!("{}", e),
}
}The code detects a cycle because node 1 leads to 2, 2 leads to 3, and 3 leads back to 1. The recursion stack detects this cycle and returns an error message.
use std::collections::{HashMap, HashSet};
fn dfs(node: usize, graph: &HashMap<usize, Vec<usize>>, visited: &mut HashSet<usize>, stack: &mut Vec<usize>) {
if visited.contains(&node) {
return;
}
visited.insert(node);
stack.push(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
dfs(neighbor, graph, visited, stack);
}
}
}
fn topological_sort(graph: &HashMap<usize, Vec<usize>>) -> Vec<usize> {
let mut visited = HashSet::new();
let mut stack = Vec::new();
for &node in graph.keys() {
dfs(node, graph, &mut visited, &mut stack);
}
stack
}
fn main() {
let mut graph = HashMap::new();
graph.insert(1, vec![2]);
graph.insert(2, vec![3]);
graph.insert(3, vec![]);
let order = topological_sort(&graph);
println!("{:?}", order);
}In topological sort using DFS, nodes must be added to the stack only after all their neighbors are visited. Here, nodes are pushed before visiting neighbors, causing incorrect order.
The recursion stack tracks nodes currently being explored. If during DFS we revisit a node in this stack, it means a cycle exists (a back edge). This is essential to detect cycles in directed graphs.
use std::collections::{HashMap, HashSet};
fn dfs(node: usize, graph: &HashMap<usize, Vec<usize>>, visited: &mut HashSet<usize>, stack: &mut Vec<usize>) {
if visited.contains(&node) {
return;
}
visited.insert(node);
if let Some(neighbors) = graph.get(&node) {
for &neighbor in neighbors {
dfs(neighbor, graph, visited, stack);
}
}
stack.push(node);
}
fn topological_sort(graph: &HashMap<usize, Vec<usize>>) -> Vec<usize> {
let mut visited = HashSet::new();
let mut stack = Vec::new();
for &node in graph.keys() {
dfs(node, graph, &mut visited, &mut stack);
}
stack.reverse();
stack
}
fn main() {
let mut graph = HashMap::new();
graph.insert(7, vec![6, 5]);
graph.insert(6, vec![4]);
graph.insert(5, vec![4]);
graph.insert(4, vec![3]);
graph.insert(3, vec![2]);
graph.insert(2, vec![1]);
graph.insert(1, vec![0]);
graph.insert(0, vec![]);
let order = topological_sort(&graph);
println!("{:?}", order);
}Starting from node 7, DFS visits 6 then 4 then 3 then 2 then 1 then 0, pushing nodes after visiting neighbors. Node 5 is visited after 6, also leading to 4 but already visited. The reversed stack is [7, 6, 5, 4, 3, 2, 1, 0].
