Prepare for course 2 assignment 1

This commit is contained in:
2020-12-01 16:17:16 -05:00
parent 9a8ffa64e1
commit 778b665ae0
4 changed files with 41 additions and 9 deletions

View File

@@ -1,13 +1,16 @@
mod merge_sort;
mod quick_sort;
mod ssc;
mod min_cut;
mod util;
use crate::util::read_to_graph;
use crate::util::read_to_directed_graph;
use crate::util::read_to_vector;
use crate::min_cut::min_cut;
use crate::quick_sort::quick_sort;
use crate::merge_sort::merge_sort_inversions;
use crate::ssc::ssc;
#[allow(dead_code)]
fn c1a2() {
@@ -44,8 +47,16 @@ fn c1a4() {
println!("course 1 assignment 4: {:?}", smalles_min_cut);
}
#[allow(dead_code)]
fn c2a1() {
let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap();
let s = ssc(g);
println!("{:?}", s);
}
fn main() {
// c1a2();
// c1a3();
c1a4();
// c1a4();
c2a1();
}

View File

@@ -1,12 +1,7 @@
use rand::Rng;
use crate::util::Graph;
use std::convert::TryInto;
#[derive(Debug, Clone)]
pub struct Graph {
pub nodes: Vec<u32>,
pub edges: Vec<(u32, u32)>,
}
pub fn min_cut(mut g: Graph) -> u32 {
let mut rng = rand::thread_rng();
// println!("min_cut({:?})\n", g);

5
src/ssc.rs Normal file
View File

@@ -0,0 +1,5 @@
use crate::util::Graph;
pub fn ssc(_g: Graph) -> u32 {
0
}

View File

@@ -1,9 +1,13 @@
use crate::min_cut::Graph;
use std::fs::File;
use std::io;
use std::io::{BufRead, BufReader, Error, ErrorKind};
use std::vec::Vec;
#[derive(Debug, Clone)]
pub struct Graph {
pub nodes: Vec<u32>,
pub edges: Vec<(u32, u32)>,
}
pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
let file = File::open(path)?;
@@ -44,4 +48,21 @@ pub fn read_to_graph(path: &str) -> Result<Graph, io::Error> {
}
}
Ok(g)
}
}
pub fn read_to_directed_graph(path: &str) -> Result<Graph, io::Error> {
let mut g = Graph { nodes: vec![], edges: vec![] };
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);
}
println!("{:?}", g);
Ok(g)
}