diff --git a/src/main.rs b/src/main.rs index 45640da..cc0a2a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,9 @@ fn c1a4() { #[allow(dead_code)] fn c2a1() { - let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap(); - let s = ssc(g); + // let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap(); + let g = read_to_directed_graph("data/course_2_assignment_1_test.txt").unwrap(); + let s = ssc(g.0, g.1); println!("{:?}", s); } diff --git a/src/ssc.rs b/src/ssc.rs index 0f9892a..e386e70 100644 --- a/src/ssc.rs +++ b/src/ssc.rs @@ -1,5 +1,47 @@ -use crate::util::Graph; +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 = 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]); + } -pub fn ssc(_g: Graph) -> u32 { 0 } diff --git a/src/util.rs b/src/util.rs index 454975f..e4585b4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -9,6 +9,13 @@ pub struct Graph { pub edges: Vec<(u32, u32)>, } +#[derive(Debug, Clone)] +pub struct DirectedGraph { + pub nodes: Vec>, + pub explored: Vec, + pub time: Vec, +} + pub fn read_to_vector(path: &str) -> Result, io::Error> { let file = File::open(path)?; let br = BufReader::new(file); @@ -52,22 +59,33 @@ pub fn read_to_graph(path: &str) -> Result { Ok(g) } -pub fn read_to_directed_graph(path: &str) -> Result { - let mut g = Graph { - nodes: vec![], - edges: vec![], +pub fn read_to_directed_graph(path: &str) -> Result<(DirectedGraph, DirectedGraph), io::Error> { + const NODES: usize = 9; + // const NODES: usize = 875714; + let mut g = DirectedGraph { + nodes: vec![vec![]; NODES], + explored: vec![false; NODES], + time: vec![0; NODES], }; + + let mut g_dash = DirectedGraph { + nodes: vec![vec![]; NODES], + explored: vec![false; NODES], + time: vec![0; NODES], + }; + let file = File::open(path)?; let br = BufReader::new(file); for line in br.lines() { let line = line?; let mut nodes = line.split_whitespace(); - let first_node = nodes.next().unwrap().parse().unwrap(); - let second_node = nodes.next().unwrap().parse().unwrap(); - let edge = (first_node, second_node); - g.edges.push(edge); + let first_node: usize = nodes.next().unwrap().parse().unwrap(); + let second_node: usize = nodes.next().unwrap().parse().unwrap(); + + g.nodes[first_node - 1].push(second_node - 1); + g_dash.nodes[second_node - 1].push(first_node - 1); } - println!("{:?}", g); - Ok(g) + + Ok((g, g_dash)) }