private static void RunAugmentationAndCheck(
            [NotNull] IMutableVertexAndEdgeListGraph <string, Edge <string> > graph)
        {
            int vertexCount = graph.VertexCount;
            int edgeCount   = graph.EdgeCount;
            int vertexId    = graph.VertexCount + 1;

            using (var augmentor = new AllVerticesGraphAugmentorAlgorithm <string, Edge <string> >(
                       graph,
                       () => (vertexId++).ToString(),
                       (s, t) => new Edge <string>(s, t)))
            {
                bool added = false;
                augmentor.EdgeAdded += _ => { added = true; };

                augmentor.Compute();
                Assert.IsTrue(added);
                VerifyVertexCount(graph, augmentor, vertexCount);
                VerifySourceConnector(graph, augmentor);
                VerifySinkConnector(graph, augmentor);
            }

            Assert.AreEqual(graph.VertexCount, vertexCount);
            Assert.AreEqual(graph.EdgeCount, edgeCount);
        }
        private static void AugmentAndCheck(
            [NotNull] IMutableVertexAndEdgeListGraph <string, Edge <string> > graph)
        {
            int vertexCount = graph.VertexCount;
            int edgeCount   = graph.EdgeCount;
            int vertexId    = graph.VertexCount + 1;

            using (var augmentor = new AllVerticesGraphAugmentorAlgorithm <string, Edge <string> >(
                       graph,
                       () => (vertexId++).ToString(),
                       (s, t) => new Edge <string>(s, t)))
            {
                augmentor.Compute();
                VerifyCount(graph, augmentor, vertexCount);
                VerifySourceConnector(graph, augmentor);
                VerifySinkConnector(graph, augmentor);
            }

            Assert.AreEqual(graph.VertexCount, vertexCount);
            Assert.AreEqual(graph.EdgeCount, edgeCount);
        }
Exemplo n.º 3
0
        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;
                }
            }
        }