algos/src/ssc.rs

48 lines
1.1 KiB
Rust

use crate::util::DirectedGraph;
fn dfs(mut g: &mut DirectedGraph, node: usize, mut time: &mut usize) -> () {
g.explored[node] = true;
for i in 0..g.nodes[node].len() {
let j = g.nodes[node][i];
if !g.explored[j] {
dfs(&mut g, j, &mut time);
}
}
*time += 1;
g.time[node] = *time;
}
pub fn ssc(g: DirectedGraph, mut g_dash: DirectedGraph) -> u32 {
let mut time: usize = 0;
println!("ssc");
for i in (0..g_dash.nodes.len()).rev() {
if !g_dash.explored[i] {
dfs(&mut g_dash, i, &mut time);
}
}
println!("finished first dfs");
let mut time_to_index: Vec<usize> = vec![0; g.nodes.len()];
for i in 0..g_dash.nodes.len() {
time_to_index[g_dash.time[i] - 1] = i;
}
println!("finish time look-up");
for i in (0..time_to_index.len()).rev() {
let _node = time_to_index[i];
// println!("time: {:?} node: {:?}", i, node);
}
println!("finished second dfs");
for i in 0..g_dash.nodes.len() {
println!("f({})={}", i + 1, g_dash.time[i]);
}
0
}