예제 #1
0
        /// Reverts the solver to its initial state.
        public void Clear()
        {
            Dictionary <Node, long> excess = new Dictionary <Node, long>();

            foreach (var n in Graph.Nodes())
            {
                excess[n] = Supply(n);
            }

            Saturated = new HashSet <Arc>();
            foreach (var arc in Graph.Arcs())
            {
                long f = LowerBound(arc);
                long g = UpperBound(arc);
                if (g < long.MaxValue)
                {
                    Saturated.Add(arc);
                }
                long flow = Flow(arc);
                excess[Graph.U(arc)] -= flow;
                excess[Graph.V(arc)] += flow;
            }

            Potential                 = new Dictionary <Node, double>();
            MyGraph                   = new Supergraph(Graph);
            ArtificialNode            = MyGraph.AddNode();
            Potential[ArtificialNode] = 0;
            ArtificialArcs            = new HashSet <Arc>();
            var artificialArcOf = new Dictionary <Node, Arc>();

            foreach (var n in Graph.Nodes())
            {
                long e   = excess[n];
                Arc  arc = e > 0 ? MyGraph.AddArc(n, ArtificialNode, Directedness.Directed) :
                           MyGraph.AddArc(ArtificialNode, n, Directedness.Directed);
                Potential[n] = (e > 0 ? -1 : 1);
                ArtificialArcs.Add(arc);
                artificialArcOf[n] = arc;
            }

            Tree         = new Dictionary <Arc, long>();
            TreeSubgraph = new Subgraph(MyGraph);
            TreeSubgraph.EnableAllArcs(false);
            foreach (var kv in artificialArcOf)
            {
                Tree[kv.Value] = Math.Abs(excess[kv.Key]);
                TreeSubgraph.Enable(kv.Value, true);
            }

            State = SimplexState.FirstPhase;
            EnteringArcEnumerator = MyGraph.Arcs().GetEnumerator();
            EnteringArcEnumerator.MoveNext();
        }
예제 #2
0
        public void Clear()
        {
            Dictionary <Node, long> dictionary = new Dictionary <Node, long>();

            foreach (Node item in Graph.Nodes())
            {
                dictionary[item] = Supply(item);
            }
            Saturated = new HashSet <Arc>();
            foreach (Arc item2 in Graph.Arcs(ArcFilter.All))
            {
                LowerBound(item2);
                long num = UpperBound(item2);
                if (num < 9223372036854775807L)
                {
                    Saturated.Add(item2);
                }
                long num2 = Flow(item2);
                Node key;
                Dictionary <Node, long> dictionary2;
                (dictionary2 = dictionary)[key = Graph.U(item2)] = dictionary2[key] - num2;
                Node key2;
                (dictionary2 = dictionary)[key2 = Graph.V(item2)] = dictionary2[key2] + num2;
            }
            Potential                 = new Dictionary <Node, double>();
            MyGraph                   = new Supergraph(Graph);
            ArtificialNode            = MyGraph.AddNode();
            Potential[ArtificialNode] = 0.0;
            ArtificialArcs            = new HashSet <Arc>();
            Dictionary <Node, Arc> dictionary3 = new Dictionary <Node, Arc>();

            foreach (Node item3 in Graph.Nodes())
            {
                long num3 = dictionary[item3];
                Arc  arc  = (num3 <= 0) ? MyGraph.AddArc(ArtificialNode, item3, Directedness.Directed) : MyGraph.AddArc(item3, ArtificialNode, Directedness.Directed);
                Potential[item3] = (double)((num3 <= 0) ? 1 : (-1));
                ArtificialArcs.Add(arc);
                dictionary3[item3] = arc;
            }
            Tree         = new Dictionary <Arc, long>();
            TreeSubgraph = new Subgraph(MyGraph);
            TreeSubgraph.EnableAllArcs(false);
            foreach (KeyValuePair <Node, Arc> item4 in dictionary3)
            {
                Tree[item4.Value] = Math.Abs(dictionary[item4.Key]);
                TreeSubgraph.Enable(item4.Value, true);
            }
            State = SimplexState.FirstPhase;
            EnteringArcEnumerator = MyGraph.Arcs(ArcFilter.All).GetEnumerator();
            EnteringArcEnumerator.MoveNext();
        }
예제 #3
0
        public BiEdgeConnectedComponents(IGraph graph, Flags flags = Flags.None)
        {
            Graph = graph;
            BridgeDfs bridgeDfs = new BridgeDfs();

            bridgeDfs.Run(graph, null);
            Count = bridgeDfs.ComponentCount;
            if ((flags & Flags.CreateBridges) != 0)
            {
                Bridges = bridgeDfs.Bridges;
            }
            if ((flags & Flags.CreateComponents) != 0)
            {
                Subgraph subgraph = new Subgraph(graph);
                foreach (Arc bridge in bridgeDfs.Bridges)
                {
                    subgraph.Enable(bridge, false);
                }
                Components = new ConnectedComponents(subgraph, ConnectedComponents.Flags.CreateComponents).Components;
            }
        }
예제 #4
0
        public BiEdgeConnectedComponents(IGraph graph, Flags flags = 0)
        {
            Graph = graph;
            var dfs = new BridgeDfs();

            dfs.Run(graph);

            Count = dfs.ComponentCount;
            if (0 != (flags & Flags.CreateBridges))
            {
                Bridges = dfs.Bridges;
            }
            if (0 != (flags & Flags.CreateComponents))
            {
                Subgraph withoutBridges = new Subgraph(graph);
                foreach (var arc in dfs.Bridges)
                {
                    withoutBridges.Enable(arc, false);
                }
                Components = new ConnectedComponents(withoutBridges, ConnectedComponents.Flags.CreateComponents).Components;
            }
        }
예제 #5
0
        public void Step()
        {
            if (State == SimplexState.FirstPhase || State == SimplexState.SecondPhase)
            {
                Arc    current = EnteringArcEnumerator.Current;
                Arc    arc     = Arc.Invalid;
                double num     = double.NaN;
                bool   flag    = false;
                do
                {
                    Arc current2 = EnteringArcEnumerator.Current;
                    if (!Tree.ContainsKey(current2))
                    {
                        bool   flag2 = Saturated.Contains(current2);
                        double num2  = ActualCost(current2) - (Potential[MyGraph.V(current2)] - Potential[MyGraph.U(current2)]);
                        if ((num2 < 0.0 - Epsilon && !flag2) || (num2 > Epsilon && (flag2 || ActualLowerBound(current2) == -9223372036854775808L)))
                        {
                            arc  = current2;
                            num  = num2;
                            flag = flag2;
                            break;
                        }
                    }
                    if (!EnteringArcEnumerator.MoveNext())
                    {
                        EnteringArcEnumerator = MyGraph.Arcs(ArcFilter.All).GetEnumerator();
                        EnteringArcEnumerator.MoveNext();
                    }
                }while (!(EnteringArcEnumerator.Current == current));
                if (arc == Arc.Invalid)
                {
                    if (State == SimplexState.FirstPhase)
                    {
                        State = SimplexState.SecondPhase;
                        foreach (Arc artificialArc in ArtificialArcs)
                        {
                            if (Flow(artificialArc) > 0)
                            {
                                State = SimplexState.Infeasible;
                                break;
                            }
                        }
                        if (State == SimplexState.SecondPhase)
                        {
                            RecalculatePotentialDfs recalculatePotentialDfs = new RecalculatePotentialDfs();
                            recalculatePotentialDfs.Parent = this;
                            recalculatePotentialDfs.Run(TreeSubgraph, null);
                        }
                    }
                    else
                    {
                        State = SimplexState.Optimal;
                    }
                }
                else
                {
                    Node       node  = MyGraph.U(arc);
                    Node       node2 = MyGraph.V(arc);
                    List <Arc> list  = new List <Arc>();
                    List <Arc> list2 = new List <Arc>();
                    IPath      path  = TreeSubgraph.FindPath(node2, node, Dfs.Direction.Undirected);
                    foreach (Node item in path.Nodes())
                    {
                        Arc arc2 = path.NextArc(item);
                        ((!(MyGraph.U(arc2) == item)) ? list2 : list).Add(arc2);
                    }
                    ulong num3  = (!(num < 0.0)) ? MySubtract(Flow(arc), ActualLowerBound(arc)) : MySubtract(ActualUpperBound(arc), Flow(arc));
                    Arc   arc3  = arc;
                    bool  flag3 = !flag;
                    foreach (Arc item2 in list)
                    {
                        ulong num4 = (!(num < 0.0)) ? MySubtract(Tree[item2], ActualLowerBound(item2)) : MySubtract(ActualUpperBound(item2), Tree[item2]);
                        if (num4 < num3)
                        {
                            num3  = num4;
                            arc3  = item2;
                            flag3 = (num < 0.0);
                        }
                    }
                    foreach (Arc item3 in list2)
                    {
                        ulong num5 = (!(num > 0.0)) ? MySubtract(Tree[item3], ActualLowerBound(item3)) : MySubtract(ActualUpperBound(item3), Tree[item3]);
                        if (num5 < num3)
                        {
                            num3  = num5;
                            arc3  = item3;
                            flag3 = (num > 0.0);
                        }
                    }
                    long num6 = 0L;
                    switch (num3)
                    {
                    case ulong.MaxValue:
                        State = SimplexState.Unbounded;
                        return;

                    default:
                        num6 = (long)((!(num < 0.0)) ? (0L - num3) : num3);
                        foreach (Arc item4 in list)
                        {
                            Dictionary <Arc, long> tree;
                            Arc key;
                            (tree = Tree)[key = item4] = tree[key] + num6;
                        }
                        foreach (Arc item5 in list2)
                        {
                            Dictionary <Arc, long> tree;
                            Arc key2;
                            (tree = Tree)[key2 = item5] = tree[key2] - num6;
                        }
                        break;

                    case 0uL:
                        break;
                    }
                    if (arc3 == arc)
                    {
                        if (flag)
                        {
                            Saturated.Remove(arc);
                        }
                        else
                        {
                            Saturated.Add(arc);
                        }
                    }
                    else
                    {
                        Tree.Remove(arc3);
                        TreeSubgraph.Enable(arc3, false);
                        if (flag3)
                        {
                            Saturated.Add(arc3);
                        }
                        double num7 = ActualCost(arc) - (Potential[node2] - Potential[node]);
                        if (num7 != 0.0)
                        {
                            UpdatePotentialDfs updatePotentialDfs = new UpdatePotentialDfs();
                            updatePotentialDfs.Parent = this;
                            updatePotentialDfs.Diff   = num7;
                            updatePotentialDfs.Run(TreeSubgraph, new Node[1]
                            {
                                node2
                            });
                        }
                        Tree[arc] = Flow(arc) + num6;
                        if (flag)
                        {
                            Saturated.Remove(arc);
                        }
                        TreeSubgraph.Enable(arc, true);
                    }
                }
            }
        }