COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/dijkstra_demo.cc @ 1852:ffa7c6e96330

Last change on this file since 1852:ffa7c6e96330 was 1715:e71778873dd0, checked in by Alpar Juttner, 18 years ago

Doc improvments

File size: 2.7 KB
Line 
1/* -*- C++ -*-
2 * demo/dijkstra_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 the usage of LEMON's 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 dijkstra_demo.cc
27
28#include <iostream>
29
30#include <lemon/list_graph.h>
31#include <lemon/dijkstra.h>
32
33using namespace lemon;
34
35
36int main (int, char*[])
37{
38
39    typedef ListGraph Graph;
40    typedef Graph::Node Node;
41    typedef Graph::Edge Edge;
42    typedef Graph::EdgeMap<int> LengthMap;
43
44    Graph g;
45
46    //An example from Ahuja's book
47
48    Node s=g.addNode();
49    Node v2=g.addNode();
50    Node v3=g.addNode();
51    Node v4=g.addNode();
52    Node v5=g.addNode();
53    Node t=g.addNode();
54
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);
62 
63    LengthMap len(g);
64
65    len.set(s_v2, 10);
66    len.set(s_v3, 10);
67    len.set(v2_v4, 5);
68    len.set(v2_v5, 8);
69    len.set(v3_v5, 5);
70    len.set(v4_t, 8);
71    len.set(v5_t, 8);
72
73    std::cout << "This program is a simple demo of the LEMON Dijkstra class."
74              << std::endl;
75    std::cout <<
76      "We calculate the shortest path from node s to node t in a graph."
77              << std::endl;
78    std::cout << std::endl;
79
80
81    std::cout << "The id of s is " << g.id(s)<< ", the id of t is "
82              << g.id(t) << "." << std::endl;
83
84    std::cout << "Dijkstra algorithm demo..." << std::endl;
85
86    Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
87   
88    dijkstra_test.run(s);
89   
90    std::cout << "The distance of node t from node s: "
91              << dijkstra_test.dist(t) << std::endl;
92
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): "
95              << std::endl;
96
97    for (Node v=t;v != s; v=dijkstra_test.predNode(v)) {
98      std::cout << g.id(v) << "<-";
99    }
100   
101    std::cout << g.id(s) << std::endl; 
102   
103    return 0;
104}
Note: See TracBrowser for help on using the repository browser.