Run rustfmt on all rs files

This commit is contained in:
Felix Martin 2020-12-03 19:52:15 -05:00
parent 778b665ae0
commit b31fe7176c
5 changed files with 118 additions and 114 deletions

View File

@ -1,16 +1,17 @@
mod merge_sort; mod merge_sort;
mod min_cut;
mod quick_sort; mod quick_sort;
mod ssc; mod ssc;
mod min_cut;
mod util; mod util;
use crate::util::read_to_graph; use crate::util::read_to_graph;
use crate::util::read_to_directed_graph;
use crate::util::read_to_vector; use crate::merge_sort::merge_sort_inversions;
use crate::min_cut::min_cut; use crate::min_cut::min_cut;
use crate::quick_sort::quick_sort; use crate::quick_sort::quick_sort;
use crate::merge_sort::merge_sort_inversions;
use crate::ssc::ssc; use crate::ssc::ssc;
use crate::util::read_to_directed_graph;
use crate::util::read_to_vector;
#[allow(dead_code)] #[allow(dead_code)]
fn c1a2() { fn c1a2() {
@ -26,32 +27,32 @@ fn c1a3() {
quick_sort(&mut vec, &mut comp_acc); quick_sort(&mut vec, &mut comp_acc);
println!("course 1 assignment 3: {:?}", comp_acc); println!("course 1 assignment 3: {:?}", comp_acc);
/* /*
1: 162085 1: 162085
2: 164123 2: 164123
3: 138382 3: 138382
*/ */
} }
#[allow(dead_code)] #[allow(dead_code)]
fn c1a4() { fn c1a4() {
let g = read_to_graph("data/course_1_assignment_4.txt").unwrap(); let g = read_to_graph("data/course_1_assignment_4.txt").unwrap();
let mut smalles_min_cut = u32::MAX; let mut smalles_min_cut = u32::MAX;
let iterations = g.nodes.len().pow(1); let iterations = g.nodes.len().pow(1);
for _ in 0..iterations { for _ in 0..iterations {
let new_min_cut = min_cut(g.clone()); let new_min_cut = min_cut(g.clone());
if new_min_cut < smalles_min_cut { if new_min_cut < smalles_min_cut {
smalles_min_cut = new_min_cut; smalles_min_cut = new_min_cut;
} }
} }
/* 17 */ /* 17 */
println!("course 1 assignment 4: {:?}", smalles_min_cut); println!("course 1 assignment 4: {:?}", smalles_min_cut);
} }
#[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 s = ssc(g);
println!("{:?}", s); println!("{:?}", s);
} }
fn main() { fn main() {

View File

@ -1,50 +1,50 @@
use rand::Rng;
use crate::util::Graph; use crate::util::Graph;
use rand::Rng;
use std::convert::TryInto; use std::convert::TryInto;
pub fn min_cut(mut g: Graph) -> u32 { pub fn min_cut(mut g: Graph) -> u32 {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
// println!("min_cut({:?})\n", g); // println!("min_cut({:?})\n", g);
while g.nodes.len() > 2 { while g.nodes.len() > 2 {
// pick random edge (u, v) // pick random edge (u, v)
let edge_index = rng.gen_range(0, g.edges.len()); let edge_index = rng.gen_range(0, g.edges.len());
let removed_edge = g.edges.swap_remove(edge_index); let removed_edge = g.edges.swap_remove(edge_index);
// merge u and v into a single node u // merge u and v into a single node u
let mut self_loops = vec![]; let mut self_loops = vec![];
let u = removed_edge.0; let u = removed_edge.0;
let v = removed_edge.1; let v = removed_edge.1;
for i in 0..g.edges.len() { for i in 0..g.edges.len() {
// replace all v with u // replace all v with u
if g.edges[i].0 == v { if g.edges[i].0 == v {
g.edges[i].0 = u; g.edges[i].0 = u;
} }
if g.edges[i].1 == v { if g.edges[i].1 == v {
g.edges[i].1 = u; g.edges[i].1 = u;
} }
// make sure lower node comes first // make sure lower node comes first
if g.edges[i].0 > g.edges[i].1 { if g.edges[i].0 > g.edges[i].1 {
g.edges[i] = (g.edges[i].1, g.edges[i]. 0); g.edges[i] = (g.edges[i].1, g.edges[i].0);
} }
if g.edges[i].0 == g.edges[i].1 { if g.edges[i].0 == g.edges[i].1 {
self_loops.push(i); self_loops.push(i);
} }
} }
// remove v from nodes // remove v from nodes
g.nodes.pop(); // we only use the nodes.len() g.nodes.pop(); // we only use the nodes.len()
// if let Some(i) = g.nodes.iter().position(|x| *x == v) { // if let Some(i) = g.nodes.iter().position(|x| *x == v) {
// g.nodes.swap_remove(i); // g.nodes.swap_remove(i);
// } // }
// remove self-loops // remove self-loops
let mut self_loops_removed: usize = 0; let mut self_loops_removed: usize = 0;
for i in self_loops { for i in self_loops {
g.edges.remove(i - self_loops_removed); g.edges.remove(i - self_loops_removed);
self_loops_removed += 1; self_loops_removed += 1;
} }
} }
// return cut represented by the final two nodes // return cut represented by the final two nodes
g.edges.len().try_into().unwrap() g.edges.len().try_into().unwrap()
} }

View File

@ -1,52 +1,50 @@
use std::cmp::PartialOrd; use std::cmp::PartialOrd;
fn median_of_three<T: PartialOrd + Copy>(v: &[T]) -> usize {
if v.len() < 3 {
return 0;
}
fn median_of_three<T: PartialOrd + Copy>( v: &[T]) -> usize { let left = 0;
if v.len() < 3 { let middle = if v.len() % 2 == 0 {
return 0; v.len() / 2 - 1
} } else {
v.len() / 2
let left = 0; };
let middle = if v.len() % 2 == 0 { let right = v.len() - 1;
v.len() / 2 - 1 let mut values = vec![v[left], v[middle], v[right]];
} else { bubble_sort(&mut values);
v.len() / 2 let median = values[1];
}; if median == v[left] {
let right = v.len() - 1; return left;
let mut values = vec![v[left], v[middle], v[right]]; } else if median == v[middle] {
bubble_sort(&mut values); return middle;
let median = values[1]; } else if median == v[right] {
if median == v[left] { return right;
return left; } else {
} else if median == v[middle] { panic!("Did not find median in values!");
return middle; }
} else if median == v[right]{
return right;
} else {
panic!("Did not find median in values!");
}
} }
fn bubble_sort<T: PartialOrd>( v: &mut [T]) -> () { fn bubble_sort<T: PartialOrd>(v: &mut [T]) -> () {
let mut unsorted = true; let mut unsorted = true;
if v.len() <= 1 {
return
}
while unsorted {
unsorted = false;
for i in 0..v.len() - 1 {
if v[i] > v[i + 1] {
v.swap(i, i + 1);
unsorted = true;
}
}
}
}
pub fn quick_sort<T: PartialOrd + Copy>( v: &mut [T], comp_acc: &mut usize) -> () {
if v.len() <= 1 { if v.len() <= 1 {
return; return;
}
while unsorted {
unsorted = false;
for i in 0..v.len() - 1 {
if v[i] > v[i + 1] {
v.swap(i, i + 1);
unsorted = true;
}
}
}
}
pub fn quick_sort<T: PartialOrd + Copy>(v: &mut [T], comp_acc: &mut usize) -> () {
if v.len() <= 1 {
return;
} }
// let pivot_index = 0; // let pivot_index = 0;
// let pivot_index = v.len() - 1; // let pivot_index = v.len() - 1;
@ -54,10 +52,10 @@ pub fn quick_sort<T: PartialOrd + Copy>( v: &mut [T], comp_acc: &mut usize) -> (
let mut i = 1; let mut i = 1;
v.swap(0, pivot_index); v.swap(0, pivot_index);
for j in 1..v.len() { for j in 1..v.len() {
if v[j] < v[0] { if v[j] < v[0] {
v.swap(i, j); v.swap(i, j);
i += 1; i += 1;
} }
} }
v.swap(0, i - 1); v.swap(0, i - 1);
quick_sort(&mut v[..i - 1], comp_acc); quick_sort(&mut v[..i - 1], comp_acc);

View File

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

View File

@ -24,9 +24,11 @@ pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
Ok(v) Ok(v)
} }
pub fn read_to_graph(path: &str) -> Result<Graph, io::Error> { pub fn read_to_graph(path: &str) -> Result<Graph, io::Error> {
let mut g = Graph { nodes: vec![], edges: vec![] }; let mut g = Graph {
nodes: vec![],
edges: vec![],
};
let file = File::open(path)?; let file = File::open(path)?;
let br = BufReader::new(file); let br = BufReader::new(file);
@ -44,14 +46,17 @@ pub fn read_to_graph(path: &str) -> Result<Graph, io::Error> {
if neightbor > current_node { if neightbor > current_node {
let edge = (current_node, neightbor); let edge = (current_node, neightbor);
g.edges.push(edge); g.edges.push(edge);
} }
} }
} }
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<Graph, io::Error> {
let mut g = Graph { nodes: vec![], edges: vec![] }; let mut g = Graph {
nodes: vec![],
edges: vec![],
};
let file = File::open(path)?; let file = File::open(path)?;
let br = BufReader::new(file); let br = BufReader::new(file);