示例#1
0
        public Prim(IGraph graph, Func <Arc, TCost> cost)
        {
            Graph       = graph;
            Cost        = cost;
            Forest      = new HashSet <Arc>();
            ForestGraph = new Subgraph(graph);
            ForestGraph.EnableAllArcs(false);

            Run();
        }
示例#2
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();
        }
示例#3
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();
        }
示例#4
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;
            }
        }
示例#5
0
        private DisjointSet <Node> components;        // The components of the current spanning forest.

        public Kruskal(IGraph graph, Func <Arc, TCost> cost, Func <Node, int> maxDegree = null)
        {
            Graph     = graph;
            Cost      = cost;
            MaxDegree = maxDegree;

            Forest      = new HashSet <Arc>();
            ForestGraph = new Subgraph(graph);
            ForestGraph.EnableAllArcs(false);
            Degree = new Dictionary <Node, int>();
            foreach (var node in Graph.Nodes())
            {
                Degree[node] = 0;
            }

            List <Arc> arcs = Graph.Arcs().ToList();

            arcs.Sort((a, b) => Cost(a).CompareTo(Cost(b)));
            arcEnumerator = arcs.GetEnumerator();
            arcsToGo      = Graph.NodeCount() - new ConnectedComponents(Graph).Count;
            components    = new DisjointSet <Node>();
        }
示例#6
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;
            }
        }