| | 1 | /* -*- C++ -*- |
| | 2 | * |
| | 3 | * This file is a part of LEMON, a generic C++ optimization library |
| | 4 | * |
| | 5 | * Copyright (C) 2003-2008 |
| | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
| | 8 | * |
| | 9 | * Permission to use, modify and distribute this software is granted |
| | 10 | * provided that this copyright notice appears in all copies. For |
| | 11 | * precise terms see the accompanying LICENSE file. |
| | 12 | * |
| | 13 | * This software is provided "AS IS" with no warranty of any kind, |
| | 14 | * express or implied, and with no claim as to its suitability for any |
| | 15 | * purpose. |
| | 16 | * |
| | 17 | */ |
| | 18 | |
| | 19 | ///\ingroup demos |
| | 20 | ///\file |
| | 21 | ///\brief Demonstrating the usage of Dijkstra algorithm |
| | 22 | /// |
| | 23 | /// Dijkstra's algorithm computes shortest paths between two nodes in a |
| | 24 | /// digraph with arc lengths. Here we only show some of the facilities |
| | 25 | /// supplied by our implementation. For the detailed documentation of |
| | 26 | /// the LEMON Dijkstra class read \ref lemon::Dijkstra "this". |
| | 27 | /// |
| | 28 | /// \include dijkstra_demo.cc |
| | 29 | |
| | 30 | #include <iostream> |
| | 31 | #include <lemon/list_graph.h> |
| | 32 | #include <lemon/dijkstra.h> |
| | 33 | |
| | 34 | using namespace lemon; |
| | 35 | |
| | 36 | int main (int, char*[]) |
| | 37 | { |
| | 38 | // Convenient type definitions |
| | 39 | typedef ListDigraph Digraph; |
| | 40 | typedef Digraph::Node Node; |
| | 41 | typedef Digraph::Arc Arc; |
| | 42 | typedef Digraph::ArcMap<int> LengthMap; |
| | 43 | |
| | 44 | // Create a directed graph and a length map |
| | 45 | Digraph g; |
| | 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 | Arc s_v2=g.addArc(s, v2); |
| | 55 | Arc s_v3=g.addArc(s, v3); |
| | 56 | Arc v2_v4=g.addArc(v2, v4); |
| | 57 | Arc v2_v5=g.addArc(v2, v5); |
| | 58 | Arc v3_v5=g.addArc(v3, v5); |
| | 59 | Arc v4_t=g.addArc(v4, t); |
| | 60 | Arc v5_t=g.addArc(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." |
| | 73 | << std::endl; |
| | 74 | std::cout << "We calculate the shortest path from node s to node t in a digraph." |
| | 75 | << std::endl << std::endl; |
| | 76 | |
| | 77 | std::cout << "The id of s is " << g.id(s) << ", " |
| | 78 | << "the id of t is " << g.id(t) << "." << std::endl; |
| | 79 | |
| | 80 | // Run Dijkstra algorithm |
| | 81 | Dijkstra<Digraph, LengthMap> dijkstra(g,len); |
| | 82 | dijkstra.run(s,t); |
| | 83 | |
| | 84 | // Print the distance |
| | 85 | std::cout << "The distance of node t from node s: " |
| | 86 | << dijkstra.dist(t) << std::endl; |
| | 87 | |
| | 88 | // Print the shortest path |
| | 89 | std::cout << "The shortest path from s to t goes through the " |
| | 90 | << "following nodes (in reverse order): " << std::endl; |
| | 91 | |
| | 92 | for (Node v = t; v != s; v = dijkstra.predNode(v)) { |
| | 93 | std::cout << g.id(v) << "<-"; |
| | 94 | } |
| | 95 | std::cout << g.id(s) << std::endl; |
| | 96 | |
| | 97 | return 0; |
| | 98 | } |