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.

88 lines
1.9 KiB

/*
bfs
This problem requires you to implement a basic BFS algorithm
*/
//I AM NOT DONE
use std::collections::VecDeque;
// Define a graph
struct Graph {
adj: Vec<Vec<usize>>,
}
impl Graph {
// Create a new graph with n vertices
fn new(n: usize) -> Self {
Graph {
adj: vec![vec![]; n],
}
}
// Add an edge to the graph
fn add_edge(&mut self, src: usize, dest: usize) {
self.adj[src].push(dest);
self.adj[dest].push(src);
}
// Perform a breadth-first search on the graph, return the order of visited nodes
fn bfs_with_return(&self, start: usize) -> Vec<usize> {
//TODO
let mut visit_order = vec![];
visit_order
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bfs_all_nodes_visited() {
let mut graph = Graph::new(5);
graph.add_edge(0, 1);
graph.add_edge(0, 4);
graph.add_edge(1, 2);
graph.add_edge(1, 3);
graph.add_edge(1, 4);
graph.add_edge(2, 3);
graph.add_edge(3, 4);
let visited_order = graph.bfs_with_return(0);
assert_eq!(visited_order, vec![0, 1, 4, 2, 3]);
}
#[test]
fn test_bfs_different_start() {
let mut graph = Graph::new(3);
graph.add_edge(0, 1);
graph.add_edge(1, 2);
let visited_order = graph.bfs_with_return(2);
assert_eq!(visited_order, vec![2, 1, 0]);
}
#[test]
fn test_bfs_with_cycle() {
let mut graph = Graph::new(3);
graph.add_edge(0, 1);
graph.add_edge(1, 2);
graph.add_edge(2, 0);
let visited_order = graph.bfs_with_return(0);
assert_eq!(visited_order, vec![0, 1, 2]);
}
#[test]
fn test_bfs_single_node() {
let mut graph = Graph::new(1);
let visited_order = graph.bfs_with_return(0);
assert_eq!(visited_order, vec![0]);
}
}