public void CreateAndSetSuperSourceOrSink_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 sourceToVertices = new[] { 3, 4 };
            var verticesToSink   = new[] { 3, 5 };

            var algorithm = new BipartiteToMaximumFlowGraphAugmentorAlgorithm <int, Edge <int> >(graph, sourceToVertices, verticesToSink, vertexFactory, edgeFactory);

            Assert.Throws <VertexNotFoundException>(() => algorithm.Compute());
        }
        public void RunAugmentation_Throws()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertexRange(new[] { 3, 4 });
            int vertexID = 0;
            VertexFactory <int>            vertexFactory = () => ++ vertexID;
            EdgeFactory <int, Edge <int> > edgeFactory   = (source, target) => new Edge <int>(source, target);
            var sourceToVertices = new[] { 3, 4 };
            var verticesToSink   = new int[] { };

            var algorithm = new BipartiteToMaximumFlowGraphAugmentorAlgorithm <int, Edge <int> >(graph, sourceToVertices, verticesToSink, vertexFactory, edgeFactory);

            RunAugmentation_Throws_Test(algorithm);
        }
        public void CreateAndSetSuperSink()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertexRange(new[] { 3, 4, 5 });
            int vertexID = 0;
            VertexFactory <int>            vertexFactory = () => ++ vertexID;
            EdgeFactory <int, Edge <int> > edgeFactory   = (source, target) => new Edge <int>(source, target);
            var sourceToVertices = new[] { 3, 4 };
            var verticesToSink   = new[] { 3, 5 };

            var algorithm = new BipartiteToMaximumFlowGraphAugmentorAlgorithm <int, Edge <int> >(graph, sourceToVertices, verticesToSink, vertexFactory, edgeFactory);

            CreateAndSetSuperSink_Test(algorithm);
        }
        /// <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();
                }
            }
        }
        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 sourceToVertices = new[] { 1, 2 };
            var verticesToSink   = new[] { 1, 2 };

            var algorithm = new BipartiteToMaximumFlowGraphAugmentorAlgorithm <int, Edge <int> >(
                graph,
                sourceToVertices,
                verticesToSink,
                vertexFactory,
                edgeFactory);

            AssertAlgorithmProperties(
                algorithm,
                graph,
                sourceToVertices,
                verticesToSink,
                vertexFactory,
                edgeFactory);

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

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                BipartiteToMaximumFlowGraphAugmentorAlgorithm <TVertex, TEdge> algo,
                IMutableVertexAndEdgeSet <TVertex, TEdge> g,
                IEnumerable <TVertex> soToV,
                IEnumerable <TVertex> vToSi,
                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);
                Assert.AreSame(soToV, algo.SourceToVertices);
                Assert.AreSame(vToSi, algo.VerticesToSink);
            }

            #endregion
        }
Esempio n. 6
0
        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;
                }
            }
        }