Sophie

Sophie

distrib > Fedora > 14 > i386 > by-pkgid > 623999701586b0ea103ff2ccad7954a6 > files > 6880

boost-doc-1.44.0-1.fc14.noarch.rpm

<HTML>
<!--
     Copyright (c) Michael Hansen 2009
    
     Distributed under the Boost Software License, Version 1.0.
     (See accompanying file LICENSE_1_0.txt or copy at
     http://www.boost.org/LICENSE_1_0.txt)
  -->
<Head>
<Title>Boost Graph Library: Dijkstra's Shortest Paths (No Color Map)</Title>
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 
        ALINK="#ff0000"> 
<IMG SRC="../../../boost.png" 
     ALT="C++ Boost" width="277" height="86"> 

<BR Clear>

<H1><A NAME="sec:dijkstra"></A>
<TT>dijkstra_shortest_paths_no_color_map</TT>
</H1>

<P>
<PRE>
<i>// named parameter version</i>
template &lt;typename Graph, typename Param, typename Tag, typename Rest&gt;
void dijkstra_shortest_paths_no_color_map
  (const Graph&amp; graph,
   typename graph_traits&lt;Graph&gt;::vertex_descriptor start_vertex,
   const bgl_named_params<Param,Tag,Rest>& params);
   
<i>// non-named parameter version</i>
template &lt;typename Graph, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>, 
	  typename PredecessorMap, typename DistanceMap,
	  typename WeightMap, typename VertexIndexMap, typename <a href="http://www.sgi.com/tech/stl/BinaryPredicate.html">DistanceCompare</a>, typename <a href="http://www.sgi.com/tech/stl/BinaryFunction.html">DistanceWeightCombine</a>, 
	  typename DistanceInfinity, typename DistanceZero&gt;
void dijkstra_shortest_paths_no_color_map
  (const Graph&amp; graph,
   typename graph_traits&lt;Graph&gt;::vertex_descriptor start_vertex, 
   PredecessorMap predecessor_map, DistanceMap distance_map, WeightMap weight_map, 
   VertexIndexMap index_map,
   DistanceCompare distance_compare, DistanceWeightCombine distance_weight_combine,
   DistanceInfinity distance_infinity, DistanceZero distance_zero);

<i>// version that does not initialize the property maps</i>
template &lt;typename Graph, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>, 
	  typename PredecessorMap, typename DistanceMap,
	  typename WeightMap, typename VertexIndexMap, typename <a href="http://www.sgi.com/tech/stl/BinaryPredicate.html">DistanceCompare</a>, typename <a href="http://www.sgi.com/tech/stl/BinaryFunction.html">DistanceWeightCombine</a>, 
	  typename DistanceInfinity, typename DistanceZero&gt;
void dijkstra_shortest_paths_no_color_map_no_init
  (const Graph&amp; graph,
   typename graph_traits&lt;Graph&gt;::vertex_descriptor start_vertex, 
   PredecessorMap predecessor_map, DistanceMap distance_map, WeightMap weight_map, 
   VertexIndexMap index_map,
   DistanceCompare distance_compare, DistanceWeightCombine distance_weight_combine,
   DistanceInfinity distance_infinity, DistanceZero distance_zero);
</PRE>

<P>
This algorithm&nbsp;[<A HREF="bibliography.html#dijkstra59">10</A>,<A
HREF="bibliography.html#clr90">8</A>] solves the single-source
shortest-paths problem on a weighted, directed or undirected graph for
the case where all edge weights are nonnegative.  Use the Bellman-Ford
algorithm for the case when some edge weights are negative.  Use
breadth-first search instead of Dijkstra's algorithm when all edge
weights are equal to one.  For the definition of the shortest-path
problem see Section <A
HREF="graph_theory_review.html#sec:shortest-paths-algorithms">Shortest-Paths
Algorithms</A> for some background to the shortest-path problem.
</P>

<P>
	<tt>dijkstra_shortest_paths_no_color_map</tt> differs from the original <tt>dijkstra_shortest_paths</tt> algorithm by not using a color map to identify vertices as discovered or undiscovered.  Instead, this is done with the distance map: a vertex <i>u</i> such that <i>distance_compare(distance_map[u], distance_infinity) == false</i> is considered to be undiscovered.
</P>

<P>
There are two main options for obtaining output from the
<tt>dijkstra_shortest_paths_no_color_map()</tt> function. If you provide a
distance property map through the <tt>distance_map()</tt> parameter
then the shortest distance from the start vertex to every other
vertex in the graph will be recorded in the distance map. Also you can
record the shortest paths tree in a predecessor map: for each vertex
<i>u in V</i>, <i>p[u]</i> will be the predecessor of <i>u</i> in
the shortest paths tree (unless <i>p[u] = u</i>, in which case <i>u</i> is
either the source or a vertex unreachable from the source).  In
addition to these two options, the user can provide their own
custom-made visitor that takes actions during any of the
algorithm's event points <a href="#4">[4]</a>.</P>

<P>
Dijkstra's algorithm finds all the shortest paths from the source
vertex to every other vertex by iteratively &quot;growing&quot; the set of
vertices <i>S</i> to which it knows the shortest path. At each step of
the algorithm, the next vertex added to <i>S</i> is determined by a
priority queue.  The queue contains the vertices in <i>V - S</i><a
href="#1">[1]</a> prioritized by their distance label, which is the
length of the shortest path seen so far for each vertex. The vertex
<i>u</i> at the top of the priority queue is then added to <i>S</i>,
and each of its out-edges is relaxed: if the distance to <i>u</i> plus
the weight of the out-edge <i>(u,v)</i> is less than the distance
label for <i>v</i> then the estimated distance for vertex <i>v</i> is
reduced.  The algorithm then loops back, processing the next vertex at
the top of the priority queue. The algorithm finishes when the
priority queue is empty.
</P>
<p>
The following is the pseudo-code for Dijkstra's single-source shortest
paths algorithm. <i>w</i> is the edge weight, <i>d</i> is the distance label,
and <i>p</i> is the predecessor of each vertex which is used to encode
the shortest paths tree. <i>Q</i> is a priority queue that supports the
DECREASE-KEY operation.  The visitor event points for the algorithm are
indicated by the labels on the right.
</p>

<table>
<tr>
<td valign="top">
<pre>
DIJKSTRA(<i>G</i>, <i>s</i>, <i>w</i>)
  <b>for</b> each vertex <i>u in V</i> <b>(This loop is not run in dijkstra_shortest_paths_no_color_map_no_init)</b>
    <i>d[u] := infinity</i> 
    <i>p[u] := u</i> 
  <b>end for</b>
  <i>d[s] := 0</i> 
  INSERT(<i>Q</i>, <i>s</i>)
  <b>while</b> (<i>Q != &Oslash;</i>)
    <i>u :=</i> EXTRACT-MIN(<i>Q</i>)
    <b>for</b> each vertex <i>v in Adj[u]</i>
      <b>if</b> (<i>w(u,v) + d[u] < d[v]</i>)
        <i>d[v] := w(u,v) + d[u]</i>
        <i>p[v] := u</i> 
        DECREASE-KEY(<i>Q</i>, <i>v</i>)
      <b>else</b>
      	...
      <b>if</b> (<i>d[v]</i> was originally infinity) 
        INSERT(<i>Q</i>, <i>v</i>)   
    <b>end for</b>
  <b>end while</b>
  return (<i>d</i>, <i>p</i>)
</pre>
</td>
<td valign="top">
<pre>

initialize vertex <i>u</i>




discover vertex <i>s</i>

examine vertex <i>u</i>
examine edge <i>(u,v)</i>

edge <i>(u,v)</i> relaxed



edge <i>(u,v)</i> not relaxed

discover vertex <i>v</i>
finish vertex <i>u</i>
</pre>
</td>
</tr>
</table>

<h3>Where Defined</h3>

<a href="../../../boost/graph/dijkstra_shortest_paths_no_color_map.hpp"><tt>boost/graph/dijkstra_shortest_paths_no_color_map.hpp</tt></a>

<h3>Parameters</h3>

IN: <tt>const Graph&amp; graph</tt> 
<blockquote>
  The graph object on which the algorithm will be applied.
  The type <tt>Graph</tt> must be a model of 
  <a href="./VertexListGraph.html">Vertex List Graph</a>
  and <a href="./IncidenceGraph.html">Incidence Graph</a>.<br>
</blockquote>

IN: <tt>vertex_descriptor start_vertex</tt> 
<blockquote>
  The source vertex. All distance will be calculated from this vertex,
  and the shortest paths tree will be rooted at this vertex.<br>
</blockquote>

<h3>Named Parameters</h3>

IN: <tt>weight_map(WeightMap weight_map)</tt>   
<blockquote>
  The weight or ``length'' of each edge in the graph. The weights
  must all be non-negative and non-infinite <a href="#3">[3]</a>. The algorithm will throw a
  <a href="./exception.html#negative_edge"><tt>negative_edge</tt></a> 
  exception is one of the edges is negative.
  The type <tt>WeightMap</tt> must be a model of
  <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  the graph needs to be usable as the key type for the weight
  map. The value type for this map must be
  the same as the value type of the distance map.<br>
  <b>Default:</b>  <tt>get(edge_weight, graph)</tt><br>
</blockquote>

IN: <tt>index_map(VertexIndexMap index_map)</tt> 
<blockquote>
  This maps each vertex to an integer in the range <tt>[0,
    num_vertices(graph))</tt>. This is necessary for efficient updates of the
  heap data structure&nbsp;[<A
  HREF="bibliography.html#driscoll88">61</A>] when an edge is relaxed.
  The type 
  <tt>VertexIndexMap</tt> must be a model of
  <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  integer type. The vertex descriptor type of the graph needs to be
  usable as the key type of the map.<br>
  <b>Default:</b> <tt>get(vertex_index, graph)</tt>.
    Note: if you use this default, make sure your graph has
    an internal <tt>vertex_index</tt> property. For example,
    <tt>adjacenty_list</tt> with <tt>VertexList=listS</tt> does
    not have an internal <tt>vertex_index</tt> property.
   <br>
</blockquote>

OUT: <tt>predecessor_map(PredecessorMap predecessor_map)</tt> 
<blockquote>
  The predecessor map records the edges in the minimum spanning
  tree. Upon completion of the algorithm, the edges <i>(p[u],u)</i>
  for all <i>u in V</i> are in the minimum spanning tree. If <i>p[u] =
  u</i> then <i>u</i> is either the source vertex or a vertex that is
  not reachable from the source.  The <tt>PredecessorMap</tt> type
  must be a <a
  href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  Property Map</a> whose key and value types are the same as the vertex
  descriptor type of the graph.<br>
  <b>Default:</b> <tt>dummy_property_map</tt><br>

  <b>Python</b>: Must be a <tt>vertex_vertex_map</tt> for the graph.<br>
</blockquote>

UTIL/OUT: <tt>distance_map(DistanceMap distance_map)</tt> 
<blockquote>
  The shortest path weight from the source vertex <tt>start_vertex</tt> to each
  vertex in the graph <tt>graph</tt> is recorded in this property map. The
  shortest path weight is the sum of the edge weights along the
  shortest path.  The type <tt>DistanceMap</tt> must be a model of <a
  href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  Property Map</a>. The vertex descriptor type of the graph needs to
  be usable as the key type of the distance map. 

  The value type of the distance map is the element type of a <a
  href="./Monoid.html">Monoid</a> formed with the <tt>distance_weight_combine</tt>
  function object and the <tt>distance_zero</tt> object for the identity
  element. Also the distance value type must have a <a
  href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">
  StrictWeakOrdering</a> provided by the <tt>distance_compare</tt> function
  object.<br>
  <b>Default:</b> <a
  href="../../property_map/doc/iterator_property_map.html">
  <tt>iterator_property_map</tt></a> created from a
  <tt>std::vector</tt> of the <tt>WeightMap</tt>'s value type of size
  <tt>num_vertices(graph)</tt> and using the <tt>index_map</tt> for the index
  map.<br>
</blockquote>

IN: <tt>distance_compare(CompareFunction distance_compare)</tt> 
<blockquote>
  This function is use to compare distances to determine which vertex
  is closer to the source vertex.  The <tt>DistanceCompareFunction</tt> type
  must be a model of <a
  href="http://www.sgi.com/tech/stl/BinaryPredicate.html">Binary
  Predicate</a> and have argument types that match the value type of
  the <tt>DistanceMap</tt> property map.<br>

  <b>Default:</b>
  <tt>std::less&lt;D&gt;</tt> with <tt>D=typename
  property_traits&lt;DistanceMap&gt;::value_type</tt><br>
</blockquote>

IN: <tt>distance_combine(CombineFunction distance_weight_combine)</tt> 
<blockquote>
  This function is used to combine distances to compute the distance
  of a path. The <tt>DistanceWeightCombineFunction</tt> type must be a model of <a
  href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary
  Function</a>. The first argument type of the binary function must
  match the value type of the <tt>DistanceMap</tt> property map and
  the second argument type must match the value type of the
  <tt>WeightMap</tt> property map.  The result type must be the same
  type as the distance value type.<br>

  <b>Default:</b> <tt>boost::closed_plus&lt;D&gt;</tt> with
   <tt>D=typename property_traits&lt;DistanceMap&gt;::value_type</tt><br>
</blockquote>

IN: <tt>distance_inf(D distance_infinity)</tt> 
<blockquote>
  The <tt>distance_infinity</tt> object must be the greatest value of any <tt>D</tt> object.
  That is, <tt>distance_compare(d, distance_infinity) == true</tt> for any <tt>d != distance_infinity</tt>.
  The type <tt>D</tt> is the value type of the <tt>DistanceMap</tt>.<br>
  <b>Default:</b> <tt>std::numeric_limits&lt;D&gt;::max()</tt><br>
</blockquote>

IN: <tt>distance_zero(D distance_zero)</tt> 
<blockquote>
  The <tt>distance_zero</tt> value must be the identity element for the
  <a href="./Monoid.html">Monoid</a> formed by the distance values
  and the <tt>distance_weight_combine</tt> function object.
  The type <tt>D</tt> is the value type of the <tt>DistanceMap</tt>.<br>
  <b>Default:</b> <tt>D()</tt>with
   <tt>D=typename property_traits&lt;DistanceMap&gt;::value_type</tt><br>
</blockquote>
  
OUT: <tt>visitor(DijkstraVisitor v)</tt>  
<blockquote>
  Use this to specify actions that you would like to happen
  during certain event points within the algorithm.
  The type <tt>DijkstraVisitor</tt> must be a model of the
  <a href="./DijkstraVisitor.html">Dijkstra Visitor</a> concept. 
 The visitor object is passed by value <a
  href="#2">[2]</a>.<br>
  <b>Default:</b> <tt>dijkstra_visitor&lt;null_visitor&gt;</tt><br>
</blockquote>


<H3>Complexity</H3>

<P>
The time complexity is <i>O(V log V)</i>.


<h3>Visitor Event Points</h3>

<ul>
<li><b><tt>vis.initialize_vertex(u, g)</tt></b>
  is invoked on each vertex in the graph before the start of the
  algorithm.
<li><b><tt>vis.examine_vertex(u, g)</tt></b>
  is invoked on a vertex as it is removed from the priority queue
  and added to set <i>S</i>. At this point we know that <i>(p[u],u)</i>
  is a shortest-paths tree edge so 
  <i>d[u] = delta(s,u) = d[p[u]] + w(p[u],u)</i>. Also, the distances 
  of the examined vertices is monotonically increasing
  <i>d[u<sub>1</sub>] <= d[u<sub>2</sub>] <= d[u<sub>n</sub>]</i>. 
<li><b><tt>vis.examine_edge(e, g)</tt></b>
  is invoked on each out-edge of a vertex immediately after it has
  been added to set <i>S</i>. 
<li><b><tt>vis.edge_relaxed(e, g)</tt></b>
  is invoked on edge <i>(u,v)</i> if <i>d[u] + w(u,v) < d[v]</i>.
  The edge <i>(u,v)</i> that participated in the last
  relaxation for vertex <i>v</i> is an edge in the shortest paths tree. 
<li><b><tt>vis.discover_vertex(v, g)</tt></b>
  is invoked on vertex <i>v</i> when the edge 
  <i>(u,v)</i> is examined and <i>v</i> has not yet been discovered (i.e. its distance was infinity before relaxation was attempted on the edge).  This
  is also when the vertex is inserted into the priority queue.
<li><b><tt>vis.edge_not_relaxed(e, g)</tt></b>
  is invoked if the edge is not relaxed (see above).
<li><b><tt>vis.finish_vertex(u, g)</tt></b>
   is invoked on a vertex after all of its out edges have
  been examined.
</ul>

<H3>Example</H3>

<P>
See <a href="../example/dijkstra-no-color-map-example.cpp">
<TT>example/dijkstra-no-color-map-example.cpp</TT></a> for an example of using Dijkstra's algorithm.

<H3>See also</H3> <a href="dijkstra_shortest_paths.html">dijkstra_shortest_paths</a> for a version of dijkstra's shortest path that uses a color map.

<H3>Notes</H3>

<p>Based on the documentation for <a href="dijkstra_shortest_paths.html">dijkstra_shortest_paths</a>.

<p><a name="1">[1]</a>
The algorithm used here saves a little space by not putting all <i>V -
S</i> vertices in the priority queue at once, but instead only those
vertices in <i>V - S</i> that are discovered and therefore have a
distance less than infinity.

<p><a name="2">[2]</a> 
  Since the visitor parameter is passed by value, if your visitor
  contains state then any changes to the state during the algorithm
  will be made to a copy of the visitor object, not the visitor object
  passed in. Therefore you may want the visitor to hold this state by
  pointer or reference.
  
<p><a name="3">[3]</a> 
  The algorithm will not work correctly if any of the edge weights are equal to infinity since the infinite distance value is used to determine if a vertex has been discovered.
  
<p><a name="4">[4]</a> 
  Calls to the visitor events occur in the same order as <tt>dijkstra_shortest_paths</tt> (i.e. <i>discover_vertex(u)</i> will always be called after <i>examine_vertex(u)</i> for an undiscovered vertex <i>u</i>).  However, the vertices of the graph given to <i>dijkstra_shortest_paths_no_color_map</i> will <b>not</b> necessarily be visited in the same order as <i>dijkstra_shortest_paths</i>.

<br>
<HR>
<TABLE>
<TR valign=top>
<TD nowrap>Copyright &copy; 2009</TD><TD>
Trustees of Indiana University</TD></TR></TABLE>

</BODY>
</HTML>