コード例 #1
0
        /**
         * Do the work
         */
        protected void DetermineShortestPaths(BaseVertex sourceVertex,
                                              BaseVertex sinkVertex, bool isSource2sink)
        {
            // 0. clean up variables
            Clear();

            // 1. initialize members
            BaseVertex endVertex   = isSource2sink ? sinkVertex : sourceVertex;
            BaseVertex startVertex = isSource2sink ? sourceVertex : sinkVertex;

            startVertexDistanceIndex.Add(startVertex, 0d);
            startVertex.SetWeight(0d);
            vertexCandidateQueue.add(startVertex, startVertex.GetWeight());

            // 2. start searching for the shortest path
            while (!vertexCandidateQueue.isEmpty())
            {
                BaseVertex curCandidate = vertexCandidateQueue.poll();

                if (curCandidate.Equals(endVertex))
                {
                    break;
                }

                determinedVertexSet.Add(curCandidate);

                UpdateVertex(curCandidate, isSource2sink);
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }