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