Exemplo n.º 1
0
 void UpdateOutEdgesOfV(int v)
 {
     foreach (var outEdge in graph.OutEdges(v))
     {
         var u = outEdge.Target;
         if (NodeIsInTree(u))
         {
             continue;
         }
         IEdge oldEdge;
         if (hedgehog.TryGetValue(u, out oldEdge))
         {
             var oldWeight = weight(oldEdge);
             var newWeight = weight(outEdge);
             if (newWeight < oldWeight)
             {
                 q.DecreasePriority(u, newWeight);
                 hedgehog[u] = outEdge;
             }
         }
         else
         {
             q.Enqueue(u, weight(outEdge));
             hedgehog[u] = outEdge;
         }
     }
 }
Exemplo n.º 2
0
        public void MSAGLAstarSSSP(Vertex[] vList, Edge[,] eList, int[] degList, int source, Dictionary <Node, int> nodeId, GeometryGraph _mainGeometryGraph,
                                   int NofNodesBeforeDetour, int n, Tiling g, Tiling g1)
        {
            var q = new BinaryHeapPriorityQueue(n);



            vList[source].Dist = 0;
            q.Enqueue(source, vList[source].Dist);

            for (int i = 0; i < n; i++)
            {
                if (vList[i].Id != source)
                {
                    vList[i].Dist = double.MaxValue;
                    q.Enqueue(i, vList[i].Dist);
                }
                vList[i].Parent  = null;
                vList[i].Visited = false;
            }


            Distance = 0;

            while (q.Count > 0)
            {
                var    deq = q.Dequeue();
                Vertex u   = vList[deq];
                u.Visited = true;
                if (u == null || u.Invalid)
                {
                    return;
                }
                for (int neighb = 0; neighb < degList[u.Id]; neighb++)
                {
                    var neighborId = eList[u.Id, neighb].NodeId;
                    int discourage = 0;

                    if (u.Id < NofNodesBeforeDetour && u.Id != source)
                    {
                        discourage = 1000;
                    }

                    Vertex neighbor = vList[neighborId];
                    double edist    = MsaglUtilities.EucledianDistance(u.XLoc, u.YLoc, neighbor.XLoc, neighbor.YLoc);
                    var    tempDist = u.Dist + edist + discourage;
                    if (tempDist >= neighbor.Dist)
                    {
                        continue;
                    }

                    neighbor.Dist   = tempDist;
                    neighbor.Parent = u;
                    if (neighbor.Visited)
                    {
                        neighbor.Visited = false;
                        q.Enqueue(neighbor.Id, neighbor.Dist);
                    }
                    else
                    {
                        q.DecreasePriority(neighbor.Id, neighbor.Dist);
                    }
                }
            }
            foreach (var node in _mainGeometryGraph.Nodes)
            {
                int target = nodeId[node];
                if (target == source)
                {
                    continue;
                }
                Vertex route     = vList[target];
                int    zoomlevel = Math.Max(vList[source].ZoomLevel, vList[target].ZoomLevel);
                Edgelist.Clear();
                while (route.Parent != null)
                {
                    ShortestPath.Add(route.Id);
                    for (int neighb = 0; neighb < degList[route.Id]; neighb++)
                    {
                        if (eList[route.Id, neighb].NodeId == route.Parent.Id)
                        {
                            SetUsed(vList, eList, degList, route.Id, eList[route.Id, neighb].NodeId, zoomlevel);
                            Edgelist.Add(new VertexNeighbor(route.Id, neighb));
                            break;
                        }
                    }

                    route = route.Parent;
                }
                if (route.Id != source)
                {
                    Debug.WriteLine("path not found");
                }
                foreach (VertexNeighbor vn in Edgelist)
                {
                    g1.AddEdge(vn.A, g.EList[vn.A, vn.Neighbor].NodeId, g.EList[vn.A, vn.Neighbor].Selected, g.EList[vn.A, vn.Neighbor].Used);
                }
            }
            return;
        }
Exemplo n.º 3
0
        public List <int> MSAGLAstarShortestPath(Vertex[] vList, Edge[,] eList, int[] degList, int source, int target, int n)
        {
            var q = new BinaryHeapPriorityQueue(n);


            vList[source].Dist   = 0;
            vList[source].Weight = vList[source].Dist +
                                   MsaglUtilities.EucledianDistance(
                vList[source].XLoc, vList[source].YLoc,
                vList[target].XLoc, vList[target].YLoc);
            q.Enqueue(source, vList[source].Weight);

            for (int i = 0; i < n; i++)
            {
                if (vList[i].Id != source)
                {
                    vList[i].Dist   = double.MaxValue;
                    vList[i].Weight = double.MaxValue;
                    q.Enqueue(i, vList[i].Weight);
                }
                vList[i].Parent  = null;
                vList[i].Visited = false;
            }

            Edgelist.Clear();
            ShortestPath.Clear();
            Distance = 0;

            while (q.Count > 0)
            {
                var    deq = q.Dequeue();
                Vertex u   = vList[deq];
                u.Visited = true;
                if (u == null || u.Invalid)
                {
                    return(new List <int>());
                }
                for (int neighb = 0; neighb < degList[u.Id]; neighb++)
                {
                    var neighborId = eList[u.Id, neighb].NodeId;
                    int discourage = 0;
                    if (eList[u.Id, neighb].Selected == 1)
                    {
                        discourage = 1000;                                   //continue;
                    }
                    if (eList[u.Id, neighb].NodeId == source || eList[u.Id, neighb].NodeId == target)
                    {
                        discourage = 0;
                    }
                    if (u.Id == source || u.Id == target)
                    {
                        discourage = 0;
                    }

                    Vertex neighbor = vList[neighborId];
                    double edist    = MsaglUtilities.EucledianDistance(u.XLoc, u.YLoc, neighbor.XLoc, neighbor.YLoc);
                    var    tempDist = u.Dist + edist + discourage;
                    if (tempDist >= neighbor.Dist)
                    {
                        continue;
                    }

                    neighbor.Dist = tempDist;
                    var tempWeight = neighbor.Dist + MsaglUtilities.EucledianDistance(vList[target].XLoc, vList[target].YLoc, neighbor.XLoc, neighbor.YLoc);
                    neighbor.Weight = tempWeight;
                    neighbor.Parent = u;
                    if (neighbor.Visited)
                    {
                        neighbor.Visited = false;
                        q.Enqueue(neighbor.Id, neighbor.Weight);
                    }
                    else
                    {
                        q.DecreasePriority(neighbor.Id, neighbor.Weight);
                    }
                }
                if (u.Id == target)
                {
                    break;
                }
            }

            Vertex route     = vList[target];
            int    zoomlevel = Math.Max(vList[source].ZoomLevel, vList[target].ZoomLevel);

            while (route.Parent != null)
            {
                ShortestPath.Add(route.Id);
                for (int neighb = 0; neighb < degList[route.Id]; neighb++)
                {
                    if (eList[route.Id, neighb].NodeId == route.Parent.Id)
                    {
                        SetUsed(vList, eList, degList, route.Id, eList[route.Id, neighb].NodeId, zoomlevel);
                        Edgelist.Add(new VertexNeighbor(route.Id, neighb));

                        Distance += Math.Sqrt((route.XLoc - route.Parent.XLoc) * (route.XLoc - route.Parent.XLoc) + (route.YLoc - route.Parent.YLoc) * (route.YLoc - route.Parent.YLoc));

                        break;
                    }
                }

                route = route.Parent;
            }
            ShortestPath.Add(route.Id);
            if (route.Id != source)
            {
                Debug.WriteLine("path not found");
            }
            return(ShortestPath);
        }