demo/dijkstra_demo.cc
author deba
Wed, 01 Mar 2006 10:25:30 +0000
changeset 1991 d7442141d9ef
parent 1875 98698b69a902
child 2391 14a343be7a5a
permissions -rw-r--r--
The graph adadptors can be alteration observed.
In most cases it uses the adapted graph alteration notifiers.
Only special case is now the UndirGraphAdaptor, where
we have to proxy the signals from the graph.

The SubBidirGraphAdaptor is removed, because it doest not
gives more feature than the EdgeSubGraphAdaptor<UndirGraphAdaptor<Graph>>.

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