Work on week 3 assignment 2
This commit is contained in:
parent
12f8f2cf43
commit
687bb45eca
@ -1,4 +1,8 @@
|
|||||||
use crate::util::DirectedWeightedGraph;
|
#[derive(Debug)]
|
||||||
|
pub struct DirectedWeightedGraph {
|
||||||
|
pub nodes: Vec<Vec<(usize, usize)>>,
|
||||||
|
pub distance: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dijkstra(g: &mut DirectedWeightedGraph) -> () {
|
pub fn dijkstra(g: &mut DirectedWeightedGraph) -> () {
|
||||||
let mut x: Vec<usize> = vec![0];
|
let mut x: Vec<usize> = vec![0];
|
||||||
|
@ -1,10 +1,63 @@
|
|||||||
use crate::prims::WeightedGraph;
|
use std::collections::HashSet;
|
||||||
use std::convert::TryInto;
|
|
||||||
|
|
||||||
pub fn k_clustering(graph: &WeightedGraph) -> i32 {
|
#[derive(Debug, Clone, Copy)]
|
||||||
for vertex in graph {
|
pub struct Edge {
|
||||||
println!("{:?}", vertex.edges.len());
|
pub weight: i32,
|
||||||
}
|
pub source_id: usize,
|
||||||
graph.len().try_into().unwrap()
|
pub target_id: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct WeightedGraph {
|
||||||
|
pub n_nodes: usize,
|
||||||
|
pub cluster: Vec<usize>,
|
||||||
|
pub edges: Vec<Edge>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn k_clustering(graph: &mut WeightedGraph) -> usize {
|
||||||
|
let k: usize = 4;
|
||||||
|
|
||||||
|
let mut cluster_count = graph.n_nodes;
|
||||||
|
let mut clusters: Vec<Vec<usize>> = (0..graph.n_nodes).map(|x| vec![x]).collect();
|
||||||
|
println!("{:?}", clusters);
|
||||||
|
|
||||||
|
// Sort edges by decreasing weight
|
||||||
|
graph
|
||||||
|
.edges
|
||||||
|
.sort_by(|a, b| b.weight.partial_cmp(&a.weight).unwrap());
|
||||||
|
|
||||||
|
for edge in &graph.edges {
|
||||||
|
let ca_id = graph.cluster[edge.source_id];
|
||||||
|
let cb_id = graph.cluster[edge.target_id];
|
||||||
|
|
||||||
|
if ca_id == cb_id {
|
||||||
|
continue; // Nodes are already in the same cluster
|
||||||
|
}
|
||||||
|
|
||||||
|
let ca: &mut Vec<usize> = &mut clusters[ca_id];
|
||||||
|
let cb = clusters[cb_id].clone();
|
||||||
|
|
||||||
|
for node in cb {
|
||||||
|
ca.push(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clusters[ca].append(&mut clusters[cb]);
|
||||||
|
|
||||||
|
// clusters[]
|
||||||
|
// clusters[cluster_target].clear();
|
||||||
|
// graph.cluster[edge.target_id]= cluster_source;
|
||||||
|
|
||||||
|
cluster_count -= 1;
|
||||||
|
|
||||||
|
if cluster_count <= k {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for cluster in &clusters {
|
||||||
|
if cluster.len() > 0 {
|
||||||
|
// println!("{:?}", cluster);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
graph.n_nodes
|
||||||
|
}
|
||||||
|
10
src/main.rs
10
src/main.rs
@ -1,6 +1,7 @@
|
|||||||
mod dijkstra;
|
mod dijkstra;
|
||||||
mod heap;
|
mod heap;
|
||||||
mod jobs;
|
mod jobs;
|
||||||
|
mod k_clustering;
|
||||||
mod merge_sort;
|
mod merge_sort;
|
||||||
mod min_cut;
|
mod min_cut;
|
||||||
mod prims;
|
mod prims;
|
||||||
@ -8,18 +9,17 @@ mod quick_sort;
|
|||||||
mod ssc;
|
mod ssc;
|
||||||
mod two_sum;
|
mod two_sum;
|
||||||
mod util;
|
mod util;
|
||||||
mod k_clustering;
|
|
||||||
|
|
||||||
use crate::dijkstra::dijkstra;
|
use crate::dijkstra::dijkstra;
|
||||||
use crate::heap::heap;
|
use crate::heap::heap;
|
||||||
use crate::jobs::{jobs_1, jobs_2};
|
use crate::jobs::{jobs_1, jobs_2};
|
||||||
|
use crate::k_clustering::k_clustering;
|
||||||
use crate::merge_sort::merge_sort_inversions;
|
use crate::merge_sort::merge_sort_inversions;
|
||||||
use crate::min_cut::min_cut;
|
use crate::min_cut::min_cut;
|
||||||
use crate::prims::prims;
|
use crate::prims::prims;
|
||||||
use crate::quick_sort::quick_sort;
|
use crate::quick_sort::quick_sort;
|
||||||
use crate::ssc::ssc;
|
use crate::ssc::ssc;
|
||||||
use crate::two_sum::find_two_sums;
|
use crate::two_sum::find_two_sums;
|
||||||
use crate::k_clustering::k_clustering;
|
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -96,7 +96,7 @@ fn c3a1() {
|
|||||||
let mut jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap();
|
let mut jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap();
|
||||||
let r1 = jobs_1(&mut jobs);
|
let r1 = jobs_1(&mut jobs);
|
||||||
let r2 = jobs_2(&mut jobs);
|
let r2 = jobs_2(&mut jobs);
|
||||||
let g = util::read_weighted_graph("data/c3a1_edges.txt").unwrap();
|
let g = util::read_weighted_graph_prims("data/c3a1_edges.txt").unwrap();
|
||||||
let r3 = prims(&g);
|
let r3 = prims(&g);
|
||||||
println!("r1 = {}; r2 = {}; r3 = {} ", r1, r2, r3);
|
println!("r1 = {}; r2 = {}; r3 = {} ", r1, r2, r3);
|
||||||
|
|
||||||
@ -105,8 +105,8 @@ fn c3a1() {
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn c3a2() {
|
fn c3a2() {
|
||||||
let graph = util::read_weighted_graph("data/c3a2_clustering.txt").unwrap();
|
let mut graph = util::read_weighted_graph_clustering("data/c3a2_clustering.txt").unwrap();
|
||||||
let r1 = k_clustering(&graph);
|
let r1 = k_clustering(&mut graph);
|
||||||
println!("r1 = {:?}", r1);
|
println!("r1 = {:?}", r1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
use crate::util::Graph;
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::convert::TryInto;
|
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 {
|
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);
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
use crate::util::DirectedGraph;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DirectedGraph {
|
||||||
|
pub nodes: Vec<Vec<usize>>,
|
||||||
|
pub explored: Vec<bool>,
|
||||||
|
pub time: Vec<usize>,
|
||||||
|
pub leader: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
fn dfs(g: &mut DirectedGraph, node: usize, time: &mut usize) -> () {
|
fn dfs(g: &mut DirectedGraph, node: usize, time: &mut usize) -> () {
|
||||||
let mut nodes: Vec<(usize, usize)> = vec![(node, 0)];
|
let mut nodes: Vec<(usize, usize)> = vec![(node, 0)];
|
||||||
g.explored[node] = true;
|
g.explored[node] = true;
|
||||||
|
89
src/util.rs
89
src/util.rs
@ -1,30 +1,13 @@
|
|||||||
use crate::jobs::{Job, Jobs};
|
use crate::jobs;
|
||||||
use crate::prims::{Edge, Vertex, WeightedGraph};
|
use crate::k_clustering;
|
||||||
|
use crate::min_cut;
|
||||||
|
use crate::prims;
|
||||||
|
use crate::ssc;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Graph {
|
|
||||||
pub nodes: Vec<u32>,
|
|
||||||
pub edges: Vec<(u32, u32)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DirectedGraph {
|
|
||||||
pub nodes: Vec<Vec<usize>>,
|
|
||||||
pub explored: Vec<bool>,
|
|
||||||
pub time: Vec<usize>,
|
|
||||||
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> {
|
pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
let br = BufReader::new(file);
|
let br = BufReader::new(file);
|
||||||
@ -40,8 +23,8 @@ 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<min_cut::Graph, io::Error> {
|
||||||
let mut g = Graph {
|
let mut g = min_cut::Graph {
|
||||||
nodes: vec![],
|
nodes: vec![],
|
||||||
edges: vec![],
|
edges: vec![],
|
||||||
};
|
};
|
||||||
@ -84,16 +67,18 @@ fn get_node_size(path: &str) -> Result<usize, io::Error> {
|
|||||||
Ok(node_size)
|
Ok(node_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_to_directed_graph(path: &str) -> Result<(DirectedGraph, DirectedGraph), io::Error> {
|
pub fn read_to_directed_graph(
|
||||||
|
path: &str,
|
||||||
|
) -> Result<(ssc::DirectedGraph, ssc::DirectedGraph), io::Error> {
|
||||||
let nodes: usize = get_node_size(path).unwrap();
|
let nodes: usize = get_node_size(path).unwrap();
|
||||||
let mut g = DirectedGraph {
|
let mut g = ssc::DirectedGraph {
|
||||||
nodes: vec![vec![]; nodes],
|
nodes: vec![vec![]; nodes],
|
||||||
explored: vec![false; nodes],
|
explored: vec![false; nodes],
|
||||||
time: vec![0; nodes],
|
time: vec![0; nodes],
|
||||||
leader: vec![0; nodes],
|
leader: vec![0; nodes],
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut g_dash = DirectedGraph {
|
let mut g_dash = ssc::DirectedGraph {
|
||||||
nodes: vec![vec![]; nodes],
|
nodes: vec![vec![]; nodes],
|
||||||
explored: vec![false; nodes],
|
explored: vec![false; nodes],
|
||||||
time: vec![0; nodes],
|
time: vec![0; nodes],
|
||||||
@ -116,9 +101,11 @@ pub fn read_to_directed_graph(path: &str) -> Result<(DirectedGraph, DirectedGrap
|
|||||||
Ok((g, g_dash))
|
Ok((g, g_dash))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_to_directed_weighted_graph(path: &str) -> Result<DirectedWeightedGraph, io::Error> {
|
pub fn read_to_directed_weighted_graph(
|
||||||
|
path: &str,
|
||||||
|
) -> Result<crate::dijkstra::DirectedWeightedGraph, io::Error> {
|
||||||
let nodes: usize = get_node_size(path).unwrap();
|
let nodes: usize = get_node_size(path).unwrap();
|
||||||
let mut g = DirectedWeightedGraph {
|
let mut g = crate::dijkstra::DirectedWeightedGraph {
|
||||||
nodes: vec![vec![]; nodes],
|
nodes: vec![vec![]; nodes],
|
||||||
distance: vec![1000000; nodes], // initial distance based on instructions
|
distance: vec![1000000; nodes], // initial distance based on instructions
|
||||||
};
|
};
|
||||||
@ -141,7 +128,7 @@ pub fn read_to_directed_weighted_graph(path: &str) -> Result<DirectedWeightedGra
|
|||||||
Ok(g)
|
Ok(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_jobs(path: &str) -> Result<Jobs, io::Error> {
|
pub fn read_jobs(path: &str) -> Result<jobs::Jobs, io::Error> {
|
||||||
let mut jobs = Vec::new();
|
let mut jobs = Vec::new();
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
let br = BufReader::new(file);
|
let br = BufReader::new(file);
|
||||||
@ -152,7 +139,7 @@ pub fn read_jobs(path: &str) -> Result<Jobs, io::Error> {
|
|||||||
let mut nodes = line.split_whitespace();
|
let mut nodes = line.split_whitespace();
|
||||||
let weight = nodes.next().unwrap().parse().unwrap();
|
let weight = nodes.next().unwrap().parse().unwrap();
|
||||||
let length = nodes.next().unwrap().parse().unwrap();
|
let length = nodes.next().unwrap().parse().unwrap();
|
||||||
let job = Job {
|
let job = jobs::Job {
|
||||||
weight: weight,
|
weight: weight,
|
||||||
length: length,
|
length: length,
|
||||||
};
|
};
|
||||||
@ -162,7 +149,7 @@ pub fn read_jobs(path: &str) -> Result<Jobs, io::Error> {
|
|||||||
Ok(jobs)
|
Ok(jobs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_weighted_graph(path: &str) -> Result<WeightedGraph, io::Error> {
|
pub fn read_weighted_graph_prims(path: &str) -> Result<prims::WeightedGraph, io::Error> {
|
||||||
let mut g = Vec::new();
|
let mut g = Vec::new();
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
let br = BufReader::new(file);
|
let br = BufReader::new(file);
|
||||||
@ -173,7 +160,7 @@ pub fn read_weighted_graph(path: &str) -> Result<WeightedGraph, io::Error> {
|
|||||||
// let n_edges: usize = fields.next().unwrap().parse().unwrap();
|
// let n_edges: usize = fields.next().unwrap().parse().unwrap();
|
||||||
|
|
||||||
for i in 0..n_nodes {
|
for i in 0..n_nodes {
|
||||||
let node = Vertex {
|
let node = prims::Vertex {
|
||||||
id: i,
|
id: i,
|
||||||
edges: vec![],
|
edges: vec![],
|
||||||
};
|
};
|
||||||
@ -187,14 +174,14 @@ pub fn read_weighted_graph(path: &str) -> Result<WeightedGraph, io::Error> {
|
|||||||
let id_b: usize = nodes.next().unwrap().parse::<usize>().unwrap() - 1;
|
let id_b: usize = nodes.next().unwrap().parse::<usize>().unwrap() - 1;
|
||||||
let weight = nodes.next().unwrap().parse().unwrap();
|
let weight = nodes.next().unwrap().parse().unwrap();
|
||||||
|
|
||||||
let edge = Edge {
|
let edge = prims::Edge {
|
||||||
weight: weight,
|
weight: weight,
|
||||||
source_id: id_a,
|
source_id: id_a,
|
||||||
target_id: id_b,
|
target_id: id_b,
|
||||||
};
|
};
|
||||||
g[id_a].edges.push(edge);
|
g[id_a].edges.push(edge);
|
||||||
|
|
||||||
let edge = Edge {
|
let edge = crate::prims::Edge {
|
||||||
weight: weight,
|
weight: weight,
|
||||||
source_id: id_b,
|
source_id: id_b,
|
||||||
target_id: id_a,
|
target_id: id_a,
|
||||||
@ -204,3 +191,35 @@ pub fn read_weighted_graph(path: &str) -> Result<WeightedGraph, io::Error> {
|
|||||||
|
|
||||||
Ok(g)
|
Ok(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_weighted_graph_clustering(
|
||||||
|
path: &str,
|
||||||
|
) -> Result<k_clustering::WeightedGraph, io::Error> {
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let mut lines = BufReader::new(file).lines();
|
||||||
|
let line = lines.next().unwrap().unwrap();
|
||||||
|
let n_nodes: usize = line.parse().unwrap();
|
||||||
|
|
||||||
|
let mut g = k_clustering::WeightedGraph {
|
||||||
|
n_nodes: n_nodes,
|
||||||
|
cluster: (0..n_nodes).collect(),
|
||||||
|
edges: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
let line = line?;
|
||||||
|
let mut nodes = line.split_whitespace();
|
||||||
|
let id_a: usize = nodes.next().unwrap().parse::<usize>().unwrap() - 1;
|
||||||
|
let id_b: usize = nodes.next().unwrap().parse::<usize>().unwrap() - 1;
|
||||||
|
let weight = nodes.next().unwrap().parse().unwrap();
|
||||||
|
|
||||||
|
let edge = k_clustering::Edge {
|
||||||
|
weight: weight,
|
||||||
|
source_id: id_a,
|
||||||
|
target_id: id_b,
|
||||||
|
};
|
||||||
|
g.edges.push(edge)
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(g)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user