]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/graph/doc/adjacency_matrix.html
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / graph / doc / adjacency_matrix.html
CommitLineData
7c673cae
FG
1<HTML>
2<!--
3 Copyright (c) Jeremy Siek 2000
4
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)
8 -->
9<Head>
10<Title>Boost Graph Library: Adjacency Matrix</Title>
11<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
12 ALINK="#ff0000">
13<IMG SRC="../../../boost.png"
14 ALT="C++ Boost" width="277" height="86">
15
16<BR Clear>
17
18<H1><A NAME="sec:adjacency-matrix-class"></A>
19<pre>
20adjacency_matrix&lt;Directed, VertexProperty,
21 EdgeProperty, GraphProperty,
22 Allocator&gt;
23</pre>
24</H1>
25
26The <tt>adjacency_matrix</tt> class implements the BGL graph interface
27using the traditional adjacency matrix storage format. For a graph
28with <i>V</i> vertices, a <i>V x V</i> matrix is used, where each
29element <i>a<sub>ij</sub></i> is a boolean flag that says whether
30there is an edge from vertex <i>i</i> to vertex <i>j</i>. <a
31href="#fig:adj-matrix-graph">Figure 1</a> shows the adjacency matrix
32representation of a graph.
33
34<P></P>
35<DIV ALIGN="center"><A NAME="fig:adj-matrix-graph"></A>
36<TABLE>
37<CAPTION ALIGN="BOTTOM"><STRONG>Figure 1:</STRONG> Adjacency Matrix Representation of a Directed Graph.</CAPTION>
38<TR><TD><IMG SRC="./figs/adj-matrix-graph3.gif" width="386" height="284"></TD>
39<TD><IMG SRC="./figs/adj-matrix.gif" width="135" height="136"></TD></TR>
40</TABLE>
41</DIV><P></P>
42
43The advantage of this matrix format over the adjacency list is that
44edge insertion and removal is constant time. There are several
45disadvantages. The first is that the amount of memory used is
46<i>O(V<sup>2</sup>)</i> instead of <i>O(V + E)</i> (where <i>E</i> is
47the number of edges). The second is that operations that traverse all
48the out-edges of each vertex (such as breadth-first search) run in
49<i>O(V<sup>2</sup>)</i> time instead of <i>O(V + E)</i> time for the
50adjacency list. In short, it is better to use the
51<tt>adjacency_matrix</tt> for dense graphs (where <i>E</i> is close to
52<i>V<sup>2</sup></i>) and it is better to use <a
53href="adjacency_list.html"><tt>adjacency_list</tt></a> for sparse
54graphs (where <i>E</i> is much smaller than <i>V<sup>2</sup></i>).
55
56The <tt>adjacency_matrix</tt> class extends the traditional
57data-structure by allowing objects to be attached to vertices and
58edges using the same property template parameters supported by <a
59href="adjacency_list.html"><tt>adjacency_list</tt></a>. These may be
60<a href="bundles.html">bundled properties</a> or standard (backward-compatible)
61<a
62href="using_adjacency_list.html#sec:adjacency-list-properties">interior
63properties</a>. The types of all property values must be
64Copy Constructible, Assignable and Default Constructible.
65
66In the case of an undirected graph, the
67<tt>adjacency_matrix</tt>. class does not use a full <i>V x V</i>
68matrix but instead uses a lower triangle (the diagonal and below)
69since the matrix for an undirected graph is symmetric. This reduces
70the storage to <i>(V<sup>2</sup>)/2</i>. <a
71href="#fig:undir-adj-matrix-graph">Figure 2</a> shows an adjacency
72matrix representation of an undirected graph.
73
74<P></P>
75<DIV ALIGN="center"><A NAME="fig:undir-adj-matrix-graph"></A>
76<TABLE>
77<CAPTION ALIGN="BOTTOM"><STRONG>Figure 2:</STRONG> Adjacency Matrix Representation of an Undirected Graph.</CAPTION>
78<TR><TD><IMG SRC="./figs/undir-adj-matrix-graph3.gif" width="260" height="240"></TD>
79<TD><IMG SRC="./figs/undir-adj-matrix2.gif" width="135" height="136"></TD></TR>
80</TABLE>
81</DIV><P></P>
82
83
84<h3>Example</h3>
85
86Creating the graph of <a href="#fig:adj-matrix-graph">Figure 1</a>.
87<pre>
88 enum { A, B, C, D, E, F, N };
89 const char* name = "ABCDEF";
90
91 typedef boost::adjacency_matrix&lt;boost::directedS> Graph;
92 Graph g(N);
93 add_edge(B, C, g);
94 add_edge(B, F, g);
95 add_edge(C, A, g);
96 add_edge(C, C, g);
97 add_edge(D, E, g);
98 add_edge(E, D, g);
99 add_edge(F, A, g);
100
101 std::cout &lt;&lt; "vertex set: ";
102 boost::print_vertices(g, name);
103 std::cout &lt;&lt; std::endl;
104
105 std::cout &lt;&lt; "edge set: ";
106 boost::print_edges(g, name);
107 std::cout &lt;&lt; std::endl;
108
109 std::cout &lt;&lt; "out-edges: " &lt;&lt; std::endl;
110 boost::print_graph(g, name);
111 std::cout &lt;&lt; std::endl;
112</pre>
113The output is:
114<pre>
115 vertex set: A B C D E F
116
117 edge set: (B,C) (B,F) (C,A) (C,C) (D,E) (E,D) (F,A)
118
119 out-edges:
120 A -->
121 B --> C F
122 C --> A C
123 D --> E
124 E --> D
125 F --> A
126</pre>
127
128Creating the graph of <a href="#fig:undir-adj-matrix-graph">Figure 2</a>.
129<pre>
130 enum { A, B, C, D, E, F, N };
131 const char* name = "ABCDEF";
132
133 typedef boost::adjacency_matrix&lt;boost::undirectedS> UGraph;
134 UGraph ug(N);
135 add_edge(B, C, ug);
136 add_edge(B, F, ug);
137 add_edge(C, A, ug);
138 add_edge(D, E, ug);
139 add_edge(F, A, ug);
140
141 std::cout &lt;&lt; "vertex set: ";
142 boost::print_vertices(ug, name);
143 std::cout &lt;&lt; std::endl;
144
145 std::cout &lt;&lt; "edge set: ";
146 boost::print_edges(ug, name);
147 std::cout &lt;&lt; std::endl;
148
149 std::cout &lt;&lt; "incident edges: " &lt;&lt; std::endl;
150 boost::print_graph(ug, name);
151 std::cout &lt;&lt; std::endl;
152</pre>
153The output is:
154<pre>
155 vertex set: A B C D E F
156
157 edge set: (C,A) (C,B) (E,D) (F,A) (F,B)
158
159 incident edges:
160 A &lt;--> C F
161 B &lt;--> C F
162 C &lt;--> A B
163 D &lt;--> E
164 E &lt;--> D
165 F &lt;--> A B
166</pre>
167
168
169<h3>Where Defined</h3>
170
171<a href="../../../boost/graph/adjacency_matrix.hpp"><tt>boost/graph/adjacency_matrix.hpp</tt></a>
172
173
174<h3>Template Parameters</h3>
175
176<p>
177<table border>
178
179<TR>
180<th>Parameter</th><th>Description</th><th>Default</th>
181</tr>
182
183<tr>
184 <td><tt>Directed</tt></td>
185 <td>A selector to choose whether the graph is directed or undirected. The options are <tt>directedS</tt> and <tt>undirectedS</tt>.</td>
186 <td><tt>directedS</tt></td>
187</tr>
188<tr>
189 <td><tt>VertexProperty</tt></td>
190 <td>for specifying internal property storage.</td>
191 <td><tt>no_property</tt></td>
192</tr>
193<tr>
194 <td><tt>EdgeProperty</tt></td>
195 <td>for specifying internal property storage.</td>
196 <td><tt>no_property</tt></td>
197</tr>
198<tr>
199 <td><tt>GraphProperty</tt></td>
200 <td>for specifying property storage for the graph object.</td>
201 <td><tt>no_property</tt></td>
202</tr>
203
204</table>
205
206<h3>Model Of</h3>
207
208<a href="./VertexAndEdgeListGraph.html">VertexAndEdgeListGraph</a>,
209<a href="./IncidenceGraph.html">Incidence Graph</a>,
210<a href="./BidirectionalGraph.html">Bidirectional Graph</a>,
211<a
212href="./AdjacencyMatrix.html">AdjacencyMatrix</a>, <a
213href="./MutablePropertyGraph.html">MutablePropertyGraph</a>,
214<a href="../../utility/CopyConstructible.html">CopyConstructible</a>,
215and <a href="../../utility/Assignable.html">Assignable</a>.
216
217
218<h3>Associated Types</h3>
219
220<hr>
221
222<tt>graph_traits&lt;adjacency_matrix&gt;::vertex_descriptor</tt>
223<br><br>
224The type for the vertex descriptors associated with the
225<tt>adjacency_matrix</tt>.<br>
226(Required by <a href="./Graph.html">Graph</a>.)
227
228<hr>
229
230<tt>graph_traits&lt;adjacency_matrix&gt;::edge_descriptor</tt>
231<br><br>
232The type for the edge descriptors associated with the
233 <tt>adjacency_matrix</tt>.<br>
234 (Required by <a href="Graph.html">Graph</a>.)
235
236<hr>
237<tt>graph_traits&lt;adjacency_matrix&gt;::vertex_iterator</tt>
238<br><br>
239 The type for the iterators returned by <tt>vertices()</tt>.
240 The vertex iterator models <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>. <br>
241 (Required by <a href="VertexListGraph.html">VertexListGraph</a>.)
242
243<hr>
244<tt>graph_traits&lt;adjacency_matrix&gt;::edge_iterator</tt>
245<br><br>
246 The type for the iterators returned by <tt>edges()</tt>. This
247 iterator models <a href="../../utility/MultiPassInputIterator.html">MultiPassInputIterator</a>.<br>
248 (Required by <a href="EdgeListGraph.html">EdgeListGraph</a>.)
249
250<hr>
251<tt>graph_traits&lt;adjacency_matrix&gt;::out_edge_iterator</tt>
252<br><br>
253 The type for the iterators returned by <tt>out_edges()</tt>. This
254 iterator models <a href="../../utility/MultiPassInputIterator.html">MultiPassInputIterator</a>. <br>
255 (Required by <a href="IncidenceGraph.html">IncidenceGraph</a>.)
256
257<hr>
258<tt>graph_traits&lt;adjacency_matrix&gt;::in_edge_iterator</tt>
259<br><br>
260 The type for the iterators returned by <tt>in_edges()</tt>. This
261 iterator models <a href="../../utility/MultiPassInputIterator.html">MultiPassInputIterator</a>. <br>
262 (Required by <a href="BidirectionalGraph.html">BidirectionalGraph</a>.)
263
264<hr>
265<tt>graph_traits&lt;adjacency_matrix&gt;::adjacency_iterator</tt>
266<br><br>
267 The type for the iterators returned by <tt>adjacent_vertices()</tt>. This
268 iterator models the same concept as the out-edge iterator.<br>
269 (Required by <a href="AdjacencyGraph.html">AdjacencyGraph</a>.)
270
271<hr>
272<tt>graph_traits&lt;adjacency_matrix&gt;::directed_category</tt>
273<br><br>
274 Provides information about whether the graph is directed
275 (<tt>directed_tag</tt>) or undirected (<tt>undirected_tag</tt>).<br>
276 (Required by <a href="Graph.html">Graph</a>.)
277
278<hr>
279<tt>graph_traits&lt;adjacency_matrix&gt;::edge_parallel_category</tt>
280<br><br>
281 An adjacency matrix does not allow the insertion of
282 parallel edges, so this type is always
283 <tt>disallow_parallel_edge_tag</tt>. <br>
284 (Required by <a href="Graph.html">Graph</a>.)
285
286<hr>
287<tt>graph_traits&lt;adjacency_matrix&gt;::vertices_size_type</tt>
288<br><br>
289 The type used for dealing with the number of vertices in
290 the graph.<br>
291 (Required by <a href="VertexListGraph.html">VertexListGraph</a>.)
292
293<hr>
294<tt>graph_traits&lt;adjacency_matrix&gt;::edges_size_type</tt>
295<br><br>
296 The type used for dealing with the number of edges in the graph.<br>
297 (Required by <a href="EdgeListGraph.html">EdgeListGraph</a>.)
298
299<hr>
300<tt>graph_traits&lt;adjacency_matrix&gt;::degree_size_type</tt>
301<br><br>
302 The type used for dealing with the number of out-edges of a vertex.<br>
303 (Required by <a href="IncidenceGraph.html">IncidenceGraph</a>.)
304
305<hr>
306<tt>property_map&lt;adjacency_matrix, PropertyTag&gt;::type</tt><br>
307<tt>property_map&lt;adjacency_matrix, PropertyTag&gt;::const_type</tt>
308<br><br>
309 The map type for vertex or edge properties in the graph. The
310 specific property is specified by the <tt>PropertyTag</tt> template
311 argument, and must match one of the properties specified in the
312 <tt>VertexProperty</tt> or <tt>EdgeProperty</tt> for the graph.<br>
313 (Required by <a href="PropertyGraph.html">PropertyGraph</a>.)
314
315<hr>
316
317<h3>Member Functions</h3>
318
319<hr>
320<pre>
321adjacency_matrix(vertices_size_type n,
322 const GraphProperty& p = GraphProperty())
323</pre>
324Creates a graph object with <tt>n</tt> vertices and zero edges.<br>
325(Required by <a href="MutableGraph.html">MutableGraph</a>.)
326
327<hr>
328<pre>
329template &lt;typename EdgeIterator&gt;
330adjacency_matrix(EdgeIterator first,
331 EdgeIterator last,
332 vertices_size_type n,
333 const GraphProperty& p = GraphProperty())
334</pre>
335 Creates a graph object with <tt>n</tt> vertices with the edges
336 specified in the edge list given by the range <tt>[first, last)</tt>.
337 The value type of the <tt>EdgeIterator</tt> must be a
338 <tt>std::pair</tt>, where the type in the pair is an integer type. The
339 integers will correspond to vertices, and they must all fall in the
340 range of
341 <tt>[0, n)</tt>. <br>
342 (Required by <a href="IteratorConstructibleGraph.html">IteratorConstructibleGraph</a>.)
343
344<hr>
345<pre>
346template &lt;typename EdgeIterator, typename EdgePropertyIterator&gt;
347adjacency_matrix(EdgeIterator first, EdgeIterator last,
348 EdgePropertyIterator ep_iter,
349 vertices_size_type n,
350 const GraphProperty& p = GraphProperty())
351</pre>
352 Creates a graph object with <tt>n</tt> vertices, with the edges
353 specified in the edge list given by the range <tt>[first, last)</tt>.
354 The value type of the <tt>EdgeIterator</tt> must be a
355 <tt>std::pair</tt>, where the type in the pair is an integer type. The
356 integers will correspond to vertices, and they must all fall in the
357 range of <tt>[0, n)</tt>. The <tt>value_type</tt> of the
358 <tt>ep_iter</tt> should be <tt>EdgeProperty</tt>.
359
360<hr>
361
362
363<h3>Non-Member Functions</h3>
364
365<hr>
366<pre>
367std::pair&lt;vertex_iterator, vertex_iterator&gt;
368vertices(const adjacency_matrix&amp; g)
369</pre>
370Returns an iterator-range providing access to the vertex set of graph <tt>g</tt>.<br>
371(Required by <a href="VertexListGraph.html">VertexListGraph</a>.)
372
373<hr>
374<pre>
375std::pair&lt;edge_iterator, edge_iterator&gt;
376edges(const adjacency_matrix&amp; g);
377</pre>
378Returns an iterator-range providing access to the edge set of graph <tt>g</tt>.<br>
379(Required by <a href="EdgeListGraph.html">EdgeListGraph</a>.)
380
381<hr>
382<pre>
383std::pair&lt;adjacency_iterator, adjacency_iterator&gt;
384adjacent_vertices(vertex_descriptor v, const adjacency_matrix&amp; g)
385</pre>
386Returns an iterator-range providing access to the vertices adjacent to
387vertex <tt>v</tt> in graph <tt>g</tt>.<br>
388(Required by <a href="AdjacencyGraph.html">AdjacencyGraph</a>.)
389
390<hr>
391<pre>
392std::pair&lt;out_edge_iterator, out_edge_iterator&gt;
393out_edges(vertex_descriptor v, const adjacency_matrix&amp; g)
394</pre>
395Returns an iterator-range providing access to the out-edges of
396vertex <tt>v</tt> in graph <tt>g</tt>. If the graph is undirected,
397this iterator-range provides access to all edges incident on
398vertex <tt>v</tt>. <br>
399(Required by <a href="IncidenceGraph.html">IncidenceGraph</a>.)
400
401<hr>
402<pre>
403vertex_descriptor
404source(edge_descriptor e, const adjacency_matrix&amp; g)
405</pre>
406Returns the source vertex of edge <tt>e</tt>.<br>
407(Required by <a href="IncidenceGraph.html">IncidenceGraph</a>.)
408
409<hr>
410<pre>
411vertex_descriptor
412target(edge_descriptor e, const adjacency_matrix&amp; g)
413</pre>
414Returns the target vertex of edge <tt>e</tt>.<br>
415(Required by <a href="IncidenceGraph.html">IncidenceGraph</a>.)
416
417<hr>
418<pre>
419degree_size_type
420out_degree(vertex_descriptor u, const adjacency_matrix&amp; g)
421</pre>
422Returns the number of edges leaving vertex <tt>u</tt>.<br>
423(Required by <a href="IncidenceGraph.html">IncidenceGraph</a>.)
424<hr>
425
426<hr>
427<pre>
428std::pair&lt;in_edge_iterator, in_edge_iterator&gt;
429in_edges(vertex_descriptor v, const adjacency_matrix&amp; g)
430</pre>
431Returns an iterator-range providing access to the in-edges of
432vertex <tt>v</tt> in graph <tt>g</tt>. If the graph is undirected,
433this iterator-range provides access to all edges incident on
434vertex <tt>v</tt>. <br>
435(Required by <a href="BidirectionalGraph.html">BidirectionalGraph</a>.)
436
437<hr>
438<pre>
439degree_size_type
440in_degree(vertex_descriptor u, const adjacency_matrix&amp; g)
441</pre>
442Returns the number of edges entering vertex <tt>u</tt>.<br>
443(Required by <a href="BidirectionalGraph.html">BidirectionalGraph</a>.)
444<hr>
445
446<hr>
447<pre>
448vertices_size_type num_vertices(const adjacency_matrix&amp; g)
449</pre>
450Returns the number of vertices in the graph <tt>g</tt>.<br>
451(Required by <a href="VertexListGraph.html">VertexListGraph</a>.)
452
453<hr>
454<pre>
455edges_size_type num_edges(const adjacency_matrix&amp; g)
456</pre>
457Returns the number of edges in the graph <tt>g</tt>.<br>
458(Required by <a href="EdgeListGraph.html">EdgeListGraph</a>.)
459
460<hr>
461<pre>
462vertex_descriptor vertex(vertices_size_type n, const adjacency_matrix&amp; g)
463</pre>
464Returns the nth vertex in the graph's vertex list.
465
466<hr>
467<pre>
468std::pair&lt;edge_descriptor, bool&gt;
469edge(vertex_descriptor u, vertex_descriptor v,
470 const adjacency_matrix&amp; g)
471</pre>
472Returns the edge connecting vertex <tt>u</tt> to vertex <tt>v</tt> in graph <tt>g</tt>.<br>
473(Required by <a href="AdjacencyMatrix.html">AdjacencyMatrix</a>.)
474
475<hr>
476<pre>
477std::pair&lt;edge_descriptor, bool&gt;
478add_edge(vertex_descriptor u, vertex_descriptor v,
479 adjacency_matrix&amp; g)
480</pre>
481 Adds edge <tt>(u,v)</tt> to the graph and returns the edge descriptor for
482 the new edge. If the edge is already in the graph then a duplicate
483 will not be added and the <tt>bool</tt> flag will be <tt>false</tt>.
484 This operation does not invalidate any of the graph's iterators
485 or descriptors.<br>
486(Required by <a href="MutableGraph.html">MutableGraph</a>.)
487
488<hr>
489<pre>
490std::pair&lt;edge_descriptor, bool&gt;
491add_edge(vertex_descriptor u, vertex_descriptor v,
492 const EdgeProperty& p,
493 adjacency_matrix&amp; g)
494</pre>
495Adds edge <tt>(u,v)</tt> to the graph and attaches <tt>p</tt> as the
496value of the edge's internal property storage. Also see the previous
497<tt>add_edge()</tt> member function for more details.
498
499<hr>
500<pre>
501void remove_edge(vertex_descriptor u, vertex_descriptor v,
502 adjacency_matrix&amp; g)
503</pre>
504Removes the edge <tt>(u,v)</tt> from the graph. <br>
505(Required by <a href="MutableGraph.html">MutableGraph</a>.)
506
507<hr>
508<pre>
509void remove_edge(edge_descriptor e, adjacency_matrix&amp; g)
510</pre>
511Removes the edge <tt>e</tt> from the graph. This is equivalent
512to calling <tt>remove_edge(source(e, g), target(e, g), g)</tt>.<br>
513(Required by <a href="MutableGraph.html">MutableGraph</a>.)
514
515<hr>
516<pre>
517void clear_vertex(vertex_descriptor u, adjacency_matrix&amp; g)
518</pre>
519Removes all edges to and from vertex <tt>u</tt>. The vertex still appears
520in the vertex set of the graph.<br>
521(Required by <a href="MutableGraph.html">MutableGraph</a>.)
522
523<hr>
524<pre>
525template &lt;typename Property&gt;
526property_map&lt;adjacency_matrix, Property&gt;::type
527get(Property, adjacency_matrix&amp; g)
528
529template &lt;typename Property&gt;
530property_map&lt;adjacency_matrix, Property&gt;::const_type
531get(Property, const adjacency_matrix&amp; g)
532</pre>
533Returns the property map object for the vertex property specified by
534<tt>Property</tt>. The <tt>Property</tt> must match one of the
535properties specified in the graph's <tt>VertexProperty</tt> template
536argument.<br>
537(Required by <a href="PropertyGraph.html">PropertyGraph</a>.)
538
539<hr>
540<pre>
541template &lt;typename Property, typename X&gt;
542typename property_traits&lt;
543 typename property_map&lt;adjacency_matrix, Property&gt;::const_type
544&gt;::value_type
545get(Property, const adjacency_matrix&amp; g, X x)
546</pre>
547This returns the property value for <tt>x</tt>, which is either
548a vertex or edge descriptor.<br>
549(Required by <a href="PropertyGraph.html">PropertyGraph</a>.)
550
551<hr>
552<pre>
553template &lt;typename Property, typename X, typename Value&gt;
554void
555put(Property, const adjacency_matrix&amp; g, X x, const Value& value)
556</pre>
557This sets the property value for <tt>x</tt> to
558<tt>value</tt>. <tt>x</tt> is either a vertex or edge descriptor.
559<tt>Value</tt> must be convertible to
560<tt>typename property_traits&lt;property_map&lt;adjacency_matrix, Property&gt;::type&gt;::value_type</tt>.<br>
561(Required by <a href="PropertyGraph.html">PropertyGraph</a>.)
562
563<hr>
564<pre>
565template &lt;typename GraphProperty, typename GraphProperty&gt;
566typename property_value&lt;GraphProperty, GraphProperty&gt;::type&amp;
567get_property(adjacency_matrix&amp; g, GraphProperty)
568</pre>
569Return the property specified by <tt>GraphProperty</tt> that is attached
570to the graph object <tt>g</tt>. The <tt>property_value</tt> traits class
571is defined in <tt>boost/pending/property.hpp</tt>.
572
573<hr>
574<pre>
575template &lt;typename GraphProperty, typename GraphProperty&gt;
576const typename property_value&lt;GraphProperty, GraphProperty&gt;::type&amp;
577get_property(const adjacency_matrix&amp; g, GraphProperty)
578</pre>
579Return the property specified by <tt>GraphProperty</tt> that is
580attached to the graph object <tt>g</tt>. The <tt>property_value</tt>
581traits class is defined in <tt>boost/pending/property.hpp</tt>.
582
583
584<hr>