public void Constructor()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            VertexFactory <int>            vertexFactory = () => 1;
            EdgeFactory <int, Edge <int> > edgeFactory   = (source, target) => new Edge <int>(source, target);

            var algorithm = new AllVerticesGraphAugmentorAlgorithm <int, Edge <int> >(graph, vertexFactory, edgeFactory);

            AssertAlgorithmProperties(algorithm, graph, vertexFactory, edgeFactory);

            algorithm = new AllVerticesGraphAugmentorAlgorithm <int, Edge <int> >(null, graph, vertexFactory, edgeFactory);
            AssertAlgorithmProperties(algorithm, graph, vertexFactory, edgeFactory);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                AllVerticesGraphAugmentorAlgorithm <TVertex, TEdge> algo,
                IMutableVertexAndEdgeSet <TVertex, TEdge> g,
                VertexFactory <int> vFactory,
                EdgeFactory <int, Edge <int> > eFactory)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                Assert.IsFalse(algo.Augmented);
                CollectionAssert.IsEmpty(algo.AugmentedEdges);
                Assert.AreSame(vFactory, algo.VertexFactory);
                Assert.AreSame(eFactory, algo.EdgeFactory);
                Assert.AreEqual(default(TVertex), algo.SuperSource);
                Assert.AreEqual(default(TVertex), algo.SuperSink);
            }

            #endregion
        }
        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);
        }
        public void RunAugmentation_Throws()
        {
            var graph    = new AdjacencyGraph <int, Edge <int> >();
            int vertexID = 0;
            VertexFactory <int>            vertexFactory = () => ++ vertexID;
            EdgeFactory <int, Edge <int> > edgeFactory   = (source, target) => new Edge <int>(source, target);
            var algorithm = new AllVerticesGraphAugmentorAlgorithm <int, Edge <int> >(graph, vertexFactory, edgeFactory);

            RunAugmentation_Throws_Test(algorithm);
        }
        public void CreateAndSetSuperSink()
        {
            var graph    = new AdjacencyGraph <int, Edge <int> >();
            int vertexID = 0;
            VertexFactory <int>            vertexFactory = () => ++ vertexID;
            EdgeFactory <int, Edge <int> > edgeFactory   = (source, target) => new Edge <int>(source, target);
            var algorithm = new AllVerticesGraphAugmentorAlgorithm <int, Edge <int> >(graph, vertexFactory, edgeFactory);

            CreateAndSetSuperSink_Test(algorithm);
        }
 private static void VerifyVertexCount <TVertex, TEdge>(
     [NotNull] IVertexSet <TVertex> graph,
     // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
     [NotNull] AllVerticesGraphAugmentorAlgorithm <TVertex, TEdge> augmentor,
     int vertexCount)
     where TEdge : IEdge <TVertex>
 {
     Assert.AreEqual(vertexCount + 2 /* Source + Sink */, graph.VertexCount);
     Assert.IsTrue(graph.ContainsVertex(augmentor.SuperSource));
     Assert.IsTrue(graph.ContainsVertex(augmentor.SuperSink));
 }
예제 #6
0
 private static void VerifySinkConnector<TVertex, TEdge>(
     [NotNull] IVertexListGraph<TVertex, TEdge> graph,
     [NotNull] AllVerticesGraphAugmentorAlgorithm<TVertex, TEdge> augmentor)
     where TEdge : IEdge<TVertex>
 {
     foreach (TVertex vertex in graph.Vertices)
     {
         if (vertex.Equals(augmentor.SuperSource))
             continue;
         if (vertex.Equals(augmentor.SuperSink))
             continue;
         Assert.IsTrue(graph.ContainsEdge(vertex, augmentor.SuperSink));
     }
 }
        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);
        }
예제 #8
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;
                }
            }
        }