コード例 #1
0
ファイル: GraphApp.cs プロジェクト: bschwind/Graph-App
 public FlowEdge AddFlowEdge(Vertex from, Vertex to)
 {
     checkNullGraph(currentGraph);
     FlowEdge fe = new FlowEdge(from, to, 0, 1f);
     currentGraph.AddEdge(fe);
     return fe;
 }
コード例 #2
0
        public FlowEdge AddFlowEdge(Vertex from, Vertex to)
        {
            checkNullGraph(currentGraph);
            FlowEdge fe = new FlowEdge(from, to, 0, 1f);

            currentGraph.AddEdge(fe);
            return(fe);
        }
コード例 #3
0
ファイル: FlowNetwork.cs プロジェクト: bschwind/Graph-App
 public void UpdateEdgeFlow(FlowEdge fe)
 {
     FlowEdge edge = FindEdge(fe);
     if (edge != null)
     {
         edge.CurrentFlow = edge.CurrentFlow + fe.CurrentFlow;
     }
 }
コード例 #4
0
        public void UpdateEdgeFlow(FlowEdge fe)
        {
            FlowEdge edge = FindEdge(fe);

            if (edge != null)
            {
                edge.CurrentFlow = edge.CurrentFlow + fe.CurrentFlow;
            }
        }
コード例 #5
0
        public FlowEdge FindEdge(FlowEdge fe)
        {
            foreach (FlowEdge flowEdge in edges)
            {
                if (flowEdge.Equals(fe))
                {
                    return(flowEdge);
                }
            }

            return(null);
        }
コード例 #6
0
ファイル: FlowNetwork.cs プロジェクト: bschwind/Graph-App
        public FlowEdge FindEdge(FlowEdge fe)
        {
            foreach (FlowEdge flowEdge in edges)
            {
                if (flowEdge.Equals(fe))
                {
                    return flowEdge;
                }
            }

            return null;
        }
コード例 #7
0
ファイル: Graph.cs プロジェクト: bschwind/Graph-App
        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());
            }
        }
コード例 #8
0
ファイル: Graph.cs プロジェクト: bschwind/Graph-App
        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());
            }
        }
コード例 #9
0
ファイル: GraphAlgorithms.cs プロジェクト: bschwind/Graph-App
        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);
        }