src/demo/dijkstra_demo.cc
changeset 1182 a1abe9452199
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/demo/dijkstra_demo.cc	Thu Mar 03 17:18:27 2005 +0000
     1.3 @@ -0,0 +1,75 @@
     1.4 +#include <iostream>
     1.5 +
     1.6 +#include <lemon/list_graph.h>
     1.7 +#include <lemon/dijkstra.h>
     1.8 +
     1.9 +using namespace lemon;
    1.10 +
    1.11 +
    1.12 +int main (int, char*[])
    1.13 +{
    1.14 +
    1.15 +    typedef ListGraph Graph;
    1.16 +    typedef Graph::Node Node;
    1.17 +    typedef Graph::Edge Edge;
    1.18 +    typedef Graph::EdgeMap<int> LengthMap;
    1.19 +
    1.20 +    Graph g;
    1.21 +
    1.22 +    //An example from Ahuja's book
    1.23 +
    1.24 +    Node s=g.addNode();
    1.25 +    Node v2=g.addNode();
    1.26 +    Node v3=g.addNode();
    1.27 +    Node v4=g.addNode();
    1.28 +    Node v5=g.addNode();
    1.29 +    Node t=g.addNode();
    1.30 +
    1.31 +    Edge s_v2=g.addEdge(s, v2);
    1.32 +    Edge s_v3=g.addEdge(s, v3);
    1.33 +    Edge v2_v4=g.addEdge(v2, v4);
    1.34 +    Edge v2_v5=g.addEdge(v2, v5);
    1.35 +    Edge v3_v5=g.addEdge(v3, v5);
    1.36 +    Edge v4_t=g.addEdge(v4, t);
    1.37 +    Edge v5_t=g.addEdge(v5, t);
    1.38 +  
    1.39 +    LengthMap len(g);
    1.40 +
    1.41 +    len.set(s_v2, 10);
    1.42 +    len.set(s_v3, 10);
    1.43 +    len.set(v2_v4, 5);
    1.44 +    len.set(v2_v5, 8);
    1.45 +    len.set(v3_v5, 5);
    1.46 +    len.set(v4_t, 8);
    1.47 +    len.set(v5_t, 8);
    1.48 +
    1.49 +    std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl;
    1.50 +
    1.51 +    std::cout << "Dijkstra algorithm test..." << std::endl;
    1.52 +
    1.53 +    Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
    1.54 +    
    1.55 +    dijkstra_test.run(s);
    1.56 +
    1.57 +    
    1.58 +    std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl;
    1.59 +
    1.60 +    std::cout << "The shortest path from s to t goes through the following nodes (the first one is t, the last one is s): "<<std::endl;
    1.61 +
    1.62 +    for (Node v=t;v != s; v=dijkstra_test.predNode(v)){
    1.63 +	std::cout << g.id(v) << "<-";
    1.64 +    }
    1.65 +    std::cout << g.id(s) << std::endl;	
    1.66 +    
    1.67 +
    1.68 +    return 0;
    1.69 +}
    1.70 +
    1.71 +
    1.72 +
    1.73 +
    1.74 +
    1.75 +
    1.76 +
    1.77 +
    1.78 +