mirror of
https://github.com/boostorg/graph.git
synced 2026-01-19 04:12:11 +00:00
Join ralf_grosse_kunstleve with HEAD
[SVN r9444]
This commit is contained in:
@@ -38,7 +38,20 @@ best meets your needs. An <a
|
||||
href="graph_theory_review.html#sec:adjacency-list-representation">adjacency-list</a>
|
||||
is basically a two-dimensional structure, where each element of the
|
||||
first dimension represents a vertex, and each of the vertices contains
|
||||
a one-dimensional structure that is its edge list. The
|
||||
a one-dimensional structure that is its edge list. <a
|
||||
href="fig:adj-list-graph">Figure 1</a> shows an adjacency list
|
||||
representation of a directed graph.
|
||||
|
||||
<P></P>
|
||||
<DIV ALIGN="center"><A NAME="fig:adj-list-graph"></A><A NAME="1509"></A>
|
||||
<TABLE>
|
||||
<CAPTION ALIGN="BOTTOM"><STRONG>Figure 1:</STRONG> Adjacency List Representation of a Directed Graph.</CAPTION>
|
||||
<TR><TD><IMG SRC="./figs/adj-matrix-graph.gif" width="386" height="284"></TD>
|
||||
<TD><IMG SRC="./figs/adj-list.gif" width="62" height="122"></TD></TR>
|
||||
</TABLE>
|
||||
</DIV><P></P>
|
||||
|
||||
The
|
||||
<TT>VertexList</TT> template parameter of the <TT>adjacency_list</TT>
|
||||
class controls what kind of container is used to represent the outer
|
||||
two-dimensional container. The <TT>EdgeList</TT> template parameter
|
||||
@@ -55,7 +68,18 @@ The <TT>Directed</TT> template parameter controls whether the graph is
|
||||
directed, undirected, or directed with access to both the in-edges and
|
||||
out-edges (which we call bidirectional). The bidirectional graph takes
|
||||
up twice the space (per edge) of a directed graph since each edge will
|
||||
appear in both an out-edge and in-edge list.
|
||||
appear in both an out-edge and in-edge list. <a
|
||||
href="fig:undir-adj-list-graph">Figure 2</a> shows an adjacency list
|
||||
representation of an undirected graph.
|
||||
|
||||
<P></P>
|
||||
<DIV ALIGN="center"><A NAME="fig:undir-adj-list-graph"></A><A NAME="1509"></A>
|
||||
<TABLE>
|
||||
<CAPTION ALIGN="BOTTOM"><STRONG>Figure 2:</STRONG> Adjacency List Representation of an Undirected Graph.</CAPTION>
|
||||
<TR><TD><IMG SRC="./figs/undir-adj-matrix-graph.gif" width="2573" height="60929"></TD>
|
||||
<TD><IMG SRC="./figs/undir-adj-list.gif" width="62" height="122"></TD></TR>
|
||||
</TABLE>
|
||||
</DIV><P></P>
|
||||
|
||||
<P>
|
||||
A tutorial on how to use the <TT>adjacency_list</TT> class is in
|
||||
@@ -395,9 +419,9 @@ The type for the iterators returned by <TT>vertices()</TT>.
|
||||
|
||||
When <tt>VertexList=vecS</tt> then the <tt>vertex_iterator</tt> models
|
||||
<a
|
||||
href="http://www.sgi.com/Technology/STL/RandomAccessIterator.html">RandomAccessIterator</a>. Otherwise
|
||||
href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>. Otherwise
|
||||
the <tt>vertex_iterator</tt> models <a
|
||||
href="http://www.sgi.com/Technology/STL/BidirectionalIterator.html">BidirectionalIterator</a>.
|
||||
href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">BidirectionalIterator</a>.
|
||||
|
||||
<hr>
|
||||
|
||||
@@ -405,7 +429,7 @@ href="http://www.sgi.com/Technology/STL/BidirectionalIterator.html">Bidirectiona
|
||||
<br><br>
|
||||
The type for the iterators returned by <TT>edges()</TT>.
|
||||
The <tt>edge_iterator</tt> models <a
|
||||
href="http://www.sgi.com/Technology/STL/BidirectionalIterator.html">BidirectionalIterator</a>.
|
||||
href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">BidirectionalIterator</a>.
|
||||
|
||||
|
||||
<hr>
|
||||
@@ -416,13 +440,13 @@ href="http://www.sgi.com/Technology/STL/BidirectionalIterator.html">Bidirectiona
|
||||
|
||||
The type for the iterators returned by <TT>out_edges()</TT>.
|
||||
When <tt>EdgeList=vecS</tt> then the <tt>out_edge_iterator</tt> models
|
||||
<a href="http://www.sgi.com/Technology/STL/RandomAccessIterator.html">
|
||||
<a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">
|
||||
RandomAccessIterator</a>. When <tt>EdgeList=slistS</tt> then the
|
||||
<tt>out_edge_iterator</tt> models <a
|
||||
href="http://www.sgi.com/Technology/STL/ForwardIterator.html">
|
||||
href="http://www.sgi.com/tech/stl/ForwardIterator.html">
|
||||
ForwardIterator</a>. Otherwise the <tt>out_edge_iterator</tt> models
|
||||
<a
|
||||
href="http://www.sgi.com/Technology/STL/BidirectionalIterator.html">
|
||||
href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">
|
||||
BidirectionalIterator</a>.
|
||||
|
||||
<hr>
|
||||
@@ -540,7 +564,7 @@ adjacency_list(vertices_size_type n,
|
||||
Creates a graph object with <TT>n</TT> vertices and with the edges
|
||||
specified in the edge list given by the range <TT>[first, last)</TT>.
|
||||
The <tt>EdgeIterator</tt> must be a model of <a
|
||||
href="http://www.sgi.com/Technology/STL/InputIterator.html">InputIterator</a>.
|
||||
href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
|
||||
The value type of the <TT>EdgeIterator</TT> must be a
|
||||
<TT>std::pair</TT>, where the type in the pair is an integer type. The
|
||||
integers will correspond to vertices, and they must all fall in the
|
||||
@@ -560,7 +584,7 @@ Creates a graph object with <TT>n</TT> vertices and with the edges
|
||||
specified in the edge list given by the range <TT>[first, last)</TT>.
|
||||
The <tt>EdgeIterator</tt> and <tt>EdgePropertyIterator</tt> must be a
|
||||
model of <a
|
||||
href="http://www.sgi.com/Technology/STL/InputIterator.html">InputIterator</a>.
|
||||
href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
|
||||
The value type of the <TT>EdgeIterator</TT> must be a
|
||||
<TT>std::pair</TT>, where the type in the pair is an integer type. The
|
||||
integers will correspond to vertices, and they must all fall in the
|
||||
@@ -612,22 +636,24 @@ Returns an iterator-range providing access to the edge set of graph
|
||||
|
||||
<pre>
|
||||
std::pair<adjacency_iterator, adjacency_iterator>
|
||||
adjacent_vertices(vertex_descriptor v, const adjacency_list& g)
|
||||
adjacent_vertices(vertex_descriptor u, const adjacency_list& g)
|
||||
</pre>
|
||||
Returns an iterator-range providing access to the vertices adjacent to
|
||||
vertex <tt>v</tt> in graph <tt>g</tt>.
|
||||
vertex <tt>u</tt> in graph <tt>g</tt>.
|
||||
|
||||
<hr>
|
||||
|
||||
|
||||
<pre>
|
||||
std::pair<out_edge_iterator, out_edge_iterator>
|
||||
out_edges(vertex_descriptor v, const adjacency_list& g)
|
||||
out_edges(vertex_descriptor u, const adjacency_list& g)
|
||||
</pre>
|
||||
Returns an iterator-range providing access to the out-edges of vertex
|
||||
<tt>v</tt> in graph <tt>g</tt>. If the graph is undirected, this
|
||||
iterator-range provides access to all edge incident on vertex
|
||||
<tt>v</tt>.
|
||||
<tt>u</tt> in graph <tt>g</tt>. If the graph is undirected, this
|
||||
iterator-range provides access to all edges incident on vertex
|
||||
<tt>u</tt>. For both directed and undirected graphs, for an out-edge
|
||||
<tt>e</tt>, <tt>source(e, g) == u</tt> and <tt>target(e, g) == v</tt>
|
||||
where <tt>v</tt> is a vertex adjacent to <tt>u</tt>.
|
||||
|
||||
<hr>
|
||||
|
||||
@@ -638,7 +664,9 @@ in_edges(vertex_descriptor v, const adjacency_list& g)
|
||||
Returns an iterator-range providing access to the in-edges of vertex
|
||||
<tt>v</tt> in graph <tt>g</tt>. This operation is only available if
|
||||
<TT>bidirectionalS</TT> was specified for the <TT>Directed</TT>
|
||||
template parameter.
|
||||
template parameter. For an in-edge <tt>e</tt>, <tt>target(e, g) == v</tt>
|
||||
and <tt>source(e, g) == u</tt> for some vertex <tt>u</tt> that is
|
||||
adjacent to <tt>v</tt>, whether the graph is directed or undirected.
|
||||
|
||||
<hr>
|
||||
|
||||
@@ -827,7 +855,7 @@ has time complexity <i>O(E/V)</i>.
|
||||
<hr>
|
||||
|
||||
<pre>
|
||||
template <class <a href="http://www.sgi.com/Technology/STL/Predicate.html">Predicate</a>>
|
||||
template <class <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
|
||||
void remove_out_edge_if(vertex_descriptor u, Predicate predicate,
|
||||
adjacency_list& g)
|
||||
</pre>
|
||||
@@ -842,7 +870,7 @@ invoking <tt>remove_edge()</tt> on each of the removed edges.
|
||||
|
||||
<pre>
|
||||
template <class <a
|
||||
href="http://www.sgi.com/Technology/STL/Predicate.html">Predicate</a>>
|
||||
href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
|
||||
void remove_in_edge_if(vertex_descriptor v, Predicate predicate,
|
||||
adjacency_list& g)
|
||||
</pre>
|
||||
@@ -860,7 +888,7 @@ This operation is available for undirected and bidirectional
|
||||
<hr>
|
||||
|
||||
<pre>
|
||||
template <class <a href="http://www.sgi.com/Technology/STL/Predicate.html">Predicate</a>>
|
||||
template <class <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
|
||||
void remove_edge_if(Predicate predicate, adjacency_list& g)
|
||||
</pre>
|
||||
Removes all edges from the graph that satisfy
|
||||
|
||||
@@ -49,19 +49,13 @@ typedef Traits::vertex_iterator vertex_iterator;
|
||||
std::vector<std::size_t> distance_list;
|
||||
|
||||
typedef boost::v_property<long> dist_t;
|
||||
boost::property_map<Graph*, dist_t>::type distance;
|
||||
boost::property_map<Graph*, dist_t>::type distance_map;
|
||||
|
||||
typedef boost::u_property<vertex_descriptor> pred_t;
|
||||
boost::property_map<Graph*, pred_t>::type predecessor;
|
||||
boost::property_map<Graph*, pred_t>::type predecessor_map;
|
||||
|
||||
typedef boost::w_property<long> color_t;
|
||||
boost::property_map<Graph*, color_t>::type color;
|
||||
|
||||
namespace boost {
|
||||
long white(long) { return 0; } // "unseen"
|
||||
long gray(long) { return 2; } // "seen"
|
||||
long black(long) { return 1; } // "known"
|
||||
}
|
||||
boost::property_map<Graph*, color_t>::type color_map;
|
||||
|
||||
class diameter_and_girth_visitor : public boost::bfs_visitor<>
|
||||
{
|
||||
@@ -71,16 +65,16 @@ public:
|
||||
|
||||
void tree_edge(edge_descriptor e, Graph* g) {
|
||||
vertex_descriptor u = source(e, g), v = target(e, g);
|
||||
k = distance[u] + 1;
|
||||
distance[v] = k;
|
||||
k = distance_map[u] + 1;
|
||||
distance_map[v] = k;
|
||||
++distance_list[k];
|
||||
predecessor[v] = u;
|
||||
predecessor_map[v] = u;
|
||||
}
|
||||
void cycle_edge(edge_descriptor e, Graph* g) {
|
||||
void non_tree_edge(edge_descriptor e, Graph* g) {
|
||||
vertex_descriptor u = source(e, g), v = target(e, g);
|
||||
k = distance[u] + 1;
|
||||
if (distance[v] + k < girth && v != predecessor[u])
|
||||
girth = distance[v]+ k;
|
||||
k = distance_map[u] + 1;
|
||||
if (distance_map[v] + k < girth && v != predecessor_map[u])
|
||||
girth = distance_map[v]+ k;
|
||||
}
|
||||
private:
|
||||
std::size_t& k;
|
||||
@@ -127,13 +121,13 @@ main()
|
||||
distance_list.resize(boost::num_vertices(g), 0);
|
||||
|
||||
// obtain property maps
|
||||
distance = get(dist_t(), g);
|
||||
predecessor = get(pred_t(), g);
|
||||
color = get(color_t(), g);
|
||||
distance_map = get(dist_t(), g);
|
||||
predecessor_map = get(pred_t(), g);
|
||||
color_map = get(color_t(), g);
|
||||
|
||||
vertex_iterator i, end;
|
||||
for (boost::tie(i, end) = boost::vertices(g); i != end; ++i)
|
||||
distance[*i] = 0;
|
||||
distance_map[*i] = 0;
|
||||
|
||||
std::size_t k = 0;
|
||||
std::size_t girth = std::numeric_limits<std::size_t>::max();
|
||||
@@ -142,7 +136,7 @@ main()
|
||||
vertex_descriptor s = *boost::vertices(g).first;
|
||||
|
||||
// Call Variant 2 of BFS
|
||||
boost::breadth_first_search(g, s, visitor, color);
|
||||
boost::breadth_first_search(g, s, visitor, color_map);
|
||||
|
||||
std::cout << "Starting at any given vertex, there are" << std::endl;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user