Implement Huffman encoding

This commit is contained in:
2021-02-15 11:34:41 -05:00
parent 7dba9d03ab
commit 28d5f2c664
4 changed files with 109 additions and 22 deletions

View File

@@ -4,22 +4,21 @@ pub type ImpliciteGraph = Vec<u32>;
fn neighbors(a: u32, n: usize) -> Vec<u32> {
let mut r = vec![a];
for _ in 0..n {
let mut r_new = r.clone();
for a in r {
for i in 0..24 {
let m = 0x1 << i;
let neighbor = a ^ m;
r_new.push(neighbor);
}
}
r_new.sort();
r_new.dedup();
r = r_new;
let mut r_new = r.clone();
for a in r {
for i in 0..24 {
let m = 0x1 << i;
let neighbor = a ^ m;
r_new.push(neighbor);
}
}
r_new.sort();
r_new.dedup();
r = r_new;
}
r
}
pub fn k_clustering_big(g: &ImpliciteGraph) -> usize {
let mut node_id_to_cluster_id: Vec<usize> = (0..g.len()).collect();
let mut clusters: Vec<Vec<usize>> = (0..g.len()).map(|x| vec![x]).collect();
@@ -35,13 +34,13 @@ pub fn k_clustering_big(g: &ImpliciteGraph) -> usize {
}
for node_a_id in 0..g.len() {
// Iterate over all nodes in the graph. Then, for each node compute all
// neighbors that are two or less bits away.
// Iterate over all nodes in the graph. Then, for each node compute all
// neighbors that are two or less bits away.
for node_b_value in neighbors(g[node_a_id], 2) {
// See if there exist nodes that match the neighbor. If such nodes
// exist iterate over them and merge the clusters if they are not
// already the same. The key insight is that we have to cluster all
// nodes that are two or less (that includes zero) bits apart.
// See if there exist nodes that match the neighbor. If such nodes
// exist iterate over them and merge the clusters if they are not
// already the same. The key insight is that we have to cluster all
// nodes that are two or less (that includes zero) bits apart.
if let Some(node_b_ids) = node_map.get(&node_b_value) {
for node_b_id in node_b_ids {
let cluster_id_a = node_id_to_cluster_id[node_a_id];