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