public void Run() { Render("flowGraph", true); // add reversed edges reversedEdgeAugmentor.AddReversedEdges(); // render with reversed edges Render("flowGraphWithReversedEdges", true); // create max flow algorithm this.maxFlow = new PushRelabelMaximumFlowAlgorithm( this.graph, this.capacities, this.reversedEdgeAugmentor.ReversedEdges ); // compute max flow double f = this.maxFlow.Compute(s, t); Console.WriteLine("Maximum flow: {0}", f); // clean up reversedEdgeAugmentor.RemoveReversedEdges(); // render without reversed edges Render("flowWithOutReversedEdges", true); }
public void RemoveReversedEdges_Throws() { var graph = new AdjacencyGraph <int, Edge <int> >(); EdgeFactory <int, Edge <int> > edgeFactory = (source, target) => new Edge <int>(source, target); var algorithm = new ReversedEdgeAugmentorAlgorithm <int, Edge <int> >(graph, edgeFactory); Assert.Throws <InvalidOperationException>(() => algorithm.RemoveReversedEdges()); }
public void AddAndRemoveAndCheckAugmented() { this.target = new ReversedEdgeAugmentorAlgorithm(new AdjacencyGraph()); Assert.IsFalse(target.Augmented); target.AddReversedEdges(); Assert.IsTrue(target.Augmented); target.RemoveReversedEdges(); Assert.IsFalse(target.Augmented); }
public void AddAndRemoveOneEdge() { AdjacencyGraph g = new AdjacencyGraph(); IVertex v = g.AddVertex(); IVertex u = g.AddVertex(); IEdge edge = g.AddEdge(u, v); this.target = new ReversedEdgeAugmentorAlgorithm(g); target.AddReversedEdges(); target.RemoveReversedEdges(); Assert.AreEqual(2, this.target.VisitedGraph.VerticesCount); Assert.AreEqual(1, this.target.VisitedGraph.EdgesCount); CollectionAssert.AreCountEqual(0, this.target.AugmentedEdges); Assert.AreEqual(0, this.target.ReversedEdges.Count); }
private static double RunMaxFlowAlgorithm <TVertex, TEdge>(IMutableVertexAndEdgeListGraph <TVertex, TEdge> g, EdgeFactory <TVertex, TEdge> edgeFactory, TVertex source, TVertex sink) where TEdge : IEdge <TVertex> { var reversedEdgeAugmentorAlgorithm = new ReversedEdgeAugmentorAlgorithm <TVertex, TEdge>(g, edgeFactory); reversedEdgeAugmentorAlgorithm.AddReversedEdges(); TryFunc <TVertex, TEdge> flowPredecessors; var flow = AlgorithmExtensions.MaximumFlowEdmondsKarp <TVertex, TEdge>( g, e => 1, source, sink, out flowPredecessors, edgeFactory, reversedEdgeAugmentorAlgorithm ); reversedEdgeAugmentorAlgorithm.RemoveReversedEdges(); return(flow); }
private static double RunMaxFlowAlgorithmAndCheck <TVertex, TEdge>( [NotNull] IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph, [NotNull] EdgeFactory <TVertex, TEdge> edgeFactory, [NotNull] TVertex source, [NotNull] TVertex sink) where TEdge : IEdge <TVertex> { var reversedEdgeAugmentorAlgorithm = new ReversedEdgeAugmentorAlgorithm <TVertex, TEdge>(graph, edgeFactory); reversedEdgeAugmentorAlgorithm.AddReversedEdges(); double flow = graph.MaximumFlow( edge => 1, source, sink, out _, edgeFactory, reversedEdgeAugmentorAlgorithm); reversedEdgeAugmentorAlgorithm.RemoveReversedEdges(); return(flow); }
public void RemoveReversedEdges() { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge23 = new Edge <int>(2, 3); var edge32 = new Edge <int>(3, 2); var graph = new AdjacencyGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge23, edge32 }); var algorithm = new ReversedEdgeAugmentorAlgorithm <int, Edge <int> >( graph, (source, target) => new Edge <int>(source, target)); algorithm.AddReversedEdges(); Assert.IsTrue(algorithm.Augmented); CollectionAssert.IsNotEmpty(algorithm.AugmentedEdges); foreach (Edge <int> edge in algorithm.AugmentedEdges) { CollectionAssert.Contains(algorithm.VisitedGraph.Edges, edge); } CollectionAssert.IsNotEmpty(algorithm.ReversedEdges); algorithm.RemoveReversedEdges(); Assert.IsFalse(algorithm.Augmented); CollectionAssert.IsEmpty(algorithm.AugmentedEdges); foreach (Edge <int> edge in algorithm.AugmentedEdges) { CollectionAssert.DoesNotContain(algorithm.VisitedGraph.Edges, edge); } CollectionAssert.IsEmpty(algorithm.ReversedEdges); }
/// <inheritdoc /> protected override void InternalCompute() { ICancelManager cancelManager = Services.CancelManager; BipartiteToMaximumFlowGraphAugmentorAlgorithm <TVertex, TEdge> augmentor = null; ReversedEdgeAugmentorAlgorithm <TVertex, TEdge> reverser = null; try { if (cancelManager.IsCancelling) { return; } // Augmenting the graph augmentor = new BipartiteToMaximumFlowGraphAugmentorAlgorithm <TVertex, TEdge>( this, VisitedGraph, SourceToVertices, VerticesToSink, VertexFactory, EdgeFactory); augmentor.Compute(); if (cancelManager.IsCancelling) { return; } // Adding reverse edges reverser = new ReversedEdgeAugmentorAlgorithm <TVertex, TEdge>( VisitedGraph, EdgeFactory); reverser.AddReversedEdges(); if (cancelManager.IsCancelling) { return; } // Compute maximum flow var flow = new EdmondsKarpMaximumFlowAlgorithm <TVertex, TEdge>( this, VisitedGraph, edge => 1.0, EdgeFactory, reverser); flow.Compute(augmentor.SuperSource, augmentor.SuperSink); if (cancelManager.IsCancelling) { return; } foreach (TEdge edge in VisitedGraph.Edges) { if (Math.Abs(flow.ResidualCapacities[edge]) < float.Epsilon) { if (edge.Source.Equals(augmentor.SuperSource) || edge.Source.Equals(augmentor.SuperSink) || edge.Target.Equals(augmentor.SuperSource) || edge.Target.Equals(augmentor.SuperSink)) { // Skip all edges that connect to SuperSource or SuperSink continue; } _matchedEdges.Add(edge); } } } finally { if (reverser != null && reverser.Augmented) { reverser.RemoveReversedEdges(); } if (augmentor != null && augmentor.Augmented) { augmentor.Rollback(); } } }
protected override void InternalCompute() { var cancelManager = this.Services.CancelManager; this.MatchedEdges.Clear(); BipartiteToMaximumFlowGraphAugmentorAlgorithm <TVertex, TEdge> augmentor = null; ReversedEdgeAugmentorAlgorithm <TVertex, TEdge> reverser = null; try { if (cancelManager.IsCancelling) { return; } //augmenting graph augmentor = new BipartiteToMaximumFlowGraphAugmentorAlgorithm <TVertex, TEdge>( this, this.VisitedGraph, this.VertexSetA, this.VertexSetB, this.VertexFactory, this.EdgeFactory); augmentor.Compute(); if (cancelManager.IsCancelling) { return; } //adding reverse edges reverser = new ReversedEdgeAugmentorAlgorithm <TVertex, TEdge>( this, this.VisitedGraph, this.EdgeFactory ); reverser.AddReversedEdges(); if (cancelManager.IsCancelling) { return; } // compute maxflow var flow = new EdmondsKarpMaximumFlowAlgorithm <TVertex, TEdge>( this, this.VisitedGraph, e => 1, this.EdgeFactory ); flow.Compute(augmentor.SuperSource, augmentor.SuperSink); if (cancelManager.IsCancelling) { return; } foreach (var edge in this.VisitedGraph.Edges) { if (flow.ResidualCapacities[edge] == 0) { if (edge.Source.Equals(augmentor.SuperSource) || edge.Source.Equals(augmentor.SuperSource) || edge.Target.Equals(augmentor.SuperSink) || edge.Target.Equals(augmentor.SuperSink)) { //Skip all edges that connect to SuperSource or SuperSink continue; } this.MatchedEdges.Add(edge); } } } finally { if (reverser != null && reverser.Augmented) { reverser.RemoveReversedEdges(); reverser = null; } if (augmentor != null && augmentor.Augmented) { augmentor.Rollback(); augmentor = null; } } }
protected override void InternalCompute() { this.matchedEdges.Clear(); AllVerticesGraphAugmentorAlgorithm <TVertex, TEdge> augmentor = null; ReversedEdgeAugmentorAlgorithm <TVertex, TEdge> reverser = null; try { if (this.IsAborting) { return; } //augmenting graph augmentor = new AllVerticesGraphAugmentorAlgorithm <TVertex, TEdge>( this.VisitedGraph, this.VertexFactory, this.EdgeFactory); augmentor.Compute(); if (this.IsAborting) { return; } // adding reverse edges reverser = new ReversedEdgeAugmentorAlgorithm <TVertex, TEdge>( this.VisitedGraph, this.EdgeFactory ); reverser.AddReversedEdges(); if (this.IsAborting) { return; } // compute maxflow EdmondsKarpMaximumFlowAlgorithm <TVertex, TEdge> flow = new EdmondsKarpMaximumFlowAlgorithm <TVertex, TEdge>( this.VisitedGraph, AlgoUtility.ConstantCapacities(this.VisitedGraph, 1), reverser.ReversedEdges ); flow.Compute(augmentor.SuperSource, augmentor.SuperSink); if (this.IsAborting) { return; } foreach (TEdge edge in this.VisitedGraph.Edges) { if (this.IsAborting) { return; } if (flow.ResidualCapacities[edge] == 0) { this.matchedEdges.Add(edge); } } } finally { if (reverser != null && reverser.Augmented) { reverser.RemoveReversedEdges(); reverser = null; } if (augmentor != null && augmentor.Augmented) { augmentor.Rollback(); augmentor = null; } } }