Start to implement clustering big assignment

This commit is contained in:
Felix Martin 2021-02-11 13:48:46 -05:00
parent 99abec12d1
commit 995ee19439
4 changed files with 62 additions and 9 deletions

19
src/k_clustering_big.rs Normal file
View 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()
}

View File

@ -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() {

View File

@ -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)
}