You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
2 months ago
|
/*
|
||
|
dfs
|
||
|
This problem requires you to implement a basic DFS traversal
|
||
|
*/
|
||
|
|
||
|
// I AM NOT DONE
|
||
|
use std::collections::HashSet;
|
||
|
|
||
|
struct Graph {
|
||
|
adj: Vec<Vec<usize>>,
|
||
|
}
|
||
|
|
||
|
impl Graph {
|
||
|
fn new(n: usize) -> Self {
|
||
|
Graph {
|
||
|
adj: vec![vec![]; n],
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn add_edge(&mut self, src: usize, dest: usize) {
|
||
|
self.adj[src].push(dest);
|
||
|
self.adj[dest].push(src);
|
||
|
}
|
||
|
|
||
|
fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
|
||
|
//TODO
|
||
|
}
|
||
|
|
||
|
// Perform a depth-first search on the graph, return the order of visited nodes
|
||
|
fn dfs(&self, start: usize) -> Vec<usize> {
|
||
|
let mut visited = HashSet::new();
|
||
|
let mut visit_order = Vec::new();
|
||
|
self.dfs_util(start, &mut visited, &mut visit_order);
|
||
|
visit_order
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn test_dfs_simple() {
|
||
|
let mut graph = Graph::new(3);
|
||
|
graph.add_edge(0, 1);
|
||
|
graph.add_edge(1, 2);
|
||
|
|
||
|
let visit_order = graph.dfs(0);
|
||
|
assert_eq!(visit_order, vec![0, 1, 2]);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_dfs_with_cycle() {
|
||
|
let mut graph = Graph::new(4);
|
||
|
graph.add_edge(0, 1);
|
||
|
graph.add_edge(0, 2);
|
||
|
graph.add_edge(1, 2);
|
||
|
graph.add_edge(2, 3);
|
||
|
graph.add_edge(3, 3);
|
||
|
|
||
|
let visit_order = graph.dfs(0);
|
||
|
assert_eq!(visit_order, vec![0, 1, 2, 3]);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_dfs_disconnected_graph() {
|
||
|
let mut graph = Graph::new(5);
|
||
|
graph.add_edge(0, 1);
|
||
|
graph.add_edge(0, 2);
|
||
|
graph.add_edge(3, 4);
|
||
|
|
||
|
let visit_order = graph.dfs(0);
|
||
|
assert_eq!(visit_order, vec![0, 1, 2]);
|
||
|
let visit_order_disconnected = graph.dfs(3);
|
||
|
assert_eq!(visit_order_disconnected, vec![3, 4]);
|
||
|
}
|
||
|
}
|
||
|
|