public void BalanceUnBalancedFlowGraph()
        {
            g = GraphFactory.UnBalancedFlow();
            IVertex source = null;
            IVertex sink = null;
            foreach (IVertex v in g.Vertices)
            {
                if (g.InDegree(v) == 0)
                {
                    source = v;
                    continue;
                }
                if (g.OutDegree(v) == 0)
                {
                    sink = v;
                    continue;
                }
            }
            Assert.IsNotNull(source);
            Assert.IsNotNull(sink);

            int vertexCount = g.VerticesCount;
            int edgeCount = g.EdgesCount;

            algo = new GraphBalancerAlgorithm(g,source, sink);
            algo.DeficientVertexAdded+=new VertexEventHandler(algo_DeficientVertexAdded);
            algo.SurplusVertexAdded+=new VertexEventHandler(algo_SurplusVertexAdded);
            algo.Balance();

            VerifyBalanced(vertexCount, edgeCount);
        }
 public void BalanceTwice()
 {
     g = GraphFactory.UnBalancedFlow();
     algo = new GraphBalancerAlgorithm(g, Traversal.FirstVertex(g), Traversal.FirstVertex(g));
     algo.Balance();
     algo.Balance();
 }
 public MinimumFlowAlgorithm(BidirectionalGraph visitedGraph, EdgeDoubleDictionary capacities)
 {
     this.reverser = null;
     this.balancer = null;
     this.maxFlowF1 = null;
     this.maxFlowf2 = null;
     if (visitedGraph == null)
     {
         throw new ArgumentNullException("visitedGraph");
     }
     if (capacities == null)
     {
         throw new ArgumentNullException("capacities");
     }
     this.visitedGraph = visitedGraph;
     this.capacities = capacities;
     this.Initialize();
 }
Exemplo n.º 4
0
        public void Compute(IVertex source, IVertex sink)
        {
            // step 1 constructor balancing graph
            this.balancer = new GraphBalancerAlgorithm(
                this.VisitedGraph,
                source,
                sink,
                this.capacities
                );

            balancer.Balance();
            this.capacities[balancer.BalancingSourceEdge] = 0;
            this.capacities[balancer.BalancingSinkEdge]   = 0;

            // step 2 find max flow
            reverser.AddReversedEdges();
            maxFlowF1.Compute(source, sink);

            // step 3
            this.capacities[balancer.BalancingSourceEdge] = double.MaxValue;
            foreach (IEdge edge in balancer.SurplusEdges)
            {
                IVertex v = edge.Target;
                // find edges
                foreach (IEdge vs in this.VisitedGraph.OutEdges(v))
                {
                    if (vs.Target == balancer.BalancingSource)
                    {
                        this.capacities[vs] = 0;
                    }
                }
            }

            // step 4:

            //           reverser.RemoveReversedEdges();
            //         balancer.UnBalance();
        }
 public MinimumFlowAlgorithm(BidirectionalGraph visitedGraph)
 {
     this.reverser = null;
     this.balancer = null;
     this.maxFlowF1 = null;
     this.maxFlowf2 = null;
     if (visitedGraph == null)
     {
         throw new ArgumentNullException("visitedGraph");
     }
     if (this.capacities == null)
     {
         throw new ArgumentNullException("capacities");
     }
     this.visitedGraph = visitedGraph;
     this.capacities = new EdgeDoubleDictionary();
     VertexEdgesEnumerator enumerator = this.visitedGraph.Edges.GetEnumerator();
     while (enumerator.MoveNext())
     {
         IEdge edge = enumerator.get_Current();
         this.capacities.Add(edge, double.MaxValue);
     }
     this.Initialize();
 }
 public void Compute(IVertex source, IVertex sink)
 {
     this.balancer = new GraphBalancerAlgorithm(this.VisitedGraph, source, sink, this.capacities);
     this.balancer.Balance();
     this.capacities.set_Item(this.balancer.BalancingSourceEdge, 0.0);
     this.capacities.set_Item(this.balancer.BalancingSinkEdge, 0.0);
     this.reverser.AddReversedEdges();
     this.maxFlowF1.Compute(source, sink);
     this.capacities.set_Item(this.balancer.BalancingSourceEdge, double.MaxValue);
     IEdgeEnumerator enumerator = this.balancer.SurplusEdges.GetEnumerator();
     while (enumerator.MoveNext())
     {
         IVertex v = enumerator.get_Current().get_Target();
         EdgeCollection.Enumerator enumerator2 = this.VisitedGraph.OutEdges(v).GetEnumerator();
         while (enumerator2.MoveNext())
         {
             IEdge edge2 = enumerator2.get_Current();
             if (edge2.get_Target() == this.balancer.BalancingSource)
             {
                 this.capacities.set_Item(edge2, 0.0);
             }
         }
     }
 }
 public void UnBalanceBeforeBalancing()
 {
     g = GraphFactory.UnBalancedFlow();
     algo = new GraphBalancerAlgorithm(g, Traversal.FirstVertex(g), Traversal.FirstVertex(g));
     algo.UnBalance();
 }
Exemplo n.º 8
0
        public void Compute(IVertex source, IVertex sink)
        {
            // step 1 constructor balancing graph
            this.balancer = new GraphBalancerAlgorithm(
                this.VisitedGraph,
                source,
                sink,
                this.capacities
                );

            balancer.Balance();
            this.capacities[balancer.BalancingSourceEdge] = 0;
            this.capacities[balancer.BalancingSinkEdge] = 0;

            // step 2 find max flow
            reverser.AddReversedEdges();
            maxFlowF1.Compute(source, sink);

            // step 3
            this.capacities[balancer.BalancingSourceEdge] = double.MaxValue;
            foreach (IEdge edge in balancer.SurplusEdges)
            {
                IVertex v = edge.Target;
                // find edges
                foreach (IEdge vs in this.VisitedGraph.OutEdges(v))
                {
                    if (vs.Target == balancer.BalancingSource)
                        this.capacities[vs] = 0;
                }
            }

            // step 4:

 //           reverser.RemoveReversedEdges();
   //         balancer.UnBalance();
        }