Start working on course 2 assignment 1

This commit is contained in:
Felix Martin 2020-12-03 21:35:58 -05:00
parent b31fe7176c
commit 57f141fa9a
3 changed files with 75 additions and 14 deletions

View File

@ -50,8 +50,9 @@ fn c1a4() {
#[allow(dead_code)] #[allow(dead_code)]
fn c2a1() { fn c2a1() {
let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap(); // 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_test.txt").unwrap();
let s = ssc(g.0, g.1);
println!("{:?}", s); println!("{:?}", s);
} }

View File

@ -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<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]);
}
pub fn ssc(_g: Graph) -> u32 {
0 0
} }

View File

@ -9,6 +9,13 @@ pub struct Graph {
pub edges: Vec<(u32, u32)>, pub edges: Vec<(u32, u32)>,
} }
#[derive(Debug, Clone)]
pub struct DirectedGraph {
pub nodes: Vec<Vec<usize>>,
pub explored: Vec<bool>,
pub time: Vec<usize>,
}
pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> { pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
let file = File::open(path)?; let file = File::open(path)?;
let br = BufReader::new(file); let br = BufReader::new(file);
@ -52,22 +59,33 @@ pub fn read_to_graph(path: &str) -> Result<Graph, io::Error> {
Ok(g) Ok(g)
} }
pub fn read_to_directed_graph(path: &str) -> Result<Graph, io::Error> { pub fn read_to_directed_graph(path: &str) -> Result<(DirectedGraph, DirectedGraph), io::Error> {
let mut g = Graph { const NODES: usize = 9;
nodes: vec![], // const NODES: usize = 875714;
edges: vec![], 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 file = File::open(path)?;
let br = BufReader::new(file); let br = BufReader::new(file);
for line in br.lines() { for line in br.lines() {
let line = line?; let line = line?;
let mut nodes = line.split_whitespace(); let mut nodes = line.split_whitespace();
let first_node = nodes.next().unwrap().parse().unwrap(); let first_node: usize = nodes.next().unwrap().parse().unwrap();
let second_node = nodes.next().unwrap().parse().unwrap(); let second_node: usize = nodes.next().unwrap().parse().unwrap();
let edge = (first_node, second_node);
g.edges.push(edge); 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))
} }