Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
destinyFvcker committed Apr 12, 2024
1 parent 1ea671b commit 4e329df
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 173 deletions.
40 changes: 28 additions & 12 deletions exercises/algorithm/algorithm5.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
bfs
This problem requires you to implement a basic BFS algorithm
bfs
This problem requires you to implement a basic BFS algorithm
*/

//I AM NOT DONE
use std::collections::VecDeque;
use std::{collections::VecDeque, io::Seek};

// Define a graph
struct Graph {
adj: Vec<Vec<usize>>,
adj: Vec<Vec<usize>>,
}

impl Graph {
Expand All @@ -21,21 +20,39 @@ impl Graph {

// 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);
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 queue = VecDeque::new();
let mut visit_order = vec![];

queue.push_back(start);
visit_order.push(start);
// for (i, vertex) in self.adj.iter().enumerate() {
// for (j, edge) in vertex.iter().enumerate() {}
// }
loop {
let next = queue.pop_front();

if let Some(current_vertex) = next {
for &dest in &self.adj[current_vertex] {
if !visit_order.contains(&dest) {
visit_order.push(dest);
queue.push_back(dest);
}
}
} else {
break;
}
}

visit_order
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -84,4 +101,3 @@ mod tests {
assert_eq!(visited_order, vec![0]);
}
}

28 changes: 16 additions & 12 deletions exercises/algorithm/algorithm6.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
dfs
This problem requires you to implement a basic DFS traversal
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>>,
adj: Vec<Vec<usize>>,
}

impl Graph {
Expand All @@ -19,17 +18,23 @@ impl Graph {

fn add_edge(&mut self, src: usize, dest: usize) {
self.adj[src].push(dest);
self.adj[dest].push(src);
self.adj[dest].push(src);
}

fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
//TODO
visit_order.push(v);
for &dest in &self.adj[v] {
if visited.insert(dest) {
self.dfs_util(dest, visited, visit_order);
}
}
}

// 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();
visited.insert(start);
let mut visit_order = Vec::new();
self.dfs_util(start, &mut visited, &mut visit_order);
visit_order
}
Expand All @@ -56,7 +61,7 @@ mod tests {
graph.add_edge(0, 2);
graph.add_edge(1, 2);
graph.add_edge(2, 3);
graph.add_edge(3, 3);
graph.add_edge(3, 3);

let visit_order = graph.dfs(0);
assert_eq!(visit_order, vec![0, 1, 2, 3]);
Expand All @@ -67,12 +72,11 @@ mod tests {
let mut graph = Graph::new(5);
graph.add_edge(0, 1);
graph.add_edge(0, 2);
graph.add_edge(3, 4);
graph.add_edge(3, 4);

let visit_order = graph.dfs(0);
assert_eq!(visit_order, vec![0, 1, 2]);
assert_eq!(visit_order, vec![0, 1, 2]);
let visit_order_disconnected = graph.dfs(3);
assert_eq!(visit_order_disconnected, vec![3, 4]);
assert_eq!(visit_order_disconnected, vec![3, 4]);
}
}

Loading

0 comments on commit 4e329df

Please sign in to comment.