Exemplo n.º 1
0
 public FlowEdge AddFlowEdge(Vertex from, Vertex to)
 {
     checkNullGraph(currentGraph);
     FlowEdge fe = new FlowEdge(from, to, 0, 1f);
     currentGraph.AddEdge(fe);
     return fe;
 }
Exemplo n.º 2
0
        public FlowEdge AddFlowEdge(Vertex from, Vertex to)
        {
            checkNullGraph(currentGraph);
            FlowEdge fe = new FlowEdge(from, to, 0, 1f);

            currentGraph.AddEdge(fe);
            return(fe);
        }
Exemplo n.º 3
0
 public void UpdateEdgeFlow(FlowEdge fe)
 {
     FlowEdge edge = FindEdge(fe);
     if (edge != null)
     {
         edge.CurrentFlow = edge.CurrentFlow + fe.CurrentFlow;
     }
 }
Exemplo n.º 4
0
        public void UpdateEdgeFlow(FlowEdge fe)
        {
            FlowEdge edge = FindEdge(fe);

            if (edge != null)
            {
                edge.CurrentFlow = edge.CurrentFlow + fe.CurrentFlow;
            }
        }
Exemplo n.º 5
0
        public FlowEdge FindEdge(FlowEdge fe)
        {
            foreach (FlowEdge flowEdge in edges)
            {
                if (flowEdge.Equals(fe))
                {
                    return(flowEdge);
                }
            }

            return(null);
        }
Exemplo n.º 6
0
        public FlowEdge FindEdge(FlowEdge fe)
        {
            foreach (FlowEdge flowEdge in edges)
            {
                if (flowEdge.Equals(fe))
                {
                    return flowEdge;
                }
            }

            return null;
        }
Exemplo n.º 7
0
        private void copyLinkedGraph(FlowNetwork to)
        {
            to.Clear();

            for (int i = 0; i < this.GetVertices().Count; i++)
            {
                vertices[i].Tag = i;
                Vertex v = new Vertex();
                v.Tag = i;
                to.AddVertex(v);
            }

            for (int i = 0; i < this.GetEdges().Count; i++)
            {
                Edge currentEdge = this.GetEdges()[i];

                Vertex v1 = new Vertex();
                v1.Label = currentEdge.GetFromVertex().Label;
                int cd = (int)currentEdge.GetFromVertex().Tag;
                if ((int)to.GetVertices()[cd].Tag == cd)
                {
                    v1 = to.GetVertices()[cd];
                }

                Vertex v2 = new Vertex();
                v2.Label = currentEdge.GetToVertex().Label;
                int cd2 = (int)currentEdge.GetToVertex().Tag;
                if ((int)to.GetVertices()[cd2].Tag == cd2)
                {
                    v2 = to.GetVertices()[cd2];
                }

                FlowEdge fe = currentEdge as FlowEdge;
                Edge     e  = new FlowEdge(v1, v2, fe.CurrentFlow, fe.Capacity);

                to.AddEdge(e);
            }

            FlowNetwork fn = this as FlowNetwork;

            if (fn != null)
            {
                to.SetSource(fn.GetSource());
                to.SetSink(fn.GetSink());
            }
        }
Exemplo n.º 8
0
        private void copyLinkedGraph(FlowNetwork to)
        {
            to.Clear();

            for (int i = 0; i < this.GetVertices().Count; i++)
            {
                vertices[i].Tag = i;
                Vertex v = new Vertex();
                v.Tag = i;
                to.AddVertex(v);
            }

            for (int i = 0; i < this.GetEdges().Count; i++)
            {
                Edge currentEdge = this.GetEdges()[i];

                Vertex v1 = new Vertex();
                v1.Label = currentEdge.GetFromVertex().Label;
                int cd = (int)currentEdge.GetFromVertex().Tag;
                if ((int)to.GetVertices()[cd].Tag == cd)
                {
                    v1 = to.GetVertices()[cd];
                }

                Vertex v2 = new Vertex();
                v2.Label = currentEdge.GetToVertex().Label;
                int cd2 = (int)currentEdge.GetToVertex().Tag;
                if ((int)to.GetVertices()[cd2].Tag == cd2)
                {
                    v2 = to.GetVertices()[cd2];
                }

                FlowEdge fe = currentEdge as FlowEdge;
                Edge e = new FlowEdge(v1, v2, fe.CurrentFlow, fe.Capacity);

                to.AddEdge(e);
            }

            FlowNetwork fn = this as FlowNetwork;
            if (fn != null)
            {
                to.SetSource(fn.GetSource());
                to.SetSink(fn.GetSink());
            }
        }
Exemplo n.º 9
0
        public static float MaximumFlow(FlowNetwork fn, int source, int sink)
        {
            float maxFlow = 0f;

            int sourceIndex = source;
            int sinkIndex   = sink;

            foreach (FlowEdge e in fn.GetEdges())
            {
                e.CurrentFlow = 0f;
            }

            Graph g = CreateResidualNetwork(fn);

            List <Edge> augmentingPath = BreadthFirstSearch(g, sourceIndex, sinkIndex);

            while (augmentingPath != null)
            {
                float minFlow = float.MaxValue;
                //find min flow
                foreach (Edge e in augmentingPath)
                {
                    if (e.Weight < minFlow)
                    {
                        minFlow = e.Weight;
                    }
                }

                maxFlow += minFlow;

                foreach (Edge e in augmentingPath)
                {
                    //Update residual graph
                    Edge  currentForwardResid  = g.FindEdge(e);
                    Edge  currentBackwardResid = g.FindEdge(new Edge(e.GetToVertex(), e.GetFromVertex()));
                    float newForwardWeight     = currentForwardResid.Weight - minFlow;
                    if (newForwardWeight <= 0f)
                    {
                        g.RemoveEdge(currentForwardResid);
                    }
                    else
                    {
                        currentForwardResid.Weight = newForwardWeight;
                    }
                    if (currentBackwardResid != null)
                    {
                        float newBackwardWeight = currentBackwardResid.Weight + minFlow;
                        currentBackwardResid.Weight = newBackwardWeight;
                    }
                    else
                    {
                        currentBackwardResid = new Edge(e.GetToVertex(), e.GetFromVertex(), minFlow);
                        g.AddEdge(currentBackwardResid);
                    }

                    //Update flow network
                    FlowEdge fe = fn.FindEdge(new FlowEdge(e.GetFromVertex(), e.GetToVertex(), 0, 0));
                    fe.CurrentFlow = fe.CurrentFlow + minFlow;
                }

                augmentingPath = BreadthFirstSearch(g, sourceIndex, sinkIndex);
                //augmentingPath = null;
            }

            return(maxFlow);
        }