From 995ee194396bba53ca3f6eedcfca21b9cc67d959 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Thu, 11 Feb 2021 13:48:46 -0500 Subject: [PATCH] Start to implement clustering big assignment --- src/k_clustering.rs | 18 +++++++++--------- src/k_clustering_big.rs | 19 +++++++++++++++++++ src/main.rs | 5 +++++ src/util.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/k_clustering_big.rs diff --git a/src/k_clustering.rs b/src/k_clustering.rs index 2444efd..0ed58a4 100644 --- a/src/k_clustering.rs +++ b/src/k_clustering.rs @@ -27,12 +27,12 @@ pub fn k_clustering(graph: &mut WeightedGraph) -> usize { let cluster_id_b = node_id_to_cluster_id[edge.target_id]; if cluster_id_a != cluster_id_b { - let mut cluster_b = std::mem::take(&mut clusters[cluster_id_b]); - for node_id in &cluster_b { - node_id_to_cluster_id[*node_id] = cluster_id_a; - } - clusters[cluster_id_a].append(&mut cluster_b); - cluster_count -= 1; + let mut cluster_b = std::mem::take(&mut clusters[cluster_id_b]); + for node_id in &cluster_b { + node_id_to_cluster_id[*node_id] = cluster_id_a; + } + clusters[cluster_id_a].append(&mut cluster_b); + cluster_count -= 1; } if cluster_count <= k { @@ -45,9 +45,9 @@ pub fn k_clustering(graph: &mut WeightedGraph) -> usize { let cluster_id_a = node_id_to_cluster_id[edge.source_id]; let cluster_id_b = node_id_to_cluster_id[edge.target_id]; if cluster_id_a != cluster_id_b { - if edge.weight < min_spacing { - min_spacing = edge.weight; - } + if edge.weight < min_spacing { + min_spacing = edge.weight; + } } } diff --git a/src/k_clustering_big.rs b/src/k_clustering_big.rs new file mode 100644 index 0000000..22628e6 --- /dev/null +++ b/src/k_clustering_big.rs @@ -0,0 +1,19 @@ +pub struct ImpliciteGraph { + pub nodes: Vec>, +} + +fn distance(a: &Vec, b: &Vec) -> u16 { + let mut distance = 0; + + for i in 0..a.len() { + if a[i] != b[i] { + distance += 1; + } + } + distance +} + +pub fn k_clustering_big(g: &ImpliciteGraph) -> usize { + println!("distance: {:?}", distance(&g.nodes[0], &g.nodes[1])); + g.nodes.len() +} diff --git a/src/main.rs b/src/main.rs index ba45ad6..9e53e6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod dijkstra; mod heap; mod jobs; mod k_clustering; +mod k_clustering_big; mod merge_sort; mod min_cut; mod prims; @@ -14,6 +15,7 @@ use crate::dijkstra::dijkstra; use crate::heap::heap; use crate::jobs::{jobs_1, jobs_2}; use crate::k_clustering::k_clustering; +use crate::k_clustering_big::k_clustering_big; use crate::merge_sort::merge_sort_inversions; use crate::min_cut::min_cut; use crate::prims::prims; @@ -108,6 +110,9 @@ fn c3a2() { let mut graph = util::read_weighted_graph_clustering("data/c3a2_clustering.txt").unwrap(); let r1 = k_clustering(&mut graph); println!("r1 = {:?}", r1); + let graph = util::read_k_cluster_big("data/c3a2_clustering_big.txt").unwrap(); + let r2 = k_clustering_big(&graph); + println!("r2 = {:?}", r2); } fn main() { diff --git a/src/util.rs b/src/util.rs index 394e2c3..8a5e9f7 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ use crate::jobs; use crate::k_clustering; +use crate::k_clustering_big; use crate::min_cut; use crate::prims; use crate::ssc; @@ -222,3 +223,31 @@ pub fn read_weighted_graph_clustering( Ok(g) } + +pub fn read_k_cluster_big(path: &str) -> Result { + let file = File::open(path)?; + let mut lines = BufReader::new(file).lines(); + let line = lines.next().unwrap().unwrap(); + + let mut fields = line.split_whitespace(); + let total_nodes: usize = fields.next().unwrap().parse().unwrap(); + let bits_per_node: usize = fields.next().unwrap().parse().unwrap(); + + println!( + "total_nodes = {:?}\nbits_per_node = {:?}", + total_nodes, bits_per_node + ); + + let mut g = k_clustering_big::ImpliciteGraph { nodes: vec![] }; + + for line in lines { + let line = line?; + let v: Vec = line + .split_whitespace() + .map(|s| s.parse().unwrap()) + .collect(); + g.nodes.push(v); + } + + Ok(g) +}