Start to implement clustering big assignment
This commit is contained in:
parent
99abec12d1
commit
995ee19439
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 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() {
|
||||
|
29
src/util.rs
29
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<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