COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/jacint/dijkstra.h @ 352:4b89077ab715

Last change on this file since 352:4b89077ab715 was 220:7deda4d6a07a, checked in by jacint, 21 years ago

* empty log message *

File size: 2.6 KB
RevLine 
[159]1// -*- C++ -*-
2/*
[211]3 *template <Graph, T, Heap=FibHeap, LengthMap=Graph::EdgeMap<T> >
[159]4 *
5 *Constructor:
6 *
[211]7 *Dijkstra(Graph G, LengthMap length)
[159]8 *
9 *
[211]10 *Methods:
[159]11 *
[211]12 *void run(Node s)
[159]13 *
[211]14 *T dist(Node v) : After run(s) was run, it returns the distance from s to v.
15 *   Returns T() if v is not reachable from s.
[159]16 *
[211]17 *Edge pred(Node v) : After run(s) was run, it returns the last
18 *   edge of a shortest s-v path. It is INVALID for s and for
19 *   the nodes not reachable from s.
[159]20 *
[211]21 *bool reached(Node v) : After run(s) was run, it is true iff v is
22 *   reachable from s
[159]23 *
24 */
25
[211]26#ifndef HUGO_DIJKSTRA_H
27#define HUGO_DIJKSTRA_H
[159]28
29#include <fib_heap.h>
[211]30#include <invalid.h>
[159]31
32namespace hugo {
[211]33 
[159]34  template <typename Graph, typename T,
[211]35    typename Heap=FibHeap<typename Graph::Node, T,
36    typename Graph::NodeMap<int> >,
37    typename LengthMap=typename Graph::EdgeMap<T> >
38  class Dijkstra{
39    typedef typename Graph::Node Node;
40    typedef typename Graph::NodeIt NodeIt;
41    typedef typename Graph::Edge Edge;
42    typedef typename Graph::OutEdgeIt OutEdgeIt;
43   
44    const Graph& G;
45    const LengthMap& length;
46    typename Graph::NodeMap<Edge> predecessor;
47    typename Graph::NodeMap<T> distance;
[217]48    //FIXME:
[211]49    typename Graph::NodeMap<bool> reach;
[217]50    //typename Graph::NodeMap<int> reach;
[211]51   
[159]52  public :
[211]53   
[159]54    /*
55      The distance of the nodes is 0.
56    */
[211]57    Dijkstra(Graph& _G, LengthMap& _length) : G(_G),
58      length(_length), predecessor(_G), distance(_G), reach(_G) { }
[159]59   
[211]60
61    void run(Node s) {
[159]62     
[211]63      NodeIt u;
64      for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
65        predecessor.set(u,INVALID);
66        distance.set(u,0);
67        reach.set(u,false);
68      }
69     
[217]70      //FIXME:
[211]71      typename Graph::NodeMap<bool> scanned(G,false);
[217]72      //typename Graph::NodeMap<int> scanned(G,false);
[211]73      typename Graph::NodeMap<int> heap_map(G,-1);
74     
75      Heap heap(heap_map);
76
77      heap.push(s,0);
78      reach.set(s, true);
79
80      while ( !heap.empty() ) {
[159]81       
[211]82        Node v=heap.top();
[220]83        T oldvalue=heap.get(v);
[211]84        heap.pop();
85        distance.set(v, oldvalue);
86        scanned.set(v,true);
[159]87
[211]88        OutEdgeIt e;
89        for( G.first(e,v); G.valid(e); G.next(e)) {
90          Node w=G.head(e);
[159]91           
[211]92          if ( !scanned[w] ) {
93            if ( !reach[w] ) {
94              reach.set(w,true);
95              heap.push(w,oldvalue+length[e]);
96              predecessor.set(w,e);
[220]97            } else if ( oldvalue+length[e] < heap.get(w) ) {
[211]98              predecessor.set(w,e);
99              heap.decrease(w, oldvalue+length[e]);
[159]100            }
101          }
102        }
[211]103      }
[217]104    }
[211]105   
106    T dist(Node v) {
107      return distance[v];
108    }
[159]109
[211]110    Edge pred(Node v) {
111      return predecessor[v];
112    }
[159]113     
[211]114    bool reached(Node v) {
115      return reach[v];
116    }
117   
118  };
[167]119 
[159]120}
121
122#endif
123
124
Note: See TracBrowser for help on using the repository browser.