2 * demo/dijkstra_demo.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 ///\brief Demonstrating the usage of LEMON's Dijkstra algorithm
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".
26 /// \include dijkstra_demo.cc
30 #include <lemon/list_graph.h>
31 #include <lemon/dijkstra.h>
33 using namespace lemon;
36 int main (int, char*[])
39 typedef ListGraph Graph;
40 typedef Graph::Node Node;
41 typedef Graph::Edge Edge;
42 typedef Graph::EdgeMap<int> LengthMap;
46 //An example from Ahuja's book
55 Edge s_v2=g.addEdge(s, v2);
56 Edge s_v3=g.addEdge(s, v3);
57 Edge v2_v4=g.addEdge(v2, v4);
58 Edge v2_v5=g.addEdge(v2, v5);
59 Edge v3_v5=g.addEdge(v3, v5);
60 Edge v4_t=g.addEdge(v4, t);
61 Edge v5_t=g.addEdge(v5, t);
73 std::cout << "This program is a simple demo of the LEMON Dijkstra class."
76 "We calculate the shortest path from node s to node t in a graph."
78 std::cout << std::endl;
81 std::cout << "The id of s is " << g.id(s)<< ", the id of t is "
82 << g.id(t) << "." << std::endl;
84 std::cout << "Dijkstra algorithm demo..." << std::endl;
86 Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
90 std::cout << "The distance of node t from node s: "
91 << dijkstra_test.dist(t) << std::endl;
93 std::cout << "The shortest path from s to t goes through the following "
94 << "nodes (the first one is t, the last one is s): "
97 for (Node v=t;v != s; v=dijkstra_test.predNode(v)) {
98 std::cout << g.id(v) << "<-";
101 std::cout << g.id(s) << std::endl;