Satsuma
a delicious .NET graph library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
MinimumCostMatching.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 MinimumCostMatching
34  {
36  public IGraph Graph { get; private set; }
38  public Func<Node, bool> IsRed { get; private set; }
40  public Func<Arc, double> Cost { get; private set; }
42  public int MinimumMatchingSize { get; private set; }
44  public int MaximumMatchingSize { get; private set; }
47  public IMatching Matching { get; private set; }
48 
49  public MinimumCostMatching(IGraph graph, Func<Node, bool> isRed, Func<Arc, double> cost,
50  int minimumMatchingSize = 0, int maximumMatchingSize = int.MaxValue)
51  {
52  Graph = graph;
53  IsRed = isRed;
54  Cost = cost;
55  MinimumMatchingSize = minimumMatchingSize;
56  MaximumMatchingSize = maximumMatchingSize;
57 
58  Run();
59  }
60 
61  private void Run()
62  {
63  // direct all edges from the red nodes to the blue nodes
64  RedirectedGraph redToBlue = new RedirectedGraph(Graph,
65  x => (IsRed(Graph.U(x)) ? RedirectedGraph.Direction.Forward : RedirectedGraph.Direction.Backward));
66 
67  // add a source and a target to the graph and some edges
68  Supergraph flowGraph = new Supergraph(redToBlue);
69  Node source = flowGraph.AddNode();
70  Node target = flowGraph.AddNode();
71  foreach (var node in Graph.Nodes())
72  if (IsRed(node)) flowGraph.AddArc(source, node, Directedness.Directed);
73  else flowGraph.AddArc(node, target, Directedness.Directed);
74  Arc reflow = flowGraph.AddArc(target, source, Directedness.Directed);
75 
76  // run the network simplex
77  NetworkSimplex ns = new NetworkSimplex(flowGraph,
78  lowerBound: x => (x == reflow ? MinimumMatchingSize : 0),
79  upperBound: x => (x == reflow ? MaximumMatchingSize : 1),
80  cost: x => (Graph.HasArc(x) ? Cost(x) : 0));
81  ns.Run();
82 
83  if (ns.State == SimplexState.Optimal)
84  {
85  var matching = new Matching(Graph);
86  foreach (var arc in ns.UpperBoundArcs.Concat
87  (ns.Forest.Where(kv => kv.Value == 1).Select(kv => kv.Key)))
88  if (Graph.HasArc(arc))
89  matching.Enable(arc, true);
90  Matching = matching;
91  }
92  }
93  }
94 }