COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/dijkstra_demo.cc

Last change on this file was 2553:bfced05fa852, checked in by Alpar Juttner, 12 years ago

Happy New Year to LEMON (+ better update-copyright-header script)

File size: 2.7 KB
Line 
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 LEMON's Dijkstra algorithm
22///
23/// Dijkstra's algorithm computes shortest paths between two nodes in
24/// a graph with edge lengths. Here we only show some of the
25/// facilities supplied by our implementation: for the detailed
26/// documentation of the LEMON Dijkstra class read \ref lemon::Dijkstra "this".
27///
28/// \include dijkstra_demo.cc
29
30#include <iostream>
31
32#include <lemon/list_graph.h>
33#include <lemon/dijkstra.h>
34
35using namespace lemon;
36
37
38int main (int, char*[])
39{
40
41    typedef ListGraph Graph;
42    typedef Graph::Node Node;
43    typedef Graph::Edge Edge;
44    typedef Graph::EdgeMap<int> LengthMap;
45
46    Graph g;
47
48    //An example from Ahuja's book
49
50    Node s=g.addNode();
51    Node v2=g.addNode();
52    Node v3=g.addNode();
53    Node v4=g.addNode();
54    Node v5=g.addNode();
55    Node t=g.addNode();
56
57    Edge s_v2=g.addEdge(s, v2);
58    Edge s_v3=g.addEdge(s, v3);
59    Edge v2_v4=g.addEdge(v2, v4);
60    Edge v2_v5=g.addEdge(v2, v5);
61    Edge v3_v5=g.addEdge(v3, v5);
62    Edge v4_t=g.addEdge(v4, t);
63    Edge v5_t=g.addEdge(v5, t);
64 
65    LengthMap len(g);
66
67    len.set(s_v2, 10);
68    len.set(s_v3, 10);
69    len.set(v2_v4, 5);
70    len.set(v2_v5, 8);
71    len.set(v3_v5, 5);
72    len.set(v4_t, 8);
73    len.set(v5_t, 8);
74
75    std::cout << "This program is a simple demo of the LEMON Dijkstra class."
76              << std::endl;
77    std::cout <<
78      "We calculate the shortest path from node s to node t in a graph."
79              << std::endl;
80    std::cout << std::endl;
81
82
83    std::cout << "The id of s is " << g.id(s)<< ", the id of t is "
84              << g.id(t) << "." << std::endl;
85
86    std::cout << "Dijkstra algorithm demo..." << std::endl;
87
88    Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
89   
90    dijkstra_test.run(s);
91   
92    std::cout << "The distance of node t from node s: "
93              << dijkstra_test.dist(t) << std::endl;
94
95    std::cout << "The shortest path from s to t goes through the following "
96              << "nodes (the first one is t, the last one is s): "
97              << std::endl;
98
99    for (Node v=t;v != s; v=dijkstra_test.predNode(v)) {
100      std::cout << g.id(v) << "<-";
101    }
102   
103    std::cout << g.id(s) << std::endl; 
104   
105    return 0;
106}
Note: See TracBrowser for help on using the repository browser.