Example #1
0
        // Compute distance from startVertex to all other vertices
        public void ComputeAllFromVertex(TVertex startVertex)
        {
            ComputedCosts[startVertex] = 0.0;
            ComputedPaths[startVertex] = startVertex;

            var remainingVertices = new HashSet <TVertex>(Graph.VertexIterator());

            while (remainingVertices.Count > 0)
            {
                var currentVertex = popShortestRemaining(remainingVertices);
                // Cost to get to current vertex
                var cummulativeCost = ComputedCosts[currentVertex];

                foreach (TEdge edgeToNeighbor in Graph.EdgeIterator(currentVertex))
                {
                    var neighbor             = edgeToNeighbor.End;
                    var neighborEdgeCost     = EdgeCosts[edgeToNeighbor];
                    var neighborComputedCost = ComputedCosts[neighbor];

                    var costFromCurrent = cummulativeCost + neighborEdgeCost;

                    // Cost to neighbor is either what it already was, or the cost
                    // to get here plus the cost of the edge
                    if (costFromCurrent < neighborComputedCost)
                    {
                        ComputedCosts[neighbor] = costFromCurrent;
                        ComputedPaths[neighbor] = currentVertex;
                    }
                }
            }
        }