Satsuma
a delicious .NET graph library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
MaximumMatching.cs
Go to the documentation of this file.
1 #region License
2 /*This file is part of Satsuma Graph Library
3 Copyright © 2013 Balázs Szalkai
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.*/
23 #endregion
24 
25 using System;
26 using System.Collections.Generic;
27 using System.Linq;
28 
29 namespace Satsuma
30 {
33  public sealed class MaximumMatching : IClearable
34  {
35  public IGraph Graph { get; private set; }
37  public Func<Node, bool> IsRed { get; private set; }
38 
39  private readonly Matching matching;
40 
42  public IMatching Matching { get { return matching; } }
43 
44  private readonly HashSet<Node> unmatchedRedNodes;
45 
46  public MaximumMatching(IGraph graph, Func<Node, bool> isRed)
47  {
48  Graph = graph;
49  IsRed = isRed;
50  matching = new Matching(Graph);
51  unmatchedRedNodes = new HashSet<Node>();
52 
53  Clear();
54  }
55 
57  public void Clear()
58  {
59  matching.Clear();
60  unmatchedRedNodes.Clear();
61  foreach (var n in Graph.Nodes())
62  if (IsRed(n)) unmatchedRedNodes.Add(n);
63 
64  }
65 
70  public int GreedyGrow(int maxImprovements = int.MaxValue)
71  {
72  int result = 0;
73  List<Node> matchedRedNodes = new List<Node>();
74  foreach (var x in unmatchedRedNodes)
75  foreach (var arc in Graph.Arcs(x))
76  {
77  Node y = Graph.Other(arc, x);
78  if (!matching.HasNode(y))
79  {
80  matching.Enable(arc, true);
81  matchedRedNodes.Add(x);
82  result++;
83  if (result >= maxImprovements) goto BreakAll;
84  break;
85  }
86  }
87  BreakAll:
88  foreach (var n in matchedRedNodes) unmatchedRedNodes.Remove(n);
89  return result;
90  }
91 
96  public void Add(Arc arc)
97  {
98  if (matching.HasArc(arc)) return;
99  matching.Enable(arc, true);
100  Node u = Graph.U(arc);
101  unmatchedRedNodes.Remove(IsRed(u) ? u : Graph.V(arc));
102  }
103 
104  private Dictionary<Node, Arc> parentArc;
105  private Node Traverse(Node node)
106  {
107  Arc matchedArc = matching.MatchedArc(node);
108 
109  if (IsRed(node))
110  {
111  foreach (var arc in Graph.Arcs(node))
112  if (arc != matchedArc)
113  {
114  Node y = Graph.Other(arc, node);
115  if (!parentArc.ContainsKey(y))
116  {
117  parentArc[y] = arc;
118  if (!matching.HasNode(y)) return y;
119  Node result = Traverse(y);
120  if (result != Node.Invalid) return result;
121  }
122  }
123  }
124  else
125  {
126  Node y = Graph.Other(matchedArc, node);
127  if (!parentArc.ContainsKey(y))
128  {
129  parentArc[y] = matchedArc;
130  Node result = Traverse(y);
131  if (result != Node.Invalid) return result;
132  }
133  }
134 
135  return Node.Invalid;
136  }
137 
140  public void Run()
141  {
142  List<Node> matchedRedNodes = new List<Node>();
143  parentArc = new Dictionary<Node, Arc>();
144  foreach (var x in unmatchedRedNodes)
145  {
146  parentArc.Clear();
147  parentArc[x] = Arc.Invalid;
148 
149  // find an alternating path
150  Node y = Traverse(x);
151  if (y == Node.Invalid) continue;
152 
153  // modify matching along the alternating path
154  while (true)
155  {
156  // y ----arc---- z (====arc2===)
157  Arc arc = parentArc[y];
158  Node z = Graph.Other(arc, y);
159  Arc arc2 = (z == x ? Arc.Invalid : parentArc[z]);
160  if (arc2 != Arc.Invalid) matching.Enable(arc2, false);
161  matching.Enable(arc, true);
162  if (arc2 == Arc.Invalid) break;
163  y = Graph.Other(arc2, z);
164  }
165 
166  matchedRedNodes.Add(x);
167  }
168  parentArc = null;
169 
170  foreach (var n in matchedRedNodes) unmatchedRedNodes.Remove(n);
171  }
172  }
173 }