demo/dijkstra_demo.cc
author alpar
Wed, 13 Jul 2005 20:02:29 +0000
changeset 1556 caf0f91e16a7
parent 1528 1aa71600000c
child 1583 2b329fd595ef
permissions -rw-r--r--
Avoid ambiguity.
athos@1182
     1
#include <iostream>
athos@1182
     2
athos@1182
     3
#include <lemon/list_graph.h>
athos@1182
     4
#include <lemon/dijkstra.h>
athos@1526
     5
//#include <lemon/graph_writer.h>
athos@1182
     6
athos@1182
     7
using namespace lemon;
athos@1182
     8
athos@1182
     9
athos@1182
    10
int main (int, char*[])
athos@1182
    11
{
athos@1182
    12
athos@1182
    13
    typedef ListGraph Graph;
athos@1182
    14
    typedef Graph::Node Node;
athos@1182
    15
    typedef Graph::Edge Edge;
athos@1182
    16
    typedef Graph::EdgeMap<int> LengthMap;
athos@1182
    17
athos@1182
    18
    Graph g;
athos@1182
    19
athos@1182
    20
    //An example from Ahuja's book
athos@1182
    21
athos@1182
    22
    Node s=g.addNode();
athos@1182
    23
    Node v2=g.addNode();
athos@1182
    24
    Node v3=g.addNode();
athos@1182
    25
    Node v4=g.addNode();
athos@1182
    26
    Node v5=g.addNode();
athos@1182
    27
    Node t=g.addNode();
athos@1182
    28
athos@1182
    29
    Edge s_v2=g.addEdge(s, v2);
athos@1182
    30
    Edge s_v3=g.addEdge(s, v3);
athos@1182
    31
    Edge v2_v4=g.addEdge(v2, v4);
athos@1182
    32
    Edge v2_v5=g.addEdge(v2, v5);
athos@1182
    33
    Edge v3_v5=g.addEdge(v3, v5);
athos@1182
    34
    Edge v4_t=g.addEdge(v4, t);
athos@1182
    35
    Edge v5_t=g.addEdge(v5, t);
athos@1182
    36
  
athos@1182
    37
    LengthMap len(g);
athos@1182
    38
athos@1182
    39
    len.set(s_v2, 10);
athos@1182
    40
    len.set(s_v3, 10);
athos@1182
    41
    len.set(v2_v4, 5);
athos@1182
    42
    len.set(v2_v5, 8);
athos@1182
    43
    len.set(v3_v5, 5);
athos@1182
    44
    len.set(v4_t, 8);
athos@1182
    45
    len.set(v5_t, 8);
athos@1182
    46
athos@1530
    47
    std::cout << "This program is a simple demo of the LEMON Dijkstra class."<<std::endl;
athos@1530
    48
    std::cout << "We calculate the shortest path from node s to node t in a graph."<<std::endl;
athos@1530
    49
    std::cout <<std::endl;
athos@1530
    50
athos@1530
    51
athos@1182
    52
    std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl;
athos@1182
    53
athos@1182
    54
    std::cout << "Dijkstra algorithm test..." << std::endl;
athos@1182
    55
athos@1521
    56
athos@1182
    57
    Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
athos@1182
    58
    
athos@1182
    59
    dijkstra_test.run(s);
athos@1182
    60
athos@1182
    61
    
athos@1182
    62
    std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl;
athos@1182
    63
athos@1182
    64
    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
    65
athos@1182
    66
    for (Node v=t;v != s; v=dijkstra_test.predNode(v)){
athos@1182
    67
	std::cout << g.id(v) << "<-";
athos@1182
    68
    }
athos@1182
    69
    std::cout << g.id(s) << std::endl;	
athos@1182
    70
    
athos@1182
    71
athos@1182
    72
    return 0;
athos@1182
    73
}
athos@1182
    74
athos@1182
    75
athos@1182
    76
athos@1182
    77
athos@1182
    78
athos@1182
    79
athos@1182
    80
athos@1182
    81
athos@1182
    82