コード例 #1
0
ファイル: Graph.Dijkstra.cs プロジェクト: htna/explsolv
 public Elem(Graph.Node node, double dist, Graph.Node prev, Graph.Edge edge)
 {
     this.node = node;
     this.dist = dist;
     this.prev = prev;
     this.edge = edge;
     if (edge != null)
     {
         HDebug.Assert(edge.nodes.Length == 2);
         HDebug.Assert(edge.nodes.Contains(node));
         HDebug.Assert(edge.nodes.Contains(prev));
     }
 }
コード例 #2
0
ファイル: Graph.Dijkstra.cs プロジェクト: htna/explsolv
            public static Elem[] BuildMinAlt(Graph graph, Dictionary <Graph.Node, double> source2initdist, Func <double, Graph.Node, Graph.Edge, Graph.Node, double> GetAlt, Func <Graph.Node, bool> IsTarget)
            {
                /// dist(v2) := min[dist(v2), alt[dist(v1), dist(v1-v2)]]
                int maxid = 0;

                foreach (Graph.Node node in graph.Nodes)
                {
                    maxid = Math.Max(maxid, node.id);
                }
                int size = maxid + 1;

                //double maxdist = edge_dist.Values.Max() + 100;

                Elem[] elems = new Elem[size];
                Dictionary <Graph.Node, Graph.Node> prev = new Dictionary <Graph.Node, Graph.Node>();

                foreach (Graph.Node node in graph.Nodes)
                {
                    elems[node.id] = new Elem(node, double.PositiveInfinity, null, null);
                }

                foreach (Graph.Node source in source2initdist.Keys)
                {
                    double initdist = source2initdist[source];
                    HDebug.Assert(initdist >= 0);
                    HDebug.Assert(elems[source.id] != null, elems[source.id].node == source);
                    elems[source.id] = new Elem(source, initdist, null, null);
                }

                SortedSet <Elem> Q = new SortedSet <Elem>(new Elem(null, double.NaN, null, null));

                foreach (Graph.Node node in graph.Nodes)
                {
                    Q.Add(elems[node.id]);
                }

                while (Q.Count > 0)
                {
                    Elem u = Q.Min;
                    Q.Remove(u);

                    if ((IsTarget != null) && (IsTarget(u.node)))
                    {
                        return(elems);
                    }

                    if (double.IsInfinity(u.dist))
                    //if(u.dist > maxdist)
                    {
                        elems[u.node.id] = new Elem(u.node, double.PositiveInfinity, null, null);
                        continue;
                    }

                    foreach (Graph.Node v_node in u.node.nexts.Keys)
                    {
                        Graph.Edge uv_edge = u.node.nexts[v_node];
                        double     alt     = GetAlt(u.dist, u.node, uv_edge, v_node);
                        HDebug.Assert(alt >= u.dist);
                        if (alt < elems[v_node.id].dist)
                        {
                            Q.Remove(elems[v_node.id]);
                            elems[v_node.id] = new Elem(v_node, alt, u.node, uv_edge);
                            Q.Add(elems[v_node.id]);
                        }
                    }
                }

                return(elems);
            }