src/demo/dijkstra_demo.cc
author alpar
Sun, 27 Mar 2005 15:59:04 +0000
changeset 1268 a1f9a4d4ea0c
permissions -rw-r--r--
Use the standard idMap
athos@1182
     1
#include <iostream>
athos@1182
     2
athos@1182
     3
#include <lemon/list_graph.h>
athos@1182
     4
#include <lemon/dijkstra.h>
athos@1182
     5
athos@1182
     6
using namespace lemon;
athos@1182
     7
athos@1182
     8
athos@1182
     9
int main (int, char*[])
athos@1182
    10
{
athos@1182
    11
athos@1182
    12
    typedef ListGraph Graph;
athos@1182
    13
    typedef Graph::Node Node;
athos@1182
    14
    typedef Graph::Edge Edge;
athos@1182
    15
    typedef Graph::EdgeMap<int> LengthMap;
athos@1182
    16
athos@1182
    17
    Graph g;
athos@1182
    18
athos@1182
    19
    //An example from Ahuja's book
athos@1182
    20
athos@1182
    21
    Node s=g.addNode();
athos@1182
    22
    Node v2=g.addNode();
athos@1182
    23
    Node v3=g.addNode();
athos@1182
    24
    Node v4=g.addNode();
athos@1182
    25
    Node v5=g.addNode();
athos@1182
    26
    Node t=g.addNode();
athos@1182
    27
athos@1182
    28
    Edge s_v2=g.addEdge(s, v2);
athos@1182
    29
    Edge s_v3=g.addEdge(s, v3);
athos@1182
    30
    Edge v2_v4=g.addEdge(v2, v4);
athos@1182
    31
    Edge v2_v5=g.addEdge(v2, v5);
athos@1182
    32
    Edge v3_v5=g.addEdge(v3, v5);
athos@1182
    33
    Edge v4_t=g.addEdge(v4, t);
athos@1182
    34
    Edge v5_t=g.addEdge(v5, t);
athos@1182
    35
  
athos@1182
    36
    LengthMap len(g);
athos@1182
    37
athos@1182
    38
    len.set(s_v2, 10);
athos@1182
    39
    len.set(s_v3, 10);
athos@1182
    40
    len.set(v2_v4, 5);
athos@1182
    41
    len.set(v2_v5, 8);
athos@1182
    42
    len.set(v3_v5, 5);
athos@1182
    43
    len.set(v4_t, 8);
athos@1182
    44
    len.set(v5_t, 8);
athos@1182
    45
athos@1182
    46
    std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl;
athos@1182
    47
athos@1182
    48
    std::cout << "Dijkstra algorithm test..." << std::endl;
athos@1182
    49
athos@1182
    50
    Dijkstra<Graph, LengthMap> dijkstra_test(g,len);
athos@1182
    51
    
athos@1182
    52
    dijkstra_test.run(s);
athos@1182
    53
athos@1182
    54
    
athos@1182
    55
    std::cout << "The distance of node t from node s: " << dijkstra_test.dist(t)<<std::endl;
athos@1182
    56
athos@1182
    57
    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
    58
athos@1182
    59
    for (Node v=t;v != s; v=dijkstra_test.predNode(v)){
athos@1182
    60
	std::cout << g.id(v) << "<-";
athos@1182
    61
    }
athos@1182
    62
    std::cout << g.id(s) << std::endl;	
athos@1182
    63
    
athos@1182
    64
athos@1182
    65
    return 0;
athos@1182
    66
}
athos@1182
    67
athos@1182
    68
athos@1182
    69
athos@1182
    70
athos@1182
    71
athos@1182
    72
athos@1182
    73
athos@1182
    74
athos@1182
    75