Finish k-clustering implementation
This commit is contained in:
@@ -1,63 +1,55 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Edge {
|
||||
pub weight: i32,
|
||||
pub weight: usize,
|
||||
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 node_id_to_cluster_id: Vec<usize> = (0..graph.n_nodes).collect();
|
||||
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
|
||||
// Sort edges by increasing weight
|
||||
graph
|
||||
.edges
|
||||
.sort_by(|a, b| b.weight.partial_cmp(&a.weight).unwrap());
|
||||
.sort_by(|a, b| a.weight.partial_cmp(&b.weight).unwrap());
|
||||
|
||||
for edge in &graph.edges {
|
||||
let ca_id = graph.cluster[edge.source_id];
|
||||
let cb_id = graph.cluster[edge.target_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];
|
||||
|
||||
if ca_id == cb_id {
|
||||
continue; // Nodes are already in the same cluster
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
clusters[cluster_id_a].append(&mut cluster_b);
|
||||
cluster_count -= 1;
|
||||
}
|
||||
|
||||
if cluster_count <= k {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for cluster in &clusters {
|
||||
if cluster.len() > 0 {
|
||||
// println!("{:?}", cluster);
|
||||
let mut min_spacing = usize::MAX;
|
||||
for edge in &graph.edges {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
graph.n_nodes
|
||||
min_spacing
|
||||
}
|
||||
|
||||
@@ -202,7 +202,6 @@ pub fn read_weighted_graph_clustering(
|
||||
|
||||
let mut g = k_clustering::WeightedGraph {
|
||||
n_nodes: n_nodes,
|
||||
cluster: (0..n_nodes).collect(),
|
||||
edges: Vec::new(),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user