Run rustfmt on all rs files
This commit is contained in:
parent
778b665ae0
commit
b31fe7176c
43
src/main.rs
43
src/main.rs
@ -1,16 +1,17 @@
|
||||
mod merge_sort;
|
||||
mod min_cut;
|
||||
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::merge_sort::merge_sort_inversions;
|
||||
use crate::min_cut::min_cut;
|
||||
use crate::quick_sort::quick_sort;
|
||||
use crate::merge_sort::merge_sort_inversions;
|
||||
use crate::ssc::ssc;
|
||||
use crate::util::read_to_directed_graph;
|
||||
use crate::util::read_to_vector;
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c1a2() {
|
||||
@ -26,32 +27,32 @@ fn c1a3() {
|
||||
quick_sort(&mut vec, &mut comp_acc);
|
||||
println!("course 1 assignment 3: {:?}", comp_acc);
|
||||
/*
|
||||
1: 162085
|
||||
2: 164123
|
||||
3: 138382
|
||||
1: 162085
|
||||
2: 164123
|
||||
3: 138382
|
||||
*/
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c1a4() {
|
||||
let g = read_to_graph("data/course_1_assignment_4.txt").unwrap();
|
||||
let mut smalles_min_cut = u32::MAX;
|
||||
let iterations = g.nodes.len().pow(1);
|
||||
for _ in 0..iterations {
|
||||
let new_min_cut = min_cut(g.clone());
|
||||
if new_min_cut < smalles_min_cut {
|
||||
smalles_min_cut = new_min_cut;
|
||||
}
|
||||
}
|
||||
/* 17 */
|
||||
println!("course 1 assignment 4: {:?}", smalles_min_cut);
|
||||
let g = read_to_graph("data/course_1_assignment_4.txt").unwrap();
|
||||
let mut smalles_min_cut = u32::MAX;
|
||||
let iterations = g.nodes.len().pow(1);
|
||||
for _ in 0..iterations {
|
||||
let new_min_cut = min_cut(g.clone());
|
||||
if new_min_cut < smalles_min_cut {
|
||||
smalles_min_cut = new_min_cut;
|
||||
}
|
||||
}
|
||||
/* 17 */
|
||||
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);
|
||||
let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap();
|
||||
let s = ssc(g);
|
||||
println!("{:?}", s);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -1,50 +1,50 @@
|
||||
use rand::Rng;
|
||||
use crate::util::Graph;
|
||||
use rand::Rng;
|
||||
use std::convert::TryInto;
|
||||
|
||||
pub fn min_cut(mut g: Graph) -> u32 {
|
||||
let mut rng = rand::thread_rng();
|
||||
// println!("min_cut({:?})\n", g);
|
||||
while g.nodes.len() > 2 {
|
||||
// pick random edge (u, v)
|
||||
let edge_index = rng.gen_range(0, g.edges.len());
|
||||
let removed_edge = g.edges.swap_remove(edge_index);
|
||||
let mut rng = rand::thread_rng();
|
||||
// println!("min_cut({:?})\n", g);
|
||||
while g.nodes.len() > 2 {
|
||||
// pick random edge (u, v)
|
||||
let edge_index = rng.gen_range(0, g.edges.len());
|
||||
let removed_edge = g.edges.swap_remove(edge_index);
|
||||
|
||||
// merge u and v into a single node u
|
||||
let mut self_loops = vec![];
|
||||
let u = removed_edge.0;
|
||||
let v = removed_edge.1;
|
||||
for i in 0..g.edges.len() {
|
||||
// replace all v with u
|
||||
if g.edges[i].0 == v {
|
||||
g.edges[i].0 = u;
|
||||
}
|
||||
if g.edges[i].1 == v {
|
||||
g.edges[i].1 = u;
|
||||
}
|
||||
// make sure lower node comes first
|
||||
if g.edges[i].0 > g.edges[i].1 {
|
||||
g.edges[i] = (g.edges[i].1, g.edges[i]. 0);
|
||||
}
|
||||
// merge u and v into a single node u
|
||||
let mut self_loops = vec![];
|
||||
let u = removed_edge.0;
|
||||
let v = removed_edge.1;
|
||||
for i in 0..g.edges.len() {
|
||||
// replace all v with u
|
||||
if g.edges[i].0 == v {
|
||||
g.edges[i].0 = u;
|
||||
}
|
||||
if g.edges[i].1 == v {
|
||||
g.edges[i].1 = u;
|
||||
}
|
||||
// make sure lower node comes first
|
||||
if g.edges[i].0 > g.edges[i].1 {
|
||||
g.edges[i] = (g.edges[i].1, g.edges[i].0);
|
||||
}
|
||||
|
||||
if g.edges[i].0 == g.edges[i].1 {
|
||||
self_loops.push(i);
|
||||
}
|
||||
}
|
||||
if g.edges[i].0 == g.edges[i].1 {
|
||||
self_loops.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
// remove v from nodes
|
||||
g.nodes.pop(); // we only use the nodes.len()
|
||||
// if let Some(i) = g.nodes.iter().position(|x| *x == v) {
|
||||
// g.nodes.swap_remove(i);
|
||||
// }
|
||||
// remove v from nodes
|
||||
g.nodes.pop(); // we only use the nodes.len()
|
||||
// if let Some(i) = g.nodes.iter().position(|x| *x == v) {
|
||||
// g.nodes.swap_remove(i);
|
||||
// }
|
||||
|
||||
// remove self-loops
|
||||
let mut self_loops_removed: usize = 0;
|
||||
for i in self_loops {
|
||||
g.edges.remove(i - self_loops_removed);
|
||||
self_loops_removed += 1;
|
||||
}
|
||||
}
|
||||
// return cut represented by the final two nodes
|
||||
g.edges.len().try_into().unwrap()
|
||||
// remove self-loops
|
||||
let mut self_loops_removed: usize = 0;
|
||||
for i in self_loops {
|
||||
g.edges.remove(i - self_loops_removed);
|
||||
self_loops_removed += 1;
|
||||
}
|
||||
}
|
||||
// return cut represented by the final two nodes
|
||||
g.edges.len().try_into().unwrap()
|
||||
}
|
||||
|
@ -1,52 +1,50 @@
|
||||
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 {
|
||||
if v.len() < 3 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let left = 0;
|
||||
let middle = if v.len() % 2 == 0 {
|
||||
v.len() / 2 - 1
|
||||
} else {
|
||||
v.len() / 2
|
||||
};
|
||||
let right = v.len() - 1;
|
||||
let mut values = vec![v[left], v[middle], v[right]];
|
||||
bubble_sort(&mut values);
|
||||
let median = values[1];
|
||||
if median == v[left] {
|
||||
return left;
|
||||
} else if median == v[middle] {
|
||||
return middle;
|
||||
} else if median == v[right]{
|
||||
return right;
|
||||
} else {
|
||||
panic!("Did not find median in values!");
|
||||
}
|
||||
let left = 0;
|
||||
let middle = if v.len() % 2 == 0 {
|
||||
v.len() / 2 - 1
|
||||
} else {
|
||||
v.len() / 2
|
||||
};
|
||||
let right = v.len() - 1;
|
||||
let mut values = vec![v[left], v[middle], v[right]];
|
||||
bubble_sort(&mut values);
|
||||
let median = values[1];
|
||||
if median == v[left] {
|
||||
return left;
|
||||
} else if median == v[middle] {
|
||||
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]) -> () {
|
||||
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) -> () {
|
||||
fn bubble_sort<T: PartialOrd>(v: &mut [T]) -> () {
|
||||
let mut unsorted = true;
|
||||
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 = 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;
|
||||
v.swap(0, pivot_index);
|
||||
for j in 1..v.len() {
|
||||
if v[j] < v[0] {
|
||||
v.swap(i, j);
|
||||
i += 1;
|
||||
}
|
||||
if v[j] < v[0] {
|
||||
v.swap(i, j);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
v.swap(0, i - 1);
|
||||
quick_sort(&mut v[..i - 1], comp_acc);
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::util::Graph;
|
||||
|
||||
pub fn ssc(_g: Graph) -> u32 {
|
||||
0
|
||||
0
|
||||
}
|
||||
|
11
src/util.rs
11
src/util.rs
@ -24,9 +24,11 @@ pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
|
||||
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 br = BufReader::new(file);
|
||||
|
||||
@ -51,7 +53,10 @@ pub fn read_to_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 br = BufReader::new(file);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user