petgraph 0.8.3

Graph data structure library. Provides graph types and graph algorithms.
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
use alloc::collections::BinaryHeap;
use core::hash::Hash;
use hashbrown::hash_map::{
    Entry::{Occupied, Vacant},
    HashMap,
};

use crate::algo::Measure;
use crate::scored::MinScored;
use crate::visit::{EdgeRef, IntoEdges, IntoEdgesDirected, VisitMap, Visitable};
use crate::Direction;

/// Dijkstra's shortest path algorithm.
///
/// Compute the length of the shortest path from `start` to every reachable
/// node.
///
/// The function `edge_cost` should return the cost for a particular edge, which is used
/// to compute path costs. Edge costs must be non-negative.
///
/// If `goal` is not `None`, then the algorithm terminates once the `goal` node's
/// cost is calculated.
///
/// # Arguments
/// * `graph`: weighted graph.
/// * `start`: the start node.
/// * `goal`: optional *goal* node.
/// * `edge_cost`: closure that returns cost of a particular edge.
///
/// # Returns
/// * `HashMap`: [`struct@hashbrown::HashMap`] that maps `NodeId` to path cost.
///
/// # Complexity
/// * Time complexity: **O((|V|+|E|)log(|V|))**.
/// * Auxiliary space: **O(|V|+|E|)**.
///
/// where **|V|** is the number of nodes and **|E|** is the number of edges.
///
/// # Example
/// ```rust
/// use petgraph::Graph;
/// use petgraph::algo::dijkstra;
/// use petgraph::prelude::*;
/// use hashbrown::HashMap;
///
/// let mut graph: Graph<(), (), Directed> = Graph::new();
/// let a = graph.add_node(()); // node with no weight
/// let b = graph.add_node(());
/// let c = graph.add_node(());
/// let d = graph.add_node(());
/// let e = graph.add_node(());
/// let f = graph.add_node(());
/// let g = graph.add_node(());
/// let h = graph.add_node(());
/// // z will be in another connected component
/// let z = graph.add_node(());
///
/// graph.extend_with_edges(&[
///     (a, b),
///     (b, c),
///     (c, d),
///     (d, a),
///     (e, f),
///     (b, e),
///     (f, g),
///     (g, h),
///     (h, e),
/// ]);
/// // a ----> b ----> e ----> f
/// // ^       |       ^       |
/// // |       v       |       v
/// // d <---- c       h <---- g
///
/// let expected_res: HashMap<NodeIndex, usize> = [
///     (a, 3),
///     (b, 0),
///     (c, 1),
///     (d, 2),
///     (e, 1),
///     (f, 2),
///     (g, 3),
///     (h, 4),
/// ].iter().cloned().collect();
/// let res = dijkstra(&graph, b, None, |_| 1);
/// assert_eq!(res, expected_res);
/// // z is not inside res because there is not path from b to z.
/// ```
pub fn dijkstra<G, F, K>(
    graph: G,
    start: G::NodeId,
    goal: Option<G::NodeId>,
    edge_cost: F,
) -> HashMap<G::NodeId, K>
where
    G: IntoEdges + Visitable,
    G::NodeId: Eq + Hash,
    F: FnMut(G::EdgeRef) -> K,
    K: Measure + Copy,
{
    with_dynamic_goal(graph, start, |node| goal.as_ref() == Some(node), edge_cost).scores
}

/// Return value of [`with_dynamic_goal`].
pub struct AlgoResult<N, K> {
    /// A [`struct@hashbrown::HashMap`] that maps `NodeId` to path cost.
    pub scores: HashMap<N, K>,
    /// The goal node that terminated the search, if any was found.
    pub goal_node: Option<N>,
}

/// Dijkstra's shortest path algorithm with a dynamic goal.
///
/// This algorithm is identical to [`dijkstra`],
/// but allows matching multiple goal nodes, whichever is reached first.
/// A node is considered a goal if `goal_fn` returns `true` for it.
///
/// See the [`dijkstra`] function for more details.
///
/// # Example
/// ```rust
/// use petgraph::Graph;
/// use petgraph::algo::dijkstra;
/// use petgraph::prelude::*;
/// use hashbrown::HashMap;
///
/// let mut graph: Graph<(), (), Directed> = Graph::new();
/// let a = graph.add_node(()); // node with no weight
/// let b = graph.add_node(());
/// let c = graph.add_node(());
/// let d = graph.add_node(());
/// let e = graph.add_node(());
/// let f = graph.add_node(());
/// let g = graph.add_node(());
/// let h = graph.add_node(());
/// // z will be in another connected component
/// let z = graph.add_node(());
///
/// graph.extend_with_edges(&[
///     (a, b),
///     (b, c),
///     (c, d),
///     (d, a),
///     (e, f),
///     (b, e),
///     (f, g),
///     (g, h),
///     (h, e),
/// ]);
/// // a ----> b ----> e ----> f
/// // ^       |       ^       |
/// // |       v       |       v
/// // d <---- c       h <---- g
///
/// let expected_res: HashMap<NodeIndex, usize> = [
///     (b, 0),
///     (c, 1),
///     (d, 2),
///     (e, 1),
///     (f, 2),
/// ].iter().cloned().collect();
/// let res = dijkstra::with_dynamic_goal(&graph, b, |&node| node == d || node == f, |_| 1);
/// assert_eq!(res.scores, expected_res);
/// assert!(res.goal_node == Some(d) || res.goal_node == Some(f));
/// ```
pub fn with_dynamic_goal<G, GoalFn, CostFn, K>(
    graph: G,
    start: G::NodeId,
    mut goal_fn: GoalFn,
    mut edge_cost: CostFn,
) -> AlgoResult<G::NodeId, K>
where
    G: IntoEdges + Visitable,
    G::NodeId: Eq + Hash,
    GoalFn: FnMut(&G::NodeId) -> bool,
    CostFn: FnMut(G::EdgeRef) -> K,
    K: Measure + Copy,
{
    let mut visited = graph.visit_map();
    let mut scores = HashMap::new();
    //let mut predecessor = HashMap::new();
    let mut visit_next = BinaryHeap::new();
    let zero_score = K::default();
    scores.insert(start, zero_score);
    visit_next.push(MinScored(zero_score, start));
    let mut goal_node = None;
    while let Some(MinScored(node_score, node)) = visit_next.pop() {
        if visited.is_visited(&node) {
            continue;
        }
        if goal_fn(&node) {
            goal_node = Some(node);
            break;
        }
        for edge in graph.edges(node) {
            let next = edge.target();
            if visited.is_visited(&next) {
                continue;
            }
            let next_score = node_score + edge_cost(edge);
            match scores.entry(next) {
                Occupied(ent) => {
                    if next_score < *ent.get() {
                        *ent.into_mut() = next_score;
                        visit_next.push(MinScored(next_score, next));
                        //predecessor.insert(next.clone(), node.clone());
                    }
                }
                Vacant(ent) => {
                    ent.insert(next_score);
                    visit_next.push(MinScored(next_score, next));
                    //predecessor.insert(next.clone(), node.clone());
                }
            }
        }
        visited.visit(node);
    }
    AlgoResult { scores, goal_node }
}

/// Bidirectional Dijkstra's shortest path algorithm.
///
/// Compute the length of the shortest path from `start` to `target`.
///
/// Bidirectional Dijkstra has the same time complexity as standard [`Dijkstra`][dijkstra]. However, because it
/// searches simultaneously from both the start and goal nodes, meeting in the middle, it often
/// explores roughly half the nodes that regular [`Dijkstra`][dijkstra] would explore. This is especially the case
/// when the path is long relative to the graph size or when working with sparse graphs.
///
/// However, regular [`Dijkstra`][dijkstra] may be preferable when you need the shortest paths from the start node
/// to multiple goals or when the start and goal are relatively close to each other.
///
/// The function `edge_cost` should return the cost for a particular edge, which is used
/// to compute path costs. Edge costs must be non-negative.
///
/// # Arguments
/// * `graph`: weighted graph.
/// * `start`: the start node.
/// * `goal`: the goal node.
/// * `edge_cost`: closure that returns the cost of a particular edge.
///
/// # Returns
/// * `Some(K)` - the total cost from start to finish, if one was found.
/// * `None` - if such a path was not found.
///
/// # Complexity
/// * Time complexity: **O((|V|+|E|)log(|V|))**.
/// * Auxiliary space: **O(|V|+|E|)**.
///
/// where **|V|** is the number of nodes and **|E|** is the number of edges.
///
/// # Example
/// ```rust
/// use petgraph::Graph;
/// use petgraph::algo::bidirectional_dijkstra;
/// use petgraph::prelude::*;
/// use hashbrown::HashMap;
///
/// let mut graph: Graph<(), (), Directed> = Graph::new();
/// let a = graph.add_node(());
/// let b = graph.add_node(());
/// let c = graph.add_node(());
/// let d = graph.add_node(());
/// let e = graph.add_node(());
/// let f = graph.add_node(());
/// let g = graph.add_node(());
/// let h = graph.add_node(());
///
/// graph.extend_with_edges(&[
///     (a, b),
///     (b, c),
///     (c, d),
///     (d, a),
///     (e, f),
///     (b, e),
///     (f, g),
///     (g, h),
///     (h, e),
/// ]);
/// // a ----> b ----> e ----> f
/// // ^       |       ^       |
/// // |       v       |       v
/// // d <---- c       h <---- g
///
/// let result = bidirectional_dijkstra(&graph, a, g, |_| 1);
/// assert_eq!(result, Some(4));
/// ```
pub fn bidirectional_dijkstra<G, F, K>(
    graph: G,
    start: G::NodeId,
    goal: G::NodeId,
    mut edge_cost: F,
) -> Option<K>
where
    G: Visitable + IntoEdgesDirected,
    G::NodeId: Eq + Hash,
    F: FnMut(G::EdgeRef) -> K,
    K: Measure + Copy,
{
    let mut forward_visited = graph.visit_map();
    let mut forward_distance = HashMap::new();
    forward_distance.insert(start, K::default());

    let mut backward_visited = graph.visit_map();
    let mut backward_distance = HashMap::new();
    backward_distance.insert(goal, K::default());

    let mut forward_heap = BinaryHeap::new();
    let mut backward_heap = BinaryHeap::new();

    forward_heap.push(MinScored(K::default(), start));
    backward_heap.push(MinScored(K::default(), goal));

    let mut best_value = None;

    while !forward_heap.is_empty() && !backward_heap.is_empty() {
        let MinScored(_, u) = forward_heap.pop().unwrap();
        let MinScored(_, v) = backward_heap.pop().unwrap();

        forward_visited.visit(u);
        backward_visited.visit(v);

        let distance_to_u = forward_distance[&u];
        let distance_to_v = backward_distance[&v];

        for edge in graph.edges_directed(u, Direction::Outgoing) {
            let x = edge.target();
            let current_edge_cost = edge_cost(edge);

            if !forward_visited.is_visited(&x) {
                let next_score = distance_to_u + current_edge_cost;

                match forward_distance.entry(x) {
                    Occupied(entry) => {
                        if next_score < *entry.get() {
                            *entry.into_mut() = next_score;
                            forward_heap.push(MinScored(next_score, x));
                        }
                    }
                    Vacant(entry) => {
                        entry.insert(next_score);
                        forward_heap.push(MinScored(next_score, x));
                    }
                }
            }

            if !backward_visited.is_visited(&x) {
                continue;
            }

            let potential_best_value = distance_to_u + current_edge_cost + backward_distance[&x];

            let improves_best_value = match best_value {
                None => true,
                Some(current_best_value) => potential_best_value < current_best_value,
            };

            if improves_best_value {
                best_value = Some(potential_best_value);
            }
        }

        for edge in graph.edges_directed(v, Direction::Incoming) {
            let x = edge.source();
            let edge_cost = edge_cost(edge);

            if !backward_visited.is_visited(&x) {
                let next_score = distance_to_v + edge_cost;

                match backward_distance.entry(x) {
                    Occupied(entry) => {
                        if next_score < *entry.get() {
                            *entry.into_mut() = next_score;
                            backward_heap.push(MinScored(next_score, x));
                        }
                    }
                    Vacant(entry) => {
                        entry.insert(next_score);
                        backward_heap.push(MinScored(next_score, x));
                    }
                }
            }

            if !forward_visited.is_visited(&x) {
                continue;
            }

            let potential_best_value = distance_to_v + edge_cost + forward_distance[&x];

            let improves_best_value = match best_value {
                None => true,
                Some(mu) => potential_best_value < mu,
            };

            if improves_best_value {
                best_value = Some(potential_best_value);
            }
        }

        if let Some(best_value) = best_value {
            if distance_to_u + distance_to_v >= best_value {
                return Some(best_value);
            }
        }
    }

    None
}