|
| 1 | +use metis::Graph; |
| 2 | + |
| 3 | +fn main() -> Result<(), metis::Error> { |
| 4 | + // 5 - 3 - 4 - 0 |
| 5 | + // | | / |
| 6 | + // 2 - 1 |
| 7 | + let adjncy = [1, 4, 0, 2, 4, 1, 3, 2, 4, 5, 0, 1, 3, 3]; |
| 8 | + let xadj = [0, 2, 5, 7, 10, 13, 14]; |
| 9 | + |
| 10 | + // iterate over adjacent nodes |
| 11 | + let mut it = xadj |
| 12 | + .windows(2) |
| 13 | + .map(|x| &adjncy[x[0] as usize..x[1] as usize]); |
| 14 | + |
| 15 | + // node 0 is adjacent to nodes 1 and 4 |
| 16 | + assert_eq!(it.next().unwrap(), &[1, 4]); |
| 17 | + // node 1 is adjacent to nodes 0, 2, and 4 |
| 18 | + assert_eq!(it.next().unwrap(), &[0, 2, 4]); |
| 19 | + // node 2 is adjacent to nodes 1 and 3 |
| 20 | + assert_eq!(it.next().unwrap(), &[1, 3]); |
| 21 | + // node 3 is adjacent to nodes 2, 4, and 5 |
| 22 | + assert_eq!(it.next().unwrap(), &[2, 4, 5]); |
| 23 | + // node 4 is adjacent to nodes 0, 1, and 3 |
| 24 | + assert_eq!(it.next().unwrap(), &[0, 1, 3]); |
| 25 | + // node 5 is adjacent to node 3 |
| 26 | + assert_eq!(it.next().unwrap(), &[3]); |
| 27 | + assert!(it.next().is_none()); |
| 28 | + |
| 29 | + let mut partition = vec![0; xadj.len() - 1]; |
| 30 | + let n_parts = 2; |
| 31 | + Graph::new(1, n_parts, &xadj, &adjncy)?.part_kway(&mut partition)?; |
| 32 | + |
| 33 | + // group 0 consists out of node 0, 1 and 4 |
| 34 | + // group 1 consists out of node 2, 3 and 5 |
| 35 | + assert_eq!(&partition, &[0, 0, 1, 1, 0, 1]); |
| 36 | + |
| 37 | + let group = |group| { |
| 38 | + partition |
| 39 | + .iter() |
| 40 | + .enumerate() |
| 41 | + .filter(|(_, b)| **b == group) |
| 42 | + .map(|(a, _)| a) |
| 43 | + .collect::<Vec<_>>() |
| 44 | + }; |
| 45 | + // group 0 consists out of node 0, 1 and 4 |
| 46 | + assert_eq!(&group(0), &[0, 1, 4]); |
| 47 | + // group 1 consists out of node 2, 3 and 5 |
| 48 | + assert_eq!(&group(1), &[2, 3, 5]); |
| 49 | + |
| 50 | + Ok(()) |
| 51 | +} |
0 commit comments