Any way to get the sum of edges cost automatically?

Topics: algorithm, graph
Mar 4, 2010 at 5:58 PM



I have that code:


var tryGetPaths = _graph.ShortestPathsDijkstra(_edgeCost,1640);
int target = 3562;
IEnumerable> path;
if (tryGetPaths(target, out path))
     foreach (var edge in path)
           { ... }
I know I can get the edges cost doing that: 
double _value = 0;
foreach (var edge in path)
       _edgeCost.TryGetValue(edge, out _value);    
       _value += value;        

Is there any other way to do that?



Mar 17, 2010 at 3:59 PM
Someone put this code in a post. There you can have the sum (Distance) from a source (root) node to all the nodes
// Define some lengths to the streets            
            Dictionary<Edge<string>, double> edgeCost = new Dictionary<Edge<string>, double>(graph.EdgeCount);
            edgeCost.Add(a_e, 1);
            edgeCost.Add(e_f, 1);
            edgeCost.Add(f_g, 5);
            edgeCost.Add(g_h, 1);

DijkstraShortestPathAlgorithm<string, Edge<string>> dijkstra = new DijkstraShortestPathAlgorithm<string, Edge<string>>(graph, AlgorithmExtensions.GetIndexer<Edge<string>, double>(edgeCost));
            // attach a distance observer to give us the shortest path distances
            VertexDistanceRecorderObserver<string, Edge<string>> distObserver = new VertexDistanceRecorderObserver<string, Edge<string>>(AlgorithmExtensions.GetIndexer<Edge<string>, double>(edgeCost));

            // Set the source
            foreach (KeyValuePair<string, double> kvp in distObserver.Distances)
                Console.WriteLine("Distance from root to node {0} is {1}", kvp.Key, kvp.Value);