Finish course 2 assignment 2 naivly and clean up
This commit is contained in:
4
data/c2a2_test.txt
Normal file
4
data/c2a2_test.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1 2,1 3,4
|
||||
2 3,2 4,6
|
||||
3 4,3
|
||||
4
|
||||
BIN
png/dijkstra_1.png
Normal file
BIN
png/dijkstra_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 962 KiB |
BIN
png/dijkstra_2.png
Normal file
BIN
png/dijkstra_2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 674 KiB |
BIN
png/dijkstra_3.png
Normal file
BIN
png/dijkstra_3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 779 KiB |
@@ -1,50 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
n = np.arange(1, 1000, 1)
|
||||
|
||||
# Version 1
|
||||
|
||||
a = 2**np.log(n)
|
||||
b = 2**2**np.log(n)
|
||||
c = n**(5/2)
|
||||
d = 2**n**2
|
||||
e = (n**2)*np.log(n)
|
||||
|
||||
# plt.plot(n, a, 'b', n, b, 'y') # a < b
|
||||
# plt.plot(n, b, 'b', n, c, 'y') # b > c
|
||||
# plt.plot(n, a, 'b', n, c, 'y') # a < c
|
||||
|
||||
# plt.plot(n, c, 'b', n, d, 'y') # c << d
|
||||
# plt.plot(n, c, 'b', n, e, 'y') # c > e
|
||||
# plt.plot(n, e, 'b', n, b, 'y') # e < b
|
||||
# plt.plot(n, e, 'b', n, a, 'y') # e > a
|
||||
|
||||
# a < e < c < b < d
|
||||
|
||||
n = np.arange(1, 1000, 1)
|
||||
# n = 1000
|
||||
a = np.sqrt(n)
|
||||
b = 10**n
|
||||
c = n**1.5
|
||||
d = 2**np.sqrt(np.log(n))
|
||||
e = n**5/3
|
||||
|
||||
# print(a, b, c, d, e)
|
||||
# d < a < c < e < b
|
||||
# print(d < a, a < c, c < e, e < b)
|
||||
|
||||
plt.plot(n, a, 'b', n, d, 'y') # a > d
|
||||
|
||||
plt.show()
|
||||
|
||||
"""
|
||||
1) n*log(n)
|
||||
2) True
|
||||
3) Yes if and Sometimes yes
|
||||
4) O(nk**2)
|
||||
5) :/ Tried:
|
||||
|
||||
ecadb
|
||||
abecd
|
||||
"""
|
||||
35
src/dijkstra.rs
Normal file
35
src/dijkstra.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::util::DirectedWeightedGraph;
|
||||
|
||||
pub fn dijkstra(g: &mut DirectedWeightedGraph) -> () {
|
||||
let mut x: Vec<usize> = vec![0];
|
||||
g.distance[0] = 0;
|
||||
|
||||
while x.len() < g.nodes.len() {
|
||||
let mut min_node = std::usize::MAX;
|
||||
let mut min_distance = std::usize::MAX;
|
||||
|
||||
for &i in x.iter() {
|
||||
let v = &g.nodes[i];
|
||||
for &edge in v.iter() {
|
||||
let w = edge.0;
|
||||
let distance = edge.1;
|
||||
if x.contains(&w) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let current_edge_distance = g.distance[i] + distance;
|
||||
if current_edge_distance < min_distance {
|
||||
min_distance = current_edge_distance;
|
||||
min_node = w;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if min_node == std::usize::MAX {
|
||||
panic!("Did not find a min_node");
|
||||
}
|
||||
|
||||
g.distance[min_node] = min_distance;
|
||||
x.push(min_node);
|
||||
}
|
||||
}
|
||||
22
src/main.rs
22
src/main.rs
@@ -1,29 +1,28 @@
|
||||
mod dijkstra;
|
||||
mod merge_sort;
|
||||
mod min_cut;
|
||||
mod quick_sort;
|
||||
mod ssc;
|
||||
mod util;
|
||||
|
||||
use crate::util::read_to_graph;
|
||||
use std::cmp::min;
|
||||
|
||||
use crate::dijkstra::dijkstra;
|
||||
use crate::merge_sort::merge_sort_inversions;
|
||||
use crate::min_cut::min_cut;
|
||||
use crate::quick_sort::quick_sort;
|
||||
use crate::ssc::ssc;
|
||||
use crate::util::read_to_directed_graph;
|
||||
use crate::util::read_to_vector;
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c1a2() {
|
||||
let vec = read_to_vector("data/c1a2.txt").unwrap();
|
||||
let vec = util::read_to_vector("data/c1a2.txt").unwrap();
|
||||
let (_sorted, inversions) = merge_sort_inversions(vec);
|
||||
println!("c1a2={:?}", inversions);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c1a3() {
|
||||
let mut vec = read_to_vector("data/c1a3.txt").unwrap();
|
||||
let mut vec = util::read_to_vector("data/c1a3.txt").unwrap();
|
||||
let mut comp_acc: usize = 0;
|
||||
quick_sort(&mut vec, &mut comp_acc);
|
||||
println!("c1a3={:?}", comp_acc);
|
||||
@@ -32,7 +31,7 @@ fn c1a3() {
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c1a4() {
|
||||
let g = read_to_graph("data/c1a4.txt").unwrap();
|
||||
let g = util::read_to_graph("data/c1a4.txt").unwrap();
|
||||
let mut smallest_min_cut = u32::MAX;
|
||||
let iterations = g.nodes.len().pow(1);
|
||||
for _ in 0..iterations {
|
||||
@@ -48,7 +47,7 @@ fn c1a4() {
|
||||
#[allow(dead_code)]
|
||||
fn c2a1() {
|
||||
// I have not checked in the graph file, because it has 70MB. This is a tiny test example.
|
||||
let g = read_to_directed_graph("data/c2a1_test.txt").unwrap();
|
||||
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]);
|
||||
@@ -57,7 +56,14 @@ fn c2a1() {
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c2a2() {
|
||||
println!("here we go again!");
|
||||
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]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
33
src/util.rs
33
src/util.rs
@@ -9,7 +9,7 @@ pub struct Graph {
|
||||
pub edges: Vec<(u32, u32)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct DirectedGraph {
|
||||
pub nodes: Vec<Vec<usize>>,
|
||||
pub explored: Vec<bool>,
|
||||
@@ -17,6 +17,12 @@ pub struct DirectedGraph {
|
||||
pub leader: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DirectedWeightedGraph {
|
||||
pub nodes: Vec<Vec<(usize, usize)>>,
|
||||
pub distance: Vec<usize>,
|
||||
}
|
||||
|
||||
pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
|
||||
let file = File::open(path)?;
|
||||
let br = BufReader::new(file);
|
||||
@@ -107,3 +113,28 @@ pub fn read_to_directed_graph(path: &str) -> Result<(DirectedGraph, DirectedGrap
|
||||
|
||||
Ok((g, g_dash))
|
||||
}
|
||||
|
||||
pub fn read_to_directed_weighted_graph(path: &str) -> Result<DirectedWeightedGraph, io::Error> {
|
||||
let nodes: usize = get_node_size(path).unwrap();
|
||||
let mut g = DirectedWeightedGraph {
|
||||
nodes: vec![vec![]; nodes],
|
||||
distance: vec![1000000; nodes], // initial distance based on instructions
|
||||
};
|
||||
|
||||
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 current_node: usize = nodes.next().unwrap().parse().unwrap();
|
||||
|
||||
for edge in nodes {
|
||||
let mut it = edge.split(",");
|
||||
let target_node: usize = it.next().unwrap().parse().unwrap();
|
||||
let distance: usize = it.next().unwrap().parse().unwrap();
|
||||
g.nodes[current_node - 1].push((target_node - 1, distance))
|
||||
}
|
||||
}
|
||||
Ok(g)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user