Work on week 3 assignment 2
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user