Start to implement clustering big assignment
This commit is contained in:
parent
99abec12d1
commit
995ee19439
@ -27,12 +27,12 @@ pub fn k_clustering(graph: &mut WeightedGraph) -> usize {
|
|||||||
let cluster_id_b = node_id_to_cluster_id[edge.target_id];
|
let cluster_id_b = node_id_to_cluster_id[edge.target_id];
|
||||||
|
|
||||||
if cluster_id_a != cluster_id_b {
|
if cluster_id_a != cluster_id_b {
|
||||||
let mut cluster_b = std::mem::take(&mut clusters[cluster_id_b]);
|
let mut cluster_b = std::mem::take(&mut clusters[cluster_id_b]);
|
||||||
for node_id in &cluster_b {
|
for node_id in &cluster_b {
|
||||||
node_id_to_cluster_id[*node_id] = cluster_id_a;
|
node_id_to_cluster_id[*node_id] = cluster_id_a;
|
||||||
}
|
}
|
||||||
clusters[cluster_id_a].append(&mut cluster_b);
|
clusters[cluster_id_a].append(&mut cluster_b);
|
||||||
cluster_count -= 1;
|
cluster_count -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if cluster_count <= k {
|
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_a = node_id_to_cluster_id[edge.source_id];
|
||||||
let cluster_id_b = node_id_to_cluster_id[edge.target_id];
|
let cluster_id_b = node_id_to_cluster_id[edge.target_id];
|
||||||
if cluster_id_a != cluster_id_b {
|
if cluster_id_a != cluster_id_b {
|
||||||
if edge.weight < min_spacing {
|
if edge.weight < min_spacing {
|
||||||
min_spacing = edge.weight;
|
min_spacing = edge.weight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/k_clustering_big.rs
Normal file
19
src/k_clustering_big.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
pub struct ImpliciteGraph {
|
||||||
|
pub nodes: Vec<Vec<usize>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn distance(a: &Vec<usize>, b: &Vec<usize>) -> 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()
|
||||||
|
}
|
@ -2,6 +2,7 @@ mod dijkstra;
|
|||||||
mod heap;
|
mod heap;
|
||||||
mod jobs;
|
mod jobs;
|
||||||
mod k_clustering;
|
mod k_clustering;
|
||||||
|
mod k_clustering_big;
|
||||||
mod merge_sort;
|
mod merge_sort;
|
||||||
mod min_cut;
|
mod min_cut;
|
||||||
mod prims;
|
mod prims;
|
||||||
@ -14,6 +15,7 @@ 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::k_clustering::k_clustering;
|
||||||
|
use crate::k_clustering_big::k_clustering_big;
|
||||||
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;
|
||||||
@ -108,6 +110,9 @@ fn c3a2() {
|
|||||||
let mut graph = util::read_weighted_graph_clustering("data/c3a2_clustering.txt").unwrap();
|
let mut graph = util::read_weighted_graph_clustering("data/c3a2_clustering.txt").unwrap();
|
||||||
let r1 = k_clustering(&mut graph);
|
let r1 = k_clustering(&mut graph);
|
||||||
println!("r1 = {:?}", r1);
|
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() {
|
fn main() {
|
||||||
|
29
src/util.rs
29
src/util.rs
@ -1,5 +1,6 @@
|
|||||||
use crate::jobs;
|
use crate::jobs;
|
||||||
use crate::k_clustering;
|
use crate::k_clustering;
|
||||||
|
use crate::k_clustering_big;
|
||||||
use crate::min_cut;
|
use crate::min_cut;
|
||||||
use crate::prims;
|
use crate::prims;
|
||||||
use crate::ssc;
|
use crate::ssc;
|
||||||
@ -222,3 +223,31 @@ pub fn read_weighted_graph_clustering(
|
|||||||
|
|
||||||
Ok(g)
|
Ok(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_k_cluster_big(path: &str) -> Result<k_clustering_big::ImpliciteGraph, io::Error> {
|
||||||
|
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<usize> = line
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|s| s.parse().unwrap())
|
||||||
|
.collect();
|
||||||
|
g.nodes.push(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(g)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user