Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 99 additions & 10 deletions doc/dijkstra_shortest_paths.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ <H1><A NAME="sec:dijkstra"></A><img src="figs/python.gif" alt="(Python)"/>

<P>
<PRE>
<i>// named parameter version</i>
<i>//////////////////////////////////////////</i>
<i>// functions that take one start vertex //</i>
<i>//////////////////////////////////////////</i>

<i>// named parameter version for a single start vertex</i>
template &lt;typename Graph, typename P, typename T, typename R&gt;
void
dijkstra_shortest_paths(Graph&amp; g,
typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
const bgl_named_params&lt;P, T, R&gt;&amp; params);
void dijkstra_shortest_paths
(const Graph&amp; g,
typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
const bgl_named_params&lt;P, T, R&gt;&amp; params);

<i>// non-named parameter version</i>
template &lt;typename Graph, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>,
<i>// non-named parameter version for a single start vertex</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.boost.org/sgi/stl/BinaryPredicate.html">CompareFunction</a>, typename <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">CombineFunction</a>,
typename DistInf, typename DistZero, typename ColorMap = <i>default</i>&gt;
Expand All @@ -46,14 +50,51 @@ <H1><A NAME="sec:dijkstra"></A><img src="figs/python.gif" alt="(Python)"/>
class PredecessorMap, class DistanceMap,
class WeightMap, class IndexMap, class Compare, class Combine,
class DistZero, class ColorMap&gt;
void
dijkstra_shortest_paths_no_init
void dijkstra_shortest_paths_no_init
(const Graph&amp; g,
typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
IndexMap index_map,
Compare compare, Combine combine, DistZero zero,
DijkstraVisitor vis, ColorMap color = <i>default</i>);


<i>/////////////////////////////////////////////////</i>
<i>// functions that take multiple start vertices //</i>
<i>/////////////////////////////////////////////////</i>

<i>// named parameter version for multiple start vertices</i>
template &lt;typename Graph, typename SourceInputIter, typename P, typename T, typename R&gt;
void dijkstra_shortest_paths
(const Graph&amp; g,
SourceInputIter s_begin, SourceInputIter s_end,
const bgl_named_params&lt;P, T, R&gt;&amp; params);

<i>// non-named parameter version for multiple start vertices</i>
template &lt;typename Graph, typename SourceInputIter, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>,
typename PredecessorMap, typename DistanceMap,
typename WeightMap, typename VertexIndexMap, typename <a href="http://www.boost.org/sgi/stl/BinaryPredicate.html">CompareFunction</a>, typename <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">CombineFunction</a>,
typename DistInf, typename DistZero, typename ColorMap = <i>default</i>&gt;
void dijkstra_shortest_paths
(const Graph&amp; g,
SourceInputIter s_begin, SourceInputIter s_end,
PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
VertexIndexMap index_map,
CompareFunction compare, CombineFunction combine, DistInf inf, DistZero zero,
DijkstraVisitor vis, ColorMap color = <i>default</i>)

<i>// version that does not initialize the property maps (except for the default color map)</i>
template &lt;class Graph, class SourceInputIter, class DijkstraVisitor,
class PredecessorMap, class DistanceMap,
class WeightMap, class IndexMap, class Compare, class Combine,
class DistZero, class ColorMap&gt;
void dijkstra_shortest_paths_no_init
(const Graph&amp; g,
SourceInputIter s_begin, SourceInputIter s_end,
PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
IndexMap index_map,
Compare compare, Combine combine, DistZero zero,
DijkstraVisitor vis, ColorMap color = <i>default</i>);
</PRE>

<P>
Expand All @@ -67,6 +108,8 @@ <H1><A NAME="sec:dijkstra"></A><img src="figs/python.gif" alt="(Python)"/>
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.
For some cases the Dijkstra algorithm can also be applied to a multiple-source
shortest-paths problem, see the section below for more details.
</P>

<P>
Expand Down Expand Up @@ -186,6 +229,38 @@ <h3>Where Defined</h3>

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

<h3>Multiple Sources</h3>
With some restrictions the Dijkstra algorithm can be used for the
multiple-source shortest path problem.

The following cases can be identified:

<h4>Multiple Sources and Single Target</h4>
<p>
Use <a href="./reverse_graph.html"><tt>reverse_graph</tt></a> to
transfer the problem into a single-source and multiple-targets
shortest path problem. Then you can use the Dijkstra algorithms that
take a single source vertex as input.
</p>

<p>
Alternatively, when you are only interested in the results (distance
and route) for the source vertex closest to the target, use the
Dijkstra algorithm that takes multiple sources as input.
</p>

<h4>Multiple Sources and Multiple Targets</h4>
<p>
You can call the Dijkstra algorithm multiple times with just one
source vertex.
</p>

<p>
Alternatively, when you are only interested in the results (distance
and route) for the source vertex closest to a target, use the Dijkstra
algorithm that takes multiple sources as input.
</p>

<h3>Parameters</h3>

IN: <tt>const Graph&amp; g</tt>
Expand All @@ -206,6 +281,18 @@ <h3>Parameters</h3>
<b>Python</b>: The parameter is named <tt>root_vertex</tt>.
</blockquote>

IN: <tt>SourceInputIter s_begin, SourceInputIter s_end</tt>
<blockquote>
Range [s_begin, s_end) of source vertices denoted by two input
iterators that are used for a multiple-source shortest path problem.
The distance to a target vertex will be calculated from the source
vertex in the range [s_begin, s_end) that is closest to the target,
the shortest path will then have this source vertex as root.
As a result, using these parameters you can not create an order of
the distances from all source vertices to a target vertex, you can
only retrieve the information for the closest target vertex.
</blockquote>

<h3>Named Parameters</h3>

IN: <tt>weight_map(WeightMap w_map)</tt>
Expand Down Expand Up @@ -433,7 +520,9 @@ <H3>Example</H3>
<P>
See <a href="../example/dijkstra-example.cpp">
<TT>example/dijkstra-example.cpp</TT></a> for an example of using Dijkstra's
algorithm.
algorithm and <a href="../example/dijkstra-example-multiple-sources.cpp">
<TT>dijkstra-example-multiple-sources.cpp</TT></a> for using multiple source
vertices.

<H3>See also</H3> <a href="dijkstra_shortest_paths_no_color_map.html">dijkstra_shortest_paths_no_color_map</a> for a version of Dijkstra's shortest path that does not use a color map.

Expand Down
97 changes: 94 additions & 3 deletions doc/dijkstra_shortest_paths_no_color_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ <H1><A NAME="sec:dijkstra"></A>

<P>
<PRE>
<i>// named parameter version</i>
<i>//////////////////////////////////////////</i>
<i>// functions that take one start vertex //</i>
<i>//////////////////////////////////////////</i>

<i>// named parameter version for a single start vertex</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>
<i>// non-named parameter version for a single start vertex</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.boost.org/sgi/stl/BinaryPredicate.html">DistanceCompare</a>, typename <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">DistanceWeightCombine</a>,
Expand All @@ -53,6 +57,44 @@ <H1><A NAME="sec:dijkstra"></A>
VertexIndexMap index_map,
DistanceCompare distance_compare, DistanceWeightCombine distance_weight_combine,
DistanceInfinity distance_infinity, DistanceZero distance_zero);


<i>/////////////////////////////////////////////////</i>
<i>// functions that take multiple start vertices //</i>
<i>/////////////////////////////////////////////////</i>

<i>// named parameter version for multiple start vertices</i>
template &lt;typename Graph, typename SourceInputIter, typename Param, typename Tag, typename Rest&gt;
void dijkstra_shortest_paths_no_color_map
(const Graph&amp; graph,
SourceInputIter s_begin, SourceInputIter s_end,
const bgl_named_params<Param,Tag,Rest>& params);

<i>// non-named parameter version for multiple start vertices</i>
template &lt;typename Graph, typename SourceInputIter, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>,
typename PredecessorMap, typename DistanceMap,
typename WeightMap, typename VertexIndexMap, typename <a href="http://www.boost.org/sgi/stl/BinaryPredicate.html">DistanceCompare</a>, typename <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">DistanceWeightCombine</a>,
typename DistanceInfinity, typename DistanceZero&gt;
void dijkstra_shortest_paths_no_color_map
(const Graph&amp; graph,
SourceInputIter s_begin, SourceInputIter s_end,
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 SourceInputIter, typename <a href="DijkstraVisitor.html">DijkstraVisitor</a>,
typename PredecessorMap, typename DistanceMap,
typename WeightMap, typename VertexIndexMap, typename <a href="http://www.boost.org/sgi/stl/BinaryPredicate.html">DistanceCompare</a>, typename <a href="http://www.boost.org/sgi/stl/BinaryFunction.html">DistanceWeightCombine</a>,
typename DistanceInfinity, typename DistanceZero&gt;
void dijkstra_shortest_paths_no_color_map_no_init
(const Graph&amp; graph,
SourceInputIter s_begin, SourceInputIter s_end,
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>
Expand All @@ -66,6 +108,8 @@ <H1><A NAME="sec:dijkstra"></A>
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.
For some cases the Dijkstra algorithm can also be applied to a multiple-source
shortest-paths problem, see the section below for more details.
</P>

<P>
Expand Down Expand Up @@ -162,6 +206,38 @@ <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>Multiple Sources</h3>
With some restrictions the Dijkstra algorithm can be used for the
multiple-source shortest path problem.

The following cases can be identified:

<h4>Multiple Sources and Single Target</h4>
<p>
Use <a href="./reverse_graph.html"><tt>reverse_graph</tt></a> to
transfer the problem into a single-source and multiple-targets
shortest path problem. Then you can use the Dijkstra algorithms that
take a single source vertex as input.
</p>

<p>
Alternatively, when you are only interested in the results (distance
and route) for the source vertex closest to the target, use the
Dijkstra algorithm that takes multiple sources as input.
</p>

<h4>Multiple Sources and Multiple Targets</h4>
<p>
You can call the Dijkstra algorithm multiple times with just one
source vertex.
</p>

<p>
Alternatively, when you are only interested in the results (distance
and route) for the source vertex closest to a target, use the Dijkstra
algorithm that takes multiple sources as input.
</p>

<h3>Parameters</h3>

IN: <tt>const Graph&amp; graph</tt>
Expand All @@ -178,6 +254,18 @@ <h3>Parameters</h3>
and the shortest paths tree will be rooted at this vertex.<br>
</blockquote>

IN: <tt>SourceInputIter s_begin, SourceInputIter s_end</tt>
<blockquote>
Range [s_begin, s_end) of source vertices denoted by two input
iterators that are used for a multiple-source shortest path problem.
The distance to a target vertex will be calculated from the source
vertex in the range [s_begin, s_end) that is closest to the target,
the shortest path will then have this source vertex as root.
As a result, using these parameters you can not create an order of
the distances from all source vertices to a target vertex, you can
only retrieve the information for the closest target vertex.
</blockquote>

<h3>Named Parameters</h3>

IN: <tt>weight_map(WeightMap weight_map)</tt>
Expand Down Expand Up @@ -356,7 +444,10 @@ <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.
<TT>example/dijkstra-no-color-map-example.cpp</TT></a> for an example of using Dijkstra's algorithm
and <a href="../example/dijkstra-no-color-map-example-multiple-sources.cpp">
<TT>dijkstra-no-color-map-example-multiple-sources.cpp</TT></a> for using multiple source
vertices.

<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.

Expand Down
2 changes: 2 additions & 0 deletions example/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ run dfs.cpp ;
run dfs_parenthesis.cpp ;
run dfs-example.cpp ;
run dijkstra-example.cpp ;
run dijkstra-example-multiple-sources.cpp ;
run dijkstra-example-listS.cpp ;
run dijkstra-no-color-map-example.cpp ;
run dijkstra-no-color-map-example-multiple-sources.cpp ;
run directed_graph.cpp ;
exe eccentricity : eccentricity.cpp ;
run edge_basics.cpp ;
Expand Down
79 changes: 79 additions & 0 deletions example/dijkstra-example-multiple-sources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//=======================================================================
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
// Copyright 2019 Matthias Fuchs
//
// 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)
//=======================================================================
#include <boost/config.hpp>
#include <iostream>
#include <fstream>

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>

using namespace boost;

int
main(int, char *[])
{
typedef adjacency_list < listS, vecS, directedS,
no_property, property < edge_weight_t, int > > graph_t;
typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
typedef std::pair<int, int> Edge;

const int num_nodes = 7;
enum nodes { A, B, C, D, E, F, G };
char name[] = "ABCDEFG";
Edge edge_array[] = { Edge(A, C), Edge(A, B), Edge(B, A),
Edge(B, C), Edge(A, D), Edge(D, A), Edge(D, E), Edge(D, F), Edge(B, F),
Edge(F, B), Edge(F, G), Edge(G, F), Edge(G, E), Edge(E, G)
};
int weights[] = { 4, 1, 2, 3, 1, 1, 3, 2, 1, 2, 5, 1, 1, 1 };
int num_arcs = sizeof(edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
std::vector<vertex_descriptor> p(num_vertices(g));
std::vector<int> d(num_vertices(g));
vertex_descriptor start_nodes[] = { vertex(A, g), vertex(B, g) };

dijkstra_shortest_paths(g, start_nodes, start_nodes + 2,
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));

std::cout << "distances and parents:" << std::endl;
graph_traits < graph_t >::vertex_iterator vi, vend;
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
endl;
}
std::cout << std::endl;

std::ofstream dot_file("figs/dijkstra-eg-multiple-sources.dot");

dot_file << "digraph D {\n"
<< " rankdir=LR\n"
<< " size=\"4,3\"\n"
<< " ratio=\"fill\"\n"
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";

graph_traits < graph_t >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
graph_traits < graph_t >::edge_descriptor e = *ei;
graph_traits < graph_t >::vertex_descriptor
u = source(e, g), v = target(e, g);
dot_file << name[u] << " -> " << name[v]
<< "[label=\"" << get(weightmap, e) << "\"";
if (p[v] == u)
dot_file << ", color=\"black\"";
else
dot_file << ", color=\"grey\"";
dot_file << "]";
}
dot_file << "}";
return EXIT_SUCCESS;
}
Loading