petgraph/algo/matching.rs
1use std::collections::VecDeque;
2use std::hash::Hash;
3
4use crate::visit::{
5 EdgeRef, GraphBase, IntoEdges, IntoNeighbors, IntoNodeIdentifiers, NodeCount, NodeIndexable,
6 VisitMap, Visitable,
7};
8
9/// Computed
10/// [*matching*](https://en.wikipedia.org/wiki/Matching_(graph_theory)#Definitions)
11/// of the graph.
12pub struct Matching<G: GraphBase> {
13 graph: G,
14 mate: Vec<Option<G::NodeId>>,
15 n_edges: usize,
16}
17
18impl<G> Matching<G>
19where
20 G: GraphBase,
21{
22 fn new(graph: G, mate: Vec<Option<G::NodeId>>, n_edges: usize) -> Self {
23 Self {
24 graph,
25 mate,
26 n_edges,
27 }
28 }
29}
30
31impl<G> Matching<G>
32where
33 G: NodeIndexable,
34{
35 /// Gets the matched counterpart of given node, if there is any.
36 ///
37 /// Returns `None` if the node is not matched or does not exist.
38 pub fn mate(&self, node: G::NodeId) -> Option<G::NodeId> {
39 self.mate.get(self.graph.to_index(node)).and_then(|&id| id)
40 }
41
42 /// Iterates over all edges from the matching.
43 ///
44 /// An edge is represented by its endpoints. The graph is considered
45 /// undirected and every pair of matched nodes is reported only once.
46 pub fn edges(&self) -> MatchedEdges<'_, G> {
47 MatchedEdges {
48 graph: &self.graph,
49 mate: self.mate.as_slice(),
50 current: 0,
51 }
52 }
53
54 /// Iterates over all nodes from the matching.
55 pub fn nodes(&self) -> MatchedNodes<'_, G> {
56 MatchedNodes {
57 graph: &self.graph,
58 mate: self.mate.as_slice(),
59 current: 0,
60 }
61 }
62
63 /// Returns `true` if given edge is in the matching, or `false` otherwise.
64 ///
65 /// If any of the nodes does not exist, `false` is returned.
66 pub fn contains_edge(&self, a: G::NodeId, b: G::NodeId) -> bool {
67 match self.mate(a) {
68 Some(mate) => mate == b,
69 None => false,
70 }
71 }
72
73 /// Returns `true` if given node is in the matching, or `false` otherwise.
74 ///
75 /// If the node does not exist, `false` is returned.
76 pub fn contains_node(&self, node: G::NodeId) -> bool {
77 self.mate(node).is_some()
78 }
79
80 /// Gets the number of matched **edges**.
81 pub fn len(&self) -> usize {
82 self.n_edges
83 }
84
85 /// Returns `true` if the number of matched **edges** is 0.
86 pub fn is_empty(&self) -> bool {
87 self.len() == 0
88 }
89}
90
91impl<G> Matching<G>
92where
93 G: NodeCount,
94{
95 /// Returns `true` if the matching is perfect.
96 ///
97 /// A matching is
98 /// [*perfect*](https://en.wikipedia.org/wiki/Matching_(graph_theory)#Definitions)
99 /// if every node in the graph is incident to an edge from the matching.
100 pub fn is_perfect(&self) -> bool {
101 let n_nodes = self.graph.node_count();
102 n_nodes % 2 == 0 && self.n_edges == n_nodes / 2
103 }
104}
105
106trait WithDummy: NodeIndexable {
107 fn dummy_idx(&self) -> usize;
108 /// Convert `i` to a node index, returns None for the dummy node
109 fn try_from_index(&self, i: usize) -> Option<Self::NodeId>;
110}
111
112impl<G: NodeIndexable> WithDummy for G {
113 fn dummy_idx(&self) -> usize {
114 // Gabow numbers the vertices from 1 to n, and uses 0 as the dummy
115 // vertex. Our vertex indices are zero-based and so we use the node
116 // bound as the dummy node.
117 self.node_bound()
118 }
119
120 fn try_from_index(&self, i: usize) -> Option<Self::NodeId> {
121 if i != self.dummy_idx() {
122 Some(self.from_index(i))
123 } else {
124 None
125 }
126 }
127}
128
129pub struct MatchedNodes<'a, G: GraphBase> {
130 graph: &'a G,
131 mate: &'a [Option<G::NodeId>],
132 current: usize,
133}
134
135impl<G> Iterator for MatchedNodes<'_, G>
136where
137 G: NodeIndexable,
138{
139 type Item = G::NodeId;
140
141 fn next(&mut self) -> Option<Self::Item> {
142 while self.current != self.mate.len() {
143 let current = self.current;
144 self.current += 1;
145
146 if self.mate[current].is_some() {
147 return Some(self.graph.from_index(current));
148 }
149 }
150
151 None
152 }
153}
154
155pub struct MatchedEdges<'a, G: GraphBase> {
156 graph: &'a G,
157 mate: &'a [Option<G::NodeId>],
158 current: usize,
159}
160
161impl<G> Iterator for MatchedEdges<'_, G>
162where
163 G: NodeIndexable,
164{
165 type Item = (G::NodeId, G::NodeId);
166
167 fn next(&mut self) -> Option<Self::Item> {
168 while self.current != self.mate.len() {
169 let current = self.current;
170 self.current += 1;
171
172 if let Some(mate) = self.mate[current] {
173 // Check if the mate is a node after the current one. If not, then
174 // do not report that edge since it has been already reported (the
175 // graph is considered undirected).
176 if self.graph.to_index(mate) > current {
177 let this = self.graph.from_index(current);
178 return Some((this, mate));
179 }
180 }
181 }
182
183 None
184 }
185}
186
187/// \[Generic\] Compute a
188/// [*matching*](https://en.wikipedia.org/wiki/Matching_(graph_theory)) using a
189/// greedy heuristic.
190///
191/// The input graph is treated as if undirected. The underlying heuristic is
192/// unspecified, but is guaranteed to be bounded by *O(|V| + |E|)*. No
193/// guarantees about the output are given other than that it is a valid
194/// matching.
195///
196/// If you require a maximum matching, use [`maximum_matching`][1] function
197/// instead.
198///
199/// [1]: fn.maximum_matching.html
200pub fn greedy_matching<G>(graph: G) -> Matching<G>
201where
202 G: Visitable + IntoNodeIdentifiers + NodeIndexable + IntoNeighbors,
203 G::NodeId: Eq + Hash,
204 G::EdgeId: Eq + Hash,
205{
206 let (mates, n_edges) = greedy_matching_inner(&graph);
207 Matching::new(graph, mates, n_edges)
208}
209
210#[inline]
211fn greedy_matching_inner<G>(graph: &G) -> (Vec<Option<G::NodeId>>, usize)
212where
213 G: Visitable + IntoNodeIdentifiers + NodeIndexable + IntoNeighbors,
214{
215 let mut mate = vec![None; graph.node_bound()];
216 let mut n_edges = 0;
217 let visited = &mut graph.visit_map();
218
219 for start in graph.node_identifiers() {
220 let mut last = Some(start);
221
222 // Function non_backtracking_dfs does not expand the node if it has been
223 // already visited.
224 non_backtracking_dfs(graph, start, visited, |next| {
225 // Alternate matched and unmatched edges.
226 if let Some(pred) = last.take() {
227 mate[graph.to_index(pred)] = Some(next);
228 mate[graph.to_index(next)] = Some(pred);
229 n_edges += 1;
230 } else {
231 last = Some(next);
232 }
233 });
234 }
235
236 (mate, n_edges)
237}
238
239fn non_backtracking_dfs<G, F>(graph: &G, source: G::NodeId, visited: &mut G::Map, mut visitor: F)
240where
241 G: Visitable + IntoNeighbors,
242 F: FnMut(G::NodeId),
243{
244 if visited.visit(source) {
245 for target in graph.neighbors(source) {
246 if !visited.is_visited(&target) {
247 visitor(target);
248 non_backtracking_dfs(graph, target, visited, visitor);
249
250 // Non-backtracking traversal, stop iterating over the
251 // neighbors.
252 break;
253 }
254 }
255 }
256}
257
258#[derive(Clone, Copy)]
259enum Label<G: GraphBase> {
260 None,
261 Start,
262 // If node v is outer node, then label(v) = w is another outer node on path
263 // from v to start u.
264 Vertex(G::NodeId),
265 // If node v is outer node, then label(v) = (r, s) are two outer vertices
266 // (connected by an edge)
267 Edge(G::EdgeId, [G::NodeId; 2]),
268 // Flag is a special label used in searching for the join vertex of two
269 // paths.
270 Flag(G::EdgeId),
271}
272
273impl<G: GraphBase> Label<G> {
274 fn is_outer(&self) -> bool {
275 self != &Label::None
276 && !match self {
277 Label::Flag(_) => true,
278 _ => false,
279 }
280 }
281
282 fn is_inner(&self) -> bool {
283 !self.is_outer()
284 }
285
286 fn to_vertex(&self) -> Option<G::NodeId> {
287 match *self {
288 Label::Vertex(v) => Some(v),
289 _ => None,
290 }
291 }
292
293 fn is_flagged(&self, edge: G::EdgeId) -> bool {
294 match self {
295 Label::Flag(flag) if flag == &edge => true,
296 _ => false,
297 }
298 }
299}
300
301impl<G: GraphBase> Default for Label<G> {
302 fn default() -> Self {
303 Label::None
304 }
305}
306
307impl<G: GraphBase> PartialEq for Label<G> {
308 fn eq(&self, other: &Self) -> bool {
309 match (self, other) {
310 (Label::None, Label::None) => true,
311 (Label::Start, Label::Start) => true,
312 (Label::Vertex(v1), Label::Vertex(v2)) => v1 == v2,
313 (Label::Edge(e1, _), Label::Edge(e2, _)) => e1 == e2,
314 (Label::Flag(e1), Label::Flag(e2)) => e1 == e2,
315 _ => false,
316 }
317 }
318}
319
320/// \[Generic\] Compute the [*maximum
321/// matching*](https://en.wikipedia.org/wiki/Matching_(graph_theory)) using
322/// [Gabow's algorithm][1].
323///
324/// [1]: https://dl.acm.org/doi/10.1145/321941.321942
325///
326/// The input graph is treated as if undirected. The algorithm runs in
327/// *O(|V|³)*. An algorithm with a better time complexity might be used in the
328/// future.
329///
330/// **Panics** if `g.node_bound()` is `std::usize::MAX`.
331///
332/// # Examples
333///
334/// ```
335/// use petgraph::prelude::*;
336/// use petgraph::algo::maximum_matching;
337///
338/// // The example graph:
339/// //
340/// // +-- b ---- d ---- f
341/// // / | |
342/// // a | |
343/// // \ | |
344/// // +-- c ---- e
345/// //
346/// // Maximum matching: { (a, b), (c, e), (d, f) }
347///
348/// let mut graph: UnGraph<(), ()> = UnGraph::new_undirected();
349/// let a = graph.add_node(());
350/// let b = graph.add_node(());
351/// let c = graph.add_node(());
352/// let d = graph.add_node(());
353/// let e = graph.add_node(());
354/// let f = graph.add_node(());
355/// graph.extend_with_edges(&[(a, b), (a, c), (b, c), (b, d), (c, e), (d, e), (d, f)]);
356///
357/// let matching = maximum_matching(&graph);
358/// assert!(matching.contains_edge(a, b));
359/// assert!(matching.contains_edge(c, e));
360/// assert_eq!(matching.mate(d), Some(f));
361/// assert_eq!(matching.mate(f), Some(d));
362/// ```
363pub fn maximum_matching<G>(graph: G) -> Matching<G>
364where
365 G: Visitable + NodeIndexable + IntoNodeIdentifiers + IntoEdges,
366{
367 // The dummy identifier needs an unused index
368 assert_ne!(
369 graph.node_bound(),
370 std::usize::MAX,
371 "The input graph capacity should be strictly less than std::usize::MAX."
372 );
373
374 // Greedy algorithm should create a fairly good initial matching. The hope
375 // is that it speeds up the computation by doing les work in the complex
376 // algorithm.
377 let (mut mate, mut n_edges) = greedy_matching_inner(&graph);
378
379 // Gabow's algorithm uses a dummy node in the mate array.
380 mate.push(None);
381 let len = graph.node_bound() + 1;
382 debug_assert_eq!(mate.len(), len);
383
384 let mut label: Vec<Label<G>> = vec![Label::None; len];
385 let mut first_inner = vec![std::usize::MAX; len];
386 let visited = &mut graph.visit_map();
387
388 for start in 0..graph.node_bound() {
389 if mate[start].is_some() {
390 // The vertex is already matched. A start must be a free vertex.
391 continue;
392 }
393
394 // Begin search from the node.
395 label[start] = Label::Start;
396 first_inner[start] = graph.dummy_idx();
397 graph.reset_map(visited);
398
399 // start is never a dummy index
400 let start = graph.from_index(start);
401
402 // Queue will contain outer vertices that should be processed next. The
403 // start vertex is considered an outer vertex.
404 let mut queue = VecDeque::new();
405 queue.push_back(start);
406 // Mark the start vertex so it is not processed repeatedly.
407 visited.visit(start);
408
409 'search: while let Some(outer_vertex) = queue.pop_front() {
410 for edge in graph.edges(outer_vertex) {
411 if edge.source() == edge.target() {
412 // Ignore self-loops.
413 continue;
414 }
415
416 let other_vertex = edge.target();
417 let other_idx = graph.to_index(other_vertex);
418
419 if mate[other_idx].is_none() && other_vertex != start {
420 // An augmenting path was found. Augment the matching. If
421 // `other` is actually the start node, then the augmentation
422 // must not be performed, because the start vertex would be
423 // incident to two edges, which violates the matching
424 // property.
425 mate[other_idx] = Some(outer_vertex);
426 augment_path(&graph, outer_vertex, other_vertex, &mut mate, &label);
427 n_edges += 1;
428
429 // The path is augmented, so the start is no longer free
430 // vertex. We need to begin with a new start.
431 break 'search;
432 } else if label[other_idx].is_outer() {
433 // The `other` is an outer vertex (a label has been set to
434 // it). An odd cycle (blossom) was found. Assign this edge
435 // as a label to all inner vertices in paths P(outer) and
436 // P(other).
437 find_join(
438 &graph,
439 edge,
440 &mate,
441 &mut label,
442 &mut first_inner,
443 |labeled| {
444 if visited.visit(labeled) {
445 queue.push_back(labeled);
446 }
447 },
448 );
449 } else {
450 let mate_vertex = mate[other_idx];
451 let mate_idx = mate_vertex.map_or(graph.dummy_idx(), |id| graph.to_index(id));
452
453 if label[mate_idx].is_inner() {
454 // Mate of `other` vertex is inner (no label has been
455 // set to it so far). But it actually is an outer vertex
456 // (it is on a path to the start vertex that begins with
457 // a matched edge, since it is a mate of `other`).
458 // Assign the label of this mate to the `outer` vertex,
459 // so the path for it can be reconstructed using `mate`
460 // and this label.
461 label[mate_idx] = Label::Vertex(outer_vertex);
462 first_inner[mate_idx] = other_idx;
463 }
464
465 // Add the vertex to the queue only if it's not the dummy and this is its first
466 // discovery.
467 if let Some(mate_vertex) = mate_vertex {
468 if visited.visit(mate_vertex) {
469 queue.push_back(mate_vertex);
470 }
471 }
472 }
473 }
474 }
475
476 // Reset the labels. All vertices are inner for the next search.
477 for lbl in label.iter_mut() {
478 *lbl = Label::None;
479 }
480 }
481
482 // Discard the dummy node.
483 mate.pop();
484
485 Matching::new(graph, mate, n_edges)
486}
487
488fn find_join<G, F>(
489 graph: &G,
490 edge: G::EdgeRef,
491 mate: &[Option<G::NodeId>],
492 label: &mut [Label<G>],
493 first_inner: &mut [usize],
494 mut visitor: F,
495) where
496 G: IntoEdges + NodeIndexable + Visitable,
497 F: FnMut(G::NodeId),
498{
499 // Simultaneously traverse the inner vertices on paths P(source) and
500 // P(target) to find a join vertex - an inner vertex that is shared by these
501 // paths.
502 let source = graph.to_index(edge.source());
503 let target = graph.to_index(edge.target());
504
505 let mut left = first_inner[source];
506 let mut right = first_inner[target];
507
508 if left == right {
509 // No vertices can be labeled, since both paths already refer to a
510 // common vertex - the join.
511 return;
512 }
513
514 // Flag the (first) inner vertices. This ensures that they are assigned the
515 // join as their first inner vertex.
516 let flag = Label::Flag(edge.id());
517 label[left] = flag;
518 label[right] = flag;
519
520 // Find the join.
521 let join = loop {
522 // Swap the sides. Do not swap if the right side is already finished.
523 if right != graph.dummy_idx() {
524 std::mem::swap(&mut left, &mut right);
525 }
526
527 // Set left to the next inner vertex in P(source) or P(target).
528 // The unwraps are safe because left is not the dummy node.
529 let left_mate = graph.to_index(mate[left].unwrap());
530 let next_inner = label[left_mate].to_vertex().unwrap();
531 left = first_inner[graph.to_index(next_inner)];
532
533 if !label[left].is_flagged(edge.id()) {
534 // The inner vertex is not flagged yet, so flag it.
535 label[left] = flag;
536 } else {
537 // The inner vertex is already flagged. It means that the other side
538 // had to visit it already. Therefore it is the join vertex.
539 break left;
540 }
541 };
542
543 // Label all inner vertices on P(source) and P(target) with the found join.
544 for endpoint in [source, target].iter().copied() {
545 let mut inner = first_inner[endpoint];
546 while inner != join {
547 // Notify the caller about labeling a vertex.
548 if let Some(ix) = graph.try_from_index(inner) {
549 visitor(ix);
550 }
551
552 label[inner] = Label::Edge(edge.id(), [edge.source(), edge.target()]);
553 first_inner[inner] = join;
554 let inner_mate = graph.to_index(mate[inner].unwrap());
555 let next_inner = label[inner_mate].to_vertex().unwrap();
556 inner = first_inner[graph.to_index(next_inner)];
557 }
558 }
559
560 for (vertex_idx, vertex_label) in label.iter().enumerate() {
561 // To all outer vertices that are on paths P(source) and P(target) until
562 // the join, se the join as their first inner vertex.
563 if vertex_idx != graph.dummy_idx()
564 && vertex_label.is_outer()
565 && label[first_inner[vertex_idx]].is_outer()
566 {
567 first_inner[vertex_idx] = join;
568 }
569 }
570}
571
572fn augment_path<G>(
573 graph: &G,
574 outer: G::NodeId,
575 other: G::NodeId,
576 mate: &mut [Option<G::NodeId>],
577 label: &[Label<G>],
578) where
579 G: NodeIndexable,
580{
581 let outer_idx = graph.to_index(outer);
582
583 let temp = mate[outer_idx];
584 let temp_idx = temp.map_or(graph.dummy_idx(), |id| graph.to_index(id));
585 mate[outer_idx] = Some(other);
586
587 if mate[temp_idx] != Some(outer) {
588 // We are at the end of the path and so the entire path is completely
589 // rematched/augmented.
590 } else if let Label::Vertex(vertex) = label[outer_idx] {
591 // The outer vertex has a vertex label which refers to another outer
592 // vertex on the path. So we set this another outer node as the mate for
593 // the previous mate of the outer node.
594 mate[temp_idx] = Some(vertex);
595 if let Some(temp) = temp {
596 augment_path(graph, vertex, temp, mate, label);
597 }
598 } else if let Label::Edge(_, [source, target]) = label[outer_idx] {
599 // The outer vertex has an edge label which refers to an edge in a
600 // blossom. We need to augment both directions along the blossom.
601 augment_path(graph, source, target, mate, label);
602 augment_path(graph, target, source, mate, label);
603 } else {
604 panic!("Unexpected label when augmenting path");
605 }
606}