1 | /* -*- C++ -*- |
---|
2 | * demo/lp_maxflow_demo.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | ///\ingroup demos |
---|
18 | ///\file |
---|
19 | ///\brief Demonstrating LEMON implementation of the Dijkstra algorithm |
---|
20 | /// |
---|
21 | /// Dijkstra's algorithm computes shortest paths between two nodes in |
---|
22 | /// a graph with edge lengths. Here we only show some of the |
---|
23 | /// facilities supplied by our implementation: for the detailed |
---|
24 | /// documentation of the LEMON Dijkstra class read \ref lemon::Dijkstra "this". |
---|
25 | |
---|
26 | #include <iostream> |
---|
27 | |
---|
28 | #include <lemon/list_graph.h> |
---|
29 | #include <lemon/dijkstra.h> |
---|
30 | //#include <lemon/graph_writer.h> |
---|
31 | |
---|
32 | using namespace lemon; |
---|
33 | |
---|
34 | |
---|
35 | int main (int, char*[]) |
---|
36 | { |
---|
37 | |
---|
38 | typedef ListGraph Graph; |
---|
39 | typedef Graph::Node Node; |
---|
40 | typedef Graph::Edge Edge; |
---|
41 | typedef Graph::EdgeMap<int> LengthMap; |
---|
42 | |
---|
43 | Graph g; |
---|
44 | |
---|
45 | //An example from Ahuja's book |
---|
46 | |
---|
47 | Node s=g.addNode(); |
---|
48 | Node v2=g.addNode(); |
---|
49 | Node v3=g.addNode(); |
---|
50 | Node v4=g.addNode(); |
---|
51 | Node v5=g.addNode(); |
---|
52 | Node t=g.addNode(); |
---|
53 | |
---|
54 | Edge s_v2=g.addEdge(s, v2); |
---|
55 | Edge s_v3=g.addEdge(s, v3); |
---|
56 | Edge v2_v4=g.addEdge(v2, v4); |
---|
57 | Edge v2_v5=g.addEdge(v2, v5); |
---|
58 | Edge v3_v5=g.addEdge(v3, v5); |
---|
59 | Edge v4_t=g.addEdge(v4, t); |
---|
60 | Edge v5_t=g.addEdge(v5, t); |
---|
61 | |
---|
62 | LengthMap len(g); |
---|
63 | |
---|
64 | len.set(s_v2, 10); |
---|
65 | len.set(s_v3, 10); |
---|
66 | len.set(v2_v4, 5); |
---|
67 | len.set(v2_v5, 8); |
---|
68 | len.set(v3_v5, 5); |
---|
69 | len.set(v4_t, 8); |
---|
70 | len.set(v5_t, 8); |
---|
71 | |
---|
72 | std::cout << "This program is a simple demo of the LEMON Dijkstra class."<<std::endl; |
---|
73 | std::cout << "We calculate the shortest path from node s to node t in a graph."<<std::endl; |
---|
74 | std::cout <<std::endl; |
---|
75 | |
---|
76 | |
---|
77 | std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl; |
---|
78 | |
---|
79 | std::cout << "Dijkstra algorithm demo..." << std::endl; |
---|
80 | |
---|
81 | |
---|
82 | Dijkstra<Graph, LengthMap> dijkstra_test(g,len); |
---|
83 | |
---|
84 | dijkstra_test.run(s); |
---|
85 | |
---|
86 | |
---|
87 | std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl; |
---|
88 | |
---|
89 | 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; |
---|
90 | |
---|
91 | for (Node v=t;v != s; v=dijkstra_test.predNode(v)){ |
---|
92 | std::cout << g.id(v) << "<-"; |
---|
93 | } |
---|
94 | std::cout << g.id(s) << std::endl; |
---|
95 | |
---|
96 | |
---|
97 | return 0; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|