fn init_cnm_communities()

in src/dachshund/algorithms/cnm_communities.rs [94:159]


    fn init_cnm_communities(&self) -> CNMCommunityIntermediaryState {
        // stores current communities
        let mut communities: HashMap<usize, Community> = HashMap::new();
        let mut degree_map: HashMap<usize, usize> = HashMap::new();
        // binary map -- for finding delta_q_ik
        let mut delta_q_bmap: HashMap<usize, HashMap<usize, f64>> = HashMap::new();
        // max heaps -- for argmax_j delta_q_ij
        // using the fact that tupled are compared in lexicographic order
        // first element holds delta_q, 2nd holds index
        let mut delta_q_maxheap: HashMap<usize, CNMCommunityMergeInstructionHeap> = HashMap::new();
        let mut reverse_id_map: HashMap<NodeId, usize> = HashMap::new();

        let mut num_edges: usize = 0;
        let mut sorted_ids: Vec<NodeId> = Vec::with_capacity(self.count_nodes());
        for id in self.get_ids_iter() {
            sorted_ids.push(*id);
        }
        sorted_ids.sort();
        for (i, id) in sorted_ids.into_iter().enumerate() {
            let mut community: Community = HashSet::new();
            community.insert(id);
            communities.insert(i, community);

            let d = self.get_node(id).degree();

            degree_map.insert(i, d);
            reverse_id_map.insert(id, i);
            num_edges += d;
            delta_q_maxheap.insert(i, BinaryHeap::new());
            delta_q_bmap.insert(i, HashMap::new());
        }
        num_edges /= 2;
        let q0: f64 = 1.0 / (num_edges as f64);
        for (_i, community) in communities.iter() {
            for id in community {
                for e in self.get_node(*id).get_edges() {
                    let neighbor_id = e.get_neighbor_id();
                    let i: &usize = reverse_id_map.get(&id).unwrap();
                    let j: &usize = reverse_id_map.get(&neighbor_id).unwrap();
                    let k_i: usize = degree_map[i];
                    let k_j: usize = degree_map[j];
                    let delta_qij: f64 =
                        q0 - 2. * ((k_i * k_j) as f64) / (((2 * num_edges).pow(2)) as f64);
                    delta_q_bmap.get_mut(i).unwrap().insert(*j, delta_qij);
                    delta_q_maxheap
                        .get_mut(i)
                        .unwrap()
                        .push(CNMCommunityMergeInstruction::new(
                            OrderedFloat(delta_qij),
                            *i,
                            *j,
                        ));
                }
            }
        }
        let maxh = self.get_max_maxheap(&delta_q_maxheap);

        CNMCommunityIntermediaryState {
            communities,
            degree_map,
            delta_q_bmap,
            delta_q_maxheap,
            maxh,
            num_edges,
        }
    }