public static ShortestPathTree ShortestPathTree(Graph graph, String sourceLabel)
        {
            G.IDictionary <String, Node> nodes = graph.GetNodes();
            if (!nodes.ContainsKey(sourceLabel))
            {
                throw new Exception("Source node not found in graph.");
            }
            ShortestPathTree predecessorTree = new ShortestPathTree(sourceLabel);

            ISet <DijkstraNode> visited = new HashSet <DijkstraNode>();

            java.util.PriorityQueue <DijkstraNode> pq = new java.util.PriorityQueue <DijkstraNode>();
            foreach (String nodeLabel in nodes.Keys)
            {
                DijkstraNode newNode = new DijkstraNode(nodeLabel);
                newNode.SetDist(double.MaxValue);
                newNode.SetDepth(int.MaxValue);
                predecessorTree.Add(newNode);
            }
            DijkstraNode sourceNode = predecessorTree.GetNodes()[predecessorTree.GetRoot()];

            sourceNode.SetDist(0);
            sourceNode.SetDepth(0);
            pq.add(sourceNode, sourceNode.GetDist());

            int count = 0;

            while (!pq.isEmpty())
            {
                DijkstraNode current   = pq.poll();
                String       currLabel = current.GetLabel();
                visited.Add(current);
                count++;
                G.IDictionary <String, Double> neighbors = nodes[currLabel].GetNeighbors();
                foreach (String currNeighborLabel in neighbors.Keys)
                {
                    DijkstraNode neighborNode = predecessorTree.GetNodes()[currNeighborLabel];
                    Double       currDistance = neighborNode.GetDist();
                    Double       newDistance  = current.GetDist() + nodes[currLabel].GetNeighbors()[currNeighborLabel];
                    if (newDistance < currDistance)
                    {
                        DijkstraNode neighbor = predecessorTree.GetNodes()[currNeighborLabel];

                        pq.remove(neighbor);
                        neighbor.SetDist(newDistance);
                        neighbor.SetDepth(current.GetDepth() + 1);
                        neighbor.SetParent(currLabel);
                        pq.add(neighbor, neighbor.GetDist());
                    }
                }
            }

            return(predecessorTree);
        }
Ejemplo n.º 2
0
        // see comment at the top of the file regarding why the below Comparable<Vertex> methods has been removed
        //public int compareTo(DijkstraNode comparedNode) {
        //    double distance1 = this.dist;
        //    double distance2 = comparedNode.getDist();
        //    if (distance1 == distance2)
        //        return 0;
        //    if (distance1 > distance2)
        //        return 1;
        //    return -1;
        //}
        //public int CompareTo(DijkstraNode other)
        //{
        //    return compareTo(other);
        //}

        public bool Equals(DijkstraNode comparedNode)
        {
            return(this.GetLabel().Equals(comparedNode.GetLabel()));
        }
        public static Path ShortestPath(Graph graph, String sourceLabel, String targetLabel)
        {
            //if (!nodes.containsKey(sourceLabel))
            //    throw new Exception("Source node not found in graph.");
            G.IDictionary <String, Node> nodes           = graph.GetNodes();
            ShortestPathTree             predecessorTree = new ShortestPathTree(sourceLabel);

            java.util.PriorityQueue <DijkstraNode> pq = new java.util.PriorityQueue <DijkstraNode>();
            foreach (String nodeLabel in nodes.Keys)
            {
                DijkstraNode newNode = new DijkstraNode(nodeLabel);
                newNode.SetDist(double.MaxValue);
                newNode.SetDepth(int.MaxValue);
                predecessorTree.Add(newNode);
            }
            DijkstraNode sourceNode = predecessorTree.GetNodes()[predecessorTree.GetRoot()];

            sourceNode.SetDist(0);
            sourceNode.SetDepth(0);
            pq.add(sourceNode, sourceNode.GetDist());

            int count = 0;

            while (!pq.isEmpty())
            {
                DijkstraNode current   = pq.poll();
                String       currLabel = current.GetLabel();
                if (currLabel.Equals(targetLabel))
                {
                    Path   shortestPath = new Path();
                    String currentN     = targetLabel;
                    String parentN      = predecessorTree.GetParentOf(currentN);
                    while (parentN != null)
                    {
                        shortestPath.AddFirst(new Edge(parentN, currentN, nodes[parentN].GetNeighbors()[currentN]));
                        currentN = parentN;
                        parentN  = predecessorTree.GetParentOf(currentN);
                    }
                    return(shortestPath);
                }
                count++;
                G.IDictionary <String, Double> neighbors = nodes[currLabel].GetNeighbors();
                foreach (String currNeighborLabel in neighbors.Keys)
                {
                    DijkstraNode neighborNode = predecessorTree.GetNodes()[currNeighborLabel];
                    Double       currDistance = neighborNode.GetDist();
                    Double       newDistance  = current.GetDist() + nodes[currLabel].GetNeighbors()[currNeighborLabel];
                    if (newDistance < currDistance)
                    {
                        DijkstraNode neighbor = predecessorTree.GetNodes()[currNeighborLabel];

                        pq.remove(neighbor);
                        neighbor.SetDist(newDistance);
                        neighbor.SetDepth(current.GetDepth() + 1);
                        neighbor.SetParent(currLabel);
                        pq.add(neighbor, neighbor.GetDist());
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
 public void Add(DijkstraNode newNode)
 {
     nodes.Add(newNode.GetLabel(), newNode);
 }