コード例 #1
0
ファイル: Subgraph.cs プロジェクト: allisterb/satsumagraph
 public IEnumerable <Arc> Arcs(ArcFilter filter = ArcFilter.All)
 {
     foreach (var arc in graph.Arcs(filter))
     {
         if (IsEnabled(arc) && IsEnabled(graph.U(arc)) && IsEnabled(graph.V(arc)))
         {
             yield return(arc);
         }
     }
 }
コード例 #2
0
        public IEnumerable <Arc> Arcs(ArcFilter filter = ArcFilter.All)
        {
            foreach (Arc item in graph.Arcs(filter))
            {
                if (IsEnabled(item) && IsEnabled(graph.U(item)) && IsEnabled(graph.V(item)))
                {
                    yield return(item);

                    /*Error: Unable to find new state assignment for yield return*/;
                }
            }
            yield break;
IL_0126:
            /*Error near IL_0127: Unexpected return in MoveNext()*/;
        }
コード例 #3
0
ファイル: Bfs.cs プロジェクト: aiime/point-and-click
        /// Performs an iteration which involves dequeueing a node.
        /// The unreached neighbors of the dequeued node are enqueued,
        /// and \e isTarget (which can be null) is called for each of them
        /// to find out if they belong to the target node set.
        /// If a target node is found among them, then the function returns immediately.
        /// \param isTarget Returns \c true for target nodes. Can be null.
        /// \param reachedTargetNode The target node that has been newly reached, or Node.Invalid.
        /// \return \c true if no target node has been reached in this step,
        /// and there is at least one yet unreached node.
        public bool Step(Func <Node, bool> isTarget, out Node reachedTargetNode)
        {
            reachedTargetNode = Node.Invalid;
            if (queue.Count == 0)
            {
                return(false);
            }

            Node node = queue.Dequeue();
            int  d    = level[node] + 1;

            foreach (var arc in Graph.Arcs(node, ArcFilter.Forward))
            {
                Node child = Graph.Other(arc, node);
                if (parentArc.ContainsKey(child))
                {
                    continue;
                }

                queue.Enqueue(child);
                level[child]     = d;
                parentArc[child] = arc;

                if (isTarget != null && isTarget(child))
                {
                    reachedTargetNode = child;
                    return(false);
                }
            }
            return(true);
        }
コード例 #4
0
ファイル: Isomorphism.cs プロジェクト: allisterb/satsumagraph
            /// Perform a step of color refinement and hashing.
            public void Iterate()
            {
                foreach (Node n in graph.Nodes())
                {
                    buffer[n] = 0;
                }

                foreach (Arc a in graph.Arcs())
                {
                    Node u = graph.U(a);
                    Node v = graph.V(a);
                    if (graph.IsEdge(a))
                    {
                        buffer[u] += Utils.ReversibleHash1(coloring[v]);
                        buffer[v] += Utils.ReversibleHash1(coloring[u]);
                    }
                    else
                    {
                        buffer[u] += Utils.ReversibleHash2(coloring[v]);
                        buffer[v] += Utils.ReversibleHash3(coloring[u]);
                    }
                }

                var temp = coloring;

                coloring = buffer;
                buffer   = temp;

                ComputeHash();
            }
コード例 #5
0
        public MaximumStableSet(ISolver solver, IGraph graph, Func <Node, double> weight = null)
        {
            Graph = graph;
            if (weight == null)
            {
                weight = n => 1.0;
            }
            Weight = weight;

            Problem problem = new Problem();

            problem.Mode = OptimizationMode.Maximize;
            foreach (Node n in graph.Nodes())
            {
                Variable v = problem.GetVariable(n);
                v.Type = VariableType.Binary;
                problem.Objective.Coefficients[v] = weight(n);
            }
            foreach (Arc a in graph.Arcs())
            {
                Node u = graph.U(a);
                Node v = graph.V(a);
                if (u != v)
                {
                    problem.Constraints.Add(problem.GetVariable(u) + problem.GetVariable(v) <= 1);
                }
            }

            Solution solution = solver.Solve(problem);

            SolutionType = solution.Type;
            Debug.Assert(SolutionType != SolutionType.Unbounded);
            if (solution.Valid)
            {
                Nodes = new HashSet <Node>();
                foreach (var kv in solution.Primal)
                {
                    if (kv.Value > 0.5)
                    {
                        Node n = (Node)kv.Key.Id;
                        Nodes.Add(n);
                    }
                }
            }
            else
            {
                Nodes = null;
            }
        }
コード例 #6
0
        public Kruskal(IGraph graph, Func <Arc, TCost> cost, Func <Node, int> maxDegree = null)
        {
            Graph     = graph;
            Cost      = cost;
            MaxDegree = maxDegree;
            Forest    = new HashSet <Arc>();
            Degree    = new Dictionary <Node, int>();
            foreach (Node item in Graph.Nodes())
            {
                Degree[item] = 0;
            }
            List <Arc> list = Enumerable.ToList <Arc>(Graph.Arcs(ArcFilter.All));

            list.Sort((Arc a, Arc b) => Cost(a).CompareTo(Cost(b)));
            arcEnumerator = list.GetEnumerator();
            arcsToGo      = Graph.NodeCount() - new ConnectedComponents(Graph, ConnectedComponents.Flags.None).Count;
            components    = new DisjointSet <Node>();
        }
コード例 #7
0
 public NetworkSimplex(IGraph graph, Func <Arc, long> lowerBound = null, Func <Arc, long> upperBound = null, Func <Node, long> supply = null, Func <Arc, double> cost = null)
 {
     Graph      = graph;
     LowerBound = (lowerBound ?? ((Func <Arc, long>)((Arc x) => 0L)));
     UpperBound = (upperBound ?? ((Func <Arc, long>)((Arc x) => 9223372036854775807L)));
     Supply     = (supply ?? ((Func <Node, long>)((Node x) => 0L)));
     Cost       = (cost ?? ((Func <Arc, double>)((Arc x) => 1.0)));
     Epsilon    = 1.0;
     foreach (Arc item in graph.Arcs(ArcFilter.All))
     {
         double num = Math.Abs(Cost(item));
         if (num > 0.0 && num < Epsilon)
         {
             Epsilon = num;
         }
     }
     Epsilon *= 1E-12;
     Clear();
 }
コード例 #8
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>();
            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>();
        }
コード例 #9
0
        /// Hint: use named arguments when calling this constructor.
        public NetworkSimplex(IGraph graph,
                              Func <Arc, long> lowerBound = null, Func <Arc, long> upperBound = null,
                              Func <Node, long> supply    = null, Func <Arc, double> cost     = null)
        {
            Graph      = graph;
            LowerBound = lowerBound ?? (x => 0);
            UpperBound = upperBound ?? (x => long.MaxValue);
            Supply     = supply ?? (x => 0);
            Cost       = cost ?? (x => 1);

            Epsilon = 1;
            foreach (var arc in graph.Arcs())
            {
                double x = Math.Abs(Cost(arc));
                if (x > 0 && x < Epsilon)
                {
                    Epsilon = x;
                }
            }
            Epsilon *= 1e-12;

            Clear();
        }
コード例 #10
0
 public IEnumerable <Arc> Arcs(ArcFilter filter = ArcFilter.All)
 {
     return(graph == null?ArcsInternal(filter) : ArcsInternal(filter).Concat(graph.Arcs(filter)));
 }
コード例 #11
0
 public IEnumerable <Arc> Arcs(ArcFilter filter = ArcFilter.All)
 {
     return((filter != 0) ? (from x in graph.Arcs(ArcFilter.All)
                             where getDirection(x) == Direction.Edge
                             select x) : graph.Arcs(ArcFilter.All));
 }
コード例 #12
0
 public IEnumerable <Arc> Arcs(ArcFilter filter = ArcFilter.All)
 {
     return(graph.Arcs(ArcFilter.All));
 }
コード例 #13
0
        public MinimumVertexCover(ISolver solver, IGraph graph,
                                  Func <Node, double> nodeCost = null, Func <Arc, double> arcWeight = null,
                                  bool relaxed = false)
        {
            Graph = graph;
            if (nodeCost == null)
            {
                nodeCost = n => 1;
            }
            NodeCost = nodeCost;
            if (arcWeight == null)
            {
                arcWeight = a => 1;
            }
            ArcWeight = arcWeight;
            Relaxed   = relaxed;

            Problem problem = new Problem();

            problem.Mode = OptimizationMode.Minimize;
            foreach (Node n in graph.Nodes())
            {
                Variable v = problem.GetVariable(n);
                v.LowerBound = 0;
                if (!relaxed)
                {
                    v.Type = VariableType.Integer;
                }

                problem.Objective.Coefficients[v] = nodeCost(n);
            }
            foreach (Arc a in graph.Arcs())
            {
                Node u = graph.U(a);
                Node v = graph.V(a);
                if (u != v)
                {
                    problem.Constraints.Add(problem.GetVariable(u) + problem.GetVariable(v) >= arcWeight(a));
                }
                else
                {
                    problem.Constraints.Add((Expression)problem.GetVariable(u) >= arcWeight(a));
                }
            }

            Solution solution = solver.Solve(problem);

            SolutionType = solution.Type;
            Debug.Assert(SolutionType != SolutionType.Unbounded);
            if (solution.Valid)
            {
                Nodes = new Dictionary <Node, double>();
                foreach (var kv in solution.Primal)
                {
                    if (kv.Value > 0)
                    {
                        Node n = (Node)kv.Key.Id;
                        Nodes[n] = kv.Value;
                    }
                }
            }
            else
            {
                Nodes = null;
            }
        }
コード例 #14
0
        public Preflow(IGraph graph, Func <Arc, double> capacity, Node source, Node target)
        {
            Graph    = graph;
            Capacity = capacity;
            Source   = source;
            Target   = target;

            flow = new Dictionary <Arc, double>();

            // calculate bottleneck capacity to get an upper bound for the flow value
            Dijkstra dijkstra = new Dijkstra(Graph, a => - Capacity(a), DijkstraMode.Maximum);

            dijkstra.AddSource(Source);
            dijkstra.RunUntilFixed(Target);
            double bottleneckCapacity = -dijkstra.GetDistance(Target);

            if (double.IsPositiveInfinity(bottleneckCapacity))
            {
                // flow value is infinity
                FlowSize = double.PositiveInfinity;
                Error    = 0;
                for (Node n = Target, n2 = Node.Invalid; n != Source; n = n2)
                {
                    Arc arc = dijkstra.GetParentArc(n);
                    flow[arc] = double.PositiveInfinity;
                    n2        = Graph.Other(arc, n);
                }
            }
            else
            {
                // flow value is finite
                if (double.IsNegativeInfinity(bottleneckCapacity))
                {
                    bottleneckCapacity = 0;                     // Target is not accessible
                }
                U = Graph.ArcCount() * bottleneckCapacity;

                // calculate other upper bounds for the flow
                double USource = 0;
                foreach (var arc in Graph.Arcs(Source, ArcFilter.Forward))
                {
                    if (Graph.Other(arc, Source) != Source)
                    {
                        USource += Capacity(arc);
                        if (USource > U)
                        {
                            break;
                        }
                    }
                }
                U = Math.Min(U, USource);
                double UTarget = 0;
                foreach (var arc in Graph.Arcs(Target, ArcFilter.Backward))
                {
                    if (Graph.Other(arc, Target) != Target)
                    {
                        UTarget += Capacity(arc);
                        if (UTarget > U)
                        {
                            break;
                        }
                    }
                }
                U = Math.Min(U, UTarget);

                Supergraph sg        = new Supergraph(Graph);
                Node       newSource = sg.AddNode();
                artificialArc = sg.AddArc(newSource, Source, Directedness.Directed);

                CapacityMultiplier = Utils.LargestPowerOfTwo(long.MaxValue / U);
                if (CapacityMultiplier == 0)
                {
                    CapacityMultiplier = 1;
                }

                var p = new IntegerPreflow(sg, IntegralCapacity, newSource, Target);
                FlowSize = p.FlowSize / CapacityMultiplier;
                Error    = Graph.ArcCount() / CapacityMultiplier;
                foreach (var kv in p.NonzeroArcs)
                {
                    flow[kv.Key] = kv.Value / CapacityMultiplier;
                }
            }
        }
コード例 #15
0
        public Preflow(IGraph graph, Func <Arc, double> capacity, Node source, Node target)
        {
            Graph    = graph;
            Capacity = capacity;
            Source   = source;
            Target   = target;
            flow     = new Dictionary <Arc, double>();
            Dijkstra dijkstra = new Dijkstra(Graph, (Arc a) => 0.0 - Capacity(a), DijkstraMode.Maximum);

            dijkstra.AddSource(Source);
            dijkstra.RunUntilFixed(Target);
            double num = 0.0 - dijkstra.GetDistance(Target);

            if (double.IsPositiveInfinity(num))
            {
                FlowSize = double.PositiveInfinity;
                Error    = 0.0;
                Node node    = Target;
                Node invalid = Node.Invalid;
                while (node != Source)
                {
                    Arc parentArc = dijkstra.GetParentArc(node);
                    flow[parentArc] = double.PositiveInfinity;
                    invalid         = Graph.Other(parentArc, node);
                    node            = invalid;
                }
            }
            else
            {
                if (double.IsNegativeInfinity(num))
                {
                    num = 0.0;
                }
                U = (double)Graph.ArcCount(ArcFilter.All) * num;
                double num2 = 0.0;
                foreach (Arc item in Graph.Arcs(Source, ArcFilter.Forward))
                {
                    if (Graph.Other(item, Source) != Source)
                    {
                        num2 += Capacity(item);
                        if (num2 > U)
                        {
                            break;
                        }
                    }
                }
                U = Math.Min(U, num2);
                double num3 = 0.0;
                foreach (Arc item2 in Graph.Arcs(Target, ArcFilter.Backward))
                {
                    if (Graph.Other(item2, Target) != Target)
                    {
                        num3 += Capacity(item2);
                        if (num3 > U)
                        {
                            break;
                        }
                    }
                }
                U = Math.Min(U, num3);
                Supergraph supergraph = new Supergraph(Graph);
                Node       node2      = supergraph.AddNode();
                artificialArc      = supergraph.AddArc(node2, Source, Directedness.Directed);
                CapacityMultiplier = Utils.LargestPowerOfTwo(9.2233720368547758E+18 / U);
                if (CapacityMultiplier == 0.0)
                {
                    CapacityMultiplier = 1.0;
                }
                IntegerPreflow integerPreflow = new IntegerPreflow(supergraph, IntegralCapacity, node2, Target);
                FlowSize = (double)integerPreflow.FlowSize / CapacityMultiplier;
                Error    = (double)Graph.ArcCount(ArcFilter.All) / CapacityMultiplier;
                foreach (KeyValuePair <Arc, long> nonzeroArc in integerPreflow.NonzeroArcs)
                {
                    flow[nonzeroArc.Key] = (double)nonzeroArc.Value / CapacityMultiplier;
                }
            }
        }
コード例 #16
0
 public IEnumerable <Arc> Arcs(ArcFilter filter = ArcFilter.All)
 {
     return(filter == ArcFilter.All ? graph.Arcs() :
            graph.Arcs().Where(x => getDirection(x) == Direction.Edge));
 }