3 Copyright (c) Jeremy Siek 2000
5 Distributed under the Boost Software License, Version 1.0.
6 (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
10 <Title>Boost Graph Library: Adjacency List</Title>
11 <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
13 <IMG SRC="../../../boost.png"
14 ALT="C++ Boost" width="277" height="86">
18 <H1><A NAME="sec:adjacency-list-class"></A>
20 adjacency_list<OutEdgeList, VertexList, Directed,
21 VertexProperties, EdgeProperties,
22 GraphProperties, EdgeList>
28 The <TT>adjacency_list</TT> class implements a generalized adjacency
29 list graph structure. The template parameters provide many
30 configuration options so that you can pick a version of the class that
31 best meets your needs. An <a
32 href="graph_theory_review.html#sec:adjacency-list-representation">adjacency-list</a>
33 is basically a two-dimensional structure, where each element of the
34 first dimension represents a vertex, and each of the vertices contains
35 a one-dimensional structure that is its edge list. <a
36 href="#fig:adj-list-graph">Figure 1</a> shows an adjacency list
37 representation of a directed graph.
40 <DIV ALIGN="center"><A NAME="fig:adj-list-graph"></A>
42 <CAPTION ALIGN="BOTTOM"><STRONG>Figure 1:</STRONG> Adjacency List Representation of a Directed Graph.</CAPTION>
43 <TR><TD><IMG SRC="./figs/adj-matrix-graph2.gif" width="386" height="284"></TD>
44 <TD><IMG SRC="./figs/adj-list2.gif" width="62" height="122"></TD></TR>
49 <TT>VertexList</TT> template parameter of the <TT>adjacency_list</TT>
50 class controls what kind of container is used to represent the outer
51 two-dimensional container. The <TT>OutEdgeList</TT> template parameter
52 controls what kind of container is used to represent the edge
53 lists. The choices for <TT>OutEdgeList</TT> and <TT>VertexList</TT> will
54 determine the space complexity of the graph structure, and will
55 determine the time complexity of the various graph operations. The
56 possible choices and tradeoffs are discussed in Section <A
57 HREF="./using_adjacency_list.html#sec:choosing-graph-type">Choosing
58 the <TT>Edgelist</TT> and <TT>VertexList</TT></A>.
61 The <TT>Directed</TT> template parameter controls whether the graph is
62 directed, undirected, or directed with access to both the in-edges and
63 out-edges (which we call bidirectional). The bidirectional graph takes
64 up twice the space (per edge) of a directed graph since each edge will
65 appear in both an out-edge and in-edge list. <a
66 href="#fig:undir-adj-list-graph">Figure 2</a> shows an adjacency list
67 representation of an undirected graph.
70 <DIV ALIGN="center"><A NAME="fig:undir-adj-list-graph"></A>
72 <CAPTION ALIGN="BOTTOM"><STRONG>Figure 2:</STRONG> Adjacency List Representation of an Undirected Graph.</CAPTION>
73 <TR><TD><IMG SRC="./figs/undir-adj-matrix-graph2.gif" width="260" height="240"></TD>
74 <TD><IMG SRC="./figs/undir-adj-list.gif" width="62" height="122"></TD></TR>
79 A tutorial on how to use the <TT>adjacency_list</TT> class is in
80 Section <A HREF="./using_adjacency_list.html">Using
81 <TT>adjacency_list</TT></A>.
89 href="../example/family-tree-eg.cpp"><tt>examples/family-tree-eg.cpp</tt></a>
90 shows how to represent a family tree with a graph.
92 <H3>Template Parameters</H3>
97 <th>Parameter</th><th>Description</th><th>Default</th>
100 <TR><TD><TT>OutEdgeList</TT></TD>
101 <TD>The selector for the container used to represent the
102 edge-list for each of the vertices.</TD>
103 <TD><TT>vecS</TT></TD>
107 <TD><TT>VertexList</TT></TD>
108 <TD>The selector for the container used to represent the
109 vertex-list of the graph.</TD>
110 <TD><TT>vecS</TT></TD>
114 <TD><TT>Directed</TT></TD>
115 <TD>A selector to choose whether the graph is directed, undirected, or directed with bidirectional edge access (access to both out-edges and in-edges). The options are <TT>directedS</TT>, <TT>undirectedS</TT>, and <TT>bidirectionalS</TT>.</TD>
116 <TD><TT>directedS</TT></TD>
120 <TD><TT>VertexProperties</TT></TD>
121 <TD>for specifying internal property storage.</TD>
122 <TD><TT>no_property</TT></TD>
126 <TD><TT>EdgeProperties</TT></TD>
127 <TD>for specifying internal property storage.</TD>
128 <TD><TT>no_property</TT></TD>
132 <TD><TT>GraphProperties</TT></TD>
133 <TD>for specifying property storage for the graph object.</TD>
134 <TD><TT>no_property</TT></TD>
137 <TR><TD><TT>EdgeList</TT></TD>
138 <TD>The selector for the container used to represent the
139 edge-list for the graph.</TD>
140 <TD><TT>listS</TT></TD>
149 <a href="./VertexAndEdgeListGraph.html">VertexAndEdgeListGraph</a>,
150 <a href="./MutablePropertyGraph.html">MutablePropertyGraph</a>,
151 <a href="../../utility/CopyConstructible.html">CopyConstructible</a>,
152 <a href="../../utility/Assignable.html">Assignable</a>,
153 and <a href="../../serialization/doc/index.html">Serializable</a>.
158 <H3>Where Defined</H3>
161 <a href="../../../boost/graph/adjacency_list.hpp"><TT>boost/graph/adjacency_list.hpp</TT></a><br><br>
162 Also, the serialization functionality is in
163 <a href="../../../boost/graph/adj_list_serialize.hpp"><tt>boost/graph/adj_list_serialize.hpp</tt></a>.
166 <H2>Vertex and Edge Properties</H2>
169 Properties such as color, distance, weight, and user-defined
170 properties can be attached to the vertices and edges of the graph
171 using properties. The property values can be read from and written to
172 via the property maps provided by the graph. The property maps are
173 obtained via the <TT>get(property, g)</TT> function. How to use
174 properties is described in Section <A
175 HREF="./using_adjacency_list.html#sec:adjacency-list-properties">Internal
176 Properties </A>. The property maps are objects that implement the
177 interface defined in Section <A
178 HREF="../../property_map/doc/property_map.html">Property Map
179 Concepts</A> or may be <a href="bundles.html">bundled properties</a>,
180 which have a more succinct syntax. The types of all property values
181 must be Copy Constructible, Assignable, and Default Constructible.
182 The property maps obtained from the
183 <TT>adjacency_list</TT> class are models of the <a
184 href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property
185 Map</a> concept. If the <TT>adjacency_list</TT> is const,
186 then the property map is constant, otherwise the property
190 If the <TT>VertexList</TT> of the graph is <TT>vecS</TT>, then the
191 graph has a builtin vertex indices accessed via the property map for
192 the <TT>vertex_index_t</TT> property. The indices fall in the range
193 <TT>[0, num_vertices(g))</TT> and are contiguous. When a vertex is
194 removed the indices are adjusted so that they retain these
195 properties. Some care must be taken when using these indices to access
196 exterior property storage. The property map for vertex index is a
197 model of <a href="../../property_map/doc/ReadablePropertyMap.html">Readable
202 <h2>Iterator and Descriptor Stability/Invalidation</h2>
204 Some care must be taken when changing the structure of a graph (via
205 adding or removing edges). Depending on the type of
206 <tt>adjacency_list</tt> and on the operation, some of the iterator or
207 descriptor objects that point into the graph may become invalid. For
208 example, the following code will result in undefined (bad) behavior:
211 typedef adjacency_list<listS, vecS> Graph; <b>// VertexList=vecS</b>
213 <b>// Fill in the graph...</b>
215 <b>// Attempt to remove all the vertices. Wrong!</b>
216 graph_traits<Graph>::vertex_iterator vi, vi_end;
217 for (boost::tie(vi, vi_end) = vertices(G); vi != vi_end; ++vi)
218 remove_vertex(*vi, G);
220 <b>// Remove all the vertices. This is still wrong!</b>
221 graph_traits<Graph>::vertex_iterator vi, vi_end, next;
222 boost::tie(vi, vi_end) = vertices(G);
223 for (next = vi; vi != vi_end; vi = next) {
225 remove_vertex(*vi, G);
229 The reason this is a problem is that we are invoking
230 <tt>remove_vertex()</tt>, which when used with an
231 <tt>adjacency_list</tt> where <tt>VertexList=vecS</tt>, invalidates
232 all iterators and descriptors for the graph (such as <tt>vi</tt> and
233 <tt>vi_end</tt>), thereby causing trouble in subsequent iterations of
238 If we use a different kind of <tt>adjacency_list</tt>, where
239 <tt>VertexList=listS</tt>, then the iterators are not invalidated by
240 calling <tt>remove_vertex</tt> unless the iterator is pointing to the
241 actual vertex that was removed. The following code demonstrates this.
244 typedef adjacency_list<listS, listS> Graph; <b>// VertexList=listS</b>
246 <b>// Fill in the graph...</b>
248 <b>// Attempt to remove all the vertices. Wrong!</b>
249 graph_traits<Graph>::vertex_iterator vi, vi_end;
250 for (boost::tie(vi, vi_end) = vertices(G); vi != vi_end; ++vi)
251 remove_vertex(*vi, G);
253 <b>// Remove all the vertices. This is OK.</b>
254 graph_traits<Graph>::vertex_iterator vi, vi_end, next;
255 boost::tie(vi, vi_end) = vertices(G);
256 for (next = vi; vi != vi_end; vi = next) {
258 remove_vertex(*vi, G);
264 The stability issue also affects vertex and edge descriptors. For
265 example, suppose you use vector of vertex descriptors to keep track of
266 the parents (or predecessors) of vertices in a shortest paths tree
268 href="../example/dijkstra-example.cpp"><tt>examples/dijkstra-example.cpp</tt></a>).
269 You create the parent vector with a call to
270 <tt>dijkstra_shortest_paths()</tt>, and then remove a vertex from the
271 graph. Subsequently you try to use the parent vector, but since all
272 vertex descriptors have become invalid, the result is incorrect.
275 std::vector<Vertex> parent(num_vertices(G));
276 std::vector<Vertex> distance(num_vertices(G));
278 dijkstra_shortest_paths(G, s, distance_map(&distance[0]).
279 predecessor_map(&parent[0]));
281 remove_vertex(s, G); <b>// Bad idea! Invalidates vertex descriptors in parent vector.</b>
283 <b>// The following will produce incorrect results</b>
284 for(boost::tie(vi, vend) = vertices(G); vi != vend; ++vi)
285 std::cout << p[*vi] << " is the parent of " << *vi << std::endl;
290 Note that in this discussion iterator and descriptor invalidation is
291 concerned with the invalidation of iterators and descriptors that are
292 <b>not directly affected</b> by the operation. For example, performing
293 <tt>remove_edge(u, v, g)</tt> will always invalidate any edge
294 descriptor for <i>(u,v)</i> or edge iterator pointing to <i>(u,v)</i>,
295 regardless of the kind <tt>adjacency_list</tt>. In this discussion
296 of iterator and descriptor invalidation, we are only concerned with the
297 affect of <tt>remove_edge(u, v, g)</tt> on edge descriptors and
298 iterators that point to other edges (not <i>(u,v)</i>).
301 In general, if you want your vertex and edge descriptors to be stable
302 (never invalidated) then use <tt>listS</tt> or <tt>setS</tt> for the
303 <tt>VertexList</tt> and <tt>OutEdgeList</tt> template parameters of
304 <tt>adjacency_list</tt>. If you are not as concerned about descriptor
305 and iterator stability, and are more concerned about memory
306 consumption and graph traversal speed, use <tt>vecS</tt> for the
307 <tt>VertexList</tt> and/or <tt>OutEdgeList</tt> template parameters.
310 The following table summarizes which operations cause descriptors and
311 iterators to become invalid. In the table, <tt>EL</tt> is an
312 abbreviation for <tt>OutEdgeList</tt> and <tt>VL</tt> means
313 <tt>VertexList</tt>. The <b>Adj Iter</b> category includes the
314 <tt>out_edge_iterator</tt>, <tt>in_edge_iterator</tt>, and
315 <tt>adjacency_iterator</tt> types. A more detailed description of
316 descriptor and iterator invalidation is given in the documentation for
322 <CAPTION ALIGN="BOTTOM"><STRONG>Table:</STRONG>
323 Summary of Descriptor and Iterator Invalidation.
335 <tt>add_edge()</tt></td>
336 <td align=center><tt>OK</tt></td>
337 <td align=center><tt>OK</tt></td>
338 <td align=center><tt>OK</tt></td>
339 <td align=center><tt>EL=vecS && <br>Directed=directedS</tt></td>
340 <td align=center><tt>EL=vecS</tt></td>
343 <td><tt>remove_edge()<br>remove_edge_if()<br>remove_out_edge_if()<br>
344 remove_in_edge_if()<br>clear_vertex()</tt>
346 <td align=center><tt>OK</tt></td>
347 <td align=center><tt>OK</tt></td>
348 <td align=center><tt>OK</tt></td>
349 <td align=center><tt>EL=vecS && <br>Directed=directedS</tt></td>
350 <td align=center><tt>EL=vecS</tt></td>
353 <td><tt>add_vertex()</tt></td>
354 <td align=center><tt>OK</tt></td>
355 <td align=center><tt>OK</tt></td>
356 <td align=center><tt>OK</tt></td>
357 <td align=center><tt>VL=vecS && <br/> Directed=directedS</tt></td>
358 <td align=center><tt>VL=vecS && <br/> Directed=directedS</tt></td>
361 <td><tt>remove_vertex()</tt></td>
362 <td align=center><tt>VL=vecS</tt></td>
363 <td align=center><tt>VL=vecS</tt></td>
364 <td align=center><tt>VL=vecS</tt></td>
365 <td align=center><tt>VL=vecS</tt></td>
366 <td align=center><tt>VL=vecS</tt></td>
370 <H2>Associated Types</H2>
374 <tt>graph_traits<adjacency_list>::vertex_descriptor</tt>
378 <tt>adjacency_list_traits<OutEdgeList, VertexList, Directed, EdgeList>::vertex_descriptor</tt>
380 The type for the vertex descriptors associated with the
381 <TT>adjacency_list</TT>.
385 <tt>graph_traits<adjacency_list>::edge_descriptor</tt><br>
387 <tt>adjacency_list_traits<OutEdgeList, VertexList, Directed, EdgeList>::edge_descriptor</tt>
389 The type for the edge descriptors associated with the
390 <TT>adjacency_list</TT>.
394 <tt>graph_traits<adjacency_list>::vertex_iterator</tt>
396 The type for the iterators returned by <TT>vertices()</TT>.
398 When <tt>VertexList=vecS</tt> then the <tt>vertex_iterator</tt> models
400 href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>. Otherwise
401 the <tt>vertex_iterator</tt> models <a
402 href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">BidirectionalIterator</a>.
406 <tt>graph_traits<adjacency_list>::edge_iterator</tt>
408 The type for the iterators returned by <TT>edges()</TT>.
409 The <tt>edge_iterator</tt> models <a
410 href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">BidirectionalIterator</a>.
416 <tt>graph_traits<adjacency_list>::out_edge_iterator</tt>
419 The type for the iterators returned by <TT>out_edges()</TT>.
420 When <tt>OutEdgeList=vecS</tt> then the <tt>out_edge_iterator</tt> models
421 <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">
422 RandomAccessIterator</a>. When <tt>OutEdgeList=slistS</tt> then the
423 <tt>out_edge_iterator</tt> models <a
424 href="http://www.sgi.com/tech/stl/ForwardIterator.html">
425 ForwardIterator</a>. Otherwise the <tt>out_edge_iterator</tt> models
427 href="http://www.sgi.com/tech/stl/BidirectionalIterator.html">
428 BidirectionalIterator</a>.
432 <tt>graph_traits<adjacency_list>::adjacency_iterator</tt>
434 The type for the iterators returned by <TT>adjacent_vertices()</TT>.
435 The <tt>adjacency_iterator</tt> models the same iterator concept
436 as <tt>out_edge_iterator</tt>.
439 <tt>adjacency_list::inv_adjacency_iterator</tt>
441 The type for the iterators returned by <TT>inv_adjacent_vertices()</TT>.
442 The <tt>inv_adjacency_iterator</tt> models the same iterator concept
443 as <tt>out_edge_iterator</tt>.
446 <tt>graph_traits<adjacency_list>::directed_category</tt><br>
448 <tt>adjacency_list_traits<OutEdgeList, VertexList, Directed, EdgeList>::directed_category</tt>
450 Provides information about whether the graph is
451 directed (<TT>directed_tag</TT>) or undirected
452 (<TT>undirected_tag</TT>).
456 <tt>graph_traits<adjacency_list>::edge_parallel_category</tt><br>
458 <tt>adjacency_list_traits<OutEdgeList, VertexList, Directed, EdgeList>::edge_parallel_category</tt>
460 This describes whether the graph class allows the insertion of
461 parallel edges (edges with the same source and target). The two tags
462 are <TT>allow_parallel_edge-_tag</TT> and
463 <TT>disallow_parallel_edge_tag</TT>. The
464 <TT>setS</TT> and <TT>hash_setS</TT> variants disallow
465 parallel edges while the others allow parallel edges.
469 <tt>graph_traits<adjacency_list>::vertices_size_type</tt><br>
471 <tt>adjacency_list_traits<OutEdgeList, VertexList, Directed_list, EdgeList>::vertices_size_type</tt><br>
473 The type used for dealing with the number of vertices in the graph.
477 <tt>graph_traits<adjacency_list>::edge_size_type</tt><br>
479 <tt>adjacency_list_traits<OutEdgeList, VertexList, Directed_list, EdgeList>::edge_size_type</tt><br>
481 The type used for dealing with the number of edges in the graph.
485 <tt>graph_traits<adjacency_list>::degree_size_type</tt>
487 The type used for dealing with the number of edges incident to a vertex
492 <tt>property_map<adjacency_list, Property>::type</tt><br>
494 <tt>property_map<adjacency_list, Property>::const_type</tt>
496 The property map type for vertex or edge properties in the graph. The
497 specific property is specified by the <TT>Property</TT> template argument,
498 and must match one of the properties specified in the
499 <TT>VertexProperties</TT> or <TT>EdgeProperties</TT> for the graph.
503 <tt>graph_property<adjacency_list, Property>::type</tt>
505 The property value type for the graph property specified by the
506 <tt>Property</tt> tag.
510 <tt>adjacency_list::out_edge_list_selector</tt>
512 The type <tt>OutEdgeListS</tt>.
516 <tt>adjacency_list::vertex_list_selector</tt>
518 The type <tt>VertexListS</tt>.
522 <tt>adjacency_list::directed_selector</tt>
524 The type <tt>DirectedS</tt>.
528 <tt>adjacency_list::edge_list_selector</tt>
530 The type <tt>EdgeListS</tt>.
534 <H2>Member Functions</H2>
539 adjacency_list(const GraphProperty& p = GraphProperty())
541 Default constructor. Creates an empty graph object with zero vertices
547 adjacency_list(const adjacency_list& x)
549 Copy constructor. Creates a new graph that is a copy of graph
550 <tt>x</tt>, including the edges, vertices, and properties.
555 adjacency_list& operator=(const adjacency_list& x)
557 Assignment operator. Makes this graph a copy of graph
558 <tt>x</tt>, including the edges, vertices, and properties.
563 adjacency_list(vertices_size_type n,
564 const GraphProperty& p = GraphProperty())
566 Creates a graph object with <TT>n</TT> vertices and zero edges.
570 <a name="sec:iterator-constructor">
572 template <class EdgeIterator>
573 adjacency_list(EdgeIterator first, EdgeIterator last,
574 vertices_size_type n,
575 edges_size_type m = 0,
576 const GraphProperty& p = GraphProperty())
578 Creates a graph object with <TT>n</TT> vertices and with the edges
579 specified in the edge list given by the range <TT>[first, last)</TT>.
580 The <tt>EdgeIterator</tt> must be a model of <a
581 href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
582 The value type of the <TT>EdgeIterator</TT> must be a
583 <TT>std::pair</TT>, where the type in the pair is an integer type. The
584 integers will correspond to vertices, and they must all fall in the
585 range of <TT>[0, n)</TT>.
591 template <class EdgeIterator, class EdgePropertyIterator>
592 adjacency_list(EdgeIterator first, EdgeIterator last,
593 EdgePropertyIterator ep_iter,
594 vertices_size_type n,
595 edges_size_type m = 0,
596 const GraphProperty& p = GraphProperty())
598 Creates a graph object with <TT>n</TT> vertices and with the edges
599 specified in the edge list given by the range <TT>[first, last)</TT>.
600 The <tt>EdgeIterator</tt> and <tt>EdgePropertyIterator</tt> must be a
602 href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
603 The value type of the <TT>EdgeIterator</TT> must be a
604 <TT>std::pair</TT>, where the type in the pair is an integer type. The
605 integers will correspond to vertices, and they must all fall in the
606 range of <TT>[0, n)</TT>. The <TT>value_type</TT> of the
607 <TT>ep_iter</TT> should be <TT>EdgeProperties</TT>.
614 Remove all of the edges and vertices from the graph.
619 void swap(adjacency_list& x)
621 Swap the vertices, edges, and properties of this graph with the
622 vertices, edges, and properties of graph <tt>x</tt>.
627 <H2>Non-Member Functions</H2>
630 <h4>Structure Access</h4>
635 std::pair<vertex_iterator, vertex_iterator>
636 vertices(const adjacency_list& g)
638 Returns an iterator-range providing access to the vertex set of graph
644 std::pair<edge_iterator, edge_iterator>
645 edges(const adjacency_list& g)
647 Returns an iterator-range providing access to the edge set of graph
653 std::pair<adjacency_iterator, adjacency_iterator>
654 adjacent_vertices(vertex_descriptor u, const adjacency_list& g)
656 Returns an iterator-range providing access to the vertices adjacent to
657 vertex <tt>u</tt> in graph <tt>g</tt>. For example, if <tt>u -> v</tt>
658 is an edge in the graph, then <tt>v</tt> will be in this iterator-range.
663 std::pair<inv_adjacency_iterator, inv_adjacency_iterator>
664 inv_adjacent_vertices(vertex_descriptor u, const adjacency_list& g)
667 Returns an iterator-range providing access to the vertices in graph
668 <tt>g</tt> to which <tt>u</tt> is adjacent. (<tt>inv</tt> is for
669 inverse.) For example, if <tt>v -> u</tt> is an edge in the graph,
670 then <tt>v</tt> will be in this iterator range. This function is only
671 available for bidirectional and undirected <tt>adjacency_list</tt>'s.
677 std::pair<out_edge_iterator, out_edge_iterator>
678 out_edges(vertex_descriptor u, const adjacency_list& g)
680 Returns an iterator-range providing access to the out-edges of vertex
681 <tt>u</tt> in graph <tt>g</tt>. If the graph is undirected, this
682 iterator-range provides access to all edges incident on vertex
683 <tt>u</tt>. For both directed and undirected graphs, for an out-edge
684 <tt>e</tt>, <tt>source(e, g) == u</tt> and <tt>target(e, g) == v</tt>
685 where <tt>v</tt> is a vertex adjacent to <tt>u</tt>.
690 std::pair<in_edge_iterator, in_edge_iterator>
691 in_edges(vertex_descriptor v, const adjacency_list& g)
693 Returns an iterator-range providing access to the in-edges of vertex
694 <tt>v</tt> in graph <tt>g</tt>. This operation is only available if
695 <TT>bidirectionalS</TT> was specified for the <TT>Directed</TT>
696 template parameter. For an in-edge <tt>e</tt>, <tt>target(e, g) == v</tt>
697 and <tt>source(e, g) == u</tt> for some vertex <tt>u</tt> that is
698 adjacent to <tt>v</tt>, whether the graph is directed or undirected.
704 source(edge_descriptor e, const adjacency_list& g)
706 Returns the source vertex of edge <tt>e</tt>.
712 target(edge_descriptor e, const adjacency_list& g)
714 Returns the target vertex of edge <tt>e</tt>.
720 out_degree(vertex_descriptor u, const adjacency_list& g)
722 Returns the number of edges leaving vertex <tt>u</tt>.
728 in_degree(vertex_descriptor u, const adjacency_list& g)
730 Returns the number of edges entering vertex <tt>u</tt>. This operation
731 is only available if <TT>bidirectionalS</TT> was specified for
732 the <TT>Directed</TT> template parameter.
738 num_vertices(const adjacency_list& g)
740 Returns the number of vertices in the graph <tt>g</tt>.
746 num_edges(const adjacency_list& g)
748 Returns the number of edges in the graph <tt>g</tt>.
754 vertex(vertices_size_type n, const adjacency_list& g)
756 Returns the nth vertex in the graph's vertex list.
762 std::pair<edge_descriptor, bool>
763 edge(vertex_descriptor u, vertex_descriptor v,
764 const adjacency_list& g)
766 If an edge from vertex <tt>u</tt> to vertex <tt>v</tt> exists, return a pair
767 containing one such edge and <tt>true</tt>. If there are no edges between
768 <tt>u</tt> and <tt>v</tt>, return a pair with an arbitrary edge descriptor and
774 std::pair<out_edge_iterator, out_edge_iterator>
775 edge_range(vertex_descriptor u, vertex_descriptor v,
776 const adjacency_list& g)
778 Returns a pair of out-edge iterators that give the range for
779 all the parallel edges from <tt>u</tt> to <tt>v</tt>. This
780 function only works when the <tt>OutEdgeList</tt> for the
781 <tt>adjacency_list</tt> is a container that sorts the
782 out edges according to target vertex, and allows for
783 parallel edges. The <tt>multisetS</tt> selector chooses
788 <h4>Structure Modification</h4>
793 std::pair<edge_descriptor, bool>
794 add_edge(vertex_descriptor u, vertex_descriptor v,
795 adjacency_list& g)
797 Adds edge <i>(u,v)</i> to the graph and returns the edge descriptor
798 for the new edge. For graphs that do not allow parallel edges, if the
799 edge is already in the graph then a duplicate will not be added and
800 the <TT>bool</TT> flag will be <TT>false</TT>. When the flag is
802 returned edge descriptor points to the already existing edge.
805 The placement of the new edge in the out-edge list is in general
806 unspecified, though ordering of the out-edge list can be accomplished
807 through the choice of <tt>OutEdgeList</tt>.
809 If the <tt>VertexList</tt> selector is
810 <tt>vecS</tt>, and if either vertex descriptor <tt>u</tt> or
811 <tt>v</tt> (which are integers) has a value greater than the current
812 number of vertices in the graph, the graph is enlarged so that the
813 number of vertices is <tt>std::max(u,v) + 1</tt>.
816 If the <TT>OutEdgeList</TT> selector is <TT>vecS</TT> then this operation
817 will invalidate any <tt>out_edge_iterator</tt> for vertex
818 <i>u</i>. This also applies if the <TT>OutEdgeList</TT> is a user-defined
819 container that invalidates its iterators when <TT>push(container,
820 x)</TT> is invoked (see Section <A
821 HREF="./using_adjacency_list.html#sec:custom-storage">Customizing the
822 Adjacency List Storage</A>). If the graph is also bidirectional then
823 any <tt>in_edge_iterator</tt> for <i>v</i> is also invalidated. If
824 instead the graph is undirected then any <tt>out_edge_iterator</tt>
825 for <i>v</i> is also invalidated. If instead the graph is directed,
826 then <tt>add_edge()</tt> also invalidates any <tt>edge_iterator</tt>.
832 std::pair<edge_descriptor, bool>
833 add_edge(vertex_descriptor u, vertex_descriptor v,
834 const EdgeProperties& p,
835 adjacency_list& g)
837 Adds edge <i>(u,v)</i> to the graph and attaches <TT>p</TT> as the
838 value of the edge's internal property storage. Also see the previous
839 <TT>add_edge()</TT> member function for more details.
844 void remove_edge(vertex_descriptor u, vertex_descriptor v,
845 adjacency_list& g)
847 Removes the edge <i>(u,v)</i> from the graph.
849 This operation causes any outstanding edge descriptors or iterators
850 that point to edge <i>(u,v)</i> to become invalid. In addition, if
851 the <TT>OutEdgeList</TT> selector is <TT>vecS</TT> then this operation
852 will invalidate any iterators that point into the edge-list for vertex
853 <i>u</i> and also for vertex <i>v</i> in the undirected and
854 bidirectional case. Also, for directed graphs this invalidates any
855 <tt>edge_iterator</tt>.
860 void remove_edge(edge_descriptor e, adjacency_list& g)
862 Removes the edge <tt>e</tt> from the graph. This differs from the
863 <tt>remove_edge(u, v, g)</tt> function in the case of a
864 multigraph. This <tt>remove_edge(e, g)</tt> function removes a single
865 edge, whereas the <tt>remove_edge(u, v, g)</tt> function removes all
868 This operation invalidates any outstanding edge descriptors and
869 iterators for the same edge pointed to by descriptor <tt>e</tt>. In
870 addition, this operation will invalidate any iterators that point into
871 the edge-list for the <tt>target(e, g)</tt>. Also, for directed
872 graphs this invalidates any <tt>edge_iterator</tt> for the graph.
877 void remove_edge(out_edge_iterator iter, adjacency_list& g)
879 This has the same effect as <tt>remove_edge(*iter, g)</tt>. The
880 difference is that this function has constant time complexity
881 in the case of directed graphs, whereas <tt>remove_edge(e, g)</tt>
882 has time complexity <i>O(E/V)</i>.
887 template <class <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
888 void remove_out_edge_if(vertex_descriptor u, Predicate predicate,
889 adjacency_list& g)
891 Removes all out-edges of vertex <i>u</i> from the graph that satisfy
892 the <tt>predicate</tt>. That is, if the predicate returns true when
893 applied to an edge descriptor, then the edge is removed.
895 The affect on descriptor and iterator stability is the same as that of
896 invoking <tt>remove_edge()</tt> on each of the removed edges.
901 template <class <a
902 href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
903 void remove_in_edge_if(vertex_descriptor v, Predicate predicate,
904 adjacency_list& g)
906 Removes all in-edges of vertex <i>v</i> from the graph that satisfy
907 the <tt>predicate</tt>. That is, if the predicate returns true when
908 applied to an edge descriptor, then the edge is removed.
910 The affect on descriptor and iterator stability is the
911 same as that of invoking <tt>remove_edge()</tt> on each of the
914 This operation is available for undirected and bidirectional
915 <tt>adjacency_list</tt> graphs, but not for directed.
920 template <class <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
921 void remove_edge_if(Predicate predicate, adjacency_list& g)
923 Removes all edges from the graph that satisfy
924 the <tt>predicate</tt>. That is, if the predicate returns true when
925 applied to an edge descriptor, then the edge is removed.
927 The affect on descriptor and iterator stability is the same as that of
928 invoking <tt>remove_edge()</tt> on each of the removed edges.
932 <a name="sec:add-vertex">
935 add_vertex(adjacency_list& g)
937 Adds a vertex to the graph and returns the vertex descriptor for the
945 add_vertex(const VertexProperties& p,
946 adjacency_list& g)
948 Adds a vertex to the graph with the specified properties. Returns the
949 vertex descriptor for the new vertex.
955 void clear_vertex(vertex_descriptor u, adjacency_list& g)
957 Removes all edges to and from vertex <i>u</i>. The vertex still appears
958 in the vertex set of the graph.
960 The affect on descriptor and iterator stability is the
961 same as that of invoking <tt>remove_edge()</tt> for all of
962 the edges that have <tt>u</tt> as the source or target.
967 void clear_out_edges(vertex_descriptor u, adjacency_list& g)
969 Removes all out-edges from vertex <i>u</i>. The vertex still appears
970 in the vertex set of the graph.
972 The affect on descriptor and iterator stability is the
973 same as that of invoking <tt>remove_edge()</tt> for all of
974 the edges that have <tt>u</tt> as the source.
976 This operation is not applicable to undirected graphs
977 (use <tt>clear_vertex()</tt> instead).
982 void clear_in_edges(vertex_descriptor u, adjacency_list& g)
984 Removes all in-edges from vertex <i>u</i>. The vertex still appears
985 in the vertex set of the graph.
987 The affect on descriptor and iterator stability is the
988 same as that of invoking <tt>remove_edge()</tt> for all of
989 the edges that have <tt>u</tt> as the target.
991 This operation is only applicable to bidirectional graphs.
996 void remove_vertex(vertex_descriptor u, adjacency_list& g)
998 Remove vertex <i>u</i> from the vertex set of the graph. It is assumed
999 that there are no edges to or from vertex <i>u</i> when it is removed.
1000 One way to make sure of this is to invoke <TT>clear_vertex()</TT>
1003 If the <TT>VertexList</TT> template parameter of the
1004 <TT>adjacency_list</TT> was <TT>vecS</TT>, then all vertex
1005 descriptors, edge descriptors, and iterators for the graph are
1006 invalidated by this operation. The builtin
1007 <tt>vertex_index_t</tt> property for each vertex is renumbered so that
1008 after the operation the vertex indices still form a contiguous range
1009 <TT>[0, num_vertices(g))</TT>. If you are using external property
1010 storage based on the builtin vertex index, then the external storage
1011 will need to be adjusted. Another option is to not use the builtin
1012 vertex index, and instead use a property to add your own vertex index
1013 property. If you need to make frequent use of the
1014 <TT>remove_vertex()</TT> function the <TT>listS</TT> selector is a
1015 much better choice for the <TT>VertexList</TT> template parameter.
1019 <h4><a name="property-map-accessors">Property Map Accessors</a></h4>
1024 template <class <a href="./PropertyTag.html">PropertyTag</a>>
1025 property_map<adjacency_list, PropertyTag>::type
1026 get(PropertyTag, adjacency_list& g)
1028 template <class <a href="./PropertyTag.html">PropertyTag</a>>
1029 property_map<adjacency_list, Tag>::const_type
1030 get(PropertyTag, const adjacency_list& g)
1032 Returns the property map object for the vertex property specified by
1033 <TT>PropertyTag</TT>. The <TT>PropertyTag</TT> must match one of the
1034 properties specified in the graph's <TT>VertexProperty</TT> template
1040 template <class <a href="./PropertyTag.html">PropertyTag</a>, class X>
1041 typename property_traits<property_map<adjacency_list, PropertyTag>::const_type>::value_type
1042 get(PropertyTag, const adjacency_list& g, X x)
1044 This returns the property value for <tt>x</tt>, where <tt>x</tt> is either
1045 a vertex or edge descriptor.
1049 template <class <a href="./PropertyTag.html">PropertyTag</a>, class X, class Value>
1051 put(PropertyTag, const adjacency_list& g, X x, const Value& value)
1053 This sets the property value for <tt>x</tt> to
1054 <tt>value</tt>. <tt>x</tt> is either a vertex or edge descriptor.
1055 <tt>Value</tt> must be convertible to
1056 <tt>typename property_traits<property_map<adjacency_list, PropertyTag>::type>::value_type</tt>
1061 template <class GraphProperties, class <a href="./PropertyTag.html#GraphPropertyTag">GraphPropertyTag</a>>
1062 typename graph_property<adjacency_list, GraphPropertyTag>::type&
1063 get_property(adjacency_list& g, GraphPropertyTag);
1065 Return the property specified by <tt>GraphPropertyTag</tt> that is
1066 attached to the graph object <tt>g</tt>. The <tt>graph_property</tt>
1067 traits class is defined in <a
1068 href="../../../boost/graph/adjacency_list.hpp"><tt>boost/graph/adjacency_list.hpp</tt></a>.
1073 template <class GraphProperties, class <a href="./PropertyTag.html#GraphPropertyTag">GraphPropertyTag</a>>
1074 const typename graph_property<adjacency_list, GraphPropertyTag>::type&
1075 get_property(const adjacency_list& g, GraphPropertyTag);
1077 Return the property specified by <tt>GraphPropertyTag</tt> that is
1078 attached to the graph object <tt>g</tt>. The <tt>graph_property</tt>
1079 traits class is defined in <a
1080 href="../../../boost/graph/adjacency_list.hpp"><tt>boost/graph/adjacency_list.hpp</tt></a>.
1082 <!-- add the shortcut property functions -->
1088 <h4><a name="serialization">Serialization</a></h4>
1093 template<class <a href="../../serialization/doc/archives.html#saving_interface">SavingArchive</a>>
1094 SavingArchive& operator<<(SavingArchive& ar, const adjacency_list& graph);
1096 Serializes the graph into the archive. Requires the vertex and edge properties of the
1097 graph to be <a href="../../serialization/doc/index.html">Serializable</a>.
1099 Include <a href="../../../boost/graph/adj_list_serialize.hpp"><tt>boost/graph/adj_list_serialize.hpp</tt></a>.
1103 template<class <a href="../../serialization/doc/archives.html#loading_interface">LoadingArchive</a>>
1104 LoadingArchive& operator>>(LoadingArchive& ar, const adjacency_list& graph);
1106 Reads the graph from the archive. Requires the vertex and edge properties of the
1107 graph to be <a href="../../serialization/doc/index.html">Serializable</a>.
1109 Include <a href="../../../boost/graph/adj_list_serialize.hpp"><tt>boost/graph/adj_list_serialize.hpp</tt></a>.
1115 <a href="./adjacency_list_traits.html"><tt>adjacency_list_traits</tt></a>,
1116 <a href="./property_map.html"><tt>property_map</tt></a>,
1117 <a href="./graph_traits.html"><tt>graph_traits</tt></a>
1125 <TD nowrap>Copyright © 2000-2001</TD><TD>
1126 <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
1127 Indiana University (<A
1128 HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
1129 <A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br>
1130 <A HREF="http://www.osl.iu.edu/~lums">Andrew Lumsdaine</A>,
1131 Indiana University (<A
1132 HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
1137 <!-- LocalWords: gif ALT OutEdgeList EdgeList VertexList html VertexProperties EdgeProperties
1139 <!-- LocalWords: GraphPropertyTag cpp enum ai cout endl VertexAndEdgeListGraph
1141 <!-- LocalWords: MutablePropertyGraph hpp const ReadablePropertyMap listS num
1143 <!-- LocalWords: ReadWritePropertyMap vecS dijkstra ucs pre Adj Iter Desc ep
1145 <!-- LocalWords: EdgeIterator EdgePropertyIterator iter bool edge's IDs siek
1147 <!-- LocalWords: multigraph typename htm Univ Quan Lumsdaine