networkit_rs/
coarsening.rs

1use std::collections::BTreeMap;
2
3use cxx::UniquePtr;
4use miette::IntoDiagnostic;
5
6use crate::{
7    base::Algorithm,
8    bridge::{self, *},
9    tools::NodeIter,
10};
11
12pub trait GraphCoarsening: Algorithm {
13    fn get_coarse_graph(&self) -> crate::Graph;
14    fn get_fine_to_coarse_node_mapping(&self) -> NodeIter;
15    fn get_coarse_to_fine_node_mapping(&self) -> BTreeMap<u64, Vec<u64>>;
16}
17
18pub struct ParallelPartitionCoarsening {
19    inner: UniquePtr<bridge::ParallelPartitionCoarsening>,
20}
21
22impl ParallelPartitionCoarsening {
23    pub fn new(g: &crate::Graph, zeta: &crate::Partition, parallel: bool) -> Self {
24        Self {
25            inner: NewParallelPartitionCoarsening(g, zeta, parallel),
26        }
27    }
28}
29
30impl GraphCoarsening for ParallelPartitionCoarsening {
31    fn get_coarse_graph(&self) -> crate::Graph {
32        ParallelPartitionCoarseningGetCoarseGraph(&self.inner).into()
33    }
34
35    fn get_fine_to_coarse_node_mapping(&self) -> NodeIter {
36        NodeIter {
37            nodes: ParallelPartitionCoarseningGetFineToCoarseNodeMapping(&self.inner),
38            at: 0,
39        }
40    }
41
42    fn get_coarse_to_fine_node_mapping(&self) -> BTreeMap<u64, Vec<u64>> {
43        let mut ret: BTreeMap<u64, Vec<u64>> = BTreeMap::new();
44        for (i, k) in self.get_fine_to_coarse_node_mapping().enumerate() {
45            ret.entry(k).or_default().push(i as u64);
46        }
47        ret
48    }
49}
50
51impl Algorithm for ParallelPartitionCoarsening {
52    fn run(&mut self) -> miette::Result<()> {
53        self.inner.pin_mut().run().into_diagnostic()
54    }
55
56    fn has_finished(&self) -> bool {
57        self.inner.hasFinished()
58    }
59}