Skip to content

Commit

Permalink
implement dfs_scc_iterative
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Nov 19, 2024
1 parent 072c5ef commit 17c1f2b
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/graph/directed_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl DirectedGraph {
for (_, vertex_index) in reversed_topo {
if !visited_set.contains(&vertex_index) {
num_scc += 1;
self.dfs_scc(vertex_index, &mut scc_vec, num_scc, &mut visited_set);
self._dfs_scc_iterative(vertex_index, &mut scc_vec, num_scc, &mut visited_set);
}
}

Expand All @@ -383,7 +383,7 @@ impl DirectedGraph {
/// if v is unexplored then
/// DFS-SCC (G,v)
///
fn dfs_scc(
fn _dfs_scc(
&self,
vertex_index: usize,
scc_vec: &mut Vec<usize>,
Expand All @@ -397,7 +397,32 @@ impl DirectedGraph {
for neighbor in &vertex.borrow().outgoing_edges {
let neighbor_index = neighbor.borrow().get_index();
if !visted_set.contains(&neighbor_index) {
self.dfs_scc(neighbor_index, scc_vec, num_scc, visted_set);
self._dfs_scc(neighbor_index, scc_vec, num_scc, visted_set);
}
}
}

fn _dfs_scc_iterative(
&self,
vertex_index: usize,
scc_vec: &mut Vec<usize>,
num_scc: usize,
visited: &mut HashSet<usize>,
) {
let mut stack = Vec::new();
stack.push(vertex_index);

while let Some(current) = stack.pop() {
if !visited.contains(&current) {
visited.insert(current);
scc_vec[current] = num_scc;

let vertex = self.vertices.get(current).unwrap();

for neighbor in &vertex.borrow().outgoing_edges {
let neighbor_index = neighbor.borrow().get_index();
stack.push(neighbor_index);
}
}
}
}
Expand Down

0 comments on commit 17c1f2b

Please sign in to comment.