src/demo/dijkstra_demo.cc
changeset 1258 88dff8bb4bf2
equal deleted inserted replaced
-1:000000000000 0:6d638d79584f
       
     1 #include <iostream>
       
     2 
       
     3 #include <lemon/list_graph.h>
       
     4 #include <lemon/dijkstra.h>
       
     5 
       
     6 using namespace lemon;
       
     7 
       
     8 
       
     9 int main (int, char*[])
       
    10 {
       
    11 
       
    12     typedef ListGraph Graph;
       
    13     typedef Graph::Node Node;
       
    14     typedef Graph::Edge Edge;
       
    15     typedef Graph::EdgeMap<int> LengthMap;
       
    16 
       
    17     Graph g;
       
    18 
       
    19     //An example from Ahuja's book
       
    20 
       
    21     Node s=g.addNode();
       
    22     Node v2=g.addNode();
       
    23     Node v3=g.addNode();
       
    24     Node v4=g.addNode();
       
    25     Node v5=g.addNode();
       
    26     Node t=g.addNode();
       
    27 
       
    28     Edge s_v2=g.addEdge(s, v2);
       
    29     Edge s_v3=g.addEdge(s, v3);
       
    30     Edge v2_v4=g.addEdge(v2, v4);
       
    31     Edge v2_v5=g.addEdge(v2, v5);
       
    32     Edge v3_v5=g.addEdge(v3, v5);
       
    33     Edge v4_t=g.addEdge(v4, t);
       
    34     Edge v5_t=g.addEdge(v5, t);
       
    35   
       
    36     LengthMap len(g);
       
    37 
       
    38     len.set(s_v2, 10);
       
    39     len.set(s_v3, 10);
       
    40     len.set(v2_v4, 5);
       
    41     len.set(v2_v5, 8);
       
    42     len.set(v3_v5, 5);
       
    43     len.set(v4_t, 8);
       
    44     len.set(v5_t, 8);
       
    45 
       
    46     std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl;
       
    47 
       
    48     std::cout << "Dijkstra algorithm test..." << std::endl;
       
    49 
       
    50     Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
       
    51     
       
    52     dijkstra_test.run(s);
       
    53 
       
    54     
       
    55     std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl;
       
    56 
       
    57     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;
       
    58 
       
    59     for (Node v=t;v != s; v=dijkstra_test.predNode(v)){
       
    60 	std::cout << g.id(v) << "<-";
       
    61     }
       
    62     std::cout << g.id(s) << std::endl;	
       
    63     
       
    64 
       
    65     return 0;
       
    66 }
       
    67 
       
    68 
       
    69 
       
    70 
       
    71 
       
    72 
       
    73 
       
    74 
       
    75