algos/src/main.rs

103 lines
2.5 KiB
Rust
Raw Normal View History

mod dijkstra;
2020-12-10 02:33:26 +01:00
mod heap;
mod merge_sort;
2020-12-04 01:52:15 +01:00
mod min_cut;
2020-11-20 02:37:42 +01:00
mod quick_sort;
2020-12-01 22:17:16 +01:00
mod ssc;
2020-11-27 18:14:35 +01:00
mod util;
mod two_sum;
2020-11-20 02:37:42 +01:00
2020-12-04 17:43:03 +01:00
use std::cmp::min;
use crate::dijkstra::dijkstra;
2020-12-10 02:33:26 +01:00
use crate::heap::heap;
2020-12-04 01:52:15 +01:00
use crate::merge_sort::merge_sort_inversions;
2020-11-28 18:59:14 +01:00
use crate::min_cut::min_cut;
2020-11-20 02:37:42 +01:00
use crate::quick_sort::quick_sort;
2020-12-01 22:17:16 +01:00
use crate::ssc::ssc;
use crate::two_sum::find_two_sums;
2020-11-20 02:37:42 +01:00
#[allow(dead_code)]
fn c1a2() {
let vec = util::read_to_vector("data/c1a2.txt").unwrap();
let (_sorted, inversions) = merge_sort_inversions(vec);
println!("c1a2={:?}", inversions);
}
2020-11-20 14:19:59 +01:00
#[allow(dead_code)]
2020-11-20 02:37:42 +01:00
fn c1a3() {
let mut vec = util::read_to_vector("data/c1a3.txt").unwrap();
2020-11-20 02:37:42 +01:00
let mut comp_acc: usize = 0;
quick_sort(&mut vec, &mut comp_acc);
println!("c1a3={:?}", comp_acc);
2020-12-04 17:43:03 +01:00
// 1: 162085, 2: 164123, 3: 138382
2020-11-20 02:37:42 +01:00
}
2020-11-28 18:59:14 +01:00
#[allow(dead_code)]
2020-11-27 18:14:35 +01:00
fn c1a4() {
let g = util::read_to_graph("data/c1a4.txt").unwrap();
let mut smallest_min_cut = u32::MAX;
2020-12-04 01:52:15 +01:00
let iterations = g.nodes.len().pow(1);
for _ in 0..iterations {
let new_min_cut = min_cut(g.clone());
if new_min_cut < smallest_min_cut {
smallest_min_cut = new_min_cut;
2020-12-04 01:52:15 +01:00
}
}
println!("c1a4={:?}", smallest_min_cut);
2020-12-11 02:04:11 +01:00
// 17
2020-11-27 18:14:35 +01:00
}
2020-12-01 22:17:16 +01:00
#[allow(dead_code)]
fn c2a1() {
2020-12-04 17:43:03 +01:00
// I have not checked in the graph file, because it has 70MB. This is a tiny test example.
let g = util::read_to_directed_graph("data/c2a1_test.txt").unwrap();
let sizes = ssc(g.0, g.1);
let max_size = min(sizes.len(), 5);
println!("{:?}", &sizes[0..max_size]);
2020-12-04 17:43:03 +01:00
// [434821, 968, 459, 313, 211]
2020-12-01 22:17:16 +01:00
}
#[allow(dead_code)]
fn c2a2() {
let mut requested_vertices: Vec<usize> = vec![7, 37, 59, 82, 99, 115, 133, 165, 188, 197];
let mut g = util::read_to_directed_weighted_graph("data/c2a2.txt").unwrap();
dijkstra(&mut g);
for i in 0..requested_vertices.len() {
requested_vertices[i] = g.distance[requested_vertices[i] - 1];
}
println!("{:?}", requested_vertices);
// [2599, 2610, 2947, 2052, 2367, 2399, 2029, 2442, 2505, 3068]
}
2020-12-10 02:33:26 +01:00
#[allow(dead_code)]
fn c2a3() {
let v = util::read_to_vector("data/c2a3.txt").unwrap();
let r = heap(&v);
2020-12-11 02:04:11 +01:00
println!("c2a3={:?}", r);
2020-12-10 02:33:26 +01:00
}
#[allow(dead_code)]
fn c2a4() {
let mut v = util::read_to_vector("data/c2a4.txt").unwrap();
let r = find_two_sums(&mut v);
println!("c2a4={:?}", r);
// c2a4=427
}
2020-12-12 16:53:27 +01:00
#[allow(dead_code)]
fn c3a1() {
println!("continue here");
}
fn main() {
2020-11-27 18:14:35 +01:00
// c1a2();
// c1a3();
2020-12-01 22:17:16 +01:00
// c1a4();
// c2a1();
2020-12-10 02:33:26 +01:00
// c2a2();
// c2a3();
c2a4();
// c3a1();
}