bit_gossip 0.0.13

Pathfinding library for calculating all node pairs' shortest paths in an unweighted undirected graph.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
use super::U16orU32;
use crate::{bitvec::BitVec, edge_id};
use std::{collections::HashMap, fmt::Debug};

#[derive(Debug, Clone)]
pub struct SeqGraph<NodeId: U16orU32 = u16> {
    pub nodes: Nodes<NodeId>,
    pub edges: HashMap<(NodeId, NodeId), BitVec>,
}

impl<NodeId: U16orU32> SeqGraph<NodeId> {
    /// Create a new SeqGraphBuilder with the given number of nodes.
    ///
    /// Default NodeId is u16, which can hold up to 65536 nodes.
    /// If you need more nodes, you can specify u32 as the NodeId type, like `SeqGraph::<u32>::builder(100_000)`
    #[inline]
    pub fn builder(nodes_len: usize) -> SeqGraphBuilder<NodeId> {
        debug_assert!(
            nodes_len <= NodeId::MAX_NODES,
            "Number of nodes exceeds the limit; Specify `u32` as the NodeId type, like `SeqGraph::<u32>::builder(100_000)`"
        );

        SeqGraphBuilder::new(nodes_len.min(NodeId::MAX_NODES))
    }

    /// Converts this graph into a builder.
    ///
    /// This is useful if you want to update the graph,
    /// like resizing nodes or adding/removing edges.
    ///
    /// Then you can build the graph again.
    #[inline]
    pub fn into_builder(self) -> SeqGraphBuilder<NodeId> {
        SeqGraphBuilder {
            edge_masks: Edges {
                inner: self.edges.iter().map(|(k, _)| (*k, BitVec::ZERO)).collect(),
            },
            edges: Edges { inner: self.edges },
            nodes: self.nodes,
        }
    }

    /// Given a current node and a destination node,
    /// return the first neighboring node that is the shortest path to the destination node.
    ///
    /// This operation is very fast as all paths for all nodes are precomputed.
    ///
    /// `None` is returned when:
    /// - `curr` and `dest` are the same node
    /// - `curr` has no path to `dest`
    ///
    /// **Note:** In case there are multiple neighboring nodes that lead to the destination node,
    /// the first one found will be returned. The same node will be returned for the same input.
    /// However, the order of the nodes is not guaranteed.
    ///
    /// You can use [neighbor_to_with](Self::neighbor_to_with) to filter matching neighbors,
    /// or [neighbors_to](Self::neighbors_to) to get all neighboring nodes.
    #[inline]
    pub fn neighbor_to(&self, curr: NodeId, dest: NodeId) -> Option<NodeId> {
        self.neighbors_to(curr, dest).next()
    }

    /// Given a current node and a destination node, and a filter function,
    /// return the neighboring node that is the shortest path to the destination node.
    ///
    /// Same as `self.neighbors_to(curr, dest).find(f)`
    ///
    /// This may be useful if you want some custom behavior when choosing the next node.
    ///
    /// **Ex)** In a game, you might want to randomize which path to take when there are multiple shortest paths.
    ///
    /// `None` is returned when:
    /// - `curr` and `dest` are the same node
    /// - `curr` has no path to `dest`
    /// - The filter function returns `false` for all neighboring nodes
    #[inline]
    pub fn neighbor_to_with(
        &self,
        curr: NodeId,
        dest: NodeId,
        f: impl Fn(NodeId) -> bool,
    ) -> Option<NodeId> {
        self.neighbors_to(curr, dest).find(|&n| f(n))
    }

    /// Given a current node and a destination node,
    /// return all neighboring nodes that are shortest paths to the destination node.
    ///
    /// The nodes will be returned in the same order for the same inputs. However, the ordering of the nodes is not guaranteed.
    #[inline]
    pub fn neighbors_to(&self, curr: NodeId, dest: NodeId) -> NeighborsToIter<'_, NodeId> {
        NeighborsToIter {
            graph: self,
            neighbors: self.nodes.neighbors(curr).iter(),
            curr,
            dest,
        }
    }

    /// Given a current node and a destination node,
    /// return a path from the current node to the destination node.
    ///
    /// The path is a list of node IDs, starting with current node and ending at the destination node.
    ///
    /// This is same as calling `.neighbor_to` repeatedly until the destination node is reached.
    ///
    /// If there is no path, the list will be empty.
    #[inline]
    pub fn path_to(&self, curr: NodeId, dest: NodeId) -> PathIter<'_, NodeId> {
        PathIter {
            map: self,
            curr,
            dest,
            init: false,
        }
    }

    /// Check if there is a path from the current node to the destination node.
    #[inline]
    pub fn path_exists(&self, curr: NodeId, dest: NodeId) -> bool {
        self.neighbor_to(curr, dest).is_some()
    }

    /// Return a list of all neighboring nodes of the given node.
    #[inline]
    pub fn neighbors(&self, node: NodeId) -> &[NodeId] {
        self.nodes.neighbors(node)
    }

    /// Return the number of nodes in this graph.
    #[inline]
    pub fn nodes_len(&self) -> usize {
        self.nodes.len()
    }

    /// Return the number of edges in this graph.
    #[inline]
    pub fn edges_len(&self) -> usize {
        self.edges.len()
    }
}

/// An iterator that returns a path from the current node to the destination node.
#[derive(Debug)]
pub struct PathIter<'a, NodeId: U16orU32> {
    map: &'a SeqGraph<NodeId>,
    curr: NodeId,
    dest: NodeId,
    init: bool,
}

impl<NodeId: U16orU32> Iterator for PathIter<'_, NodeId> {
    type Item = NodeId;

    fn next(&mut self) -> Option<Self::Item> {
        if self.curr == self.dest {
            return None;
        }

        if !self.init {
            self.init = true;
            return Some(self.curr);
        }

        let Some(next) = self.map.neighbor_to(self.curr, self.dest) else {
            return None;
        };

        self.curr = next;

        Some(next)
    }
}

/// An iterator that returns neighboring nodes that are shortest paths to the destination node.
#[derive(Debug)]
pub struct NeighborsToIter<'a, NodeId: U16orU32> {
    graph: &'a SeqGraph<NodeId>,
    curr: NodeId,
    dest: NodeId,
    neighbors: std::slice::Iter<'a, NodeId>,
}

impl<NodeId: U16orU32> Iterator for NeighborsToIter<'_, NodeId> {
    type Item = NodeId;

    fn next(&mut self) -> Option<Self::Item> {
        if self.curr == self.dest {
            return None;
        }

        while let Some(&neighbor) = self.neighbors.next() {
            let bit = self
                .graph
                .edges
                .get(&edge_id(self.curr, neighbor))?
                .get_bit(self.dest.as_usize());
            let bit = if self.curr > neighbor { !bit } else { bit };

            if bit {
                return Some(neighbor);
            }
        }

        None
    }
}

/// A builder for creating a [SeqGraph].
#[derive(Debug, Clone)]
pub struct SeqGraphBuilder<NodeId: U16orU32> {
    /// key: node_id
    ///
    /// value: neighbors of node
    pub nodes: Nodes<NodeId>,

    /// key: edge_id
    ///
    /// value: for each bit, if this edge is the shortest path
    /// to that bit location's node, bit is set to 1
    pub edges: Edges<NodeId>,

    /// key: edge_id
    ///
    /// value: for each edge, bit is set to 1 if the node is computed
    pub edge_masks: Edges<NodeId>,
}

impl<NodeId: U16orU32> SeqGraphBuilder<NodeId> {
    /// Create a new SeqGraphBuilder with the given number of nodes.
    #[inline]
    pub fn new(nodes_len: usize) -> Self {
        Self {
            nodes: Nodes::new(nodes_len),
            edges: Edges::new(),
            edge_masks: Edges::new(),
        }
    }

    /// Resize the graph to the given number of nodes.
    ///
    /// All edges that are connected to nodes that are removed will also be removed.
    pub fn resize(&mut self, nodes_len: usize) {
        let should_truncate = nodes_len < self.nodes.len();

        self.nodes.resize(nodes_len);

        if should_truncate {
            self.edges.truncate(nodes_len);
            self.edge_masks.truncate(nodes_len);
        }
    }

    /// Add a edge between node_a and node_b
    #[inline]
    pub fn connect(&mut self, a: NodeId, b: NodeId) {
        self.nodes.connect(a, b);

        // edge value is flipped to b -> a, which means from node b's perspective, this edge is:
        // - gets further away from b
        // - shortest path to a
        // - gets further away from all other nodes
        let val = if a > b { a } else { b };

        let ab = edge_id(a, b);

        if let Some(edge) = self.edges.inner.get_mut(&ab) {
            edge.set_bit(val.as_usize(), true);
        } else {
            let edge = BitVec::one(val.as_usize());
            self.edges.inner.insert(ab, edge);
        }

        if let Some(edge) = self.edge_masks.inner.get_mut(&ab) {
            edge.set_bit(a.as_usize(), true);
            edge.set_bit(b.as_usize(), true);
        } else {
            let mut edge = BitVec::one(a.max(b).as_usize());
            edge.set_bit(a.min(b).as_usize(), true);
            self.edge_masks.inner.insert(ab, edge);
        }
    }

    #[inline]
    pub fn disconnect(&mut self, a: NodeId, b: NodeId) {
        // if the edge doesn't exist, return
        self.nodes.disconnect(a, b);

        let ab = edge_id(a, b);

        if self.edge_masks.inner.remove(&ab).is_some() {
            self.edges.inner.remove(&ab);
        }
    }

    #[inline]
    pub fn build(self) -> SeqGraph<NodeId> {
        let Self {
            nodes,
            mut edges,
            mut edge_masks,
            ..
        } = self;

        // (neighbors at current depth, neighbors at previous depths)
        let mut neighbors_at_depth: Vec<(BitVec, BitVec)> = nodes
            .inner
            .iter()
            .enumerate()
            .map(|(i, e)| {
                let mut neighbors = BitVec::ZERO;
                for n in e {
                    neighbors.set_bit(n.as_usize(), true);
                }
                (neighbors, BitVec::one(i))
            })
            .collect();

        let mut active_neighbors_mask = BitVec::ZERO;

        // each rooom's bit is set to 1 if all its edges are done computed
        let mut done_nodes = BitVec::ZERO;

        let full_mask = BitVec::ones(nodes.len());

        let mut neighbor_upserts: Vec<(BitVec, BitVec, BitVec)> = Vec::new();

        for (a, a_neighbors) in nodes.inner.iter().enumerate() {
            // setup
            // clear upserts
            neighbor_upserts.iter_mut().for_each(|(e1, e2, e3)| {
                e1.clear();
                e2.clear();
                e3.clear();
            });
            if neighbor_upserts.len() < a_neighbors.len() {
                neighbor_upserts.resize(
                    a_neighbors.len(),
                    (BitVec::ZERO, BitVec::ZERO, BitVec::ZERO),
                );
            }

            // for each edge in this node
            // set the bit value for a and b as 1
            for (i, b) in a_neighbors.iter().enumerate() {
                let b = b.as_usize();

                let mut val = true;

                // edge value is flipped to b -> a, which means from node b's perspective, this edge is:
                // - gets further away from b
                // - shortest path to a
                // - gets further away from all other nodes
                if a > b {
                    val = false;
                }

                // for all other edges in this node, set the value for this node bit as 0
                for (j, c) in a_neighbors.iter().enumerate() {
                    if i == j {
                        continue;
                    }

                    // if both b and c are in the same corner (tl or br)
                    // flip the bit
                    let should_set = if (a > b) == (a > c.as_usize()) {
                        !val
                    } else {
                        val
                    };

                    let (upsert, computed, _) = &mut neighbor_upserts[j];
                    if should_set {
                        upsert.set_bit(b, true);
                    }
                    computed.set_bit(b, true);
                }
            }

            let a = NodeId::from_usize(a);

            // apply computed values
            for (b, upserts) in a_neighbors.into_iter().zip(neighbor_upserts.drain(..)) {
                let ab = edge_id(a, *b);

                let (upsert, computed, _) = upserts;

                if !computed.is_zero() {
                    if !upsert.is_zero() {
                        edges.insert(ab, upsert);
                    }
                    edge_masks.insert(ab, computed);
                }
            }
        }

        let mut set_done_list = Vec::new();

        loop {
            // iterate through all undone nodes
            for a in done_nodes.iter_zeros() {
                if a >= nodes.len() {
                    break;
                }

                let a_usize = a;
                let a = NodeId::from_usize(a);

                let a_neighbors = nodes.neighbors(a);

                // clear upserts
                neighbor_upserts.iter_mut().for_each(|(e1, e2, e3)| {
                    e1.clear();
                    e2.clear();
                    e3.clear();
                });
                if neighbor_upserts.len() < a_neighbors.len() {
                    neighbor_upserts.resize(
                        a_neighbors.len(),
                        (BitVec::ZERO, BitVec::ZERO, BitVec::ZERO),
                    );
                }

                // collect all nodes that need to update their neighbors to next depth
                let mut a_active_neighbors_mask = BitVec::ZERO;

                // are all edges computed for this node?
                let mut all_edges_done = true;

                // get all neighbors' masks
                // so we can just reuse it
                for (i, b) in a_neighbors.iter().enumerate() {
                    let mask = edge_masks.get(edge_id(a, *b)).unwrap();
                    neighbor_upserts[i].2 = mask.clone();

                    if !mask.eq(&full_mask) {
                        all_edges_done = false;
                    }
                }

                if all_edges_done {
                    set_done_list.push(a);

                    continue;
                }

                for (i, b) in a_neighbors.iter().copied().enumerate() {
                    let b_usize = b.as_usize();

                    // neighbors' bits to gossip from edge a->b to other edges
                    let mut neighbors_mask = neighbors_at_depth[b_usize].0.clone();

                    neighbors_mask.set_bit(a_usize, false);

                    // if no neighbors to gossip at this depth, skip
                    if neighbors_mask.is_zero() {
                        continue;
                    }

                    a_active_neighbors_mask.set_bit(b_usize, true);

                    let ab = edge_id(a, b);

                    let val = edges.get(ab).unwrap();

                    // gossip to other edges about its neighbors at current depth
                    for (j, c) in a_neighbors.iter().copied().enumerate() {
                        // skip if same neighbor
                        if i == j {
                            continue;
                        }

                        let mask_ac = &neighbor_upserts[j].2;
                        if mask_ac.eq(&full_mask) {
                            continue;
                        }
                        all_edges_done = false;

                        let mut compute_mask = neighbors_mask.clone();
                        // dont set bits that are already computed
                        compute_mask.bitand_not_assign(&mask_ac);

                        // if all bits are already computed, skip
                        if compute_mask.is_zero() {
                            continue;
                        }

                        let (upsert, computed, _) = &mut neighbor_upserts[j];

                        // if both b and c are in the same corner (tl or br)
                        // flip the bit
                        if (a_usize > b_usize) == (a_usize > c.as_usize()) {
                            upsert.bitor_not_and_assign(val, &compute_mask);
                        } else {
                            upsert.bitor_and_assign(val, &compute_mask);
                        };

                        computed.bitor_assign(&compute_mask);
                    }
                }

                // if all edges are computed or none of a's neighbors are active,
                // then a is done
                if all_edges_done || a_active_neighbors_mask.is_zero() {
                    set_done_list.push(a);
                } else {
                    for (b, upserts) in a_neighbors.iter().copied().zip(neighbor_upserts.drain(..))
                    {
                        let ab = edge_id(a, b);

                        let (upsert, computed, _) = upserts;

                        if !computed.is_zero() {
                            if !upsert.is_zero() {
                                edges.insert(ab, upsert);
                            }
                            edge_masks.insert(ab, computed);
                        }
                    }
                }

                active_neighbors_mask.bitor_assign(&a_active_neighbors_mask);
            }

            for a in &set_done_list {
                done_nodes.set_bit(a.as_usize(), true);
            }
            set_done_list.clear();

            if done_nodes.eq(&full_mask) {
                break;
            }

            for a in active_neighbors_mask.iter_ones() {
                let (a_neighbors_at_depth, prev_neighbors) = &mut neighbors_at_depth[a];

                if a_neighbors_at_depth.is_zero() {
                    continue;
                }

                // add previous neighbors to prev neighbors
                prev_neighbors.bitor_assign(&a_neighbors_at_depth);

                let mut new_neighbors = BitVec::ZERO;
                for b in a_neighbors_at_depth.iter_ones() {
                    for c in nodes.neighbors(NodeId::from_usize(b)) {
                        new_neighbors.set_bit(c.as_usize(), true);
                    }
                }

                // new neighbors at this depth without the previous neighbors
                new_neighbors.bitand_not_assign(&prev_neighbors);
                *a_neighbors_at_depth = new_neighbors;
            }

            active_neighbors_mask.clear();
        }

        SeqGraph {
            nodes,
            edges: edges.inner,
        }
    }

    /// Return the number of nodes in this graph.
    #[inline]
    pub fn nodes_len(&self) -> usize {
        self.nodes.len()
    }

    /// Return the number of edges in this graph.
    #[inline]
    pub fn edges_len(&self) -> usize {
        self.edges.inner.len()
    }

    /// Return the neighbors of the given node.
    #[inline]
    pub fn neighbors(&self, node: NodeId) -> &[NodeId] {
        self.nodes.neighbors(node)
    }
}

/// Map of nodes and their neighbors.
///
/// index: node_id
///
/// value: neighbors of node
#[derive(Debug, Clone)]
pub struct Nodes<NodeId: U16orU32> {
    pub inner: Vec<Vec<NodeId>>,
}

impl<NodeId: U16orU32> Nodes<NodeId> {
    #[inline]
    pub fn new(nodes_len: usize) -> Self {
        Self {
            inner: vec![vec![]; nodes_len],
        }
    }

    #[inline]
    pub fn resize(&mut self, nodes_len: usize) {
        let prev_len = self.inner.len();
        self.inner.resize(nodes_len, vec![]);

        if nodes_len < prev_len {
            let nodes_len = NodeId::from_usize(nodes_len);

            for neighbors in self.inner.iter_mut() {
                neighbors.retain(|&i| i < nodes_len);
            }
        }
    }

    /// Get the neighboring nodes
    #[inline]
    pub fn neighbors(&self, node: NodeId) -> &[NodeId] {
        &self.inner[node.as_usize()]
    }

    /// Add a edge between node_a and node_b
    #[inline]
    pub fn connect(&mut self, a: NodeId, b: NodeId) {
        if a == b {
            return;
        }

        if !self.inner[a.as_usize()].contains(&b) {
            self.inner[a.as_usize()].push(b);
        }

        self.inner[b.as_usize()].push(a);
    }

    /// Remove a edge between node_a and node_b
    #[inline]
    pub fn disconnect(&mut self, a: NodeId, b: NodeId) {
        if a == b {
            return;
        }

        if let Some(index) = self.inner[a.as_usize()].iter().position(|&x| x == b) {
            self.inner[a.as_usize()].swap_remove(index);
        }
        if let Some(index) = self.inner[b.as_usize()].iter().position(|&x| x == a) {
            self.inner[b.as_usize()].swap_remove(index);
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len()
    }
}

/// Map of edges and their shortest paths to other nodes.
///
/// key: edge_id
///
/// value: for each bit, if this edge is the shortest path
/// to that bit location's node, bit is set to 1
#[derive(Debug, Clone)]
pub struct Edges<NodeId: U16orU32> {
    /// key: edge_id
    ///
    /// value: for each bit, if this edge is the shortest path
    /// to that bit location's node, bit is set to 1
    inner: HashMap<(NodeId, NodeId), BitVec>,
}

impl<NodeId: U16orU32> Edges<NodeId> {
    #[inline]
    fn new() -> Self {
        Self {
            inner: HashMap::new(),
        }
    }

    /// Return the shortest-paths-indicating bit vector.
    #[inline]
    pub fn get(&self, edge_id: (NodeId, NodeId)) -> Option<&BitVec> {
        self.inner.get(&edge_id)
    }

    /// Insert a new edge with its shortest paths.
    ///
    /// If the edge already exists, the shortest paths will be merged.
    #[inline]
    pub fn insert(&mut self, edge_id: (NodeId, NodeId), val: BitVec) {
        if let Some(bits) = self.inner.get_mut(&edge_id) {
            bits.bitor_assign(&val);
        } else {
            self.inner.insert(edge_id, val);
        }
    }

    /// Truncate the edges to the given length of nodes.
    pub fn truncate(&mut self, nodes_len: usize) {
        let keys_to_remove = self
            .inner
            .keys()
            .filter(|&(a, b)| a.as_usize() >= nodes_len || b.as_usize() >= nodes_len)
            .cloned()
            .collect::<Vec<_>>();

        for key in keys_to_remove {
            self.inner.remove(&key);
        }

        for edge in self.inner.values_mut() {
            edge.truncate(nodes_len);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[ignore]
    #[test]
    fn test_seq_graph() {
        pub const NODES_X_LEN: usize = 100;
        pub const NODES_Y_LEN: usize = 200;
        pub const NODES_LEN: usize = NODES_X_LEN * NODES_Y_LEN;

        let mut builder = SeqGraphBuilder::new(NODES_LEN);

        // place a edge between every adjacent node
        for y in 0..NODES_Y_LEN {
            for x in 0..NODES_X_LEN {
                let node_id = y * NODES_X_LEN + x;

                if x > 0 {
                    let a = (node_id - 1) as u16;
                    let b = node_id as u16;
                    builder.connect(a, b);
                }

                if y > 0 {
                    let a = node_id as u16;
                    let b = (node_id - NODES_X_LEN) as u16;
                    builder.connect(a, b);
                }
            }
        }

        let now = std::time::Instant::now();
        let _map = builder.build();
        println!("Time: {:?}", now.elapsed());

        // std::thread::sleep(std::time::Duration::from_secs(20));
        // drop(map);
    }
}