Work on week 3 assignment 2

This commit is contained in:
Felix Martin 2021-02-05 19:28:19 -05:00
parent 12f8f2cf43
commit 687bb45eca
6 changed files with 139 additions and 51 deletions

View File

@ -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) -> () {
let mut x: Vec<usize> = vec![0];

View File

@ -1,10 +1,63 @@
use crate::prims::WeightedGraph;
use std::convert::TryInto;
use std::collections::HashSet;
pub fn k_clustering(graph: &WeightedGraph) -> i32 {
for vertex in graph {
println!("{:?}", vertex.edges.len());
}
graph.len().try_into().unwrap()
#[derive(Debug, Clone, Copy)]
pub struct Edge {
pub weight: i32,
pub source_id: usize,
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
}

View File

@ -1,6 +1,7 @@
mod dijkstra;
mod heap;
mod jobs;
mod k_clustering;
mod merge_sort;
mod min_cut;
mod prims;
@ -8,18 +9,17 @@ mod quick_sort;
mod ssc;
mod two_sum;
mod util;
mod k_clustering;
use crate::dijkstra::dijkstra;
use crate::heap::heap;
use crate::jobs::{jobs_1, jobs_2};
use crate::k_clustering::k_clustering;
use crate::merge_sort::merge_sort_inversions;
use crate::min_cut::min_cut;
use crate::prims::prims;
use crate::quick_sort::quick_sort;
use crate::ssc::ssc;
use crate::two_sum::find_two_sums;
use crate::k_clustering::k_clustering;
use std::cmp::min;
#[allow(dead_code)]
@ -96,7 +96,7 @@ fn c3a1() {
let mut jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap();
let r1 = jobs_1(&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);
println!("r1 = {}; r2 = {}; r3 = {} ", r1, r2, r3);
@ -105,8 +105,8 @@ fn c3a1() {
#[allow(dead_code)]
fn c3a2() {
let graph = util::read_weighted_graph("data/c3a2_clustering.txt").unwrap();
let r1 = k_clustering(&graph);
let mut graph = util::read_weighted_graph_clustering("data/c3a2_clustering.txt").unwrap();
let r1 = k_clustering(&mut graph);
println!("r1 = {:?}", r1);
}

View File

@ -1,7 +1,12 @@
use crate::util::Graph;
use rand::Rng;
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 {
let mut rng = rand::thread_rng();
// println!("min_cut({:?})\n", g);

View File

@ -1,6 +1,13 @@
use crate::util::DirectedGraph;
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) -> () {
let mut nodes: Vec<(usize, usize)> = vec![(node, 0)];
g.explored[node] = true;

View File

@ -1,30 +1,13 @@
use crate::jobs::{Job, Jobs};
use crate::prims::{Edge, Vertex, WeightedGraph};
use crate::jobs;
use crate::k_clustering;
use crate::min_cut;
use crate::prims;
use crate::ssc;
use std::fs::File;
use std::io;
use std::io::{BufRead, BufReader, Error, ErrorKind};
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> {
let file = File::open(path)?;
let br = BufReader::new(file);
@ -40,8 +23,8 @@ 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 {
pub fn read_to_graph(path: &str) -> Result<min_cut::Graph, io::Error> {
let mut g = min_cut::Graph {
nodes: vec![],
edges: vec![],
};
@ -84,16 +67,18 @@ fn get_node_size(path: &str) -> Result<usize, io::Error> {
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 mut g = DirectedGraph {
let mut g = ssc::DirectedGraph {
nodes: vec![vec![]; nodes],
explored: vec![false; nodes],
time: vec![0; nodes],
leader: vec![0; nodes],
};
let mut g_dash = DirectedGraph {
let mut g_dash = ssc::DirectedGraph {
nodes: vec![vec![]; nodes],
explored: vec![false; nodes],
time: vec![0; nodes],
@ -116,9 +101,11 @@ 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> {
pub fn read_to_directed_weighted_graph(
path: &str,
) -> Result<crate::dijkstra::DirectedWeightedGraph, io::Error> {
let nodes: usize = get_node_size(path).unwrap();
let mut g = DirectedWeightedGraph {
let mut g = crate::dijkstra::DirectedWeightedGraph {
nodes: vec![vec![]; nodes],
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)
}
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 file = File::open(path)?;
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 weight = nodes.next().unwrap().parse().unwrap();
let length = nodes.next().unwrap().parse().unwrap();
let job = Job {
let job = jobs::Job {
weight: weight,
length: length,
};
@ -162,7 +149,7 @@ pub fn read_jobs(path: &str) -> Result<Jobs, io::Error> {
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 file = File::open(path)?;
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();
for i in 0..n_nodes {
let node = Vertex {
let node = prims::Vertex {
id: i,
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 weight = nodes.next().unwrap().parse().unwrap();
let edge = Edge {
let edge = prims::Edge {
weight: weight,
source_id: id_a,
target_id: id_b,
};
g[id_a].edges.push(edge);
let edge = Edge {
let edge = crate::prims::Edge {
weight: weight,
source_id: id_b,
target_id: id_a,
@ -204,3 +191,35 @@ pub fn read_weighted_graph(path: &str) -> Result<WeightedGraph, io::Error> {
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)
}