COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/dijkstra_demo.cc @ 1528:1aa71600000c

Last change on this file since 1528:1aa71600000c was 1528:1aa71600000c, checked in by athos, 19 years ago

Graph input-output demo, some documentation.

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