This is the fiftieth part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we are going to solve three graph problems: vinimum spanning tree, vertex cover and edge cover.

# Helpers

Let’s start with some helper code:

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 |
using MilpManager.Abstraction; using MilpManager.Implementation; using OrToolsMilpManager.Implementation; using System; using System.Collections.Generic; using System.Linq; namespace MST { class Program { static void Main(string[] args) { Vertex a = new Vertex(); Vertex b = new Vertex(); Vertex c = new Vertex(); Vertex d = new Vertex(); Edge ab = new Edge { Weight = 1, A = a, B = b }; Edge bc = new Edge { Weight = 2, A = b, B = c }; Edge ac = new Edge { Weight = 1, A = a, B = c }; Edge ad = new Edge { Weight = 3, A = a, B = d }; a.Edges.Add(ab); b.Edges.Add(ab); a.Edges.Add(ac); c.Edges.Add(ac); a.Edges.Add(ad); d.Edges.Add(ad); b.Edges.Add(bc); c.Edges.Add(bc); var solver = new OrToolsMilpSolver(5); var edges = new[] { ab, bc, ac, ad }; foreach(var edge in edges) { edge.Var = solver.CreateAnonymous(Domain.BinaryInteger); } var vertices = new[] { a, b, c, d }; foreach (var vertex in vertices) { vertex.Var = solver.CreateAnonymous(Domain.BinaryInteger); } var cost = Constraints(solver, edges, vertices); solver.MakeGoal(GoalType.Minimize, cost); solver.Solve(); Console.WriteLine("Vertices"); foreach (var vertex in vertices) { Console.WriteLine(solver.GetValue(vertex.Var)); } Console.WriteLine("Edges"); foreach(var edge in edges) { Console.WriteLine(solver.GetValue(edge.Var)); } Console.WriteLine("Cost"); Console.WriteLine(solver.GetValue(cost)); } } class Vertex { public List< Edge> Edges { get; set; } public IVariable Var { get; set; } public Vertex() { Edges = new List< Edge>(); } } class Edge { public IVariable Var { get; set; } public Vertex A { get; set; } public Vertex B { get; set; } public int Weight { get; set; } } } |

Data structures for graph and code for bookkeeping. The real part is in `Constraints`

methods.

# Minimum spanning tree

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
static IVariable Constraints(IMilpSolver solver, Edge[] edges, Vertex[] vertices) { foreach (var vertex in vertices) { var sum = solver.Operation(OperationType.Addition, vertex.Edges.Select(e => e.Var).ToArray()); sum.Set(ConstraintType.GreaterOrEqual, solver.FromConstant(1)); } solver.Operation(OperationType.Addition, edges.Select(e => e.Var).ToArray()).Set(ConstraintType.Equal, solver.FromConstant(vertices.Length - 1)); var cost = solver.Operation(OperationType.Addition, edges.Select(e => e.Var.Operation(OperationType.Multiplication, solver.FromConstant(e.Weight))).ToArray()); return cost; } |

For every vertex we calculate the disjunction of the variables representing the edges coinciding with the vertex. Calculated value must be at least one meaning that there is at least one edge chosen for the solution.

Next, we make sure that we have exactly edges in the solution.

Finally, the cost is the sum of the weights.

We also need constraints for the MST being a connected graph, otherwise we may end up with forest and not a tree.

# Vertex cover

1 2 3 4 5 6 7 8 9 10 11 |
static IVariable Constraints(IMilpSolver solver, Edge[] edges, Vertex[] vertices) { foreach (var edge in edges) { edge.A.Var.Operation(OperationType.Addition, edge.B.Var).Set(ConstraintType.GreaterOrEqual, solver.FromConstant(1)); } var cost = solver.Operation(OperationType.Addition, vertices.Select(v => v.Var).ToArray()); return cost; } |

For every edge we make sure that at least one of its endings is included in the solution. The cost is the number of selected vertices.

# Edge cover

1 2 3 4 5 6 7 8 9 10 11 12 |
static IVariable Constraints(IMilpSolver solver, Edge[] edges, Vertex[] vertices) { foreach (var vertex in vertices) { var sum = solver.Operation(OperationType.Addition, vertex.Edges.Select(e => e.Var).ToArray()); sum.Set(ConstraintType.GreaterOrEqual, solver.FromConstant(1)); } var cost = solver.Operation(OperationType.Addition, edges.Select(e => e.Var).ToArray()); return cost; } |

For every vertex we make sure that there is at least one selected edge. The cost is the number of the edges.

# Analysis

All three methods look very easy but as for now we know polynomial time solutions for only two of them — the MST and the edge cover. The vertex cover is too hard. What’s more, OrTools or CPLEX solver don’t find optimal solutions for those problems using default settings.