demo/dijkstra_demo.cc
author alpar
Tue, 26 Jul 2005 13:15:13 +0000
changeset 1587 8f1c317ebeb4
parent 1530 d99c3c84f797
child 1636 260ac104190f
permissions -rw-r--r--
Doc improvements
athos@1583
     1
/* -*- C++ -*-
athos@1583
     2
 * demo/lp_maxflow_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".
athos@1583
    25
athos@1182
    26
#include <iostream>
athos@1182
    27
athos@1182
    28
#include <lemon/list_graph.h>
athos@1182
    29
#include <lemon/dijkstra.h>
athos@1526
    30
//#include <lemon/graph_writer.h>
athos@1182
    31
athos@1182
    32
using namespace lemon;
athos@1182
    33
athos@1182
    34
athos@1182
    35
int main (int, char*[])
athos@1182
    36
{
athos@1182
    37
athos@1182
    38
    typedef ListGraph Graph;
athos@1182
    39
    typedef Graph::Node Node;
athos@1182
    40
    typedef Graph::Edge Edge;
athos@1182
    41
    typedef Graph::EdgeMap<int> LengthMap;
athos@1182
    42
athos@1182
    43
    Graph g;
athos@1182
    44
athos@1182
    45
    //An example from Ahuja's book
athos@1182
    46
athos@1182
    47
    Node s=g.addNode();
athos@1182
    48
    Node v2=g.addNode();
athos@1182
    49
    Node v3=g.addNode();
athos@1182
    50
    Node v4=g.addNode();
athos@1182
    51
    Node v5=g.addNode();
athos@1182
    52
    Node t=g.addNode();
athos@1182
    53
athos@1182
    54
    Edge s_v2=g.addEdge(s, v2);
athos@1182
    55
    Edge s_v3=g.addEdge(s, v3);
athos@1182
    56
    Edge v2_v4=g.addEdge(v2, v4);
athos@1182
    57
    Edge v2_v5=g.addEdge(v2, v5);
athos@1182
    58
    Edge v3_v5=g.addEdge(v3, v5);
athos@1182
    59
    Edge v4_t=g.addEdge(v4, t);
athos@1182
    60
    Edge v5_t=g.addEdge(v5, t);
athos@1182
    61
  
athos@1182
    62
    LengthMap len(g);
athos@1182
    63
athos@1182
    64
    len.set(s_v2, 10);
athos@1182
    65
    len.set(s_v3, 10);
athos@1182
    66
    len.set(v2_v4, 5);
athos@1182
    67
    len.set(v2_v5, 8);
athos@1182
    68
    len.set(v3_v5, 5);
athos@1182
    69
    len.set(v4_t, 8);
athos@1182
    70
    len.set(v5_t, 8);
athos@1182
    71
athos@1530
    72
    std::cout << "This program is a simple demo of the LEMON Dijkstra class."<<std::endl;
athos@1530
    73
    std::cout << "We calculate the shortest path from node s to node t in a graph."<<std::endl;
athos@1530
    74
    std::cout <<std::endl;
athos@1530
    75
athos@1530
    76
athos@1182
    77
    std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl;
athos@1182
    78
athos@1583
    79
    std::cout << "Dijkstra algorithm demo..." << std::endl;
athos@1182
    80
athos@1521
    81
athos@1182
    82
    Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
athos@1182
    83
    
athos@1182
    84
    dijkstra_test.run(s);
athos@1182
    85
athos@1182
    86
    
athos@1182
    87
    std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl;
athos@1182
    88
athos@1182
    89
    std::cout << "The shortest path from s to t goes through the following nodes (the first one is t, the last one is s): "<<std::endl;
athos@1182
    90
athos@1182
    91
    for (Node v=t;v != s; v=dijkstra_test.predNode(v)){
athos@1182
    92
	std::cout << g.id(v) << "<-";
athos@1182
    93
    }
athos@1182
    94
    std::cout << g.id(s) << std::endl;	
athos@1182
    95
    
athos@1182
    96
athos@1182
    97
    return 0;
athos@1182
    98
}
athos@1182
    99
athos@1182
   100
athos@1182
   101
athos@1182
   102
athos@1182
   103
athos@1182
   104
athos@1182
   105
athos@1182
   106
athos@1182
   107