Dijkstra shortest path algorithm with edge cost

Topics: algorithm, graph
Nov 24, 2010 at 11:09 AM

Hi,

I start using the library QuickGraph, to execute the Dijkstra Shortest Path algorithm by using
GraphML file format for graphs,
Until now I could execute the algorithm, but I don't know how can I set the target on my program, and also how can I use the cost of the edges from my GraphML file

an example of the code


            //read GRAPHML file
            var g = new BidirectionalGraph<int, Edge<int>>();
            using (var xreader = XmlReader.Create(@"C:\GraphML\test2.graphml"))
                g.DeserializeFromGraphML(xreader,
                    id => int.Parse(id),
                    (source, target, id) => new Edge<int>(source, target)
                );
 
 
            int root = 68; // constant cost
 
            Func<Edge<int>, double> edgeCost1 = b => 1; // Le cout definie comme constante
 
            var predecessors = new VertexPredecessorRecorderObserver<int, Edge<int>>();
 
 
            //  Dijkstra 
            DijkstraShortestPathAlgorithm<int, Edge<int>> dijkstra1 = new DijkstraShortestPathAlgorithm<int, Edge<int>>(g, edgeCost1);
 
             VertexDistanceRecorderObserver<int, Edge<int>> distObserver1 = new VertexDistanceRecorderObserver<int, Edge<int>>(edgeCost1);
            distObserver1.Attach(dijkstra1);
 
            VertexPredecessorRecorderObserver<int, Edge<int>> predecessorObserver1 = new VertexPredecessorRecorderObserver<int, Edge<int>>();
            predecessorObserver1.Attach(dijkstra1);
 
            dijkstra1.Compute(root);
 
            foreach (KeyValuePair<int, double> kvp in distObserver1.Distances)
                 Console.WriteLine("Distance from root to node {0} is {1}", kvp.Key, kvp.Value);
             foreach (KeyValuePair<int, Edge<int>> kvp in predecessorObserver1.VertexPredecessors)
                 Console.WriteLine("If you want to get to {0} you have to enter through the in edge {1}", kvp.Key, kvp.Value);
 
Thanks