Академический Документы
Профессиональный Документы
Культура Документы
T h e B o o s t
G r a p h L i b r a r y
Addison-Wesley
Boston San Francisco New York Toronto
Montreal London Munich Paris Madrid Capetown
Sydney Tokyo Singapore Mexico City
, -,
B o o s t G r a p h l i b r a r y
-
--
2006
, - ,
C++ Boost Graph Library.
.
.
.
.
.
. , .
.
.
. , .
.
32.973-018.1
004.43
. , . , .
35 C++ Boost Graph Library. / . .
.: , 2006. 304 : .
ISBN 5-469-00352-3
, C++ in Depth,
Boost Graph Library (BGL) -
, : -
.
BGL, .
, , -
,
BGL. , -
BGL, .
Addison-Wesley Longman.
.
.
, , , .
, ,
, .
ISBN 5-469-00352-3
190005, -, ., 29
11
15
I. 23
1. 24
2. C++ 39
3. BGL 59
4. 77
5. 89
6. 102
7. 109
8. 117
9. : 124
10. 130
11. 137
I I . 145
12. BGL 146
13. BGL 170
14. BGL 215
15. 274
16. , 284
294
297
299
11
15
16
BGL 17
Boost? 18
BGL 18
19
20
21
22
22
I. 23
1. 24
1.1. 24
1.2. 26
1.2.1. 26
1.2.2. 27
1.2.3. 28
1.2.4. 29
1.2.5. 30
1.3. 32
1.3.1. 32
1.3.2. 33
1.4. 34
1.4.1. 34
1.4.2. 38
2. C++ 39
2.1. 39
2.1.1. - 40
2.1.2. 41
2.1.3. 41
2.2. STL 44
2.3. 47
2.3.1. 47
2.3.2. : Inputlterator 48
7
2.4. 49
2.4.1. 49
2.4.2. , 49
2.4.3. 50
2.4.4. 51
2.4.5. 52
2.5. ,. 53
2.5.1. 54
2.5.2. 55
2.6. 56
2.6.1. 56
2.6.2. 56
2.7. 58
3. BGL 59
3.1. 59
3.2. 60
3.2.1. , 61
3.2.2. 61
3.3. 62
3.3.1. 62
3.3.2. 64
3.3.3. 64
3.3.4. 65
3.4. 66
3.5. : 67
3.6. : 69
3.7. 71
3.8. 72
3.9. 74
3.10. 76
4. 77
4.1. 77
4.1.1. 77
4.1.2. 78
4.2. 82
4.2.1. 83
4.2.2. 84
5. 89
5.1. 89
5.2. 90
5.3. -
91
5.4. 95
6. 102
6.1. 102
6.2. 102
6.3. 104
6.4. 106
8
7. 109
7.1. 109
7.2. 110
7.3. - 114
8. 117
8.1. 117
8.2. 118
9. : 124
9.1. 125
9.2. 127
9.3. 128
10. 130
10.1. BGL LEDA 131
10.2. BGL SGB 132
10.3. 134
11. 137
11.1. 137
11.1.1. 138
11.2. 144
I I . 145
12. BGL 146
12.1. 146
12.1.1. 148
12.1.2. Graph 151
12.1.3. IncidenceGraph 152
12.1.4. BidirectionalGraph 153
12.1.5. AdjacencyGraph 154
12.1.6. VertexListGraph 155
12.1.7. EdgeListGraph 156
12.1.8. AdjacencyMatrix 157
12.2. 157
12.2.1. VertexMutableGraph 159
12.2.2. EdgeMutableGraph 160
12.2.3. MutablelncidenceGraph 161
12.2.4. MutableBidirectionalGraph 161
12.2.5. MutableEdgeListGraph 162
12.2.6. PropertyGraph 162
12.2.7. VertexMutablePropertyGraph 163
12.2.8. EdgeMutablePropertyGraph 164
12.3. 164
12.3.1. BFSVisitor 165
12.3.2. DFSVisitor 166
12.3.3. DijkstraVisitor 167
12.3.4. BellmanFordVisitor 168
13. BGL 170
13.1. 170
13.1.1. 171
9
13.2. 172
13.2.1. breadth_first_search 172
13.2.2. breadth_first_visit 176
13.2.3 depth_first_search 177
13.2.4. depth_first_visit 181
13.2.5. topological_sort 182
13.3. 183
13.3.1. drjkstra_shortest_paths 183
13.3.2. bellman_ford_shortest_paths 188
13.3.3. johnson_all_pairs_shortest_paths 191
13.4. 194
13.4.1. kruskal_minimum_spanning_tree 194
13.4.2. prim_minimum_spanning_tree 197
13.5. 200
13.5.1. connected_components 200
13.5.2. strong_components 202
13.6. 205
13.6.1. initialize_incremental_components 207
13.6.2. incremental_components 207
13.6.3. same_component 207
13.6.4. component_index 208
13.7. 209
13.7.1. edmunds_karp_max_flow 209
13.7.2. push_relabel_max_flow 212
14. BGL 215
14.1. 215
14.1.1. adjacencyjist 215
14.1.2. adjacency_matrix 235
14.2. 243
14.2.1. graph_traits 243
14.2.2. adjacency_list_traits 246
14.2.3. adjacency_matrix_traits 247
14.2.4. property_map 248
14.2.5. property 249
14.3. 250
14.3.1. edgejist 250
14.3.2. reverse_graph 252
14.3.3. filtered_graph 256
14.3.4. SGB Graph 261
14.3.5. GRAPH<V,E> LEDA 265
14.3.6. std::vector<EdgeList> : 271
15. 274
15.1. 275
15.1.1. ReadablePropertyMap 276
15.1.2. WritablePropertyMap 277
15.1.3. ReadWritePropertyMap 277
15.1.4. LvaluePropertyMap 278
15.2. 278
15.2.1. propertyJxaits 278
10
, .
, Boost Graph Library (BGL),
. 1984 --
, ,
, . -
, :
,
, . , , -
, ,
. , Standard
Template Library (STL), , -
, - -
, ,
-, .
: - .
STL. std:: 1 i st:: spl i -
:
, , - .
, ,
-
(John Backus's FP system).
, . ,
, - O(log n)
, , -
. , ,
, (. 2.3 ),
(structure types),
(multi-sorted algebras).
, -
,
12
.
, .
, ,
.
, ,
,
.
, ,
. :
Stanford GraphBase [22]
(binary heaps) .
, -
(). -
, BGL (. -
13.4.2). Scheme ,
. -
, : -
:
(define dijkstra
(make-scan-based-algorithm-with-mark
make-heap-with-membership-and-values + < ))
(define prim
(make-scan-based-algorithm-with-mark
make-heap-with-membership-and-values (lambda (x ) ) < ))
-
. C++, -
, . , C++
, . -
C++, STL.
C++,
, ,
.
. , -
.
, , , jo-
. . -
. , -
, . -
, , < , + < b + (
). .
, ,
, , .
, -
, .
, ( :
). , (generics) -
13
, , ,
. C++ ,
.
, ,
C++. , ,
. , --
, .
(. 2.1.3),
( ). -
, , -
: ,
. , , -
. C++ .
, ,
. , reduce :
template <class Inputlterator. class BinaryOperationWithIdentity>
typename iterator_traits<lnputlterator>::value_type
reduce(Inputlterator first. Inputlterator last.
BinaryOperationWithldentity op)
typedef typename iterator_traits<lnputlterator>::value_type T;
if (first == last) return identity_element(op);
T result - *first;
while (++first ! last) result - op(result. *first):
return result;
}
value_type(Inputlterator)
reduce(Inputlterator first. Inputlterator last.
BinaryOperationWithldentity op)
(value_type(Input Iterator) == argument_type(BinaryOperationWithldentity))
{
if (first last) return identity_element(op);
value_type(Inputlterator) result *first;
while (++f1rst !" last) result - op(result, *f1rst);
return result;
}
14
(generic functions) ,
-
.
, -
.
. -
, :
Outputlterator merge(lnputlterator[l] firstl, Inputlterator[l] lastl.
Inputlterator[2] first2, Inputlterator[2] Iast2,
Outputlterator result)
(bool operator<(value_type(lnputlterator[l]). value_type(lnputlterator[2])),
value_type(lnputlterator[l]) == value_type(lnputlterator[2]).
output_type(OutputIterator) value_type(lnputlterator[2]));
, , STL. -
-
. STL , ,
.
, -
.
C++ : -
. -
-
. -
, . -
.
-
, -
. , STL-
,
C++... , -
. ,
, . ,
, -
:
operator* operator++, , -
. !
. , ,
, , -
, , BGL
MTL. -
. , !
.
-, . , 2001'
, ,
.
,
.
, -
, , ,
, WWW, -
, -
, . .
, -
. , -
- -
. ,
, ,
-
, .
. -
, , -
, ?
, -
. -
.
- , , -
. , -
, , -
, .
, ,
, , ,
. ,
-
.
16
, -
.
, ,
. -
, -
,
, . , -
. -
,
.
, -
,
.
(Standard Template Library, STL) [40] -
1994 C++. STL
. STL
, STL- -
: , ,
. . -
, C++ -
. STL -
, ,
. ,
, , -
. , STL
, -
.
-
.
.
, ,
. -
, . -
, , -
.
Boost Graph Library (BGL) ,
-
.
BGL 17
BGL
Boost Graph Library -
(Generic Graph Component Library, GCCL) -
(Lab for Scientific Computing, LSC). -
, -
, ,
1 . -
, -
-
, .
STL, LSC -
. -
(Matrix Template Library, MTL) .
, MTL, -
GGCL.
, -
. LSC
MTL, -
( ) -
. , (LEDA, GTL,
Stanford GraphBase),
STL MTL. , -
LSC .
C++.
AT&T, . -
, , -
.
,
,
1998 . -
GGCL, .
-
. -
GGCL,
GGCL STL.
Boost, ,
,
C++ . Boost , -
,
.
GGCL ,
, Boost Graph Library .
4 2000 GGCL
Boost Graph Library. BGL
27 2000 . BGL . -
-
. Boost
BGL.
Boost?
Boost , -
C++.
,
( ) C++. -
( ) -
. Boost ( -
) ,
Boost-. -
- http://www.boost.org. ,
Boost -
.
BGL
Boost Graph Library -
Boost. Boost -
: http://www.boost.org/boost_all.zip (zip-
Windows), http://www.boost.org/boost_all.tar.gz ( Unix), FTP
ftp://boost.sourceforge.net/pub/boost/release/.
Zip- Boost
WinZip . - Unix
:
gunzip -cd boost_a11.tar.gz | tar xvf -
, boost
: , 1.31.0 boost_l_31_0.
: boost libs. boost
. libs
. -
.
19
, - boost_l_31_0/
index.htm.
BGL boost/graph/. BGL
, -
Boost. libs/
graph/doc/, libs/graph/example/. libs/
graph/test/ BGL.
Graphviz- libs/graph/src/.
, , BGL -
. , , -
Boost . , Windows 2000
1.31.0 ,
Borland, GCC Metrowerks -Ic:/boost_l_31_0 -
, Microsoft Visual C++ /1 ": /boost_l_31_0".
(IDE) : /boost_l_31_0 ( ,
) ,
. BGL- LEDA Stanford GraphBase
. -
read_graphviz() ( Graphviz- AT&T)
boost_l_31_0/libs/graph/src.
Boost Graph Library ISO/IEC Standard C++ -
C++. -
, - Boost -
Compiler Status http://www.boost.org/status/compiler_status.html.
-
BGL. ,
BGL , . -
, -
.
, ( -
STL), -
, .
, -
. ,
(
),
STL.
BGL. BGL
, .
, , BGL
.
20
, , -
-
.
BGL
.
. ,
( ) -
, . , -
BGL -
, .
-
- . , -
BGL,
.
, C++. -
C++ , -
( [42] [25]).
STL (. [34] [3]).
C++, -
BGL .
, -
.
. [10].
-
(literate programming style),
.
. -
. -
, -
.
.
. , , -
, , -
. , -
, , . -
, -
[10]. :
:
< >
template <typename RandomAccessIterator. typename Compare>
void merge_sort(RandomAccessIterator first.
RandomAccessIterator last, Compare cmp)
21
,
. , -
.
< >
RandomAccessIterator mid - f i r s t + (last - first)/?;
merge_sort(first, mid, cmp):
inerge_sort(mid, l a s t , cmp);
std:; inp1acejnerge() -
, :
( )
std::inplace_merge(first, mid, last, cmp):
. , -
.
libs/graph/example/ Boost. -
merge_sort() :
( merge-sort.hpp )
fifndef MERGE_SORT_HPP
#define MERGE_SORT_HPP
< )
#endif // MERGE_SORT_HPP
, -
BGL .
-
-
, BGL.
.
STL -
BGL. -
,
. , -
C++ .
, , , , ,
Boost BGL, -
, -
. BGL,
BGL ( ): , ,
, .
22
: , , -
.
-
, , -
Addison-Wesley
.
BGL NSF ACI-9982205.
BGL , -
(
). -
dot Graphviz.
,
comp@piter.com ( , ).
!
- -
http://www.piter.com.
-
Boost Graph Library (BGL). -
.
-.
, BGL, 1.2. -
, , -
1.3.
1.4 .
1.1.
- . 1.1. -
,
( ).
1.2
. 1.1. -
,
(vertex) ( ), -
1.1. 25
(edge) ( ). G V
, G = (V, ).
( ) |V|,
\ |. , , -
. (, ) , -
V.
(. 1.1) -
:
V" {a,b, c,d,e]
= {(a, b), (a, d), (b, d), (, ), (, ), (d, ), (d, e))
G-(V,E)
,
, . -
(
). ()
{, v), , a v
. (, v) (v, ) .
, -
: (, v) (v, ) . , -
, .
. ,
, , , , -
.
{, v), , v -
. (, v) -
v. {, >) -
( v). . 1.1
:
Adjacent[a] = {b, d)
Adjacent[b] = [d]
Adjacent[c] = {a, e)
Adjacent[d\ ~ {c, e)
Adjacent[e] = {}
:
OutEdges[a] = {(a, b), (a, d)}
OutEdges[b] - {(b, d)}
OutEdges[c] - {(, ), (, )}
OutEdges[d] = {(d, c), {d, e)}
OutEdges[e] = {}
:
InEdges[a] = {(, )}
InEdges[b] = {(a, b)}
InEdges[c] - {(d, )}
InEdges[d] = {(a, d), (b, d)}
InEdges[e] - {(c, e), (d, e)}
26 1
1.2.
,
,
. , -
, , -
, (
),
.
STL
(. ), (con-
cept) . -
(Standard Template Library, STL)
, -
. ,
BGL .
.
. , -
. , BGL,
BGL , -
. 1.3 -
, BGL.
1.2.1.
BGL -
, (vertex descriptors) -
(edge descriptors,).
. ,
, . 1 -
graph_traits.
2.4, graph_traits , -
, 14.2.1.
. -
, , -
. -
, .
2
is_self_1oop(), , :
template <typename Graph>
bool is_self_loop(typename graph_traits<Graph>::edge_descriptor e.
const Graph& g) {
' . . .
2
typename
class.
1.2. 27
1.2.2.
. -
, . 1.1 , -
, . BGL
.
,
: (struct); , -
; - . . -
, , . -
(property map).
, -
- -. -
:
get(p_map. key) - key;
put(p_map. key. value) value -, -
key;
p_map[key] -.
pnnt_vertex_name(),
v namejnap:
template <typename VertexDescriptor. typename VertexNameMap>
void print vertex name(VertexDescriptor v. VertexNameMap namejnap)
std::cout get(namejnap, v):
}
-
pnnt_trans_delay():
template <typename Graph, typename TransDelayMap, typename VertexNameMap>
void printjtransjjelay(typename graph_traits<Graph>::edge_descriptor e.
const Graphs g. TransDelayMap delayjnap. VertexNameMap namejnap)
{
std::cout "trans-delay(" get(name_map. source(e. g)) "."
get(namejnap. target(e. g)) ") = " get (delayjnap. e):
}
print_vertex_name() print_trans_delay() -
.
15,
, .
3.6.
28 1
1.2.3.
(collections): -
, , .
STL, BGL
. , :
1. . -
.
2. .
.
3.
. -
. , , -
.
4. -
v. .
, v -
, v , .
5. , .
.
, , -
graph_traits.
BGL , std:: pai --
: , -
. print_vertex_name(),
, 1.1.
1.1.
template <typename Graph, typename VertexNameMap>
void print_vertex_names(const Graphs g. VertexNameMap namejnap)
{
std::cout "vertices(g) = { ":
typedef typename graph_traits<Graph>: :vertex__iterator iter_t:
for (std::pair<iter_t. iter_t> p - vertices(g):
p.first != p.second: ++p.first) {
print_vertex_name(*p.first, namejnap): std::cout ' ';
}
std::cout "}" std::endl:
}
-, -
(. . 1.1), :
vertices(g) = { a b d e }
print_trans_delay(), ,
, 1.2. -
t i e ( ) ( boost/tupLe/tupLe.hpp)
std:: pai f i rst 1 ast.
1.2. 29
1.2.
template <typename Graph, typename TransDelayMap, typename VertexNameMap>
void prirvt_trans_delays(const Graphs g, TransDelayMap trans_delay_map,
VertexNameMap namejnap)
{
typename graph_traits<Graph>::edge_iterator first, last;
for (tie(first, last) = edges(g); first != last: ++first) {
print_trans_delay(*first, g, trans_delay_map. name_map);
Std: :cout std::endl;
. 1.1 :
trans-delay(a.b) - 1.2
trans-delay(a.d) = 4.5
trans-delay(b.d) = 1.8
trans-delay(c.a) = 2.6
trans-delay(c.e) = 5.2
trans-delay(d.c) = 0.4
trans-delay(d.e) = 3.3
verti ces () edges () out_edges (), i n_edges ()
adjacent_vertices(), -
- .
-
,
.
, .
, -
, .
, .
2.5 .
, ,
. 12-14 -
, , -
. , ,
, , -
.
1.2.4.
BGL -
. -
, , . 1.1.
add_vertex() ,
. add_edge() ,
.
template <typename Graph, typename VertexNameMap. typename TransDelayMap>
void build_router_network(Graph& g, VertexNameMap namejnap,
TransDelayMap delayjnap)
30 1
{
< )
( )
add_vertex() .
:
< )
typename graph_traits<Graph>::vertex_descriptor a. b, . d. e:
= add_vertex(g); name_map[a] = ''
b = add_vertex(g): name_map[b] - 'b'
= add_vertex(g): name_map[c] = 'c'
d - add_vertex(g): name_map[d] - 'd';
e = add_vertex(g); name_map[e] = 'e':
add_edge() std::pai ,
, , ,
( ,
).
1.3.
1.3.
{ )
typename graph_traits<Graph>::edge_descriptor ed:
bool inserted:
1.2.5.
STL -,
. -
std:: sort(), -
:
template <typename RandomAccessIterator. typename BinaryPredicate>
void sort(RandomAccessIterator first, RandomAccessIterator last.
BinaryPredicate compare)
compare - (
). .
.
std:: sort ()
-. :
1.2. 31
struct comparejastjiame {
bool operatorO (const address_info& x. const address_info& y) const {
return x.last_name < y.lastjiame:
}
std:: sort () -
.
std::vector<address_info> addresses:
// ...
compare_last_name compare;
std::sort(addresses.begin(), addresses.end(), compare);
BGL , -
, . -
(algorithm visitors). BGL
. operator^) BGL
, -
( ). -
, BGL ,
[14] ., .,
., . ( ). , -
,
. -
BGL . -
, BGL , .
1.4 -
(. . 1.1) breadth_f i rst_sea rch ()
. -
(. 4.1.1, -
). ,
BFSVisitor.
1.4.
template <typename VertexNameMap>
class bfs_name_printer : public default_bfs_visitor {
// ()
public;
bfs_name_printer(VertexNameMap njnap) : m_name_map(n_map) { }
template <typename Vertex, typename Graph>
void discover_vertex(Vertex u. const Graphs ) const {
std::cout get(m_name_map, u) ' ':
private:
VertexNameMap mjiamejnap;
}'
- bf s_name_pri nter
breadth_first_search(). visitorO, , -
, 2.7.
bfs_name_printer<VertexNameMap> vis(name_map);
std::cout " : ";
breadth_first_search(g. a, v i s i t o r ( v i s ) ) :
std::cout std::end!:
32 1
:
: a b d
. 1.2. .
S -v
)
. 1.2.
1.3.
, BGL, .
, ,
(adapters),
(views) BGL , .
1.3.1.
BGL :
adjacency_l i st adjacencyjnetri x.
adjacencyjist.
.
,
. -
. adjacency_list
: EdgeList, VertexList, Directed, VertexProperties,
EdgeProperties GraphProperties.
EdgeLi st VertexLi st , -
.
/, -
. , EdgeList
, .
Di rected , , -
(bidirectional graph).
, .
VertexProperti es, EdgeProperti es GraphProperti es ,
, .
1.3. 33
1.3.2.
BGL .
BGL- .
:
reverse_graph ,
, -
;
filtered_graph , , -
- ,
.
BGL , -
BGL. -
(overloaded functions). -
.
edge_l i st , BGL- -
.
Stanford GraphBase -
boost/graph/stanford_graph.hpp.
GraphBase- Graph* BGL.
LEDA ( - ) -
. GRAPH<vtype, etype> LEDA -
BGL -
boost/graph/leda_graph.hpp.
std:: vector<std:: 1 i st<i n t STL -
boost/graph/vector_as_graph.hpp.
BGL -
12. ( ) -
. adjacencyj ist ()
BGL-, BGL-
.
2 . 375
34 1
1.4.
BGL . ;
, , -
. -
topological_sort() ,
depthjfi rst_search()
topol ogical_sort().
1.4.1.
, (, v),
v . topological_sort()
: . -
.
, , -
. . 1.3 , -
, (-
, ).
2:
6:
*- - ^
. 1.3.
1.4. 35
, -
. -
BGL .
, -
std:: vector< std:: 1 i st<i nt> >. -
1.5.
1.5. . topo-sortl.cpp
< topo-sortl.cpp ) =
#include <deque> //
#include <vector>
#include < l i s t >
#include <iostream>
#incl ude <boost/graph/vector_as_graph.hpp>
#i ncl ude <boost/graph/topologi cal_sort.hpp>
int mainO
{
using namespace boost;
( >
< >
< >
return EXIT_SUCCESS:
}
,
. 1.6 -
.
1.6.
< )
const char* tasks[ ] = {
" ",
" ",
" ",
" ",
" ",
" ",
" ".
}:
const int n_tasks - sizeof (tasks) / sizeof (char*):
. -
. , -
. ,
. (, v)
v . -
. 1.4.
boost/graph/vector_as_graph.hpp -
BGL- VertexLi stGraph, ,
topological_sort. 1.7
36 1
. 1.4.
1.7.
( > =
std::vector< std::list<int> > g(n_tasks):
g[0].push_back(3);
g[l].push_back(3);
g[l].push_back(4):
g[2].push_back(l);
g[3].push_back(5):
g[4].push_back(6);
g[5].push_back(6);
topol ogi cal _sort (), -
. BGL- -
( -
). -
, . ( 1.8) -
std:: dequeue ,
std::dequeue ,
. , topol ogical_sort() -
: 1) ;
2) ,
-
. ,
i denti ty_property_map -
. vertex_index_map() -
(. 2.7).
1.8.
1.8.
( ) =
std::deque<int> topo_order:
topological_sort(g.
std::front_inserter(topo_order).
vertex_index_map(identity_property_map())):
int n = 1:
1.4. 37
adjacencyjist
topological_sort() -
: adjacencyjist.
topol ogical _sort () , -
. , , -
, .
ad jacency_l i st . -
l i s t s , std: : l i s t -
. vecS , std:: vector -
. ad jacencyj i st
, .
( )
adjacency lisMistS, vecS. directedS> g(n_tasks):
add_edge()
adjacencyjist ( , EdgeMu-
tableGraph). std::vector ,
adjacency_1ist .
.
(
add edge(0, 3, 9):
add" [edged. 3. 9):
add "edged. 4. g):
add "edge(2. 1, g);
add' ~edge(3, 5, g);
add "edge(4, 6, g):
add" edge(5. 6. g):
(. 1.5),
, vector_as_graph.hpp
adjacency_list.hpp. 1.9 -
( ).
1.9. . topo-sort2.cpp
< topo-sort2.cpp 18 > =
#1 ncl ude <vector> *
38 1
1.9 {)
#iinclude <deque>
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/adjacency_li st.hpp>
int mainO
{
using namespace boost;
< )
( )
( )
< )
return EXIT_SUCCESS;
}
1.4.2.
topol ogi cal_sort() BGL ,
depthjfi rst_search(),
. depth_fi rst_search()
, ,
. , -
,
3.3.
1.10 , -
. ,
, -
.
1.10.
template <typename Outputlterator>
class topo_sort_visitor : public default_dfs_visitor {
// ()
public:
topo_sort_visitor(OutputIterator iter) : m_iter(iter) { }
template <typename Vertex, typename Graph>
void finish_vertex(Vertex u. const Graph&) { *m_iter++ = u; }
private:
Outputlterator m_iter;
}:
, topological_sort()
depth_first_search() topo_sort_visitor() .
template <typename Graph, typename Outputlterator>
void topological_sort(Graph& g. Outputlterator result_iter) {
topo_sort_visitor<OutputIterator> vis(resultjter);
depth_first_search(g. visitor(vis));
C++
2.1.
(, generic programming, GP)
,
. C++
(templates) . -
BGL. BGL -
.
( , C++)
.
C++, .
:
C++.
. -
.
, - -
( ) [30].
(push) (pop) . -
: , ,
,
.
-
. -
,
(semantic behavior). ,
, (concept). ,
40 2 C++
, , -
.
( ) .
2.1.1. -
- () -
, -
.
. -
(assertions), .
. , ( -
) . -
,
( C++ ).
.
, -
[45]. C++,
:
// AdditiveAbelianGroup
class AdditiveAbelianGroup {
public:
virtual void add(AdditiveAbelianGroup* y) = 0:
virtual AdditiveAbelianGroup* inverseO = 0;
virtual AdditiveAbelianGroup* zeroO = 0;
}:
, -
sum():
AdditiveAbelianGroup* sum(array<AdditiveAbelianGroup*> v)
AdditiveAbelianGroup* t o t a l = v [ 0 ] - > z e r o ( ) :
f o r ( i n t i - 0; i < v . s i z e O ; + + i )
total->add(v[i]);
return t o t a l :
}
sum() , -
AdditiveAbelianGroup.
.
class Real : public AdditiveAbelianGroup {
// ...
}:
class Vector : public AdditiveAbelianGroup {
2.1. 41
2.1.2.
-
. .
sum() . Additive-
Abel i anGroup , -
.
template <typename AdditiveAbelianGroup>
AdditiveAbelianGroup sum(array<AdditiveAbelianGroup> v)
I
AdditiveAbelianGroup total = v[0].zero():
for (int i = 0: i < v.sizeO; ++i)
total.add(v[i]):
return total;
j
C++ , -
.
,
.
, -
STL (. -
). AdditiveAbelianGroup, -
AdditiveAbelianGroup.
// AdditiveAbelianGroup ( )
// :
// x.add(y) //
// = x.inverseO //
// = x.zeroO //
// :
, AdditiveAbelianGroup, -
.
( -
). -
, , . , Real
Vector AdditiveAbelianGroup.
struct Real { //
I.
struct Vector { //
// ...
}:
2.1.3.
-
, - -
. ,
.
42 2 C++
, -
, - -
. C++
,
, (signatures extension) GNU C++ [4] -
. (
, CLOS [21]) , -
, ( -
).
, C++, -
, ( ) -
C++.
, -
( , ),
-
.
,
: , -
( , inline-) -
.
, -
.
STL BGL -
. -
, operator++() .
-
, , , ,
STL BGL.
, , --
. -
,
,
. -
.
,
. ,
, -
, , -
(
).
2.1. 43
: ,
-
- . -
, .
,
, , . -
std:: 1 i st STL SGI.
( ) -
, ,
. [8]. -
( )
Point (, ), -
equal . BGL,
BGL ( , )
operator==() equal () Point.
Point.
class Point {
public:
virtual bool equal(const Point* p) const = 0:
}:
, -
, Point
, .
void print_equal(const Point* a, const Point* b) {
std::cout std::boolalpha a->equal(b) std::endl:
}
, ,
ColorPoint. , -
, . -
Col orPoi nt, -
.
class ColorPoint : public Point {
public:
ColorPoint (float x. floaty, std: :string c) : x(x), y(y). color(c) { }
virtual bool equal(const ColorPoint* p) const
{ return color == p->color && x == p->x && == p->y: }
protected:
f l o a t x. y:
std: :string color:
}:
, Col orPoi nt:: equal ()
Poi nt:: equal (). Col orPoi nt -
:
44 2 C++
2.2. STL
STL (-
, , ).
2.2. STL 45
, -
. STL -
( ).
[35] , -
:
1. .
2. ( -
, -
).
3. () , -
.
4. (framework), -
.
STL.
-
, ,
. , -
, .
. -
,
( STL ).
STL
( ).
. -
-
, . -
,
.
(), : Input-
Iterator, Forwardlterator, RandomAccesslterator. ,
. 2.1.
. 2.1.
STL .
, ( )
.
STL (
Forwardlterator), . 2.1. X
46 2 C++
, , U . , , -
, m , .
2.1. STL
== b bool * *
!= b bool !( == )
<b bool b - >
* T&
a->m U& (*).
++r X& == s ++r == ++s
-- X& == s -- == s
+= n x& , ++
a +n X {tmp = a: return tmp += n;}
b - a ( < b) ? distance(a. b) : -
d i s t a n c e d , a)
a[n] T *(a + n)
accumul ate(), -
-
. , -
. ,
accumulate() C++. first last , -
.
, , -
, Inputlterator. -
, ;
, operator++() -
. . 2.1
( ).
, , -
operator*() operator!]
.
template <typename Inputlterator. typename T. typename BinaryOperator>
T accumulatednputlterator first. Inputlterator last, T init.
BinaryOperator binary_cp)
{
for (; first !- last; ++first)
init = binary_op(init. *first);
return init;
}
, ,
accumul ate() (
STL) ( 2.1).
2.3. 47
2.1.
// accumulateO
std::vector<double> x(10. 1.0):
double suml:
suml - std::accumulate(x.begin(), x.endO, 0.0, std: :plus<double>()):
// accumulateO
std::list<double> :
double sum2;
//
std: :copy(x.begin(), x.endO, std::back inserter(y)):
sum2 = std: :accumulate(y .beginO , y.endO, 0.0, std: :plus<double>()):
assert(suml ~ sum2): //
2.3.
RandomAccessIterator.
, Inputlterator
accumul ate() std:: 1 i st:: i terator
std:: vector:: iterator.
, .
-
, -
, . -
, .
C++.
. , ,
STL, -
STL (. ) -,
SGI STL http://www.sgi.com/tech/stl/. -
BGL.
2.3.1.
, -
, . ,
. -
. (refinement) .
C++,
, ,
.
, -
, .
. ,
val ue_type -
, , -
, . C++
(traits class) .
48 2 C++
, -
.
- . ,
.
BGL. -
, -
. BGL, , -
.
,
.
2.3.2. : Inputlterator
Inputlterator . -,
Inputlterator Tri vi a lite rat , , -
Assignable EqualityComparable. , Inputlterator -
TriviaLIterator (
Assignable EqualityComparable).
, Inputlterator, -
, , , -
- == !".
Inputlterator -
. -
. i j , Input-
Iterator.
i -j // ( Assignable)
i(j): // ( Assignable)
1 j // ( EqualityComparable)
i !- j // ( EqualityComparable)
*i // ( Trivial Iterator)
++i //
i++ //
std: :iterator_traits
. , ( X),
value_type . -
reference, pointer, difference_type iterator_category.
-
2.4. iterator_traits
va I ue_type :
template <typename Iterator void dereference_example(Iterator i)
i
typename iterator_traits<Iterator>::value_type t:
t - *i;
, Inputlterator -
. , Inputlterator, -
std: :1 ist<int>::iterator, double* std: :istream iterator<char>.
2.4. 49
. std: :for_each().
- fI rst 1 ast
: operator !=(), operator++(), -
operator*() . -
, -
. Inputlterator -
( ),
for_each().
template <typename Inputlterator. typename Function>
Function for_each(Inputlterator first. Inputlterator last, Function f )
{
for ( ; first !- last: ++first)
f (*first):
return f ;
2.4.
, -
, (traits class), -
[36]. -
(- ), .
, -
STL BGL.
2.4.1.
,
. , sum():
template <typename Array>
X sum(const Array& v. int n)
X total - 0:
for (int i = 0: i < n; ++i)
total += v[i];
return total;
}
Array -
. , .
total,
, Array. X , -
- , sum().
2.4.2. ,
, -
:: typedef,
. , :
50 2 C++
class my_array {
public:
typedef double value_type: //
doubles operator[ ] ( i n t i ) {
return m_data[i]:
}:
private:
double* m_data;
}:
array: :value_type.
sum()
(, X typename Array:: val ue_type'):
template <typename Array>
typename Array::value_type sum(const ArrayS v, int n)
{
typename Array::value_type total = 0:
for (int i - 0: i < n: ++i)
total +- v[i];
return total:
sum() typedef , Array
, typedef. ,
typedef . -
, sum() -
, typedef. -
sum() doubl e*:
int n = 100:
double* = new double[n]:
sum(x, n);
,
: operator[]() double*, -
.
,
, sum().
2.4.3.
, -
, -
, . C++,
, (template specialization).
-
-. , , a rray_traits
sum().
a rray_t raits Array -
value_type ( ) . ( -
:: -
, typename .
2.4. 51
) , typedef,
my_array:
template <typename Array>
struct array_traits {
typedef typename Array::value_type value_type;
}:
array_traits
, , doubl e*:
template <> struct array_traits<dout>1e*> {
typedef double value_type;
};
, , johns_int_array, -
:
template <> struct array_traits<johns_int_array> {
typedef int value_type:
}:
sum(), array_traits,
. total,
va1ue_type array_traits.
template <typename Array>
typename array_traits<Array>::value_type sum(const Array& v, Int n)
{
typename array_traits<Array>::value_type total = 0;
for (int i - 0; i < n; ++i)
total += v [ i ] ;
return total;
2.4.4.
. ,
array_traits . C++ -
, traits_cl ass, -
* .
* -. doubl e*
( 2.4.3.)
-.
template <typename T>
struct array_traits<T*> {
typedef T value_type:
-
array_traits .
template <typename T>
struct array_traits<johns_array<T> > {
typedef T value_type:
}
52 2 C++
itera-
tor_trai ts STL. BGL ,
graph_traits property_traits.
. iterator_traits -
, graph_traits -
BGL.
2.4.5.
(tag dispatching) ,
. -
, . -
std: :advance() STL, -
.
. -
, advance() -
i += . , -
,
.
, ,
( iterator_category), trai ts_cl ass.
( 2.2) advance() itera-
t o r ^ its iterator_category. -
advancejji spatch(). advance_di spatch() -
, (
) iterator_category. , -
-
. ,
_tag. forwardj terator_tag,
input_iterator_tag.
2.2.
struct inputjterator_tag {};
struct output_iterator_tag {}:
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
template <typename Inputlterator, typename Distance>
void advance_dispatch(lnputlterator& i. Distance n, input_iterator_tag)
{ while (n--) ++i; }
template <typename Bidirectionallterator. typename Distance>
void advance_dispatch(BidirectionalIterators i, Distance n,
bidirectional_iterator_tag)
{
if (n >= 0)
while (n--) ++i:
else
while (n++) - - i ;
2.5. 53
2.5.
-
. ,
, -
. ,
, -
[2,41]. -
, .
, 2.3, , std:: sort
. operator<()
foo , foo LessThanComparable (
std: :sort()).
2.3. :
#include <algorithm>
class foo { }:
int maindnt. char*[ ])
{
foo array_of_foo[10]:
std::sort(array_of_foo, array_of_foo + 10):
return 0;
, , , , -
C++,
, -
. LessThanComparable, -
, ,
std:: sort (). , , -
. std:: sort (). :
stljieap.h: In function void adjust_heap<foo*.int.foo>(foo*.int.int.foo):
stl_heap.h:214: instantiated from makejieap<foo*.foo.ptrdiff_t>(foo*.
foo*.foo*.ptrdiff_t*)
stl_heap.h:225: instantiated from make_heap<foo*>(foo*,foo*)
stl_algo.h:1562: instantiated from partial_sort<foo*,foo>(foo*,foo*,
foo*,foo*)
54 2 C++
2.5.1.
C++ -
, -
[39].
BCCL (Boost Concept Checking Library) [6]. BCCL -
, , ,
LessThanComparable. -
- constraintsO.
template <typename T>
struct LessThanComparableConcept {
void constraintsO {
(bool)(a < b):
}
T a, b;
}:
-
functi on_requi res () BCCL -
.
linclude <boost/concept_check.hpp>
template <typename Iterator
void safe_sort(Iterator first. Iterator last)
typedef typename std::iterator_traits<Iterator>::value_type T:
function_requires< LessThanComparableConcept<T> >();
// ...
std::sort(first. last);
}
, safe_sort() , -
( ) : ,
, ,
.
boost/concept_check.hpp: In method
void boost::LessThanComparableConcept<foo>::constraintsO:
boost/concept_check.hpp:31: instantiated from
boost::function_requires<boost::LessThanComparableConcept<foo> >()
sort_eg.cpp:ll: instantiated from safe_sort<foo*>(foo*. foo*)
sort_eg.cpp:21: instantiated from here
boost/concept_check.hpp:260: no match for foo & < foo &
BGL -
.
,
boost/graph/graph_concepts.hpp. -
BGL .
graph_concepts.hpp, , , -
2.5. 55
-
.
2.5.2.
,
-
. (concept covering).
, -
. C++, -
C++ [39] .
BCCL. BCCL -
, . - -
. ,
, - .
, -
std: :sort() , RandomAccessIterator,
LessThanComparable.
#include <algorithm>
#include <boost/concept_archetype.hpp>
int mainO
using namespace boost;
typedef less_than_comparable_archetype<> T;
random_access_iterator_archetype<T> r i :
std::sort(ri. r i ) ;
}
,
std:: sort .
, ,
CopyConstructible.
nul1_archetype(const null_archetype<int> &) is private
, -
. Assignable.
- Assignable. -
Base , -
. std:: sort - -
Assignable LessThanComparable.
template <typename Base = nu11_archetype<> >
class assignable_archetype : public Base {
typedef assignable_archetype self :
public:
assignable_archetype(const self &) { }
self & operator=(const self &) { return *this; }
}:
BGL -
boost/graph/graph_archetypes.hpp. -
BGL -
libs/graph/test/.
56 2 C++
2.6.
Boost-, Boost Graph Library -
boost , -
. ,
BGL boost.
2.6.1.
BGL .
adjacencyl ist boost.
{ //
boost::adjacency_list<> g:
)
{ // using
using boost::adjacencyjist:
adjacency_list<> g;
}
{ // Boost
using namespace boost;
adjacency_list<> g:
}
-
boost::, using namespace boost;
. , Boost--
, boost::
, using
. using namespace
boost:, . -
( ) -
usi ng namespace .
2.6.2.
BGL ,
. , num_vertices() , -
, .
BGL. (, , ),
.
, , C++ -
^.
.
, -
. ,
numj/erti ces().
namespace libjack {
class graph { /* ... */ }:
int num_vertices(const graphS) { / * . . . * / }
}
namespace l i b j i l l {
class graph { /* ... */ };
int num_vertices(const graphS) { / * . . . * / }
}
, -
, , boost::pail (), .
int maint)
libjack::graph gl;
boost::pail(gl):
libjill: :graph g2;
boost::pail(g2):
}
boost: :pai 1 () num_vertices().
, 1 i bjack
l i b j a c k : :num_vertices(), l i b j i l 1 1 ib_ji 11: :num_vertices().
, C++, . -
, C++ -
.
namespace boost {
template <typename Graph>
void pail(Graphs g)
{
typename graph_traits<Graph>::vertices_size_type
N = num_vertices(g); //
// ...
}
} // namespace boost
BGL ,
, .
BGL
boost:: -
, 2.6.1. , -
breadth_first_search() boost:: :
boost::breadth_first_search(g, start, visitor(vis)):
58 2 C++
2.7.
BGL ,
.
-
. , , -
.
template <typename X, typename Y, typename Z>
void f (X x. Y y. Z z);
( ), -
. ,
z. -
, (named
parameters keyword parameters).
-
( ), , .
// C++ , :
int ;
int b:
f (z=b. x=a): // b z.
//
, C++ ,
. BGL
, bgl_named_params,
, 1. -
bel lman_ford_shortest_path() -
. ,
, . , -
, . bg1_named_params
weight_map(), -
di stance_map() predecessorjnapC).
bool = boost::bellman_ford_shortest_paths(g, int(N).
boost::weight_map(weight).
distance_map(&distance[O]).
predecessor_map(Sparent[0])):
, , ,
. bel lman_-
ford_shortest_paths() .
bool = boost::bellman_ford_shortest_paths(g, int(N).
boost::predecessor_map(&parent[0]).
distance_map(&distance[O]).
weightjnap(weight));
, [41].
BGL
, -
. , -
.
(BGL, Boost Graph Library) ,
,
. -
,
BGL.
(. 2 )
, ,
.
. -
, . -
-
.
, ,
.
3.1.
. -
-
. -
, make, Visual C++
, -
(, , )
.
. 3.1 , -
, ,
60 3 BGL
. , -
(, -
, ).
. . 1 .
, , -
make, .
:
1. , -
?
2. ? -
,
.
3. ? -
, -
(, -
)?
-
. , . 3.1.
.
3.2.
,
.
, BGL, .
3.2. 61
3.2.1. ,
BGL , .
BGL -
,
BGL. BGL adjacencyjist adjacencyjnatrix.
, -
.
, . adjacen-
cyjnatrix ,
.
adjacencyj i st, -
adjacencyjnatrix,
adjacencyj i st.
adjacencyjist, 1.4.1.
typedef adjacencyj ist<
lists. // std::list
vecS. // std::vector
directedS //
> file_dep_graph;
3.2.2.
1.2.4 , add_vertex()
add_edge() . -
,
. adjacencyjist ,
, .
Inputlterator, std: :pai (i,j),
. i j ,
< < \V| 0 <)' < \V\.
. , -
. -
. , , -
:
template <typename Edgelterator>
adjacencyjistCEdgelterator first, Edgelterator last.
vertices_size_type n = 0. edges_size_type m = 0.
const GraphPropertiesS p = GraphPropertiesO)
-
. std:: i streamj terator , -
. ,
, . (-
) . -
std: :i streamj terator .
std::ifstream file_in("makefile-dependencies.dat"):
typedef graph_traits<file_dep_graph>::vertices_size_type size_type;
size_type n_vertices;
62 3 BGL
f i l e j i n n_vertices: //
std::istream_iterator<std::pair<size_type. size_type> >
input_begin(file_in), input_end:
fi1e_dep_graph g(input_begin, input_end, n_vertices);
std: :istream_iterator std: :pair,
std:: pai .
namespace std {
template <typename T>
std: :istream& operator(std: :istream& in. std: :pair<T.T>& p) {
in p.first p.second:
return in:
3.3.
. -
,
, . , -
1.4.1, .
3.3.1.
1.4.2,
(depth-first search). -
, ,
, . -
. , -
( ). -
,
, , .
, ,
, . , -
, ,
.
.
, .
.
-
. -
, .
-
. , -
, , -
.
, . , -
, . -
. 3.2.
3.3. 63
, -
( ).
ig.cpp 27/28) (boz.h 17/20)ftag.cpp29 .h 15/1?)(dax.h 1/1(. 23/24) (zow.h 21/22) (foo.cpp 25/26
. 3.2.
-
, -
, (, v). -
v . ,
( ).
1. v . , v -
, ,
, .
2. v . ,
> , .
3. v .
, -
. , , -
.
,
.
topo_sort_dfs(), , -
. -
, . -
, .
topo_sort_dfs() , , -
,
. topo_order -
.
64 3 BGL
, topoorder , -
( topo_order -
, ).
3.1 topo_sort_dfs().
3.1. topo_sort_dfs()
void topo_sort_dfs(const file_dep_graph& g. vertex_t u.
vertex_t*& topo_order. int* mark)
{
mark[u] = 1 ; // 1 "", 0 - " "
( )
*--topo_order - ;
}
vertex_t edge_t -
f i I e_dep_graph.
typedef graph_traits<file_dep_graph>::vertex_descriptor vertex_t;
typedef graph_traits<file_dep_graph>::edge_descriptor edge_t:
3.3.2.
.
,
.
(external property storage)
-,
. ,
, .
,
. adjacencyj ist,
num_vertices(g) - 1. -
.
3.3.3.
topo_sort_dfs() ,
. AdjacencyGraph -
. adjacent_vertices()
, .
,
.
. ad j acency_i terator -
graph_traits. adjacency_1ist ( 14.1.1) ,
ad jacencyj i st AdjacencyGraph, ,
adjacent_verti ces()
. topo_sort_dfs() :
< > =
graph_traits<file_dep_graph>::adjacency_iterator vi, vi_end:
3.3. 65
3.3.4.
,
( ,
), topo_sort_dfs() .
VertexListGraph.
verti ces () -
.
topo_sort() ( 3.2).
3.2. topo_sort()
void topo_sort(const file_dep_graph& g, vertex_t* topo_order)
std::vector<int> mark(num_vertices(g). 0);
graph_traits<file_dep_graph>::vertex_iterator vi. vi_end:
for (tie(vi. vi_end) = vertices(g): vi != vi_end; ++vi)
if (mark[*vi] == 0)
topo_sort_dfs(g. *vi. topo_order, &mark[0]):
-
. ( , -
) ,
, .
std::vector<std::string> name(num_vertices(g));
std: Mfstream name_in("makefile-target-names.dat"):
graph_traits<file_dep_graph>::vertex_iterator vi. vi_end:
for (tieCvi, vi_end) = vertices(g): vi !- vi_end: ++vi)
name_in name[*vi]:
order -
.
std::vector<vertex_t> order(num_vertices(g)):
topo_sort(g. &order[0] + num_vertices(g)):
for (int i = 0: i < num_vertices(g): ++1)
std::cout name[order[i]] std::endl:
:
zag.cpp
zig.cpp
foo.cpp
bar.cpp
zow.h
boz.h
zig.o
vow h
dax.h
zag.o
foo.o
bar.o
. 375
66 3 BGL
libfoobar.a
libzigzag.a
killerapp
3.4.
, -
. 3.3.1, -
. Makefile -
,
.
.
,
, , . -
. ,
.
, :
, ,
( ), ,
.
, boost/graph/properties.hpp
:
enum default_color_type { white_color, gray_color. black_color };
,
. 3.3
, .
3.3.
bool has_cycle_dfs(const fi1e_dep_graph& g. vertex_t u.
default_color_type* color)
{
color[u] = gray_color;
graph_traits<file_dep_graph>::adjacency_iterator vi, vi_end;
for (tie(vi. vi_end) = adjacent_vertices(u, g): vi != vi_end: ++vi)
i f (color[*vi] == white_color)
if (has_cycle_dfs(g, *vi, color))
return true: // ,
else i f ( c o l o r [ * v i ] gray_color) // *vi
return true:
color[u] = black_color:
return false:
}
, has_cycle() ( 3.4) -
,
.
3.4. has_cycle()
bool has_cycle(const file_dep_graph& g)
{
std: :vector<default_color_type> color(num_vertices(g). white_color):
3.5. : 67
graph_traits<file_dep_graph>::vertex_iterator v i , vi_end:
for ( t i e ( v i , vi_end) = vertices(g): vi != vi_end: ++vi)
i f (color[*vi] white_color)
i f (has_cycle_dfs(g. *vi, &color[0]))
return false:
3.5.
:
: topo_sort() has_cycle(),
, --
. -
. , -
, topo_sort() has_cycl e()
.
STL ,
.
STL -
-. -
, topo_sort() has_cycl () -.
, , -
STL. , , -
. , topo_sort() -
topo_sort_df s (),
has_cycle() .
-
. operatorO
- ,
( ). - -
(algorithm visitor). -
: discover_vertex(), tree_edge(), back_edge(),
forward_or_cross_edge() f i ni sh_vertex(). -
. -
, .
, 3.5, .
3.5.
template <typename Visitor>
void dfs_vl(const file_dep_graph& g, vertex_t u.
default_color_type* color. Visitor vis)
{
color[u] = gray_color:
vis.discover_vertex(u, g):
graph_traits<file_dep_graph>::out_edge_iterator ei. ei_end:
for (tie(ei. ei_end) = out_edges(u. g); ei != ei_end; ++ei) {
i f (color[target(*ei . g)] == white_color) {
vis.tree edge(*ei. g):
>
68 3 BGL
3.5 {)
dfs_vl(g. target(*ei. g). color, vis);
} else i f (color[target(*ei, g)] == gray_color)
vis.back_edge(*ei. g);
else
vis.forward_or_cross_edge(*ei. g):
}
color[u] = black_color;
vis.finish_vertex(u. g):
}
- -
, , , , , -
- . -
,
, ( 3.6).
..
struct default_dfs_visitor {
//
template <typename V. typename G>
void discover_vertex(V. const G&) { }
//
template <typename E, typename G>
void tree__edge(E. const G&) { }
//
template <typename E. typename G>
void back__edge(E, const G&) { }
//
template <typename E. typename G>
void forward_or_cross_edge(E. const G&) { }
//
template <typename V. typename G>
void f i n i s h vertex(V. const G&) { }
, -
, topo_sort() has_cyc1e(). --
, , -
.
:
struct topo_visitor : public default_dfs_visitor {
topo_visitor(vertex_t*& order) : topo_order(order) { }
void finish_vertex(vertex_t u. const file_dep_graph&) {
3.6. : 69
*--topo_order - ;
vertex_t*& topo_order;
}:
topo_sort () -
. --
, .
void topo_sort(const file_dep_graph& g, vertex_t* topo_order)
{
topo_visitor vis(topo_order);
generic_dfs_vl(g, vis);
}
has_cycl e() , -
, (back edge).
struct cycle_detector : public default_dfs_visitor {
cycle_detector(bool& cycle) : has_cycle(cycle) { }
void back_edge(edge t. const file_dep_graph&) {
has_cycle = true:
}
bool& has_cycle;
has_cycl e()
.
bool has_cycle(const file_dep graphs g)
{
bool has_cycle - false:
cycle_detector vis(has_cycle);
generic_dfs_vl(g. vis);
return
} "as.cycle:
3.6. :
,
. -
, .
, , -
(
).
- .
adjacencyj i st VertexProperti es EdgeProperti es,
()
. property<Tag, T>
. Tag , , -
. (. -
15.2.3), vertex_name_t edge_weight_t. , -
std:: stri ng -:
property<vertex name t. std::string>
- -
70 3 BGL
, .
- vertex_xxx_t edge_xxx_t,
( _t).
B0OST_INSTALL_PROPERTY property_ki nd propertyjium1.
(cost),
.
namespace boost {
enum vertex_compile_cost_t { vertex_compile_cost =111 }; //
BOOST_INSTALL_PROPERTY(vertex. compile_cost):
)
property . -
property -
. 3.7 typedef ,
.
3.7.
typedef adjacency_list<
lists, // std::list
lists, // std::list
directedS. //
//
property<vertex_name_t, std::string,
property<vertex_compi1e_cost_t, f1 oat,
property<vertex_di stance_t. f1 oat.
property<vertex_color_t, default_color_type> > > >,
//
property<edge_weight_t, float>
> file_dep_graph2;
adjacencyjlist vecS l i s t s .
. -
( vecS -
). ,
,
. ,
.
1.2.2 . ,
,
(, ) (, ).
adjacency_l i st ( ), -
PropertyGraph.
: .
propertyjnap .
typedef property_map<file_dep_graph2, vertex_name_t>::type name_map_t:
typedef property_map<file_dep_graph2. vertex_compi ie_cost_t>::type
compi1e_cost_map_t;
typedef propertyjnap'<file_dep_graph2. vertex_distance_t>: :type
, C++
.
3.7. 71
distance_map_t;
typedef property_map<file_dep_graph2, vertex_color_t>::type color_map_t:
get () :
name_map_t namejiap = get(vertex_name, g);
compi 1 e_cost_map_t compile_cost_map = get(vertex_compile_cost. g);
distance_map_t distance_map - get(vertex_distance, g):
color_map_t color_map - get(vertex_color. g);
make-
. std:: i fstream,
namejnap com-
pi 1e_cost_map. LvaluePropertyMap, ,
operator^ ] (), -
.
std: :ifstream name_inCmakefi1e-target-names.dat"):
std: :ifstream compile_cost_inCtarget-compile-costs.dat");
graph_traits<file_dep_graph2>::vertex_iterator vi, vi_end:
for (tie(vi, vi_end) = vertices(g): vi != vi_end; ++vi) {
name_in name_map[*vi]:
compile_cost_in compi1e_cost_map[*vi];
}
-
, -
.
3.7.
, :
? -
? . -
.
std: accumulate. , -
, .
,
.
graph_property_i ter_range (. 16.8) .
graph_property_iter_range<file_dep_graph2.
vertex_compile_cost_t>::iterator ci. ci_end;
tie(ci. ci_end) = get_property_iter_range(g. vertex_compi1e_cost):
std::cout " : "
std::accumulate(ci. ci_end. 0.0) std::endl:
:
: 21.3
, -
. -
. -
?
. , -
.
72 3 BGL
libfoobar.a
. 3.3. libfoobar.a. -
, , bar.o foo.o.
libfoobar.a
bar.o foo.o .
, , (distance)
, : ? -
, (, ), , -
v, v -
. .
kilerapp
- '
. 3.3. libfoobar.a
3.8.
f 11 e_dep_graph f 11 e_dep_graph2, -
topo_sort(), 3.4. -
, col or, -
neric_dfs_vl(), ,
( fi!e_dep_graph2).
. topo_sort () :
1. file_dep_graph .
,
, . -
BGL. topo_sort() -
, VertexListGraph IncidenceGraph.
3.8. 73
2. vertex_t* -
. -
, STL.
.
3. .
. topo_sort()
-
. Boost Property Map Library (. 15) -
. -
LvaluePropertyMap. -
coiorjnap, 3.6,
LvaluePropertyMap, 3.3.4.
boost/property_map.hpp,
LvaluePropertyMap.
3.8 t o p o s o r t O .
topo_vi si tor Hgeneric_dfs_v2().
3.8. topo_sort(),
template <typename Graph, typename Outputlterator, typename ColorMap>
void topo_sort(const Graphs g, Outputlterator topo_order, ColorMap color)
{
topo_visitor<OutputIterator> vis(topo_order);
generic_dfs_v2(g. vis, color);
}
topo_vi si tor ( 3.9), -
.
( ).
, topo_sort(), -
, , .
3.9. topo_visitor
template <typename Outputlterator>
struct topo_visitor : public default_dfs_visitor {
topo_visitor(OutputIteratorS order) : topo_order(order) { }
template <typename Graph>
void finish_vertex(typename graph_traits<Graph>::vertex_descriptor u.
const GraphS)
{ *topo_order++ = u; }
Outputlterator& topo_order;
}:
( 3.10) -
. ,
( property t r a i t s ) ColorMap.
, white_color,
, color_traits.
3.10. generic_dfs_v2()
template <typename Graph, typename Visitor, typename ColorMap>
void generic dfs v2(const Graphs g, Visitor vis. ColorMap color)
&
74 3 BGL
3.10 {)
{
typedef color_traits<typename
property_tits<ColorMap>::value_type> ColorT;
typename graph_traits<Graph>::vertex_iterator v i , vi_end:
for ( t i e ( v i . vi_end) = vertices(g): vi != vi_end: ++vi)
c o l o r [ * v i ] = ColorT::white();
for ( t i e ( v i , vi_end) = vertices(g); vi != vi_end; ++vi)
i f ( c o l o r [ * v i ] == ColorT::white())
dfs_v2(g. * v i . color, v i s ) ;
}
dfs_vl , -
- .
vertex_t -
, graphjxaits.
-
3.11. , , depth_first_ visit BGL
3.11. dfs_v2()
template <typename Graph, typename ColorMap, typename Visitor>
void dfs_v2(const Graphs g,
typename graph_traits<Graph>::vertex_descriptor u,
ColorMap color, Visitor vis)
{
typedef typename property_traits<ColorMap>::value_type color_type:
typedef color_traits<color_type> ColorT:
color[u] - ColorT::gray():
vis.discover_vertex(u. g):
typename graph_traits<Graph>::out_edge_iterator ei. ei_end:
for (tie(ei. ei_end) = out_edges(u. g): ei != ei_end: ++ei)
if (color[target(*ei. g)] == ColorT::white()) {
vis.tree_edge(*ei. g);
dfs_v2(g. target(*ei. g). color, vis):
} else i f (color[target(*ei. g)] == ColorT::gray())
vis.back_edge(*ei. g):
el e
^ ., ,*
vis.forward_or_cross_edge(*ei. g);
color[u] = ColorT::black():
vis.finish_vertex(u. g):
}
BGLdepth_first_search() topological_sort() -
, .
depth_first_search() 4.2,
depth_f i rst_sea rch () 13.2.3.
topological sort() 13.2.5.
3.9.
, -
, -
. -, , -
topo_order. topo_sort ()
, ( ).
3.9. 75
std::vector<vertex_t> topo_order(num_vertices(g));
topo_sort(g. topo_order.rbegin(). colorjnap);
(
) ( 3.12). ,
( ),
, make- .
. -
, .
3.12.
graph_traits<file_dep_graph2>::vertex_iterator i, i_end:
graph_traits<file_dep_graph2>::adjacency_iterator vi. vi end;
//
for (tied. i_end) = vertices(g): i !- i_end; ++i)
color_map[*i] - white_color;
for (tied. i_end) = vertices(g); i != i_end; ++i)
for (tie(vi, vi_end) = adjacent_vertices(*i. g); vi ! vi_end; ++vi)
color_map[*vi] - black_color;
// 0,
for (tied. i_end) = vertices(g): i != i_end; ++i)
if (color_map[*i] -= white_color)
distance_map[*i] = compile_cost_map[*i]:
else
distance_map[*i] = 0:
. ,
topo_order, (
) . ,
. ,
' . -
, -
. , -
, .
std::vector<vertex_t>::iterator ui;
for (ui = topo_order.begin(): ui != topo_order.end(); ++ui) {
vertex_t u = *ui;
for (tie(vi. vi_end) = adjacent_vertices(u. g): vi != vi_end; ++vi)
if (distance_map[*vi] < distance_map[u] + compile_cost_map[*vi])
distance_map[*vi] = distance_raap[u] + compile_cost_map[*vi];
}
-
. -
graph_property_i ter_range. std:: max_el e-
ment() .
graph_property_iter_range<file_dep_graph2.
vertex_distance_t>::iterator ci. ci_end:
tie(ci. ci_end) = get_property_iter_range(g, vertex_distance);
. . .
76 3 BGL
. 3.4.
3.10.
BGL ,
: -
make-? ?
?
.
adjacency_l i st BGL.
.
.
-
-
.
adjacencyjist,
( )
. -
. , -
-
.
4.1.
(breadth-first search, BFS) -
,
. BGL -
breadth_first_search(). -
, .
.
4.1.1.
, ,
. -
. -
, .
,
, .
.
. 4.1. -
{d}, {/, g}, {, h, b, e), {} (
d).
v (, v), , -
.
. {, v) -
, , v. . 4.1.
, .
d.
5(s, v) 5 v -
s v. ,
78 4
8(s, ). , -
. ,
.
. 4.1.
5 ,
, , .
4.1.2.
. , -
1 , ,
. , (
- ) Rudy
, Sleepers .
- , ,
, ,
. , -
, , , -
.
-
. , -
,
. -
, .
-
.
.
4.1. 79
, -
. -
, .
,
. ,
breadth_fi rst_search(), -
.
Internet Movie Database1. example/kevin_bacon.txt
, .
, , -
.
.
Patrick Stewart;Prince of Egypt. The (1998):Steve Martin
std: :ifstream
.
std::ifstream datafile("./kevin-bacon.dat"):
if (! datafile) {
std::cerr "No ./kevin-bacon.dat file" std::endl:
return EXITJAILURE;
}
adjacency_list, a undirectedS
, . 3.6, -
, property -
.
typedef adjacency_list<vecS. vecS. undirectedS.
property<vertex_name_t. std: :sthng>.
property<edge_name_t. std: :sthng> > Graph;
Graph g;
-
. , -
.
typedef property_map<Graph. vertex_name_t>::type actor_name_map_t:
actor_name_map_t actorjiame = get(vertex_name. g ) ;
typedef property_map<Graph, edge_name_t>::type movie_name_map_t;
movie_name_map_t connectingjnovie = get(edge_name. g):
, -
. -
Boost Tokenizer Library. -
4.1.
4.1.
for (std::string line: std::getlineCdatafile.line): ) {
char delimiters_separator<char> sep(false, "", " ; " ) :
tokenizer<> line_toks(line. sep):
tokenizer<>::iterator i = line_toks.begin();
( )
< )
< )
< , , )
}
, ,
. -
.
, .
, . -
,
.
.
(
) , .
std:: map.
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef std::map<std::string, Vertex> NameVertexMap:
NameVertexMap actors:
. -
, , name
, ( 4.2).
, std:: map:: i nsert () -
, .
4.2.
( >
std::string actors_name = *i++:
NameVertexMap::iterator pos:
bool inserted;
Vertex u. v;
tie(pos. inserted) = actors.insert(std: :makej)air(actors_name, VertexO));
if (inserted) {
u = add_vertex(g);
actor_name[u] = actors_name:
pos->second = u:
} else {
u = pos->second;
( ) ,
. ,
. -
.
< >
std::string moviejiame - *i++:
4.1. 81
. -
, .
( ) s
tietpos, inserted) = actors.insert(std: :make_pair(*i, VertexO));
if (inserted) {
v = add_vertex(g):
actor_name[v] = * i ;
pos->second = v;
} else v = pos->second:
. EdgeLi st sets,
.
( , , >
graph_traits<Graph>::edge_descriptor e;
t i e ( e , inserted) = add_edge(u, v. g);
i f (inserted) connecting_movie[e] = moviejname;
-
. adjacencyj i st VertexLi st=vecS, -
[0, | V |). -
std:: vector, .
std::vector<int> bacon_number(num_vertices(g)):
breadth_first_search() : ,
. ,
, actors
(-). , , .
Vertex src = actors["Kevin Bacon"];
bacon_number[src] = 0;
. , (, v), -
v d[v] < d[u] + 1.
- bacon_number_recorder,
BFSVisitor. -
- tree_edge()
. bacon_number_recorder defaul t_bfs_vi si tor
() -
( 4. 3).
, -
LvaluePropertyMap.
4.3. bacon_number_recorder
template <typename DistanceMap>
class bacon number recorder : public default bfs visitor {
public:
bacon_number_recorder(DistanceMap dist) : d(dist) { } &
82 4
4.3 {)
template <typename Edge, typename Graph>
void tree_edge(Edge e. const Graphs g) const {
typename graph_traits<Graph>::vertex_descriptor
u = source(e. g ) . v = target(e, g ) :
d[v] = d[u] + 1:
}
private:
DistanceMap d:
}:
//
template <typename DistanceMap>
bacon_number_recorder<DistanceMap>
record_bacon_number(DistanceMap d)
return bacon_number_recorder<DistanceMap>(d);
}
breadth_fi rst_search(). -
, ,
visitor . -
baconjnumber.
breadth_first_search(g, src,
visitor(record_bacon_number(&bacon_number[0])));
.
graph_traits<Graph>::vertex_iterator i. end:
for (tie(i. end) - vertices(g): i ! end: ++i) {
std::cout actor_name[*i] " "
bacon_number[*i] std::endl:
}
.
William Shatner 2
Denise Richards 1
Kevin Bacon
Patrick Stewart 2
Steve Martin 1
4.2.
.
(. 13.5.2), (. 13.2.5).
. ,
(. 3.4).
-
, -
. ,
depth_first_search() depth_first_visit() -
.
4.2. 83
4.2.1.
.
( ).
, ,
.
, -
. -
( ),
. -
, .
. 4.2. .
,
. -
. , -
. -
.
: (tree
edge), (back edge) (forward or cross
edge). , -
( ) . , (, v) -
, v (, v).
v. , , -
v , {, v) . -
.
. 4.2.
84 4
, v (, v)
. . -
(, v), , -
v . , -
. v
, , ( , -
).
, ,
( ) .
.
-
. (-
) .
, -
.
,
,
. . 4.2 -
.
, -
. (. -
3.4).
( ( (f (g (d (b (e e) b) d) g)(h h) f) c) a) (i ( j j ) i)
4.2.2.
-
.
. 4.3. , -
-
. ({, 6), :;
6 - Bv ,
[32].
.
. (u, v)
, v -
, (, v) . v . -
. (7, ,)
(. 4.3) . -
, .
fi nd_l oops () ( 4.4).
: entry, g
. Graph BidirectionalGraph,
, (-
4.7). Loop , .
4.2. 85
Entry
/
\
, 3
. 4.3.
4.4. find_loops()
( find_loops >
template <typename Graph, typename Loops>
void find_loops(
typename graph_traits<Graph>::vertex_descriptor entry.
const Graph& g,
Loops& loops) //
back_edge_recorder DFSVisitor, -
. -
, , -
Outputlterator. back_edge_recorder, ,
defaul t_df s_vi si tor,
, back_edge_recorder . -
- back_edge(). 4.5
back_edge_recorder .
86 4
4.5.
< )
template <typename Outputlterator>
class back_edge_recorder : public default_dfs_visitor {
public:
back_edge_recorder(OutputIterator out) : m_out(out) { }
template <typename Edge, typename Graph>
void back_edge(Edge e. const Graph&) { *m_out++ - e; }
private:
Outputlterator m_out;
}:
//
template <typename Outputlterator>
back_edge_recorder<OutputIterator^
make_back_edge_recorder(OutputIterator out) {
return back edge recorder<0utputlterator>(out);
} " "
.
depth_fi rst_vi s i t ( ) depth_fi rst_search(), -
. entry
. , -
. make_back_edge_recorder()
, std: :back_insert_iterator
. depth_first_visit() -
, -
. -
colorjnap (. 15.2.2).
< )
d e p t h _ f i r s t _ v i s i t ( g , entry,
make_back_edge_recorder(std::back_inserter(back_edges)),
make_iterator_property_map(color_map.begin(). g e t ( v e r t e x j ndex, g ) ) ) ;
, -
( 4.6). , -
, compute_loop_extent(), -
, .
4.6.
< )
for (std::vector<Edge>::size_type i = 0: i < back_edges.size(): ++1) {
loops.push_back(typename Loops: :value_typeO):
compute_loop_extent(back_edges[i], g. loops.backO):
}
v {t, h), v
h t . compute_
loop_extent() ( 4.7) : , -
, ,
, .
4.7.
( )
template <typename Graph, typename Set>
4.2. 87
void compute_loop_extent(
typename graph_traits<Graph>::edge_descriptor back_edge.
const Graphs g, Set& loop set)
r
function_requires< BidirectionalGraphConcept<Graph> >():
typedef typename graph_traits<Graph>::vertex_descriptor Vertex:
typedef color_traits<default_color_type> Color:
Vertex loopjiead, loop_tail:
loop_tail - source(back_edge. g):
loopjiead = target back_edge, g):
( : )
< : )
< : >
, , -
depth_fi rst_vi sit(). -
, , ,
. -
, ( ) ,
. reachabl e_f rom_head.
, Bv . 4.4.
. 4.4. ,
< : ) =
std::vector<default_color_type>
reachable_from_head(num_vertices(g), Color::white()):
d e p t h _ f i r s t _ v i s i t ( g , loopjiead, d e f a u l t _ d f s _ v i s i t o r ( ) .
makej terator_property_map(reachable_fromjnead.begi n ( ) .
get(vertex_index, g ) ) ) ;
88 4
,
7. .
. -
depth_f i rst_vi s i t () BGL out_edges () -
, ,
reverse_graph. BidirectionalGraph -
,
.
, . ,
7, . 4.5.
( : ) =
std: :vector<default_color_type> reachable_to_tail(rium_vertices(g)):
reverse_graph<Graph> reverse_g(g):
depth_first_visit(reverse_g. l o o p _ t a i l . d e f a u l t _ d f s _ v i s i t o r ( ) .
make_iterator_property_map(reachable_to_tail .beginO.
get(vertex_index. g ) ) ) ;
, , -
. -
1 oop_set ,
.
{ : )
typename graph_traits<Graph>: : v e r t e x j t e r a t o r v i , vi_end:
for ( t i e ( v i . vi_end) = vertices(g): vi != vi_end: ++vi)
i f (reachable_from_head[*vi] != Color::white()
&& reachable_to_tail[*vi] !- Color::white())
loop_set.insert(*vi):
. 4.5. , 7
-
. -
-
. .
-
-
BGL.
5.1.
<v0, vv ..., vk> G = (V, ),
, (> vX4l) ( -
). -
(, v) w(u, v). ( )
:
4-1
w(p) = Yw(vitvi+l).
i-0
{, v) v
. v , 5(, v) - :
f min{w(p):u-+v}
, -
. . 5.1.
-
, .
90 5
-
. ,
, . -
-
.
. 5.1.
, -
, -
, ,
BGL
: -
. BGL
.
.
. , -
,
.
5.2.
- (IP), .
( ) ,
, , . .
, , -
. -
, .
, -
- (internet router).
,
, -
. (
, hop), . ,
.
, (Trans-
mission Control Protocol, TCP). ( -
) ,
5.3. - 91
G
1.2ms j j ,
\^
F
. 5.2. -,
5.3. -
- ,
(Routing Information Protocol, RIP) [19],
. RIP -
92 5
.
,
, -
. -
, , -
,
.
RIP -
- [5,13].
-
. , -
{, v), :
d[v] = min(w(u, v) + d[u],d[v]).
- ,
. | V | ,
(,
, ). -
, , -
(, v), w(u, v) + d[u] < d[v], w , ad .
, , -
, , .
bel I man_f ord_shortest_paths () -.
,
. bellman-ford-internet.
5.1.
5.1. bellman-ford-internet.cpp
< bellman-ford-internet.cpp )
iinclude <iostream>
finclude <boost/array.hpp>
#i nclude <boost/graph/edge_li st.hpp>
#include <boost/graph/bel1man_ford_shortest_paths.hpp>
int mainO
r
using namespace boost;
< )
< )
< )
( - )
< )
return EXIT SUCCESS;
j
bel I man_ford_shortest_paths ()
. Edge-
ListGraph. BGL EdgeListGraph , -
, . -
e d g e j i s t , .
.
5.3. - 93
std:: pai . -
,
.
boost:: array, std:: pai .
,
. edge_l 1 st ,
1 . -
5.2.
5.2.
( > =
II ()
enum { , , . D, . F. G, H. n_vertices };
const int n_edges = 11;
typedef std::pair<int, int> Edge:
// ,
array<Edge. n_edges> edges = { { Edge(A. B). Edge(A, ) .
EdgetB. D), Edge(B. E). Edge(C. E), Edge(C. F). Edge(D. H ) .
Edge(D. E), Edge(E. H). Edge(F. G). EdgetG. H) } };
//
typedef edge_list<array<Edge. n_edges>::iterator> Graph;
Graph g(edges.begin(). edges.endO);
( ) -
, ReadablePropertyMap.
weightmapO -
, get (edge_wei ght, g).
edge_l i st -
,
. edgel i st -
-,
. -
. -
.
( >
//
array<float. n_edges> delay -
{ { 5.0. 1.0. 1.3. 3.0. 10.0. 2.0. 6.3. 0.4, 1.3, 1.2, 0.5 } }:
del ay , -
, -
.
- iterator_property_map Boost Property Map
Library. (, -
) LvaluePropertyMap. -
make_iterator_property_map().
1
std::iterator_traits edge_list
, .
94 5
, ,
, .
make_iterator_property_map() :
< )
make_iterator_property_map(delay.begin(), get(edge_index. g), delay[0])
, , -
, -. get
edge_i ndex - -
PropertyGraph.
. . 5.2,
( ).
. , parent -
.
-
, (parent[u], u) -
.
edge_l 1 st (-
).
( 5.3).
, -
.
5.3.
< > s
// ""
char name[ ] = "ABCDEFGH";
array<int. n_vertices> parent:
for (int i = 0; i < n_vertices: ++i)
parent[i] = i ;
array<float, n_vertices> distance:
distance.assign(std::numeric_limits<float>::max()):
//
distanced] = 0:
edge_l 1 st , -
, Boost
Property Map Library
(. 15.2.1).
bellman_ford_shortest_paths().
, ( -
) .
< - >
bool = be11man_ford_shortest_paths(g. int(n_vertices).
weight_map( < ) ).
di stance_map(&di stance[0]).
predecessor_map(&parent[0])):
-
.
5.4. 95
( )
i f ()
for ( i n t i = 0: i < n_vertices: ++i)
std::cout name[i] ": " distanced]
" " name[parent[i]] std::endl;
else
std::cout " " std::endl;
:
: 0
: 5
: 1
D: 6. 3
: 6. 7 D
F: 3
G: 4. 2 F
: 4. 7 G
, , ,
(A,C,F,G,H).
5.4.
1980- -
. :
, ,
, -
.
,
.
-
(Link-State Routing) [28, 37]. -
, .
-
: (up),
(down). ,
.
( ), -
,
. ,
, -
. -
,
.
(Open Shortest Path First
96 5
N
RT1
3/ >
N1
RT2 RT4
N2 RT5
RT3 RT10 6
N4 N8 N6
N9 N7 N15
RT9
. 5 . 3 . -
( dot- Graphviz )
GraphvizDigraph g _ d o t ;
read_graphviz("figs/ospf-graph.dot", g_dot);
4 . 375
98 5
GraphvizDi graph .
- ,
,
. , g_dot .
Graphvi zDi graph , std:: map<std:: st ri ng.
std:: stri ng>. label . -
int boost: :lexica1_cast, -
. Graph Graphvi zDi graph ad jacencyj i st -
VertexList=vecS,
. source(*ei. g_dot) ,
add_edge() .
5.5.
5.5.
( , >
typedef a d j a c e n c y j i s t < vecS. vecS, directedS. no_property.
property < edge_weight_t, i n t > > Graph;
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor:
Graph g(num_vertices(g_dot)):
property_map < GraphvizDigraph, edge_attribute_t >::type
edge_attr_map = get(edge_attribute, g_dot):
graphjtraits < GraphvizDigraph >::edge_iterator e i , ei_end:
for ( t i e ( e i . ei_end) = edges(g_dot): ei != ei_end: ++ei) {
int weight = lexical_cast < i n t >(edge_attr_map[*ei]["label"]);
property < edge_weight_t, i n t >edge_property(weight);
add_edge(source(*ei, g_dot). t a r g e t ( * e i . g_dot), edge_property, g ) :
}
-
, . -
RT6.
< ) =
vertex_descriptor router^six:
propertyjrap < GraphvizDigraph, vertex_attribute_t >::type
vertex_attr_map - get(vertex_attribute, g_dot);
graph_traits < GraphvizDigraph >::vertex_iterator v i , vi_end;
for ( t i e ( v i . vi_end) = vertices(g d o t ) ; vi != vi_end; ++vi)
i f ("RT6" == vertex_attr_map[*vT]["label"]) {
router_six = * v i ;
break;
}
-
. -
.
std:: vector .
(
)
std:;vector < vertex_descriptor > parent(num_vertices(g)):
// /
typedef graph_traits < Graph >::vertices_size_type size_type;
5.4. 99
. 5.4.
< >
vertex_descriptor v = * v i . c h i l d :
i n t path_cost = 0;
propertyjnap < Graph, edge_weight_t >::type
5.4. 1 0 1
6.1.
.
G = (V, )
TczE,
. 7:
w(T) = ^ w(u,v).
(,)
, ,
. -
.
6.2.
,
. -
. -
, , . -
6.2. 103
: -
. -
, -
.
, , ,
. , -
, . 6.1. -
. -
. ,
, .
(Parry Sound)
11
20
30 (Dunchurch)
10
12
20
\20 (Kearny)
14
(Mactier)
// 30
^""-
Glen Orchard) (Huntsville
4
<L
4 , 1 5
30
(Bracebridge)
. 6.1. , ,
104 6
6.3.
,
, , -
.
. ,
.
, ( ) -
.
kruskal-telephone.cpp, kruskal _
minimum_spanning_tree()
, 6.1.
6.1. kruskal-telephone.cpp
< kruskal-telephone.cpp >
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#i nclude <boost/1exi ca1_cast.hpp>
#i nclude <boost/graph/graphviz.hpp>
#incl ude <boost/graph/kruskal_min_spanning_tree.hpp>
int
mainO
{
using namespace boost:
< dot- Graphviz >
( ,
>
( >
( >
< dot- >
return EXIT_SUCCESS;
}
, . 6.1, dot- Graphviz -
read_graphvi z () boost/graph/graphviz.hpp.
, Graphvi zGreph.
< dot- Graphviz > =
GraphvizGraph g_dot;
read_graphviz("figs/telephone-network.dot". g_dot):
5.4,
. Graphvi zGraph
1 exi cal_cast ( 6.2).
6.2.
{ ,
)
typedef a d j a c e n c y j i s t < vecS, vecS, undirectedS. no_property,
property < edge_weight_t. i n t > > Graph;
Graph g(num_vertices(g_dot));
propertyjnap < GraphvizGraph, edge_attribute_t >::type
6.3. 105
(HuntsvilleJ
ho
(Bracebridge)
. 6.2.
6.4.
( , ).
-
. -
, .
. ( -
BGL
.)
primjninirnurn_spanning_tree() -
(. . 6.1). prim-tele-
phone. ( 6.4) , -
, .
6.4. 1 0 7
6.4. prim-telephone.
< prim-telephone. ) =
linclude <iostream>
#include <fstream>
iinclude <vector>
#include <boost/lexical_cast.hpp>
#include <boost/graph/graphviz.hpp>
#incl ude <boost/graph/pri m_mi nimum_spanni ng_tree.hpp>
int mainO
{
using namespace boost:
< dot- Graphviz >
( .
)
(
>
< )
< dot- )
return EXITJUCCESS;
}
( dot- ) ,
. , -
.
. v
parent[v] v .
parent[v] ,
. -
primjni nimum_spanni ng_tree() -
( ). -
, Graph,
. -
*vertices(g) .first, ,
.
<
> =
typedef graph_traits<Graph>::vertex_descriptor Vertex;
std::vector<Vertex> parent(num_vertices(g)):
primjninimum_spanning_tree(g, &parent[O]);
pa rent, -
(parent[v], v).
pa rent [ v ] = v, , v ,
, . -
(parent[v], v) .
, . 6.1, 145 .
( >
property_map<Graph, edge_weight_t>::type_weight = get(edge_weight. g):
int total_weight = 0;
for (int v = 0: v < num_vertices(g): ++v)
if (parent[v] != v)
total_weight += get(weight. edge(parent[v], v. g).first);
std:;cout " : " total_weight std;:endl:
108 6
-
dot-. -
. 6.3. ,
. Magnetawan
Magnetawan Sprucedale. ,
:
.
. 6..
( dot- )
for ( i n t u = 0; u < num__vertices(g): ++u)
i f (parent[u] != u)
edge_attr_map[edge(parent[u]. u, g _ d o t ) . f i r s t ] [ " c o l o r " ] = "black";
std::ofstream out("figs/telephone-mst-prim.dot");
graph_property < GraphvizGraph, graph_edge_attribute_t >::type &
graph_edge_attr_map = get_property(g_dot. graph_edge_attribute);
graph_edge_attr_map["color"] = "gray";
write_graphviz(out, g_dot);
. , -
, -
. , -
.
, , -
*.
200 - , 56
[7]. -
, -
, 150
50 (
, , ).
BGL
: ( )
, . BGL -
.
-
BGL WWW
(World Wide Web).
7.1.
,
. ,
, w . -
,
, . . .
110 7
. -
, . -
: , .
, ,
. , ,
. , -
-
. -
.
7.2.
. ,
-
. connected_components (:
BGL depth_fi rst_search() --
, -
-
.
. 7.1 -, -
.
: 1) ; 2)
BGL; 3) connected_components().
, ,
. cc-internet.cpp 7.1.
7.1. cc-internet.cpp
< cc-internet.cpp >
finclude <fstream>
find ude <vector>
#include <string>
#i nclude <boost/graph/connected_components.hpp>
#include <boost/graph/graphviz.hpp>
int mainO
{
using namespace boost:
( )
( >
< connected_components() >
< dot- )
}
. 7.1 cc-internet.dot, -
dot- Graphviz. Graphvi zGraph, Graphvi zDi graph,
.
7.2. 1 1 1
(I^.bbnplanetnef)
Iios.ee.lblgov>^ambridge1 -nbr2.bbnplaneTnef)
rcngw-ner-cc.Berkeley.EDU)
corel-ord1 -oc48.ord2.abovenej
; )
. 7.1. -
112 7
( }
GraphvizGraph g;
read_graphviz("figs/cc-internet.dot". g):
, ,
component num_vertices(g).
( ) =
std::vector<int> component(num_vertices(g));
connected_components()
VertexListGraph IncidenceGraph. Graphvi zGrapl"
, . -
.
iterator_propertyjnap,
, component. -
ted_components()
component.
connected_components() -
, . -
.
.
< connected_components() )
int num_comp = connected_components(g,
make_iterator_propertyjTiap(component.begin(),
get(vertex_index, g), component[0]));
. dot-
.
. 7.2.
( dot- )
property_map < GraphvizGraph. vertex_attribute_t >::type
vertex_attr_map = get(vertex_attribute, g);
s t d : : s t r i n g c o l o r [ ] - {"white", "gray", "black", "lightgray"};
// , , , -
graph_traits < GraphvizGraph >::vertex_iterator v i . vi_end;
for ( t i e ( v i . vi_end) vertices(g): vi != vi_end; ++vi) {
vertex_attr_map[*vi]["color"] - co1or[component[*vi]];
vertex_attrjnap[*vi]["style"] = " f i l l e d " : // -
i f (vertex_attr_map[*vi]["color"] "black")
vertex_attr_map[*vi]["fontcolor"] - "white": // -
}
write_graphviz("figs/cc-internet-out.dot", g);
7.3. - 113
. 7.2.
114 7
7.3.
-
- '
(. 7.3). -
.
scc.cpp -
7.2. dot- Graph viz .
,
. strong_components (),
.
anubis.dkuug.dk) (sourceforge.net
(www.lam-mpi.org)
. 7.3. , URL -
7.2. scc.cpp
< scc.cpp > =
finclude <boost/config.hpp>
finclude <fstream>
#include <map>
#include <string>
7.3. - 115
strong_components() component,
.
num_comp - 1. , strong_components(), -
VertexListGraph IncidenceGraph. ,
.
ReadWritePropertyMap. -
, , -
.
.
( strong_components() )
int num_comp = strong_components(g. make_assoc_property_map(component)):
,
. dot-.
. 7.4.
116 7
. 7.4.
(
dot- >
property_map < GraphvizDigraph. vertex_attribute_t >::type
vertex_attr_map = get(vertex_attribute, g):
s t d : : s t r i n g c o l o r [ ] - {"white", "gray", "black", "lightgray"}:
// . , , -
graph_traits < GraphvizDigraph >::vertex_iterator v i , vi_end;
for ( t i e ( v i . vi_end) = vertices(g); vi != vi_end; ++vi) {
v e r t e x _ a t t r j n a p [ * v i ] [ " c o l o r " ] = color[component[*vi]];
vertex_attr_map[*vi]["style"] = " f i l l e d " ; // -
i f (vertex_attr_map[*vi]["color"] = "black")
vertex_attr_map[*vi]["fontcolor"] = "white": // -
}
write_graphviz("figs/scc-out.dot". g):
, -
(, ) . -
.
[12]. -
(push-relabel
algorithm) [9, 16, 17], (preflow),
[20]. BGL -
: - ( -
-) .
8.1.
(flow network) G = (V, ) -
s t. -
, -
/. :
, t.
(, v) = c(u, v) - f(u, v). (, v) > 0 -
Ef Gf = (V, Ej). (, v) = 0 -
.
118 8
| / |
,
.
[12]
-
(5, ) . (5, )-
5 , s e S t .
S ,
5 .
( ).
, s t,
, -
.
8.2.
, -
, , -
. , -
, , ? -
, (-
, ).
a ( G ) .
, , -
(minimum disconnecting set). -
5* 5*, -
[S", 5*]. ,
. -
-
[27].
(, v) , -
UHV . -
,
(
) . , -
, -
, .
, ,
. -
.
-
, , -
8.2. 119
.
[S', S"]. , 5 . ( G) = 5,
S* . a(G) < 8 - 1, , -
5" ( S), ,
5, . ,
, 5 = , k
S, ( , ) k S. ,
5 .
,
BGL- VertexListGraph. -
, (disconnected set) -
. -
8.1.
8.1.
( >
template < typename VertexListGraph, typename Outputlterator >
typename graph_traits < VertexListGraph >::degree_size_type
edge_connectivity(VertexListGraph & g, Outputlterator disconnecting_set)
{
( )
< >
( >
{ S S )
< )
( )
return :
}
( 8.2)
, .
( ) -
, adjacencyj 1 St.
8.2.
( ) =
typedef typename graph_traits <
VertexListGraph >::vertex_descriptor vertex_descriptor;
typedef typename graph_traits <
VertexListGraph >::degree_size_type degree_size_type;
typedef c o l o r _ t r a i t s < default_color_type > Color:
typedef typename a d j a c e n c y j i s t _ t r a i t s < vecS. vecS,
directedS >::edge_descriptor edge_descriptor;
typedef a d j a c e n c y j i s t < vecS. vecS. directedS, no_property.
property < edge_capacityj;. degree_size_type.
property < edge_residual_capacity_t. degree_sizej:ype,
property < edge_reverse_t, edge_descriptor > > > > FlowGraph:
8.3 std:: set S S
( nei ghbor_S), -
. 5 * ( Sstar) S (
nonneighbor_S) std:: vector , , -
.
120 8
8.3.
( )
vertex_descriptor u. v. p. :
edge_descriptor e l , e2:
bool inserted;
typename graphjtraits < VertexListGraph >::vertex_iterator v i . vi_end;
degree_size_type delta. alpha_star, alphajSjk:
std::set < vertex_descriptor > S. neighbor_S;
std: -.vector < vertex_descriptor > S_star, nonneighborjS;
std::vector < default_color_type > color(num_vertices(g)):
std::vector < edge_descriptor > pred(num_vertices(g});
( 8.4). -
: , -
. --
: cap, res_cap rev_edge .
8.4.
< ) =
FlowGraph flow_g(num_vertices(g)): //
typename propertyjnap < FlowGraph, edge_capacity_t >::type
cap = get(edge_capacity. flow_g);
typename propertyjnap < FlowGraph, edge_residual_capacity_t >::type
res_cap = get(edge_residua!_capacity, flow_g):
typename propertyjnap < FlowGraph. edge_reversejt >::type
revjedge = get(edge_reverse. flow_g);
( 8.5). -
.
8.5.
( )
template < typename Graph >
std: .-pair < typename graph_traits < Graph >: :vertex_descriptor.
typename graphjtraits < Graph >::degree_size_type >
minjdegree_vertex(Graph & g)
{
typename graphjtraits < Graph >::vertexjdescriptor p;
typedef typename graphjtraits < Graph >::degree sizejtype sizejtype;
sizejtype delta = std::numeric_limits < sizejtype >::max();
typename graphjtraits < Graph >::vertex_iterator i . iend:
for ( t i e d , iend) = vertices(g); i != iend; ++i)
i f (degree(*i. g) < delta)
{
delta - degree(*i, g):
8.2. 1 2 1
=* i ;
}
return std::make_pair(p. delta):
}
( -
) . adjacent_vertices().
, std:: i n s e r t j tera tor std:: set.
8.6.
8.6.
{ > =
template < typename Graph, typename Outputlterator >
void neighbors(const Graph & g.
typename graph_traits < Graph >::vertex_descriptor u,
Outputlterator result)
{
typename graph_traits < Graph >::adjacency_iterator ai, aend;
for (tie(ai, aend) = adjacent_vertices(u, g): ai !- aend; ++ai)
*result++ = *ai;
( 8.7)
/?, S = , -
S. std: :set_difference() V- 5, -
.
8.7. S
< S S >
tie(p, delta) = min_degree_vertex(g);
S_star.push_back(p):
alpha_star = delta:
S.insert(p):
neighbor_S.insert(p):
neighbors(g, S.beginO, S.endO. std: :inserter(neighbor_S,
neighbor_S.begin())):
std::set difference(vertices(g).first, vertices(g).second,
neighbor^, begino. neighbors, end .
std::back_inserter(nonneighbor_S)):
, 5 -
. ( 8.8)
k - (. -
13.7.1). , , -
. , , -
( ) 5*, k S,
5 .
122 8
8.8.
( ) =
while (!nonneighbor_S.empty()) {
k - nonneighbor_S.front();
alpha_S_k edmunds_karp_max_flow
(flow_g. p. k. cap. res_cap. rev^edge, &color[0]. &pred[0]);
i f (alpha_S_k < alpha_star) {
alpha_star = alpha_S_k;
S_star.clearO:
for (tieCvi. vi_end) - vertices(f1ow_g): vi !- vi_end; ++vi)
i f (color[*vi] != Color::white())
S_star.push_back(*vi);
}
S.insert(k);
neighbor_S.insert(k);
neighbors(g, k, std::inserter(neighbor_S, neighbor_S.begin())):
nonneighbor_S.clearO:
std::set_difference(vertices(g) . f i r s t , verticesCg).second.
neighborJB.beginO, neighbor_S.end().
std::back_inserter(nonneighbor_S)):
}
( 8.9) , -
5*, 5*.
disconnect!ng_set ( ),
return.
8.9.
( ) =
std::vector < bool > in_S_star(num_vertices(g). false):
typename std::vector < vertex_descriptor >::iterator si;
for (si " S_star.begin(): si != S_star.end(); ++si)
in_S_star[*si] = true;
degree_size_type = 0;
for (si = S_star.begin(); si != S_star.end(); ++si) {
typename graph_traits <Vertexl_istGraph>: :out_edge_iterator ei. ei_end:
for (tie(ei, ei_end) = out_edges(*si. g); ei != ei_end: ++ei)
i f (!in_S_star[target(*ei, g)]) {
*disconnecting_set++ *ei:
edge-connectivity.cpp, -
, 8.10.
8.10. edge-connectivity.cpp
( edge-connectivity.cpp )
#include <algorithm>
#include <uti1ity>
#include <boost/graph/edmunds_karp_max_flow.hpp>
#i nclude <boost/graph/push_relabel_max_f1ow.hpp>
#include <boost/graph/adjacency_li st.hpp>
#include <boost/graph/graphvi z.hpp>
namespace boost {
< >
8.2. 123
< )
( >
}
i n t mainO {
using namespace boost:
GraphvizGraph g:
read_graphviz("figs/edge-connectivity.dot". g):
edge-connectivity.cpp :
: 2.
: { (D.E) (D.H) }.
,
( 8.10), . 8.1.
. 8 . 1 .
:
, -
.
.
, .
.
(,
). -
.
,
, , [46].
NP- [15] ( -
). -
8 x 8 . 9.1.
1 2 3 4 5 6 7
X \/
\#"><
/Iv
11
\
i
1
1
-
1
1 1
i
1
. 9.1.
9.1. 125
( adjacency_list) .
kni ghts_tour_graph .
9.1.
kni ghts_tour_graph AdjacencyGraph, -
adjacent_vertices(), -
. , -
, , .
:
typedef std::pai < int. int > Position;
Position knight_jumps[8] = { Position(2. -1), PositionCl. -2),
PositionM, -2). Position(-2, -1), Position(-2, 1).
Positional. 2). Positiond. 2). Position(2. 1) }:
km ght_ad jacency_i terator -:
m_pos, m_i knightjumps -
m_g. ( operator++()) -
m_i. ( ), -
m_i, -
va1id_position(). ,
valid_position() . -
,
( ).
(operator*0)
. kni ght_adjacency_i terator -
9.1. boost: :forward_iterator_hel per -
operator++(i nt)
operator++() operator !=() operator==().
9.1. knight_adjacency_iterator
struct knight_adjacency_iterator:
public boost::forward_iterator_helper < knight_adjacency_iterator,
Position, std::ptrdiff_t. Position *. Position > {
knight_adjacency_iterator() { }
knight_adjacency_iterator(int i i . Position p,
const knights_tour_graph & g) : m_pos(p), m_g(&g), m_i(ii) {
valid_position(): }
Position operator *() const { return m_pos + knight_jumps[m_i]: }
void operator++ () { ++m_i; valid_position(): }
bool operator == (const knight_adjacency_iterator & x) const {
return m_i == x.m_i: }
protected:
void valid_position();
Position m_pos;
const knights_tour_graph * m_g:
i nt m_i:
i.
126 9 :
- va I i d_pos i t i on ()
, .
void knight_adjacency_iterator::valid_position() {
Position new_pos = m_pos + knight_juraps[m_i]:
while (m_i < 8 && (new_pos.first < 0 || new_pos.second < 0
| new_pos.first >- m_g->m_board_size
I new_pos.second >= m_g->m_board_size)) {
++m_i;
new_pos = m_pos + knight_jumps[m_i];
adjacent_vertices() ,
8
.
std::pair < knights_tour_graph::adjacency_iterator.
knights_tour_graph::adjacency_iterator >
adjacent_vertices(knights_tour_graph::vertex_descriptor v.
const knights_tour_graph & g) {
typedef knights_tour_graph::adjacency_iterator Iter:
return std::make_pair(Iter(O, v, g). Iter(8, v. g));
}
knights_tour_graph ( 9.2) ( -
) typedef, AdjacencyGraph. num_vertices()
.
9.2. knights_tour_graph
struct knights_tour_graph
{
typedef Position vertex_descriptor;
typedef std::pair < vertex_descriptor, vertex_descriptor >
edge_descriptor;
typedef knight_adjacency_iterator adjacency_iterator;
typedef void out_edge_iterator:
typedef void in_edge_iterator;
typedef void edgejterator;
typedef void vertex_iterator:
typedef int degree_size_type:
typedef int vertices_size_type;
typedef int edges_size_type:
typedef directed_tag directed_category;
typedef disallow_parallel_edge_tag edge_paranel_category:
typedef adjacency_graph_tag traversal_category;
knights_tour_graph(int n): m_board_size(n) { }
int m_board_size:
}:
int num_vertices(const knights_tour_graph & g) {
return g.m_board_size * g.m_board_size;
}
, Boost, -
, -
.
9.2. 127
9.2.
(backtracking graph search)
, ,
. ,
, , -
. 9.3 -
( ),
. ,
. -
, .
, , ,
, ,
Graph BGL, Graph,
.
, Graph.
9.3.
template < typename Graph, typename TimePropertyMap >
boo! backtracking_search(Graph & g.
typename graphjtraits < Graph >::vertexjjescriptor src,
TimePropertyMap timejnap)
{
( >
S.push(std::make_pair(time_stamp, src)):
while (IS.emptyO) { //
( , >
( >
( , >
} // while (IS.emptyO)
return false;
}
, , std:: stack. -
.
< > =
typedef typename graph_traits < Graph >::vertexjjescriptor Vertex:
typedef s t d : : p a i r < i n t . Vertex > P;
std::stack < P > S:
int time_stamp = 0:
, .
.
( , ) =
Vertex x: .
tie(time_stamp. x) - S . t o p O :
put(time_map. x. time_stamp);
// , !
if (time_stamp == num_vertices(g) - 1)
return true;
128 9 :
,
, .
.
( )
boo! deadend - true: //
typename graph_traits < Graph >::adjacency_iterator i. end:
for (tied, end) = adjacent_vertices(x, g): i != end; ++1)
if (get(time_map, *i) == -1) {
S.push(std::make_pair(time_stamp + 1, *i));
deadend = false;
}
, , :
.
, , ,
.
( , )
if (deadend) { //
put(time_map, x. -1):
S.popO:
tie(time_stamp, x) = S.topO:
while (get(time_map. x) != -1) {
//
put(time_map. x, -1):
S.popO:
tie(time_stamp. x) = S.topO;
9.3.
,
. -
. -
. , , , , -
, .
number_of_successors() 9.4 -
.
9.4. number_of_successors()
template < typename Vertex, typename Graph, typename TimePropertyMap >
int number_of_successors(Vertex x. Graph & g, TimePropertyMap timejnap)
int s_x - 0;
typename graph_traits < Graph >::adjacency_iterator i. end;
for (tied, end) - adjacent_vertices(x, g); i != end; ++1)
if (get(time_map, *i) -1)
return s_x;
}
9.3. 129
( 9.5) -
, -
.
(priority queue). -
.
.
9.5.
template < typename Graph, typename TimePropertyMap >
bool warnsdorff(Graph & g,
typename graph_traits < Graph >::vertex_descriptor src,
TimePropertyMap timejriap)
{
< )
S.push(std::make_pair(time_stamp, src));
while (IS.emptyO) {
( , )
//
std::priority_queue < P. std::vector < P >. compare_first > Q;
typename graph_traits < Graph >::adjacency_iterator i, end;
int num_succ;
for (tied, end) - adjacent_vertices(x. g); i !- end: ++i)
if (get(time_map, *i) == -1) {
num_succ number_of_successors(*i. g. timejnap);
Q.push(std::make_pair(num_succ. *i)):
bool deadend = Q. empty :
//
for (: IQ.emptyO: Q.popO) {
tie(num_succ. x) = Q.topO:
S.push(std::make_pair(time_stamp + 1. x));
if (deadend) {
put(time_map. x. -1);
S.popO:
tie(time_stamp. x) = S.topO:
while (get(time_map. x) != -1) {
//
1
put(time_rnap. x, -1);
S.popO;
tie(time_stamp. x) = S.topO:
} // while (IS.emptyO)
return false:
)
5 3 a r 3 7 5
^
s
10.1.
BGL LEDA
boost/graph/leda_graph.hpp ,
GRAPH LEDA BGL.
BGL- LEDA GRAPH 14.3.5. LEDA
BGL LEDA 4.1, -
LEDA. leda_graph.hpp LEDA
, , a LEDA-
. -
LEDA.
10.1 ,
GRAPH LEDA .
10.1.
( topo-sort-with-leda.cpp )
#i nclude <vector>
#i nclude <string>
#include <boost/graph/topological_sort.hpp>
#i nclude <boost/graph/1eda_graph.hpp>
// LEDA. C++ Standard Library
#undef s t r i n g
iundef vector
i n t mainO {
using namespace boost;
< LEDA , >
( LEDA )
( LEDA )
return EXIT_SUCCESS;
}
GRAPH LEDA --
, (
std::stri ng) . add_vertex() -
1 eda_g -
. , add_vertex(),
,
. LEDA 10.2.
10.2. LEDA
( LEDA , >
typedef GRAPH < s t d : : s t r i n g , char >graph_t:
graph_t leda_g:
typedef graph_traits < graph_t >: :vertex_deschptor vertex_t;
std::vector < vertex_t > vert(7):
vert[O] add_vertex(std::string(" "). leda__g);
v e r t [ l ] = add_vertex(std::string(" "). leda_g);
vert[2] add_vertex(std::string("nofly4HTb "). leda_g):
vert[3] = add_vertex(std::string("npnBecTH "). leda_g):
vert[4] = add_vertex(std: :string("npnroTOBHTb "), leda_g): r,nn,ou, ^
*
132 10
10.2 {)
vert[5] - add_vertex(std::stri' "). leda_g);
vert[6] = add_vertex(std::string(" "), 1eda_g);
.
add edgeO.
< LEDA > =
add_edge(vert[O], vert[3]. leda_g):
add_edge(vert[l], vert[3], leda_g):
add_edge(vert[l]. vert[4]. leda_g);
add_edge(vert[2]. vert[l], leda_g);
add_edge(vert[3], vert[5]. leda_g):
add_edge(vert[4]. vert[6]. leda_g):
add_edge(vert[5], vert[6], leda_g):
, , topol ogical_sort(). -
LED BGL GRAPH LED -
BGL-. 1 eda_g .
topol ogi cal_sort() , -
LED A node_array -
. 1 eda_node_property_map() boost/graph/leda_graph.hpp
, LvaluePropertyMap -
node_array. topo_order -
. , .
operator[ ] () GRAPH LED
. 10.3.
10.3.
( LEOA >
std::vector < vertex_t > topo_order;
node_array < default_color_type > color_array(leda_g):
topological_sort(leda_g. std::back_inserter(topo_order).
color_map(make_leda_node_propertyjnap(color_array))):
std::reverse(topo_order.begin(). topo_order.end());
int n = 1:
for (std::vector < vertex_t >::iterator i = topo_order.begin():
i != topo_order.end(); ++1, ++n)
std::cout n ": " leda_g[*i] std::endl:
10.2.
BGL SGB
Stanford GraphBase Graph,
. boost/
graph/stanford_graph.hpp Graph BGL.
stanford_graph.hpp , -
SGB PROTOTYPES ( -
SGB). , SGB
ANSI,
C++. SGB-BGL -
10.2. BGL SGB 133
int mainO {
using namespace boost;
( SGB- >
< >
( SGB- >
( SGB- >
gb_recycle(sgb_g);
return EXIT_SUCCESS:
10.3.
.
BGL LED .
, , -
BGL .
LEDA: VertexListGraph, BidirectionalGraph, VertexMutableGraph EdgeMutableGraph.
, BGL,
graph_t raits. -
GRAPH LED 1 ( 10.6). node edge -
. GRAPH
, directed_tag direc-
ted_category. GRAPH
, allow_paranel_edge_tag edge_pa 1 -
1 el_category. number_of_nodes () LED ,
vertices_size_type. , traver-
sal_category, ,
, bidirectional_graph_tag, adjacency_-
graph_tag vertex_l i st_graph_tag. .
10.6. LEDA
( LEDA- >
namespace boost {
struct leda_graph_traversal_category :
public v i r t u a l bidirectional_graph_tag,
public v i r t u a l adjacency_graph_tag.
public v i r t u a l vertex_list_graph_tag { }:
, Visual C++6.0, -
.
.
-, LEDA- .
10.3. 135
10.7 {)
template <typename Iter>
static bool equal(const Iter& x. const Iter& y)
{ return x.baseO ==y.base(); }
}:
iterator_adaptor edgejterator.
edgejterator . -
,
.
( >
typedef iterator_adaptor<leda_edge, leda_out_edge_1terator_policies.
leda_edge. const leda_edge&. const leda_edge*.
std::forward_iterator_tag. s t d : : p t r d i f f _ t
> out_edge_iterator:
-
outedgesO. ( 10.8) -
,
std:: pai r graph_trai ts .
,
( LEDA
). Fi rst_Ad j_Edge() -
LEDA, , .
10.8. out_edges() LEDA
( out_edges() LEDA >
template <typename V, typename E>
std::pair<typename graph_traits< GRAPH<V.E> >::out_edge_iterator.
typename graph_traits< GRAPH<V,E> >::out_edge_iterator >
out_edges(typename graph_traits< GRAPH<V,E> >::vertex_descriptor u,
const GRAPH<V.E>& g)
typedef typename graph_traits< GRAPH<V,E> >::out_edge_iterator Iter:
return std::make_pair( Iter(First_Adj_Edge(u.O)). Iter(O) );
}
.
LEDA boost/graph/leda_graph.hpp.
10.9 BGL, -
BGL.
( test/graph.).
10.9.
( leda-concept-check.cpp >
finclude <boost/graph/graph_concepts.hpp>
#include <boost/graph/leda_graph.hpp>
int main()
{
using namespace boost:
typedef GRAPH<int. int> Graph;
function_requires < VertexListGraphConcept<Graph> >();
function_requires < BidirectionalGraphConcept<Graph> >():
function_requires < VertexMutableGraphConcept<Graph> >():
function_requires < EdgeMutableGraphConcept<Graph> >():
return EXIT SUCCESS;
BGL ad jacency_l i st . -
BGL ,
. -
, -
adjacencylist BGL.
-
(Microsoft Visual C++ GNU C++).
BGL-, adjacencyjist -
, -
. , EdgeLi st VertexLi st,
-
.
vecS, l i s t s sets EdgeLi st
std::vector, std: : l i s t std: :set .
vecS 1 i stS std:: vector s t d : : 1 i st
.
11.1.
adjacency_l i st. -
:
, ,
.
(100 ), (1000 ) (10 000 -
) . 10
138 11
. -
.
Dell
733 , 512 . -
: Microsoft Visual C++ 6.0 GNU C++ 2.95.3 ( cygwin). Vi-
sual C++ . GNU C+ +
-03 -funroll -loops. , adjacen-
cy^ i st STL, -
.
POSIX- clock(),
. -
,
. 3 , -
. , -
10 %.
, . -
, -
.
vec
adjacency_list<vecS. vecS, directedS, property<vertex_distance_t, int>,
property<edge_weight_t. int> >
list
adjacency_list<listS. vecS. directedS, property<vertex_distance_t. int>.
property<edge_weight_t. int> >
set
adjacency_list<setS. vecS, directedS, property<vertex_distance_t, int>.
property<edge_weight_t. int> >
listlist
adjacency_list<listS, lists. directedS, propert.y<vertex_distance_t, int>,
property<edge_weight_t. int> >
11.1.1.
add_vertex() add_edge(),
\ | | V | . -
. 11.1. adjacencyj i st
VertexLi st1 i stS.
, | V | . -
. 11.2. Visual C++ -
adjacency_l 1 st VertexLi st=vecS, .
GNU C++ adjacency_list EdgeLi st=l i stS
.
11.1. 139
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
i 0.8
i
3
m
0,6
0,4
I vec
mist
set
0 CD listlist
1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0
. 1 1 . 1 .
, , ,
Visual C++ ,
GNU C++ Visual C++ GNU C++
I 0,8
I
2
0,6
2
0,4
0
100 1000 10000 100 1000 10000 100 1000 100 1000
. 11.2.
140 11
\ | | V | . -
. 11.3. .
, adjacencyjist VertexLi st=setS
.
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
ii 0,8
S
1
I
3
0,6
9
Q.
0,4
Q.
^ 0,2
vec
CZH set
1 listlist
100 1000 10000 100 1000 10000 100 1000 100 1000
. 11.3.
| V | \ | ,
. . 11.4.
l i s t l i s t , .
adjacencyj i st , -
.
| V | \ | , -
. clearj/ertexO ,
, . . 11.5. -
adjacencyjist VertexList=vecS -
.
11.1. 1 4 1
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
0,8
0,6
I
0)
0,4
Q-
vec
set
I CZD listlist
100 1000 10000 100 1000 10000 100 1000 100 1000
. 11.4.
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
i 0,8
2 0,6
>
.
0,4
I
.
ve Ic
IHst
1 1 t.m .,.[ SSI
100 1000 10000 100 1000 10000 100 1000
100 II lis1tl0is00t
. 11.5.
142 11
, -
. . 11.6.
.
, Vertexl_ist=vecS. -
l i s t l i s t , Ver-
texList=listS.
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
0,8
0,6
Q.
S
0,4
vec
list
set
listlist
100 1000 10000 100 1000 10000 100 1000 100 1000
. 11.6.
, -
. . 11.7.
adjacency_l i st EdgeLi st=vecS.
, -
-
. . 11.8.
adjacency l i s t EdgeList=vecS.
11.1. 143
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
a
|-
i 0,8
iCD
X
IIOJ
3 :
0,6 .;
s
p
'
Q.
0>
i
ra 0,4 i
a
m
i3
2 :
D.
?0,2
i vec
| list
; :
~ set
1 CZJIistlist
n
100 1000 10000 100 1000 10000 100 1000 100 1000
. 11.7.
, , , ,
Visual C++ GNU C++ Visual C++ GNU C++
: 0,6
0,4
0,2
vec
list
! I
listlist
0
100 1000 10000 100 1000 10000 100 1000 100 1000
. 11.8.
144 11
11.2.
-
. -
.
vecS EdgeLi st
.
vecS VertexLi st -
.
sets EdgeLi St. -
, -
std: -.set.
vecS 11 stS
EdgeLi st.
, lists
VertexLi st, std:: 11st -
.
VertexLi st vecS (-
clear_vertex() :1:
).
148 12 BGL
12.1 {)
BidirectionalGraph IncidenceGraph
graph_traits<G>::in_edge_iterator
in_edges(v, g) std ::pair<in_edge_iterator, in_edge_iterator>
in_degree(v, g) degree_size_type
degree(e, g) degree_size_type
AdjacencyGraph Graph
graph_traits<G>::adjacency_iterator .
adjacent_vertices(v, g) std::pair<adjacency_iterator, adjacency_iterator:
VertexListGraph Graph
graph_traits<G>::vertex_iterator
graph_traits<G>::vertices_size_type
num_vertices(g) vertices_size_type
vertices(g) std::pair<vertex_iterator, vertex_iterator>
EdgeListGraph Graph
graph_traits<G>::edge_descriptor
graph_traits<G>::edge_iterator
graph_traits<G>::edges_size_type
num_edges(g) edges_size_type
edges(g) std::pair<edge_iterator, edge_iterator>
source(e, g) vertex_descriptor
target(e, g) vertex_descriptor
AdjacencyMatrix Graph
edge(u, v, g) std::pair<edge_descriptor, bool>
12.1.1.
, BGL -
, , . ,
-
.
, (, v)
(, v) (v, ). -
. . 12.2 -
. ,
.
, BGL out_edges () ( 1 n_edges ()) -
. sourceO target
. -
. , -
12.1. 149
, BGL -
, .
. 12.2.
12.1 out_edges (),
source() target .
,
out_edges () -
, . in_edges(), , .
12.1.
template <typename UndirectedGraph> void undirected_graph_demol() {
const int V = 3;
UndirectedGraph undigraph(V);
typename graph_traits<UndirectedGraph>::vertex_descriptor zero. one. two;
typename graph_traits<UndirectedGraph>::out_edge_iterator out, out_end;
typename graph_traits<UndirectedGraph>::in_edge_iterator in, in_end;
zero = vertex(0. undigraph):
one - vertexd. undigraph):
two = vertex(2, undigraph);
add_edge(zero, one, undigraph);
add_edge(zero. two. undigraph):
add_edge(one, two, undigraph);
std::cout "(): ":
for (tie(out, out_end) = out_edges(zero. undigraph); out != out_end;
++out)
std::cout *out:
Std::COUt Std::endl "(): ";
for (tie(in. in_end) - in_edges(zero. undigraph); in != in_end; ++in)
std: :cout *in;
std: :cout std::endl;
:
(): (0,1) (0,2)
(): (1,0) (2.0)
,
,
152 12 BGL
graph_traits<G>::directed_category
: di rected_tag undi rected_tag.
graph_traits<G>::edge_parallel_category
, -
( ).
: al Iow_paral Iel_edge_tag disallow_paralle1_edge_tag.
graph_traits<G>::traversal_category
, .
:
struct incidence_graph_tag { };
struct adjacency_graph_tag { }:
struct bidirectional_graph_tag : public virtual incidence_graph_tag { };
struct vertex_list_graph_tag { };
struct edge_list_graph_tag { }:
struct adjacency_matrix_tag { };
12.1.3. IncidenceGraph
IncidenceGraph
. (out-edges) -
. out_edges(v, g)
v g -
. v,
. -
.
.
, -
.
Graph
IncidenceGraph.
graph_traits<G>::edge_descr1ptor
.
DefaultConstructible, Assignable EqualityComparable.
graph_traits<G>::out_edge_iterator
v -
v. edge_descriptor -
. Mult-
Passlnputlterator.
graph_traits<G>::degree_size_type
, -
.
12.1. 1 5 3
IncidenceGraph.
source(e, g)
: vertex_descriptor.
: (, v), -
.
: .
target(. )
: vertex_descriptor.
: v {, ), -
.
: .
out_edges(v, g)
: std: :pair<out_edge_iterator,out_edge_iterator>.
: , -
( ) (
) . v
. , v, -
(, ).
: v .
out_degree(v. g)
: degree_size_type.
: ( -
) ( ) -
V.
: v .
source(), target out_edges() -
. out_degree()
.
-1-1 1 P..J- _.- I/-.
1 2 . 1 . 4 . B i d i r e c t i o n a l G r a p h
BidirectionalGraph IncidenceGraph:
. -
IncidenceGraph,
, . -
, -
.
154 12 BGL
IncidenceGraph
BidirectionalGraph.
graph_traits<G>::in_edge_iterator
v
v. edge_descriptor .
MultiPassInput-
Iterator.
BidirectionalGraph.
in_edges(v. g)
: std: :pair<in_edge_iterator. in_edge_iterator>.
: ,
( ) ( -
) v. v
. , v , -
(, ).
: v .
in_degree(v, g)
: degree_size_type.
: ( )
( ) v.
: v .
degree(v, g)
: degree_size_type.
: ( -
) ( )
v.
: v .
i n_edges () , i n_degree ()
, .
12.1.5. AdjacencyGraph
AdjacencyGraph -
. -
.
.
12.1. 1 5 5
Graph
AdjacencyGraph.
graph_traits<G>::adjacency_iterator
v , -
v. -
. MultiPassInputlterator.
AdjacencyGraph.
adjacent_vertices(v. g)
: std: :pair<adjacency_iterator,adjacency_iterator>.
: , -
, v. :
v.
: v .
adjacent_verti ces() .
12.1.6. VertexListGraph
VertexListGraph
.
Graph
VertexListGraph.
g ph_t raits <G>::vertex_iterator
( verti ces (g))
. MultiPassInput-
Iterator. .
graph_traits<G>::vertices_size_type
,
.
VertexListGraph.
vertices(g)
: std: :pair<vertex_iterator. vertex_iterator>.
156 12 BGL
: ,
.
num vertices(g)
: vertices size type.
_
: .
verti ces . -
num_vertices() .
12.1.7. EdgeListGraph
EdgeListGraph Graph. -
.
Graph
EdgeListGraph.
graph_traits<G>::edge_descriptor
.
DefaultConstructible, Assignable EqualityComparable.
graph_traits<G>::edge_iterator
( edges (g)) -
. edgeiterator Inputlterator.
,
.
graph_traits<G>::edges_size_type
,
.
EdgeListGraph.
edges(g)
: std: :pair<edge_iterator, edge_iterator>.
: -
.
source(e, g)
: vertex_descriptor.
: -
(, v), .
: .
12.2. 157
target(e. g)
: vertex_descri ptor.
: v -
(, v), .
: .
nurn edges(g)
: edges_size_type.
: .
edges(), source() target .
num_edges() .
12.1.8. AdiacencyMatrix
AdjacencyMatrix Graph
-
.
Graph
AdjacencyMatrix.
edge(u. v, g)
: std:: pa i r<edge_descri ptor. boo! >.
: , , , -
g v, , .
: u, v g .
edge() .
12.2.
BGL- , -
,
. ,
, -
-
. . 12.2
, . 12.3 -
. , . 12.1,
. 12.3, .
158 12 BGL
VertexMutableGraph VertexMutablePropertyGraph
Graph EdgeMutablePropertyGraph
MutablelncidenceGraph
MutableBidirectionalGraph
EdgeMutableGraph BidirectionalGraph
12.2.
VertexMutableGraph Graph
addvertex(g) vertex_descriptor
remove_vertex(v, g) void
EdgeMutableGraph Graph
clear_vertex(v, g) void
add_edge(u, v, g) std::pair<edge_descriptor, bool>
remove_edge(u, v, g) void
remove_edge(e, g) void
MutablelncidenceGraph IncidenceGraph EdgeMutableGraph
remove_edge(eiter, g) void
remove_out_edge_if(u, p, g) void
MutableBidirectionalGraph MutablelncidenceGraph BidirectionalGraph
remove_edge(eiter, g) void
remove_out_edge_if(u, p, g) void
MutableEdgeListGraph EdgeMutabLeGraph EdgeListGraph
remove_edge_if(p, g) void
PropertyGraph Graph
property_map<G, PropertyTag>::type
property_map<G, Property-
Tag>::const_type
12.2. 159
get(ptag, g) -
get(ptag, g, x)
put(ptag, g, x, v)
v
VertexMutablePropertyGraph VertexMutableGraph PropertyGraph
add_vertex(vp, g) vertex_descriptor
EdgeMutablePropertyGraph EdgeMutableGraph PropertyGraph
add_edge(u, v, ep, g) std::pair<edge_descriptor, bool>
12.2.1. VertexMutableGraph
VertexMutableGraph ,
(vertex mutable), . -
. -
add_vertex() remove_vertex(), .
Graph, DefaultConstructible
VertexMutableGraph.
add vertex(g)
-
: vertex_descri ptor.
: . -
.
remove_vertex(u. g)
: void.
. : .
: g , -
. clear_vertex()
.
: num_vertices(g) ;
,
.
VertexMutableGraph :
-
;
(\ | + \V |).
160 12 BGL
12.2.2. EdgeMutableGraph
EdgeMutableGraph , (edge
mutable), .
. -
add_edge() remove_edge(), .
Graph
EdgeMutableGraph.
add_edge(u. v. g)
: std: :pair<edge_descriptor. bool>.
: (, v) ,
, ,
.
,
. ,
, .
: (, ) .
: (, v) .
v .
,
. {, v)
, . {, v) -
, .
remove_edge(u, v. g)
: void.
: (, v) . -
, (, v) .
: {, v) .
: {, v) .
remove_edge(e, g)
: void.
: .
: .
: .
clear_vertex(u, g)
: void.
: , .
: .
:
.
12.2. 1 6 1
EdgeMutableGraph :
, O(log(| |/| V|)), -
;
(\ |);
(\ | +
+ \V\).
12.2.3. MutabielncidenceGraph
MutabielncidenceGraph
.
IncidenceGraph EdgeMutableGraph
MutabielncidenceGraph.
remove_edge(eiter, g)
: void.
: , enter, , eiter
.
: *eiter .
:
j *eiter
i * .
remove_out_edge_if(u. p. )
1 : void.
: ,
. ,
IncidenceGraph.
: .
:
, , .
MutabielncidenceGraph :
remove_edge() ;
remove_out_edge_if()
.
12.2.4. MutableBidirectionalGraph
MutableBidirectionalGraph
.
6 375
162 12 BGL
BidirectionalGraph MutablelncidenceGraph
MutabLeBidirectionaLGraph.
remove_in_edge_if(v, p, g)
: void.
: v,
.
: v .
:
, , .
MutabLeEdgeListGraph :
remove_i n_edge_i f ()
.
12.2.5. MutableEdgeListGraph
MutableEdgeListGraph
.
EdgeMutabLeGraph
MutableEdgeList-
Graph.
remove_edge_if(p. g)
: void.
: , .
: -
, .
MutableEdgeListGraph :
remove_edge_i f ()
.
12.2.6. PropertyGraph
PropertyGraph , ,
. -
, ,
. PropertyTag ,
12.2. 1 6 3
a tag PropertyTag. , -
.
Graph
PropertyGraph.
property_map<G. PropertyTag>::type
, PropertyTag. -
LvaluePropertyMap , -
.
property_map<G. PropertyTag>::const_type
, PropertyTag.
LvaluePropertyMap -
, .
PropertyGraph.
getCptag. g)
: property_map<G. PropertyTag>: :type, g -
property_map<G. PropertyTag>:: const_type .
: ,
PropertyTag. ptag .
getCptag. g. x)
: property_traits<PMap>: :value_type.
: ( PropertyTag), -
( ). ptag
. get (get (ptag. g). x).
get .
12.2.7. VertexMutablePropertyGraph
VertexMutablePropertyGraph VertexMutableGraph PropertyGraph -
-
.
VertexMutableGraph PropertyGraph
VertexMutablePropertyGraph.
vertex_property<G>::type
-, .
164 12 BGL
VertexMutaropertyG h.
add_vertex(vp, g)
: vertex_descriptor.
: vp -
. .
VertexMutabLePropertyGraph :
add_vertex()
.
12.2.8. EdgeMutablePropertyGraph
EdgeMutablePropertyGraph EdgeMutableGraph PropertyGraph -
.
EdgeMutableGraph PropertyGraph
EdgeMutablePropertyGraph.
edge_property<G>::type
-, .
EdgeMutablePropertyGraph.
add_edge(u. v, ep. g)
: std: :pair<edge_descriptor, bool>.
: (, v) --
.
: u, v .
EdgeMutablePropertyGraph :
, O(log(| |/| V |)),
.
12.3.
(visitor concepts) BGL, -
(functors) STL. -
. one-
12.3. 165
. STL
,
(callback) .
, operator^), ,
, .
BGL.
STL,
BGL. , -
-.
, , :
V , ;
vis V;
G , Graph;
g G;
graph_traits<G>: :edge_descriptor;
s, u graph_traits<G>: :vertex_descriptor.
12.3.1. BFSVisitor
BFSVisitor
breadth_fi rst_search(). BFSVisitor
breadth_f i rst_sea rch (), -
, .
CopyConstructible
BFSVisitor.
vis.initialize_vertex(u. g)
: void.
: .
vis.discover_vertex(u. g)
: void.
: , -
. , , -
, , , .
vis.examine_edge(e. g)
: void.
: , -
.
vis.tree_edge(e. g)
: void.
166 12 BGL
: ,
. exami ne_edge(),
vis.non_tree_edge(e. g)
: void.
: , -
.
examine_edge().
, , .
vis.gray_target(e. g)
: void.
: ,
. cycle_edge().
, .
vis.black target(e. g)
: void.
: ,
. cycl e_edge().
, .
vis.finish_vertex(u. g)
: void.
: , -
-
( , ).
1 2 . 3 . 2 . DFSVisitor
DFSVisitor
depth_first_search(). DFSVisitor
depth_first_search(), -
, .
CopyConstructible
DFSVisitor.
vis.initialize_vertex(u. g)
: void.
:
.
vis.start_vertex(s, g)
: void.
12.3. 167
: .
vis.discover_vertex(u. g)
: void.
: ,
.
vis.examine_edge(e. g)
: void.
: -
.
vis.tree_edge(e. g)
: void.
: , -
.
vis.back_edge(e. g)
: void.
: .
-
, (, v) (v, ) ,
tree_edge() back_edge() . -
, -
, .
tree_edge().
vis.forward_or_cross_edge(e. g)
: void.
: . -
.
vis.finish vertexCu. g)
: void.
: , finishvertexO
.
, finish_vertex() -
.
12.3.3. DijkstraVisitor
DijkstraVisitor di jkstra_shortest_-
paths () . ,
, di jkstra_shortest_paths()
, .
CopyConstructible
168 12 BGL
DijkstraVisitor.
vis.discover_vertex(u. g)
: void.
: , .
vis.examine_edge(e. g)
: void.
: -
.
vis.edge_relaxed(e. g)
: void.
: (, v) , d , a w -
. d[u] + w(u, v) < d[v],
( ) .
vis.edge_not_relaxed(e. g)
: void.
: ,
.
vis.finish_vertex(u. g)
: void.
: ,
].i
( , ).
1 2 . 3 . 4 . Bel I m a n F o r d V i s i t o r
BellmanFordVisitor bel lman_ford -
shortest_paths(). BellmanForc-
Visitor bel I man_ford_shortest_paths() -
vi si tor () , >'.
CopyConstructible
Bellman FordVisitor.
vis.initialize_vertex(s, g)
: void.
:
.
vis.examine_edge(e. g)
: void.
: num_vertices(g) .
12.3. 169
vis.edge_relaxed(e. g)
: void.
: (, v) , d , w -
. d[u] + w(u, v) < d[v], ( -
) .
vis.edge_not_relaxed(e. g)
: void.
: (. ),
.
vis.edge_mi ni mi zed(e. g)
: void.
: num_vert i ces (g)
, -
. , . (, v) -
, d[u] + w(u, v) > d[v].
vis.edge_notjninimized(e. g)
: void.
: , .
, .
BGL
13.1.
-
Boost Graph Library.
BGL :
1. .
2. .
3. .
4. .
5. .
6. .
,
. -
, . -
,
, .
, -
.
, ,
, .
, . -
, -
(call-backs),
.
13.1. 171
13.1.1.
:
, , , , -
, , , .
.
,
.
.
bgl _named_pa rams. -
, 2.7
. params = al I defaul ts, -
, .
, , -
, -
. -
.
, -
( #i ncl ude) .
( -
). (
).
:
IN
.
;
OUT
.
OUT-;
UTIL
, ,
UTIL-, .
, .
2.7, BGL
,
172 13 BGL
. -
, ,
. ,
.
. -
.
0-.
O(|F|), .
13.2.
13.2.1. breadth_first_search
tempa l te <ytpename Graph, ytpename P. ytpename T. ytpename R>
void breadth_frist_search(Graph& g,
ytpename graph_tratis<Graph>:vertex_descrp
i tor s.
constbg_lnamed_params<P, T. R>& params)
breadth_first_search() [31] -
. -
, ,
.
. breadth_fi rst_search()
. -
, ,
4.1.
:
.
, ,
. , ,
, .
, {, v).
v ,
.
. -
. -
, d t. -
breadth_fi rst_search() ,
, .
13.2. 1 7 3
_ _ ( , s)
V[G] >
color[u] <-
d[u] <
7l\u\*r-U
color[s] - /
4s] -
_(<2,5) > s
( Q * 0 )
<- _() >
v e Adj[u] > (, v)
(/[] = ) > (, v)
color[v] <-
d[v] <- d[u] + 1
7t[v]<r-U
_2, ) > v
(|] = ) > (, v)
> (, v)
breadth_first_search() -
, .
-, -
, BFSVisitor. -
. breadth_-
fi rst_search() ,
.
.
boost/graph/breadth_first_search.hpp.
breadth_fi rst_search().
IN: Graph& g
, -
Vertex Li stG ra ph IncidenceGraph.
IN: vertex_descriptor s
, .
174 13 BGL
breadth_fi rst_search().
IN: visitor(BFSVisitor vis)
-,
, BFSVisitor.
: def aul t_bf s_vi si tor.
UTIL/OUT: color_map(ColorMap color)
. Col -
ReadWritePropertyMap, -
, col orjnap ColorValue.
: iterator_property_map, std:: vector -
default_color_type num_vertices(). ijnap
.
IN: vertex_index_map(VertexIndexMap ijnap)
[, \V\), -
. VertexIndexMap ReadablePropertyMap.
.
.
: get(vertex_index, g).
UTIL: buffer(Buffer& Q)
,
. FIFO (
), -
. . ,
-
. Buffer Buffer.
: boost::queue.
(\\ + \V\). -
0(1 ) .
( 13.1)
, . 13.1.
. ,
.
example/bfs-exampLe.cpp.
13.2. 175
. 13.1.
13.1.
< >
template < typename TimeMap > class bfs_time_visitor
: public default_bfs_visitor {
typedef typename property_traits < TimeMap >::value_type T:
public:
bfs_time_visitor(TimeMap tmap. T & t):m_timemap(tmap). m_time(t)
template < typename Vertex, typename Graph >
void discover_vertex(Vertex u. const Graph & g) const {
put(m_timemap. u, m_time++):
}
TimeMap m_timemap;
T & m time:
( bfs-example.cpp )
#include <boost/graph/adjacencyji st.hpp>
#i nclude <boost/graph/breadth_fi rst_search.hpp>
#i nclude <boost/pendi ng/i ndi rect_cmp.hpp>
#include <boost/pending/integer^range.hpp>
#indude <iostream>
using namespace boost:
( )
int mainO {
using namespace boost:
// ,
typedef adjacencyjist < vecS, vecS, undirectedS > graph_t;
//
enum { , s, t. u. v. w, x, y. N }:
const char *name = "rstuvwxy";
//
typedef std::pair < int. int >E:
E edge_array[] - { E(r. s), E(r, v), E(s. w). E(w, r), E(w, t).
E(w. x), E(x, t), E(t. u). E(x. y ) , E(u, y)
}
//
const int n_edges sizeof(edge_array) / sizeofCE):
176 13 BGL
13.1 {)
typedef graph_traits<graph_t>::vertices_size_type v_size_t;
graph_t g(edge_array. edge_array + n_edges. v_si,:e_t(N));
//
typedef graph_traits < graph_t >::vertexjjescriptor Vertex;
typedef graph_traits < graph_t >::vertices_size_type Size;
typedef Size* l i t e r ;
// " "
std::vector < Size > dtime(numj/ertices(g));
Size time = 0;
bfs_time_visitor < Size * >vis(&dtime[0]. time);
breadth first search(g, vertexts. g). visitor(vis));
// std::sort
std: :vector<graph_traits<graph_t>;:vertices_size type > discover_order(N):
integer_range < int >range(0. N);
std::copy(range.beginO. range.end(). discover_order.begin()):
std::sort(discover_order.begin(). discover_order.end(),
indirect_cmp < liter. std::less < Size > >(&dtime[0])):
std::cout " : ";
for ( i n t i = 0: i < N; ++i)
std::cout name[discover_order[i]] " ";
Std::cout Std::endl;
return EXITJUCCESS;
}
:
: s r w v t x u y
13.2.2. breadth_first_visit
IN: vertex_descriptor s
, .
breadth_first_visit().
IN: vi sitor(BFSVi si tor vis)
-,
, BFSVisitor.
: bfs visitor<null visitor>.
IN/UTIL/OUT: color_map(ColorMap color)
. -
breadth_f i rst_sea rch ().
Col ReadWritePropertyMap, -
, colorjnap CoLorValue.
: get(vertex_color. g).
UTIL: buffer(Buffer& Q)
,
. FIFO (
), -
. . ,
.
Buffer Buffer.
: boost: : queue.
13.2.3 depth_first_search
template <typename Graph, typename P. typename T, typename R>
void depth_first_search(Graph& g. const bgl_named_params<P. T. R>& params)
depth_f i rst_sea rch ()
. , ,
, . -
,
, . -
,
. , .
.
4.2 .
,
. ,
, . -
, .
depth_f i rst_search()
. -
,
.
178 13 BGL
. , ,
- , -
DFSVisitor. ,
d /.
depth_first_search() , -
- .
__()
V >
color [] <-
[] =
time <-
V
(color [] = )
call _
__( G,u) >
(, d, f)
___( )
color [v] <- >
d[u] <r- time 4- time + 1
v e Adj[u]
(color [v] = ) > (, v)
7r[v] = U
color [v] 4-
call _
__(, > (, v)
(color [v] = )
> (, v)
(color[v] = )
> (, v)
color [] <- >
f [] <- time <- time + 1
boost/graph/depth_first_search.hpp.
depth_fi rst_search().
IN: Graphs g
, -
VertexListGraph IncidenceGraph.
depth_fi rst_search().
IN: visitor(DFSVisitor vis)
13.2. 179
-,
, DFSVisitor.
: default_dfs_vi si tor.
UTIL/OUT: colorjnap(ColorMap color)
. Col
ReadWritePropertyMap,
, color_map CoLorValue.
: i terator_property_map, std:: vector -
THnadefault_color_type num_vertices(). ijnap
.
IN: vertex_indexjriap(VertexIndexMap ijnap)
[, \V\).
, -
. VertexIndexMap ReadablePropertyMap.
.
.
: get(vertex_index. g).
( 13.2) ,
. 13.2. . -
example/dfs-example.cpp.
. 13.2.
13.2.
(
> =
template < typename TimeMap >
180 13 BGL
13.2 {)
class dfs_time_visitor:public default_dfs_visitor {
typedef typename property_traits < TimeMap >::value_type T:
public:
dfs_time_visitor(TimeMap dmap. TimeMap fmap. T & t)
: m dtimemap(dmap), m_ftimeinap(fmap). m_time(t) {
} "
template < typename Vertex, typename Graph >
void discover_vertex(Vertex u. const Graph & g) const {
put(m_dtimemap. u, m_time++):
}
template < typename Vertex, typename Graph >
void finish_vertex(Vertex u, const Graph & g) const {
put(m_ftimemap, u, m_time++);
}
TimeMap m_dtimemap;
TimeMap m_ftimemap;
T & m_time;
< dfs-example.cpp >
linclude <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_fi rst_search. hpp>
#i nclude <boost/pendi ng/i nteger_range.hpp>
#i ncl ude <boost/pendi ng/i ndi rect_cmp.hpp>
linclude <iostream>
using namespace boost:
<
)
int mainO {
// ,
typedef adjacency_list < vecS, vecS, directedS > graph_t:
typedef graph_traits < graph_t >::vertices_size_type sizejtype:
//
enum
{ u. v, w, , , z, N }:
char name[] = { V . '', 'w', 'x', 'y', 'z' }:
//
typedef std::pair < int. int >E;
E edge_array[] = { E(u. v). E(u. x), E(x. v). E(y, x),
E(v. y ) . E(w. y ) . E(w. z). E(z. z)
graph_t g(edge_array, edge_array + sizeof(edge_array) / sizeof(E). N);
//
typedef boost::graph_traits < graph_t >::vertex_descriptor Vertex:
typedef size_type* liter;
// " "
" "
std::vector < size_type > dtime(num_vertices(g));
std::vector < size_type > ftime(num_vertices(g)):
size_type t = 0;
dfs_time_visitor < size_type * >vis(&dtime[0], &ftime[0], t ) ;
depth_first_search(g, visitor(vis)):
// std::sort
std::vector < size_type > discover_order(N):
13.2. 1 8 1
:
: u v w z
: v u w
13.2.4. depth_first_visit
template <typename IncidenceGraph. typename DFSVisitor, typename ColorMap>
void dept,h_first_visit(IncidenceGraph& G,
typename graph_traits<IncidenceGraph>::vertex_descriptor s,
DFSVisitor vis. ColorMap color):
depth_fi rst_vi sit .
depth_f i rst_sea rch (),
. . depth_fi rst_-
searchO.
depth_fi rst_v 1 si t () boost/graph/depth_first_search.hpp.
depth_first_visit().
IN: IncidenceGraph& g
, -
IncidenceGraph.
IN: vertex_descriptor s
, .
IN: DFSVisitor visitor
-,
, DFSVisitor.
UTIL: ColorMap color
. !
ReadWritePropertyMap,
, colorjnap ColorValue.
182 13 BGL
(||) O(\V|).
13.2.5. t o p o l o g i c a l _ s o r t
template <typename Graph, typename Outputlterator.
typename P, typename T, typename R>
void topological_sort(Graph& G. Outputlterator result,
const bgl_namedj)arams<P. T. R>& params = all defaults)
topological_sort() ,
, , (, v) -
, , >.
.
result.
.
. std:: vector ||
result. back_insert_1 terator -
, std:: reverse!).
front_i nsertjterator std:: 1 i st std:: deque.
[ 10]. 1.4.1
,
3
.
boost/graph/topologicaL_sort.hpp.
topological_sort.
IN: Graphs g
, -
VertexListGraph IncidenceGraph.
IN: Outputlterator result
.
Outputlterator , -
Outputlterator.
topol ogical_sort .
UTIL/OUT: colorjnapCColorMap color)
. Col
ReadWritePropertyMap,
, color_map ColorValue.
: iterator_property_map, std::vector -
THnadefault_color_type, nurn_vertices(). ijnap
.
13.3. 183
O(|V| + \\)
. 1.4.1, top~logical_sort().
13.3.
13.3.1. dijkstra_shortest_paths
tempa l te <ytpename Graph, ytpename P. ytpename T. ytpename R>
void dijkstra_shortest_paths(const Graphs g.
typename graph_tratis<Graph>:vertex_descrp
i tor s,
constbg_lnamed_params<P. T,R>& params)
[10, 11]
,
. ,
, -,
, .
5.1.
di jkst ra_shortest_paths ().
di stan-
cejnapO.
. -
:
V, [] (,
, [] = , ,
). -
, -
.
, S,
.
S .
V - 5, -
1
, .
, , , V- 5 -
, V - S,
.
184 13 BGL
5,
.
, v, -
v . -
. , .
(, ) -
.
5, V- 5. ,
.
.
-
color_map().
. w , d ,
,
. Q ,
_.
.
(,5,)
V t>
d\u] <-
[]<-
color[u] <-
color[s] <-
d[s] <-
((2, s) > s
( Q * 0 )
<- _ (Q) >
v e Adj[u] > (, v)
{w{u, v) + d[u] < d[v])
d[v] <- w(u, v) + d[u] > (, v)
7t[v] <-
(color[v] - )
color[v] <-
BCTABHTb(Q, v) > v
(color [v] = )
_ ( 2 , v, w(u, v) + d[u\)
> (, v)
color [] <- >
(d, )
13.3. 185
boost/graph/dijkstra_shortest_paths.hpp.
di jkstra_shortest_paths().
IN: const Graph& g
, . Graph
VertexListGraph IncidenceGraph.
IN: vertex_descriptor s
. ,
.
di jkstra_shortest_paths() .
IN: weight_map(WeightMap wjrap)
. WeightMap
ReadablePropertyMap.
.
, .
: get(edge_weight. g).
IN: vertex_index_map(VertexIndexMap ijnap)
[, |V|). -
-
. VertexIndexMap ReadablePropertyMap.
.
.
: get(vertex_index, g).
OUT: predecessor_map(PredecessorMap pjnap)
. ( [], ) V -
. [] = , , -
, . PredecessorMap -
ReadWritePropertyMap ,
.
: dummy_property_map.
UTIL/OUT: distance_map(DistanceMap d_map)
. ,
. DistanceMap ReadWrite-
PropertyMap.
. Monoid, -
combi ne zero -
(. 16).
186 13 BGL
StrictWeakOrdering ( ), --
compare.
: iterator_property_map, std:: vector -
, WeightMap, num_vertices().
imap .
IN: distance_combine(BinaryFunction combine)
-, Monoid -
. -
.
: cl osed_pl us<D>, D .
closed_plus boost/graph/relax.hpp.
IN: distance_compare(BinaryPredicate compare)
-, StrictWeakOrdering .
, .
: std: :less<D>, D .
IN: distance_inf(D inf)
inf D.
compare(d. i n f ) == true d != inf. D DistanceMap.
: std: :numeric_limits<D>: :max().
IN: distance_zero(D zero)
Monoid,
- combi ne. D
DistanceMap.
: D.
UTIL/OUT: color_map(ColorMap cjnap)
. -
,
.
. , , -
. . Col -
ReadWritePropertyMap.
, -
Col orValue.
: iterator_property_map, std:: vector -
defaul t_col or_type, num_verti ces (). ijnap
.
IN: visitor(Vis v)
,
. Vi s
DijkstraVisitor.
: defaul t_di jkstra_vi si tor.
13.3. 187
13.3 example/dijkstra-
example.cpp. , , . 13.3.
.
. 13.3. ,
13.3.
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 graph_traits < graph_t >::edge_descriptor edge_descriptor;
typedef std::pair<int. int> Edge;
const int numjiodes - 5:
enum nodes { A . B, C O . E } :
char narae[] = "ABCDE";
Edge edge_array[] = { Edge(A, C). Edge(B, B). Edge(B. D), Edge(B, E),
Edge(C. B). Edge(C, D). Edge(O. E). Edge(E. A), Edge(E. B)
int weights!!] = { 1 , 2 , 1, 2. 7, 3, 1. 1, 1 };
int nutn arcs - sizeof(edge array) / sizeof(Edge):
13.3 {)
for (tie(vi, vend) = vertices(g): vi ! = vend: ++vi) {
std::cout "distanceC name[*vi] ") = " d[*vi] ". ":
std::cout "parent(" name[*vi] ") = " name[p[*vi]] std::
endl;
Std: :cout std: :endl:
:
:
distance(A) = 0, parent(A) = A
distance(B) = 6. parent(B) -
distance(C) = 1. parent(C) -
distance(D) - 4, parent(D) -
distance(E) - 5. parent(E) = D
13.3.2. bellman_ford_shortest_paths
template <typename EdgeListGraph. typename Size,
typename P, typename T. typename R>
bool bellman_ford_shortest_paths(EdgeListGraph& g. Size N,
const bgljiamed_params<P. T, R>& params)
- [5, 10,13, 26]
.
5.1.
, -
. -
, .
be! I man_ford_shortest_paths () -
( Monoid,
combi ne),
( -
, compare). std:: numeri c_ i -
mits<D>: :max() , D -
. -
, .
v , u, w
, d ,
.
{u, v, w, d)
(w(u, v) + d[u] < d[v\)
d[v] <r- w(u, v) + d[u]
|| , , -
, -
. ,
, , (, v),
w(u, v) + d[u] < d[v].
, , , ,
, .
13.3. 189
bel lman_-
ford_shortest_paths().
distancejnapO,
(-
, ).
, -
predecessor_map(). -
,
(. Bellman FordVisitor). -
, -
defau1t_beTlman_vi si tor , -
.
- boost/graph/bellrnan_ford_shortest_paths.hpp.
bel lman_ford_shortest_paths().
IN: EdgeListGraphS g
, -
EdgeListGraph.
IN: Size N
. Size .
bel lman_ford_shortest_paths() .
IN: weight_map(WeightMap w)
( ) .
Wei ghtMap ReadablePropertyMap.
.
, .
: get(edge_weight. g).
OUT: predecessor_map(PredecessorMap pjnap)
. ([], ) VM e V -
. [] = ,
, . PredecessorMap
ReadWritePropertyMap, -
, .
: dummy_property_map.
UTIL/OUT: distance_map(DistanceMap djnap)
s -
.
190 13 BGL
, . DistanceMap Read-
WritePropertyMap.
. Monoid,
combi ne zero -
. StrictWeakOrdering
( ), -
compare.
: get(vertex_distance. g).
IN: visitorCBeTlmanFordVisitor v)
-,
, Bellman FordVisitor.
defaul t_bel lman_vi sitor, .
: defaul t_bel I man_vi si tor.
IN: distance_combine(BinaryFunction combine)
-, Monoid
. -
.
: el osed_p! us<D>, D
c!osed_p1us boost/graph/relax.hpp.
IN: distance_compare(BinaryPredicate compare)
-, StrictWeakOrdering
, .
: std:: 1 ess<D>, D ,
O(\V| x \ |).
13.4 example/bellman-
example.cpp. , , . 13.4.
13.4. -
{ u . v . x . y . z . N } ;
char name[] = { V , V , ' ' . ' ' , 'z' }:
typedef s t d : : p a i r < i n t . i n t >E:
const i n t n_edges = 10;
E edge_array[] - { E(u, y ) . E(u. x). E(u. v ) . E(v, u),
E(x. y ) . ECx. v). E(y. v). E(y. z ) . ECz. u). E(z.x) }:
i n t weight[n_edges] = { -4. 8. 5. -2, 9. -3. 7. 2. 6, 7 }:
i f (r)
for ( i = 0 ; i < N; ++i)
std::cout name[i] ": " std::setw(3) distance[i]
" " name[parent[i]] std::endl:
else
std::cout " " std::endl:
:
: 2 v
v: 4
: 7 z
: -2
z: 0z
. 13.4. , -
13.3.3. johnson_all_pairs_shortest_paths
johnson_al l_pairs_shortest_paths() ( )
. -
fal se, , true -
. D.
,
O(\V\ \E | log |V\).
boost/graph/johnson_all_pairs_shortest_paths.hpp.
johnson_an_pairs_shortest_paths() .
IN: const Graph& g -
, . Graph
VertexListGraph, IncidenceGraph EdgeListGraph.
OUT: DistanceMatrix& D
D[M][W].
johnson_al l_pai rs_shortest_paths()
.
IN: weight_map(WeightMap wjnap)
. WeightMap
ReadabLePropertyMap.
. ,
.
: get (edge_weight. g).
UTIL: weight_map2(WeightMap2 w_map2)
. WeightMap2
ReadablePropertyMap. *
. ,
.
: get (edge_wei ght2. g).
IN: vertex_index_map(VertexIndexMap ijnap)
[, ||). -
-
. Vertex IndexMap ReadablePropertyMap.
.
.
: get (vertex_i ndex. g).
UTIL/OUT: distance_map(DistanceMap d_map)
s g -
.
13.3. 193
, . Di stanceMap ReadWrite-
PropertyMap.
. Monoid, -
zero .
LessThanComparable.
: iterator_property_map, std:: vector -
, Wei ghtMap, num_verti ces (). -
ijnap .
IN: distance_zero(D zero)
Monoid, -
. D Di stanceMap.
: DO.
(-
13.5) . 568 Introduction to Algorithms [10],
. 13.5. D[u][v]
UKV.
. 13.5. ,
13.5.
typedef adjacency_list<vecS. vecS, directedS. no_property.
property< edge_weight_t. int. property< edge_weight2_t, int > > > Graph:
const int V = 6:
typedef std::pair < int, int >Edge;
Edge edge_array[]
{ Edge(O. 1). Edge(O. 4). EdgefO. 2), Edged. 3), Edged. 4).
Edge(2. 1). Edge(3. 2). Edge(3. 0). Edge(4. 3)
'
7 375
194 13 BGL
13.5 {)
const std::size_t E = sizeof(edge_array) / sizeof(Edge):
Graph g(edge_array, edge_array + E, V):
property_map < Graph, edge_weight_t >::type w = get(edge_weight, g);
int weights[] = { 3, -4. 8. 1, 7. 4. -5. 2. 6 };
int *wp = weights;
graph_traits < Graph >::edge_iterator e, e_end:
for (boost::tie(e, e_end) = edges(g): e != e_end; ++e)
w[*e] = *wp++:
std::vector < int >d(V, std::numeric_limits < int >::max()):
int D[V][V];
johnson_all_pairs_shortest_paths(g. D. distance_map(&d[O])):
:
0 1 2 3 4 5
0 0 0 -1 -5 0 -4
1 inf 0 1 -3 2 -4
2 inf 3 0 -4 1 -1
3 inf 7 4 0 5 3
4 inf 2 -1 -5 0 -2
5 inf 8 5 1 6 0
13.4.
13.4.1. kruskal_minimum_spanning_tree
template <typename Graph, typename Outputlterator.
typename P. typename T, typename R>
void kruskal_minimum_spanning_tree(Graph& g,
Outputlterator spanning_tree_edges,
const bgl_named_params<P. T, R>& params = all defaults)
kruskal jmmmum_spanrnng_tree() -
. -
, ,
. kruskal jTiinimum_spanmng_tree() -
spanning_tree_edges,
[10, 18,23,44].
,
, ,
.
. ,
. -
-
(-
, - -).
.
13.4. 195
___(, w)
V
_(5, )
<-0
(, v) e E
_(5, ) _(5, V)
_(5, , v)
boost/graph/kruskal_mimmum_spanning_tree.hpp.
kruskal_minimum_spanning_tree().
IN: const Graph& g
. Graph VertexListGraph
EdgeListGraph.
IN: Outputlterator spanning_tree_edges
Outputlterator.
kruskal_minimum_spanning_tree() .
IN: weight_map(WeightMap wjnap)
. WeightMap
ReadablePropertyMap, LessThanComparable.
.
: get(edge_weight, g).
UTIL: rank_map(RankMap rjnap)
RankMap ReadWritePropertyMap. -
.
.
: iterator_propertyjnap, std:: vector -
, numverticesO. ijrap -
.
UTIL: predecessor_map(PredecessorMap pjnap)
PredecessorMap ReadWritePropertyMap.
.
: iterator_property_map, std:: vector -
, num_vertices(g).
i_map .
196 13 BGL
(\ | log \ |).
. 13.6. ,
13.6.
typedef adjacencyjist < vecS. vecS. undirectedS.
no_property. property < edge_weight_t. int > > Graph;
typedef graph_traits < Graph >::edge_descriptor Edge:
typedef graph_traits < Graph >::vertex_descriptor Vertex:
typedef std::pair<int. int> E:
const int numjnodes = 5;
E edge_array[] - { E(0. 2). E(l. 3). Ed, 4), E(2. 1), E(2. 3).
E(3. 4). E(4, 0). E(4, 1)
}:
int weights[] = { 1 , 1 , 2. 7. 3. 1. 1. 1 }:
int num_edges = sizeof(edge_array) / sizeof(E):
Graph g(edge_array. edge_array + num_edges. weights, num_nodes):
property_map < Graph. edge_weight_t >::type weight = get(edge_weight. g);
std::vector < Edge > spanning_tree;
kruskal_minimum_spanning_tree(g. std::back_inserter(spanning_tree));
std::cout " :" std::endl:
for (std::vector < Edge >:iterator ei = spanning_tree.begin():
ei !- spanning_tree.end(): ++ei) {
std::cout source(*ei, g) " <--> " target(*ei, g)
" " weight[*ei] std::endl;
13.4. 197
:
:
0 <--> 2 1
3 <--> 4 1
4 <--> 0 1
1 <--> 3 1
13.4.2. prim_minimum_spanning_tree
template <typename Graph, typename PredecessorMap,
typename P. typename T, typename R>
void primjninimiim_spanning_tree(Graph& G, PredecessorMap pjnap,
const bgl_named_params<P. T. R>& params = all defaults)
pnm_minimum_spanmng_tree()
. -
, ,
. pri mjni ni mum_spanni ng_tree()
: v e V,
p[v] v . -
[10, 18, 38, 44].
-
, -
1 . -
. ,
.
, . -
, ([], ) -
.
.
___(, , w)
V >
d[u] <-
[] <
color[u\ <-
colour] <-
d[r] <- 0
_(<2, ) >
Q, * 0
<- _((2) >
v (//[] > (, v)
BGL -
distance_compare() distance_combine().
198 13 BGL
boost/graph/prim_minirnum_spanning_tree.hpp.
prim_minirnum_spanning_tree().
IN: const Graph& g
, . Graph
VertexListGraph IncidenceGraph.
OUT: PredecessorMap pjnap
. ([], ) VM e V -
. [] = ,
, . PredecessorMap
ReadWritePropertyMap, -
, .
prim_minimum_spanning_tree()
.
IN: root_vertex(vertex_descriptor )
, . -
,
.
: *vertices(g). fi rst.
IN: weight_map(WeightMap wjrap)
. WeightMap
ReadablePropertyMap.
. ,
.
: get (edge_wei ght. g).
13.4. 199
(\ \ log \V|).
13.7 example/prim-example.cpp.
13.7.
typedef adjacencyjist < vecS. vecS. undirectedS.
property<vertex_distance_t. int>. property < edge_weight_t. int > > Graph:
typedef std::pair < int. int >E;
const int numjiodes = 5:
E edges[] = { E(0. 2). E(l.l). Ed. 3). Ed. 4). E(2. 1). E(2. 3).
E(3. 4). E(4, 0)
int weights[] = { 1 , 2 . 1. 2, 7. 3. 1. 1 };
Graph g(edges. edges + sizeof(edges) / sizeof(E). weights, numjiodes):
>
200 13 BGL
13.7 {)
property_map<Graph. edge_weight_t>::type weightmap = get(edge_weight. g);
std::vector < graph_traits < Graph >: :vertex_deschptor >
p(num_vertices(g));
prim minimum spanning tree(g. &p[0]);
for (std::size_t i = 0: i !- p.sizeO: ++i)
if (p[i] !- i)
std::cout "parent[" i "] = " p[i] std::endl:
else
std::cout "parent[" i "] = no parent" std::endl;
:
parent[O] -
parent[l] -3
parent[2] =
parent[3] =4
parent[4] =0
13.5.
13.5.1. connected_components
template <typename Graph, typename ComponentMap.
typename P. typename T. typename R>
typename property_traits<ComponentMap>::value_type
connected_components(const Graphs g, ComponentMap c.
const bgl_named_params<P, T. R>& params = all defaults)
connected_components () -
, , . -
,
. -
, ,
( incremental_components()), .
[10].
, -
. -
.
boost/graph/connected_components.hpp.
connected_components().
IN: const Graphs g
. Vertex Li stG ra ph
IncidenceGraph.
OUT: ComponentMap
13.5. 2 0 1
, , -
. -
, ,
. ComponentMap WritablePropertyMap.
vertices_size_type . -
.
connected_components().
UTIL: color_map(ColorMap color)
. Col
ReadWritePropertyMap,
, col orjnap ColorValue.
: i terator_propertyjnap, std:: vector -
default_color_type, num_vertices(). ijnap
.
IN: vertex_index_map(VertexIndexMap ijnap)
[0, | V |).
, -
. VertexIndexMap ReadablePropertyMap.
. -
.
: get(vertex_index. g).
(\ V \ + \ |). -
.
-
13.8.
13.8.
< connected-components. > =
#include <boost/config.hpp>
#i nclude <iostream>
#include <vector>
#i nclude <boost/graph/connected_components.hpp>
#include <boost/graph/adjacency_li st.hpp>
i n t mainO {
using namespace boost:
typedef adjacency_list < vecS. vecS. undirectedS > Graph:
typedef graph_traits < Graph >::vertex_descriptor Vertex:
const int N = 6:
Graph G(N):
add_edge(O, 1. G ) :
202 13 BGL
13.8 {)
add_edge(l. 4. G):
add_edge(4. 0. G);
add_edge(2. 5. G):
std::vector<int> c(num_vertices(G)):
int num = connected_components
(G. make_iterator_property_map(c.begin(). get(vertex_index, G),
Std: :cout Std: :endl;
std::vector < int >::iterator 1;
std::cout " : " num std::endl;
for (i = c.beginO; i != c.endO; ++i)
std::cout " " i - c.beginO
" " *i std::endl:
std::cout std::endl :
return EXIT_SUCCESS:
:
: 3
0
1
2 1
3 2
4
5 1
13.5.2. strong_components
template <class Graph, class ComponentMap. class P. class T. class R>
typename property_traits<ComponentMap>::value_type
strong_components(Graphs g. ComponentMap comp,
const bgl_named_params<P, T, R>& params = all defaults)
strong_components () -
, -
[43].
comp,
.
. -
.
boost/graph/strong_components.hpp.
G = (V, )
(/, , UWVQU -
v, v , .
-
. -
.
13.5. 203
( -
), -
.
.
d[u], -
root[u] - . root
. root[u] = ,
, .
, . ,
, .
,
. , , .
:
1. d[a] < d[u], , ,
, -
. root[u] = ,
.
2. = , , -
, .
,
, , -
.
3. d[a] > d[u], -
. .
strong_components().
IN: const Graph& g
. VertexListGraph
IncidenceGraph.
OUT: ComponentMap comp
. ,
,
. ComponentMap
WritablePropertyMap. ,
, vertices_size_type .
.
strong_components().
UTIL: root_map(RootMap r_map)
.
get(r_map, v)
204 13 BGL
, v. RootMap ReadWrite-
PropertyMap, -
.
: 1 terator_property_map, std:: vector -
, num_vertices(g). i_map
.
UTIL: discover_time(TimeMap tjnap)
. TimeMap ReadWritePropertyMap -
. -
.
: iterator_property_map, std:: vector
, num_vertices(g). imap
UTIL: color_map(ColorMap cjnap)
. Col
ReadWritePropertyMap,
, color_map ColorVaLue.
: iterator_property_map, std::vector -
defaul t_col or_type, num_verti ces (). i_map
.
IN: vertex_index_map(VertexIndexMap ijnap)
[, N), N
. ,
. Vertexlndex
Map ReadablePropertyMap. -
.
.
: get (vertex index, g).
! i
(|| + [|).
connected_components () i incremental _com-
ponentsO.
-
13.9.
13.9.
< strong-components. >
finclude <boost/config.hpp>
#include <vector>
finclude <iostream>
13.6. 205
#1 nclucte <boost/graph/strong_components.hpp>
#i nclude <boost/graph/adjacencyj i st.hpp>
int mainO {
using namespace boost:
typedef adjacencyjist < vecS. vecS. directedS > Graph;
const int N = 6;
Graph G(N):
add__edge(0, 1, G): add_edge(l. 1. G): add_edge(l, 3. G);
add_edge(l. 4, G): add_edge(3, 4. G); add_edge(3. 0. G);
add_edge(4. 3. G): add_edge(5. 2, G):
std::vector<int> c(N):
int num = strong_components
(G, make_iterator_property_map(c.begin(), get(vertex_index. G), [ 0])):
std::cout " : " num std::endl;
std::vector < int >::iterator i;
for (i = c.beginO; i != c.endO: ++i)
std::cout " " i - c.beginO
<< " " *i std::endl:
return EXIT_SUCCESS:
:
: 3
0
1
2 1
3
4
5 2
13.6.
, -
. ,
, -
(disjoint-sets) [10, 44], ,
( ) -
.
() 16.6.
. ,
- ds v.
Initial i ze_i incremental _components (g. ds)
.
g .
incremental_components(g.ds)
g ,
- ds.
206 13 BGL
ds.find_set(v)
v -.
ds.union_set(u.v)
-, (, v) .
13.10 -
, .
example/incrernental-cornponents-eg.cpp.
13.10.
// .
typedef a d j a c e n c y j i s t < vecS. vecS. undirectedS > Graph;
typedef graph_traits < Graph >: :vertex_descriptor Vertex:
const i n t N = 6:
Graph G(N):
add_edge(O. 1. G):
add_edge(l, 4. G);
// -.
std::vector < Vertex > rank(num_vertices(G)):
std::vector < Vertex > parent(num_vertices(G));
typedef graph_traits<Graph>::vertices_size_type* Rank:
typedef Vertex* Parent;
disjoint_sets < Rank. Parent > ds(&rank[O]. &parent[O]);
// , -
initialize_incremental_components(G, ds):
incremental_coinponents(G. ds):
//
graph_traits < Graph >::edge_descriptor e;
bool f l a g :
t i e ( e . flag) = add_edge(4, 0, G);
ds.union_set(4. 0);
t i e ( e . flag) = add_edge(2, 5. G);
ds.union_set(2. 5);
boost/graph/incremental_components.hpp.
13.6.1. initialize_incremental_components
template <typename VertexListGraph, typename DisjointSets>
void initia1ize_incremental_co[nponents(VertexListGraph& G. DisjointSets& ds)
initialize_incremental_components() -
-
,
.
(||).
13.6.2. incremental_components
template <typename EdgeMstGraph, typename DisjointSets>
void incremental_components(EdgeListGraph& g. DisjointSetsS ds)
incrementa1_components() -
, - .
(\ |).
13.6.3. s a m e _ c o m p o n e n t
template <typename Vertex, typename DisjointSets>
bool same_component(Vertex u. Vertex v. DisjointSetsS ds)
same_component() , -
.
((||, \V\)).
208 13 BGL
13.6.4. component_index
component_i ndex<Index>
component_i ndex ,
STL.
, componentj ndex
operator!;]. component J ndex
, 1 ncremental_components ().
Index , .
componentj ndex boost/graph/incremental_components.hpp.
componentj ndex.
componentj ndex:: value J;y pe
-. .
componentj ndex:: si ze_type
, .
componentj ndex.
template <typename ComponentsContainer>
componentj ndex:: componentj ndex(const ComponentsContainer& c)
component J ndex, ,
incren~ental_components.
template <typename Parentlterator>
componentj ndex: :componentJndex(ParentIterator first, Parentlterator last.)
, incre-
mental _components().
value_type componentJndex: :operator[ ](size_type i) const
i - .
size_type componentj ndex:: si ze() const
.
val ue_type componentj ndex , -
.
valuejype: :va1ue_type
- .
valuejtype::iterator
13.7. 209
value_type::const_iterator
.
.
va I ue_type component_i ndex
.
iterator beginO const
, .
iterator end const
, .
13.7.
13.7.1. edmunds_karp_max_flow
tempa l te <ytpename Graph, typename P, ytpename T. typename R>
typename deta:liedge_capactiy_vau l e<Graph. P. T, R>:type
edmunds_karp_max_o flw(Graph& g.
typename graph_traits<Graph>:vertex_descriptor src.
ytpename graph_tratis<Graph>:vertex_descrpi tor sink.
const bg_lnamed_params<P. T, R>& params = all defaults)
edmunds_karp_max_fl ow() (. -
8). .
f(u, v) V(u, v) e E, -
r(u, v) = (, v) -f(u, v).
boost/graph/edmundsj<arp_max_now.hpp.
edmunds_karp_max_fl ow().
IN: Graph& g
. VertexListGraph Inci-
denceGraph. (, v) (v, )
.
IN: vertex_descriptor src
.
IN: vertexjdescnptor sink
.
edmunds_karp_max_f1ow().
IN: capacity_map(CapacityEdgeMap cap)
210 13 BGL
. -
LvaluePropertyMap. -
.
: get(edge_capacity. g).
OUT: residual_capacity_map(ResidualCapacit.yEdgeMap res)
. -
LvaluePropertyMap. -
.
: get(edge_residual_capacity. g).
IN: reverse_edge_map(ReverseEdgeMap rev)
, (, v) -
(v, ). Lvalue-
PropertyMap. .
: get(edge_reverse. g).
UTIL: predecessor_map(PredecessorMap pjnap)
, ,
. .
: i terator_property_map, std:: vector -
, num_vertices(g). -
ijnap.
UTIL: color_map(ColorMap cjnap)
. ! -
ReadWritePropertyMap. -
,
ColorValue.
: iterator_property_map, std::vector -
default_color_type, num_vertices(g).
ijnap.
IN: vertex_index_map(VertexIndexMap indexjrap)
, -
.
[, \V\).
LvaluePropertyMap.
.
: get (vertexj ndex. g).
13.11 (
) DIMACS [1].
13.11.
< edmunds-karp-eg.cpp ) =
iinclude <boost/config.hpp>
13.7. 2 1 1
#include <iostream>
#include <string>
#incl ude <boost/graph/edmunds_karpjnaxjflow.hpp>
#include <boost/graph/adjacencyj ist.hpp>
#incl ude <boost/graph/readjJimacs.hpp>
#i nclude <boost/graph/graph_uti1i ty.hpp>
int mainO {
using namespace boost:
typedef adjacency_list_traits < vecS. vecS, directedS > Traits:
typedef adjacencyjist < lists. vecS. directedS,
property < vertexjiamejt. std::string >.
property < edgej;apacity_t. long.
property < edge_residualj^apacityjc, long.
property < edge_reverse_t. Traits: :edgejjescriptor > > > > Graph:
Graph g:
propertyjnap < Graph. edge_capacity_t >::type
capacity - get(edgej:apacity. g):
propertyjnap < Graph. edge_reverse_t >::type rev = get(edge_reverse. g):
propertyjnap < Graph. edge_residualjrapacityjt >::type
residualj;apacity - get(edge_residualj:apacity, g);
Traits::vertexjJescriptor s, t :
readjJimacsjnax_flow(g. capacity, rev, s, t ) :
long flow = edmunds_karpjnax_flow(g. s, t ) :
Std::cout " :" std::endl;
std::cout "s " flow std::endl std::endl:
:
:
13
:
0 63
0 16
0 24
f 151
f 1 00
f I 35
f 2 44
f 2 30
f 2 00
f 3 75
f 3 20
212 13 BGL
f3
f 5 4
f 57 5
f 67 3
f 64
f 76
f 75
13.7.2. push_relabel_max_flow
template <typename Graph, typename P. typename T. typename R>
typename detail::edge_capacity_value<Graph, P, T, R>::type
push_relabel_max_flow(Graph& g.
typename graph_traits<Graph>::vertex_descriptor src.
typename graph_traits<Graph>::vertex_descriptor sink,
const bgl_named_params<P. T, R>& params)
push_rel abel_max_f 1 ow() (. -
8). .
/(, ) V(M, V) G E, -
(, v) ~ c(u, v) - /(, v). , -
, . 13.7.
-
. -, G = (V, ),
, ,
.
Gm(V,{E\JE7}). rev ReverseEdgeMap
, (, v) > (v, ) V'(, v) e E.
cap Capaci tyEdgeMap -
, 0. ,
: (, v) > 0 (, ) = 0 (, v) e E.
. 13.7. ,
boost/graph/push_relabeUTiax_flow.hpp.
push_relabel_max_flow().
13.7. 213
IN: Graph& g
. VertexListGraph
IncidenceGraph. (, v) (v, )
.
IN: vertex_descriptor src
.
IN: vertex_descriptor sink
.
push_relabel_max_flow().
IN: capacity_map(CapacityEdgeMap cap)
. -
LvaluePropertyMap. -
.
: get(edge_capacity, g).
OUT: res1dual_capacity_map(ResidualCapacityEdgeMap res)
. -
LvaluePropertyMap.
.
: get(edge_residual_capacity, g).
IN: reverse_edge_map(ReverseEdgeMap rev)
, (, v)
(, ). Lvalue-
PropertyMap. .
: get(edge_reverse. g).
IN: vertex_index_map(VertexIndexMap indexjiap)
[, N ), N -
. Lvalue-
PropertyMap. .
: get(vertex_index. g).
13.12 (
) DIMACS [1].
13.12.
( push-relabel-eg.cpp ) =
iinclude <boost/config.hpp>
#1nclude <iostream>
#include <string>
#include <boost/graph/push_relabel_max_flow.hpp>
#i ncl ude <boost/graph/adjacencyj i st. hpp>
#include <boost/graph/read_dimacs.hpp>
214 13 BGL
13.12 ()
using namespace boost:
typedef adjacency_list_traits < vecS. vecS, directedS > Traits:
typedef adjacencyjist < vecS, vecS. directedS.
property < vertex_name_t, std:-.string >,
property < edge_capacity_t. long.
property < edge_residual_capacity_t. long,
property < edge_reverse_t. Traits::edge_descriptor > > > > Graph;
Graph g;
property_map < Graph, edge_capacity_t >::type
capacity = get(edge_capacity, g ) ;
property_map < Graph. edge_residual_capacity_t >::type
residual_capacity = get(edge_residual_capacity. g ) :
property_map < Graph. edge_reverse_t >::type rev = get(edge_reverse. g ) :
Traits::vertex_descriptor s. t:
read_dimacs_max_flow(g, capacity, rev, s, t ) :
long flow = push_relabel_max_flow(g, s. t ) ;
std::cout "c The total flow:" std::endl;
Std::cout "s " flow std::endl std::endl;
std::cout "c flow values:" std::endl;
graph_traits < Graph >::vertex_iterator u_iter, u_end:
graph_traits < Graph >::out_edge_iterator ei, e_end;
for (tie(u_iter, u_end) vertices(g): u_iter != u_end; ++u_iter)
for (tie(ei. e_end) = out_edges(*u_iter, g); ei - e__end: ++ei)
i f (capacity[*ei] > 0)
std::cout "f " *u_iter " " target(*ei, g) " "
(capacity[*ei] - residual_capacity[*ei]) std::endl:
return EXIT_SUCCESS:
}
:
:
s 13
:
f 0 63
f 0 10
f 0 2 10
f 1 51
f 1 00
f 1 30
f 2 44
f 2 36
f 2 00
f 3 75
f 3 20
f 3 11
f 4 54
f 4 60
f 5 40
f 5 75
f 6 73
f 6 40
f 7 60
f 7 50
BGL Bill
l1i1
lS11
14.1.
14.1.1. adjacencyjist
adjacency_list<EdgeList. VertexList, Directed,
VertexProperties. EdgePropertei s. 6raphProperte
i s>
ad jacencyj i st BGL-, -
.
.
,
(| V j + \\), 0(|| 2 ). , -
. -
. 14.1.
. 14.1.
adjacency_1ist -
, .
216 14 BGL
VertexLi st adjacency_l i st -
( . 14.1). -
EdgeLi st -
( . 14.1). EdgeLi st VertexLi st
. -
.
Di rected , , -
-
( ).
2 ( ), ,
, . -
. 14.2.
. 14.3.
. 14.2.
. 14.3.
( 14.1) -
.
14.1.
{ family-tree-eg.cpp )
iinclude <iostream>
finclude <vector>
14.1. 217
linclude <string>
#i nclude <boost/graph/adjacencyJi st.hpp>
#include <boost/tuple/tuple.hpp>
enum family { Jeanie. Debbie, Rick, John, Amanda, Margaret. Benjamin. N };
int mainO {
using namespace boost;
const char *name[] - { "Jeanie". "Debbie". "Rick",
"John", "Amanda". "Margaret". "Benjamin"
:
Jeanie Debbie, Rick. John
Debbie Amanda
Rick Margaret
John Benjamin
Amanda
Margaret
Benjamin
218 14 BGL
adjacencyJ1 St.
EdgeLi st
.
: vecS.
VertexLi st -
.
: vecS.
Di rected , -
, -
( ).
: di rectedS, undi -
rectedS bidirectionalS.
: di rectedS.
VertexProperti es
.
: no_property.
EdgeProperti es
.
: no_property.
GraphProperti es .
: no_property.
DefaultConstructible, CopyConstructible, Assignable, VertexListGraph, EdgeListGraph, Incidence-
Graph, AdjacencyGraph, VertexMutableGraph EdgeMutableGraph.
ad jacencyj i st BidirectionalGraph, Di rected=bi -
di recti onal S Di rected=undi rectedS, VertexMutablePropertyGraph Ed-
geMutablePropertyGraph, .
adjacencyjist boost/graph/adjacency_List.hpp.
ad jacencyj i st.
graph_traits<adjacency_list>::vertex_descriptor
, adjacency_11 st.
( Graph.)
graph_traits<adjacencyjist>::edge_descriptor
, ad jacencyj i st.
( Graph.)
graph_traits<adjacencyjist>: :vertexjterator
, vertices().
( VertexListGraph.)
14.1. 2 1 9
graph_traits<adjacency_list>::edge_iterator
, edges .
( EdgeListGraph.)
graph_traits<adjacency_list>::out_edge_iterator
, out_edges().
( IncidenceGraph.)
graph_traits<adjacency_list>::i n_edge_i terator
, . in_edge_i terator -
, in_edges().
( BidirectionalGraph.)
graph traits<adjacency list>::adjacency iterator
- - -
, adjacent_vertices().
( AdjacencyGraph.)
graph_traits<adjacency_list>::directed_category
, (di -
ted_tag) (undi rected_tag).
( Graph.)
graph_traits<adjacency_list>: :edge_parallel_category
, (,
). :
al I ow_paral I el_edge di sal 1 ow_para! 1 el_edge_tag. sets hash_setS
, .
( Graph.)
graph_traits<adjacency_list>::traversal_category
(traversal category)
. -
, , . -
, -
.
graph_traits<adjacency_list>::vertices_size_type
.
( VertexListGraph.)
graph_traits<adjacency_1ist>::edges_size_type
.
( EdgeListGraph.)
graph_traits<adjacency_list>::degree_size_type
.
( IncidenceGraph.)
property_map<adjacencyji st, PropertyTag>::type
property_map<adjacency_list. PropertyTag>::const_type
. -
Property Tag Vertex-
Properties EdgeProperties .
( PropertyGraph.)
220 14 BGL
adjccency_l i st.
adjacency_list(const GraphPropertiesS p =
GraphPropertiesO)
. -
.
( DefaultConstructible.)
adjacency_list(vertices_size_type n. const GraphProperties& p =
GraphPropertiesO)
- .
template <typename Edgelterator>
adjacency_list(EdgeIterator f i r s t , Edgelte~ator last,
vertices_size_type n, edges_size_type m = 0,
const GraphProperties& p = GraphPropertiesO)
.
[first, last). ,
. Edgelterator std:: pal ,
. ,
[0, ).
template <typename Edgelterator.
typename EdgePropertiesIterators
adjacency_list(Edgelterator f i r s t .
Edgelterator last. EdgePropertiesIterator e p j t e r .
vertices_size_type n. edges_size_type m = 0,
const GraphProperties& p = GraphPropertiesO)
.
[first, last). ,
. Edgelterator
std:: pai , . -
, [0, ). value_type -
ep_iter EdgeProperties.
adjacencyj i st.
std::pair<vertex_iterator. vertex_iterator>
vertices(const adjacency_list& g)
,
g.
( VertexListGraph.)
std::pai r<edge_iterator. edge_iterator>
edges(const adjacency_list& g)
, g.
( EdgeListGraph.)
14.1. 2 2 1
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_verti ces(vertex_descri ptor v,
const adjacency_list& g)
, ,
v g.
( AdjacencyGraph.)
std::pa i r<out_edge_i terator. out_edge_1terator>
out_edges(vertex_descriptor v. const adjacency_list& g)
,
v g. ,
, v.
( IncidenceGraph.)
std::pair<in_edge_iterator. in_edge_iterator>
in_edges(vertex_descriptor v, const adjacency_list& g)
, -
v g. , Di rected
di rectedS, , undirectedS bidirectionalS.
( BidirectionalGraph.)
vertex_descriptor source(edge_descriptor e.
const adjacency_list& g)
.
( IncidenceGraph.)
vertex_descriptor target(edge_descriptor e.
const adjacency_list& g)
.
( IncidenceGraph.)
degree_size_type out_degree(vertex_descriptor u.
const adjacency_1ist& g)
, .
( IncidenceGraph.)
degree_size_type in_degree(vertex_descriptor u,
const adjacency_list& g)
, . ,
Directed bidirectionalS.
( BidirectionalGraph.)
vertices_size_type num_vertices(const adjacency_!ist& g)
g.
( VertexListGraph.)
edges_size_type num_edges(const adjacency_list& g)
g.
( EdgeListGraph.)
222 14 BGL
vertex_descriptor vertex(vertices_size_type n,
const adjacency_list& g)
- .
std::pair<edge_descriptor. bool>
edge(vertex_descriptor u, vertex_descriptor v,
const adjacencyjists g)
, v g.
( AdjacencyMatrix.)
std::pair<out_edge_iterator. out_edge_iterator>
edge_range(vertex_descriptor u, vertex_descriptor v,
const adjacency_list& g)
, -
v. , EdgeList
adjacencyjist , -
, , multisets -
EdgeLi st .
std::pair<edge_descriptor. bool>
add_edge(vertex_descriptor u. vertex_descriptor v.
adjacency_list& g)
(, v) .
, ,
, , bool
. , V -
, , -
bool .
, -
. -
, EdgeLi s;.
VertexLi st=vecS V ( )
, , ,
, std: :max(u.v) + 1.
EdgeLi st=vecS, -
(out_edge_iterator) . ,
EdgeList ,
push(container, x). -
, v (in_edge_iterator) .
, out_edge_iterator
v . , add_edge() -
(out_edge).
( EdgeMutableGraph.)
std::pair<edge_descriptor. bool>
add_edge(vertex_descriptor u. vertex_descriptor v.
const EdgeProperties& p. adjacency_list& g)
(, v)
. . .
( EdgeMutablePropertyGraph.)
14.1. 223
, ,
,
. ,
remove_edge() .
( MutableEdgeListGraph.)
vertex_descriptor add_vertex(adjacency_lis.t& g)
-
.
( VertexMutableGraph.)
vertex_discriptor add_vertex(const VertexProperties& p.
adjacency_list& g)
-
.
( VertexMutabLePropertyGraph.)
void clear_vertex(vertex_descriptor u. adjacency_list& g)
, .
. ,
remove_edge() ,
.
( EdgeMutableGraph.)
void clear_out_edges(vertex_descriptor u.
adjacency_list& g)
, . -
. , -
move_edge() , .
( el ear_vertex()).
void clear_in_edges(vertex_descriptor u.
adjacency_list& g)
, .
. , -
remove_edge() , . -
.
void remove_vertex(vertex_descriptor u. adjacency_list& g)
. , -
.
, cl ear_vertex().
VertexLi st ad jacency_l i st vecS,
, -
. vertex_i ndex_t
, --
[0, | V |).
, ,
. -
14.1. 225
-
. remove_vertex(), I i stS
VertexLi st.
( VertexMutableGraph.)
template <typename PropertyTag>
property_map<adjacency_list, PropertyTag>::type
get(PropertyTag, adjacency_list& g)
- ,
PropertyTag. Property Tag , -
VertexProperties .
( PropertyGraph.)
template <typename PropertyTag>
propertyjnap<adjacencyjist, PropertyTag>: :const_type
get(PropertyTag, const adjacency_list& g)
- ,
PropertyTag. PropertyTag , -
VertexProperties .
( PropertyGraph.)
template <typename PropertyTag. typename X>
typename property_traits<
typename property_map<ad jacencyjli st.
PropertyTag>::const_type>::value_type
get(PropertyTag. const adjacency_list& g. X x)
, .
( PropertyGraph.)
template <typename PropertyTag. typename X.
typename Value>
void put(PropertyTag. const adjacency_list& g, X x.
const Value& value)
value , -
. typename property_traits<proper-
ty_map<adjacency_list. PropertyTag>: :type>: :value_type.
( PropertyGraph.)
template <typename GraphProperties,
typename GraphProperties>
typename property_value<GraphProperties,
GraphProperties>::type&
get_property(adjacency_list& g. GraphProperties);
, GraphProperti es
. property_value boost/pending/property.hpp.
template <typename GraphProperties.
typename GraphProperties>
const typename property_value<GraphProperties,
GraphProperties>::type&
get_property(const adjacency_list& g. GraphProperties);
8 . 375
226 14 BGL
, GraphProperties,
. property_val ue boost/pending/property.hpp.
EdgeList VertexList
, adjacencyjist -
. -
. EdgeList VertexList
, . EdgeL'i st
VertexList -
.
BGL STL, s t d : : vector,
std:: 1 i st std:: set -
( ) .
EdgeLi st VertexLi st :
vecS std:: vector.
l i s t s s t d : : l i s t .
siistS s t d : : s i i s t 1 .
setS std::set.
hash_setS std:: hash_set2.
VertexList
VertexLi st ,
. -
Sequence RandomAccessContainer.
l i s t s ,
. -
vecS.
: std:: 1 i st
, std:: vector, .
: VertexList -
.
add_vertex()
vecS,
l i s t s ( push_back()). , VertexList=vecS
vecS, -
, ,
remove_vertex()
1 i stS (\ V\ + \E\)
vecS. vecS , -
( ,
1
STL, , std::slist.
2
STL, , std::hash_set. , SGI STL
.
14.1. 227
)
.
vertexO
vecS O(|V|) l i s t s .
EdgeList
EdgeList ,
(, , ) .
, , -
Sequence, AssociativeContainer.
,
EdgeLi st, ,
. , , -
sets hash_setS. ,
, -
: vecS, l i s t s slistS. ,
-
. | V| -
\ | . , , -
.
: EdgeList , -
. -
: vecS, si i stS, 1 i stS, hash_setS sets.
: -
| | / | |
. , ,
||/|V| .
|| ( -
). \\ , \V\, -
.
add_edge()
EdgeList UniqueAssociativeContainer (
) std:: set,
. -
O(log(||/|V|)). EdgeList, Sequence
(), add_edge()
. ,
, , , -
, -
EdgeList. add_edge() -
EdgeList push_front() push_back().
std: : l i s t std: : s l i s t ,
std:: vector, .
remove_edge()
EdgeLi st -
std: :remove_if(). , ||/|V|.
228 14 BGL
EdgeLi st -
erase(), log(| | / |V|).
edge
(\ | /1V |), EdgeLi st -
Sequence, O(log(|| /1 V|)), EdgeLi st AssoriativeContainer.
clear_vertex()
EdgeLi st
(\\ + \V\), EdgeLi st -
, (\ V |log(| |/| V |)).
-
\\ / \\) O(||log(||/ |V|) / \V\).
remove_vertex()
(\\ + \V\) EdgeLi st
out_edge_i terator::operator++()
EdgeLi st. -
( ) -
. ,
.
: vecS, si i stS, 1 i stS, sets, hash_setS.
i n_edge_i terator::operator++()
. .
vertex_i terator::operator++()
(-
). OneD
.
edge_iterator::operator++()
-
, out_edge_i terator EdgeLi st.
(\ \ + \V\).
adjacency_iterator::operator++()
-
, out_edge_i terator EdgeLi st.
( ) -
. adjacency_l i st
- -, ,
. , -
14.2 .
14.2.
// VertexList=vecS
typedef adjacency_list<listS, vecS> Graph:
14.1. 229
Graph G(N);
// ...
// . !
graph_traits<Graph>::vertex_iterator vi. vi_end:
for (tie(vi. vi_end) = vertices(G); vi != vi_end; ++vi)
remove_vertex(*vi, G);
// . !
graph_traits<Graph>::vertex_iterator vi. vi_end. next:
tie(vi. vi_end) = vertices(G);
for (next = vi; vi != vi_end; vi - next) {
++next;
remove_vertex(*vi, G):
}
, remove_vertex(),
adjacency_11st Vertexl_ist=vecS
( vi vi_end), , -
.
adjacency_l i st, VertexLi st=l i stS, -
, , ,
. 14.3.
14.3. (2)
// VertexList=1istS
typedef adjacency_list<listS. listS> Graph:
Graph G(N):
// ...
// . !
graph_traits<Graph>::vertex_iterator vi, vi_end:
for (tie(vi. vi_end) = vertices(G); vi != vi_end: ++vi)
remove_vertex(*vi, G):
// . .
graph_traits<Graph>::vertex_iterator vi, vi_end. next;
tie(vi, vi_end) = vertices(G);
for (next - vi; vi !- vi_end: vi = next) {
++next:
remove_vertex(*vi, G):
}
adjacency_l 1 st remove_edge_i f ().
. -
, , -
( )
(. example/dijkstra-example.cpp). -
di jkstra_shortest_paths(), . -
,
, .
14.4.
230 14 BGL
14.4.
std::vector<Vertex> parent(num_vertices(G));
std::vector<Vertex> distance(num_vertices(G));
dijkstra_shortest_paths(G. s. distance_map(&distance[O]).
predecessor_map(&parent[0])):
// !
//
remove_vertex(s, G ) :
//
for(tie(vi, vend) = vertices(G): vi != vend; ++vi!
std::cou.t p[*vi] " " *vi std::endl:
-
, ,
. , remove_edge(u, v, g) -
(, v) , (, ),
adjacencyl ist. To ,
remove_edge(u. v. g) , -
(, v) .
, , -
( ), l i s t s sets
VertexList EdgeList adjacencyjist.
, vecS ! [-
VertexList / EdgeList.
ad jacency_l i st ! [-
, , ,
Di rected. di rectedS bi di recti onal S
, undi rectedS . . pa s-
12.1.1, -
BGL. bidi recti onal S , -
i n_edges () out_edges ().
( , in_edges() ).
, -
, (property interface). -
VertexProperties EdgeProperties adjacency_1 ist -
, .
template <typename PropertyTag. typename T,
typename NextProperty = no_property>
struct property:
PropertyTag ,
. (. 15.2.3),
. BGL -
( enum)
, -
- ( , , get() adjacencyjist).
14.1. 2 3 1
property . Next -
Property ,
.
, -
.
float std::string .
float.
typedef property<distance_t, float.
property<name_t. std;;string> > VertexProperties;
typedef property<weight_t, float> EdgeProperties:
typedef adjacency_list<mapS, vecS, undirectedS.
VertexProperties. EdgeProperties> Graph:
Graph g(num_vertices): //
-
. , , .
3.6. 15 .
VertexLi st vecS, , -
vertexj ndex_t. -
[, \V |), . , -
, .
-
, ,
.
,
.
. -
, -
.
enum edge_capacity_t { edgejrapacity }:
enum edge_flow_t { edge_flow }:
namespace boost {
BOOST_INSTALL_PROPERTY(edge. flow);
BOOST_INSTALL_PROPERTY(edge. capacity):
}
, .
typedef property<capacity_t. int> Cap:
typedef property<f1ow_t. int. Cap> EdgeProperties;
typedef adjacency_list<vecS. vecS. no_property, EdgeProperties> Graph;
,
get .
property_map<Graph. edge_capacity_t>::type
capacity = get(edge_capacity. G);
property_map<Graph. edge_f1ow_t>::type
flow - get(edge_flow, G);
232 14 BGL
edge_property.cpp
.
,
,
. .
enum vertex_first_name_t { vertex_fi rstjiame };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, firstjiame);
}
property -
. ( 14.5) -
-.
. , .
14.5.
typedef adjacency_list<vecS, vecS. directedS.
property<vertex_first_name_t, std::string> > MyGraphType:
MyGraphType G(5);
for (int 1-0; i < l l ; ++i)
add_edge(edge_array[i].first, edge_array[i].second, G):
boost::put(name, 0, "Jeremy");
boost::put(name. 1. "Rich"):
boost::put(name, 2. "Andrew"):
boost::put(name. 3. "Jeff"):
name[4] = "Kinis": //
{
//
typedef typename propertyjnap<Graph, vertex_first_name_t>
::const_type NamePA;
NamePA name - get(vertex first name, G ) ;
-
typedef typename boost::property_traits<NamePA>::value_type NameType;
NameType src_name, targ_name;
while (first !- last) {
src_name = boost::get(name, source(*first.G)):
targ_name = boost::get(name. target(*first.G));
cout src_name " "
targ_name " " endl:
++first:
}
}
:
Jeremy Rich
Jeremy Andrew
Jeremy Jeff
Jeremy Kinis
Andrew Jeremy
Andrew Kinis
Jeff Jeremy
Jeff Rich
Jeff Kinis
Kinis Jeremy
Kinis Rich
interior_proper-
tyjnap.cpp.
ad jacencyj i st .
,
(, , ) . BGL -
, -
STL.
. VertexList -
. EdgeList -
. container_gen.cpp -
, .
ad jacencyj 1 st , contai ner_gen, -
EdgeLi st VertexLi st , -
.
14.7 , -
l i s t s .
14.7.
namespace boost {
template <typename Selector, typename ValueType>
&
234 14 BGL
14.7 {)
truct container_gen { };
template <typename ValueType>
truct container_gen<listS, ValueType>
typedef std::list<ValueType> type;
--
container_gen ( 14.8).
14.8.
struct custom_containerS { }; //
namespace boost {
//
template <typename ValueType>
struct container_gen<custom containers, ValueType> {
typedef custom_container<Va1ueType> type;
, ,
, Val ueType. ,
(allocator type). -
container_gen. , -
-. 14.9 ,
, std:: 11 St.
14.9.
template <typename Allocator* struct list_with_allocatorS {};
namespace boost {
template <typename Alloc. typename ValueType>
struct container_gen<list_with_allocatorS<Alloc>. ValueType>
{
typedef typename Alloc:;tempiate_rebind<ValueType>::other Allocator;
typedef std::list<ValueType, Allocator type;
}
// . std::11st
//
typedef adjacency_list< list_with_al1ocatorS< std::allocator<int> >,
vecS. directedS> MyGraph:
contai ner_gen
11 el_edge_trai ts ,
( Sequence) ( Associative-
Container).
template <typename StorageSelector>
struct parallel_edge_traits { };
template <> struct parallel_edge_traits<vecS> {
typedef allow_parallel_edge_tag type;
14.1. 235
: push() eraseO
adjacency_l 1 st
.
pushO eraseO . pushO
, , -
, , . al I ow_pa -
ral1el_edge_tag paral lel_edge_t its, push
. di sal I ow_paral 1 el_edge_tag, push
, -
, .
pushO eraseO ( 14.10)
STL. push_di s-
patchO erase_dispatch() ,
.
14.10. push() erase()
template <typename Container, typename T>
std::pair<typename Container::iterator, bool>
push(Container& const T& v)
14.1.2. adjacency_matrix
D F
0 0 0 0 0 0
0 0 1 0 0 1
1 0 1 0 0 0
D 0 0 0 0 1 0
0 0 0 1 . 0 0
F 1 0 0 0 0 0
. 14.4.
adjacencyjnatri x ,
. . 3.6.
adjacencyjnatrix
| V | | V |, ( ),
.
(| V | | V |)/2. -
. 14.5.
D E F
0
0 0
1 1 0
0 0 0 0
0 0 0 1 0
1 1 0 0 0 0
. 14.5.
14.11 , . 14.4,
14.12 . 14.5.
14.11. ( . 14.4)
enum { , , . D. E. F. N };
const char* name = "ABCDEF":
typedef adjacency_matrix<directedS> Graph;
Graph g(N):
add_edge(B, g): add_edge(B. F. g);
add_edge(C. A, g); add_edge(C, g);
add_edge(D. E. g): add_edge(E. D. g):
add_edge(F. A. g):
std::cout " : ";
print_vertices(g. name);
std;;cout std::endl;
14.1. 237
:
<--> F
<--> F
<-->
D <-->
<--> D
F <-->
238 14 BGL
adjacencyjratrix boost/graph/adjacency_matrix.hpp.
adjacencyjnatrix.
Di rected :
.
: di rectedS undi rectedS.
: di rectedS.
VertexProperty
.
: no_property.
EdgeProperty
.
: no_property.
GraphProperty
.
: no_property.
VertexListGraph, EdgeListGraph, IncidenceGraph, AdjacencyGraph, AdjacencyMatrix, Vertex
MutablePropertyGraph EdgeMutablePropertyGraph.
DefaultConstructible CopyConstructible.
adjacencyjnatrix.
graph_traits<adjacency_matrix>::vertex_descriptor
, .
( Graph.)
graph_traits<adjacency_matrix>::edge_descriptor
, .
( Graph.)
graphjtraits<adjacencyjnatrix>::vertex_iterator
, vertices .
( VertexListGraph.)
graph_traits<adjacency_matrix>::edge_iterator
, edges ().
( EdgeListGraph.)
14.1. 239
graph_traits<adjacency_matrix>::out_edge_iterator
, out_edges().
( IncidenceGraph.)
graph_traits<adjacency_matrix>::adjacency_iterator
, adjacent_vertices().
( AdjacencyGraph.)
graph_traits<adjacency_matrix>::directed_category
, (direc-
ted_tag) (undirected_tag).
( Graph.)
graph_traits<adjacency_matrix>: :edge_paranel_category
,
disal low_pa! Ie1_edge_tag.
( Graph.)
graph_traits<adjacency_matrix>::vertices_size_type
.
( VertexListGraph.)
graph_traits<adjacency_matrix>::edges_size_type
.
( EdgeListGraph.)
graph_traits<adjacency_matrix>::degree_size_type
.
( IncidenceGraph.)
propertyjnap<adjacency_matrix. PropertyTag>: :type
property_map<adjacency_matrix. PropertyTag>::const_type
. -
Property Tag , -
VertexProperty EdgeProperty .
( PropertyGraph.)
adjacencyjnatrix.
adjacency_matrix(vertices_size_type n,
const GraphPropertyS p = GraphPropertyO) ;
.
template <typename Edgelterator>
adjacency_matrix(EdgeIterator first. Edgelterator last.
vertices_size_type n.
const GraphPropertyS p = GraphPropertyO)
240 14 BGL
, -
[first, last). Edgelterator
std: :pair, . -
[0, ).
template <typename Edgelterator, typename
EdgePropertyIterator>
adjacency_matrix(Edgelterator f i r s t , Edgelterator last.
EdgePropertylterator e p j t e r , vertices_size_type n,
const GraphProperty& p = GraphPropertyO)
,
[first, last), , ep_i ter
. Edgelterator std::pai,
.
[0, ). e p j t e r
EdgeProperty.
adjacencyjnatri x.
std::pair<vertex_iterator, vertex_iterator>
vertices(const adjacency_matrix& g)
,
.
( VertexListGraph.)
std::pair<edge_iterator. edge_iterator>
edges(const adjacency_matrix& g)
, .
( EdgeListGraph.)
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(vertex_descriptor v.
const adjacency_matrix& g)
, ,
v .
( AdjacencyGraph.)
std::pair<out_edge_iterator. out_edge_iterator>
out_edges(vertex_descnptor v. const adjacency_matrix& g)
,
v . , -
, v.
( IncidenceGraph.)
vertex_descriptor source(edge_descriptor e.
const adjacency_matrix& g)
.
( IncidenceGraph.)
14.1. 2 4 1
vertex_descriptor target(edge_descriptor e,
const adjacency_matrix& g)
.
( IncidenceGraph.)
degree_size_type out_degree(vertex_descriptor u.
const adjacency_matnx& g)
, .
( IncidenceGraph.)
vertices_size_type num_vertices(const adjacency_matrix& g)
g.
( VertexListGraph.)
edges_size_type num_edges(const adjacency _matnx& g)
g.
( EdgeListGraph.)
vertex descriptor_vertex(vertices_size_type n.
const adjacency_matrix& g)
- .
std::pai r<edge_descriptor. bool>
edge(vertex_descriptor u. vertex_descriptor v,
const adjacency_matrix& g)
, .
( AdjacencyMatrix.)
std: :pair<edge_descriptor, bool>
add_edge(vertex_descriptor u. vertex_descriptor v,
adjacencyjnatrix& g)
(, v) -
. ,
. -
.
( EdgeMutableGraph.)
std: :pair<edge_descriptor, bool>
add_edge(vertex_descriptor u. vertex_descriptor v.
const EdgeProperty& p, adjacency_matrix& g)
(, v)
(. - add_edge ()).
void remove_edge(vertex_descnptor u, vertex_descnptor v,
adjacency_matrix& g)
(, v) .
( EdgeMutableGraph.)
void remove_edge(edge_descriptor e, adjacency_matnx& g)
.
( EdgeMutableGraph.)
9 . 375
242 14 BGL
void clear_vertex(vertex_descriptor u.
adjacency_matrix& g)
, , .
.
,
remove_edge().
( EdgeMutableGraph.)
template <typename Property>
property_map<adjacency_matrix. Property>::type
get(Property. adjacency_matrix& g)
- , Property (-
). , -
VertexProperty .
( PropertyGraph.)
template <typename Property>
property_map<adjacencyjnatrix, Property>::const_type
get(Property. const adjacency_matrix& g)
- , Property (-
). ,
VertexProperty .
( PropertyGraph.)
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)
, .
( PropertyGraph.)
template <typename Property, typename X. typename Value>
void put(Property. const adjacency_matrix& g. X x,
const ValueS value)
val ue,
. value -
, Property.
( PropertyGraph.)
template <typename GraphProperties.
typename GraphProperty>
typename property_value<GraphProperties.
GraphProperty>::type&
get_property(adjacency_matrix& g, GraphProperty);
, GraphProperty, -
. property_value boost/
pending/property, hpp.
template <typename GraphProperties.
typename GraphProperty>
14.2. 243
14.2.
14.2.1. graph_traits
graph_tratis<Graph>
graph_traits
BGL (. 12.1).
, graph_traits -
(typedef). ,
:
template <typename Graph> void my_graph_algorithm(Graph& g) {
// graph_traits Graph.
typedef boost::graph_traits<Graph> Traits:
// .
typedef typename Traits::vertex_descriptor Vertex:
// ...
}
( ) graph_traits
,
. 14.13.
14.13. graphjxaits
namespace boost {
template <typename G>
struct graphjtraits {
// :
typedef typename G::vertex_descriptor vertex_descriptor:
typedef typename G::edge_descriptor edge_descriptor;
typedef typename G: :adjacencyJterator adjacency_iterator:
typedef typename G::out_edge_iterator out_edge_iterator:
typedef typename G::in_edge_iterator in_edge_iterator:
typedef typename G::vertex_iterator vertex_iterator:
typedef typename G: :edge_iterator edgejterator:
//
typedef typename G::directed_category directed_category;
typedef typename G::edge_parallel_category edge_parallel_category:
typedef typename G::traversal_category traversa1_category:
//
typedef typename G::vertices_size_type vertices_size_type:
typedef typename G::edges_size_type edges_size_type;
typedef typename G::degree_size_type degree_size_type:
}:
} // namespace boost
244 14 BGL
, graph_traits }
. , graph_trai ts Graph
Stanford GraphBase. SGB-
boost/graph/stanford_graph.hpp.
namespace boost {
template <>
struct graph_traits<Graph*>
, graph_traits
. , -
. graph_traits -
GRAPH LEDA. -
boost/graph/leda_graph.hpp.
namespace boost {
template <typename vtype. typename etype>
struct graph_traits< GRAPH<vtype.etype> > {
, -
. , -
, -
, , vo" d
( -
) typedef graph_traits
.
di rected_category
.
namespace boost {
struct directed_tag { }:
struct undirected_tag { };
edge_paral 1 el_category -
.
namespace boost {
struct allow_parallel_edge_tag {};
struct disallow_parallel_edge_tag {};
t versa 1 _category
, .
namespace boost {
struct incidence_graph_tag { }:
struct adjacency_graph_tag { };
struct bidirectional_graph_tag :
14.2. 245
graph_traits boost/graph/graph_traits.hpp.
graph_traits.
graph_traits::vertex_descriptor
, Graph.
graph_traits::edge_descri ptor
, Graph.
graph_traits::vertex_iterator
, vertices().
graph_traits::edge_iterator
, edges .
graph_tra its::out_edge_iterator
, out_edges().
graph_traits: :adjacency_iterator
, adjacent_vertices .
graph_traits::directed_category
, .
graph_traits::edge_parallel_category
, .
graph_traits::traversal_category
, .
graph_traits::vertices_size_type
,
.
graph_traits::edges_size_type
, .
246 14 BGL
graph_traits::degree_size_type
,
.
14.2.2. adjacency_list_traits
adjacencyj istJ:raits<Edgel_ist, VertexList. Directed>
adjacencyj i st_t raits
adjacency_list.
, , -
.
graph_traits, --
. adjacency_l i st jtrai ts,
-
, .
template <typename EdgeList. typename VertexList, typename Directed>
struct adjacencyj ist_traits {
typedef ... vertex_descriptor;
typedef ... edge_descriptor;
typedef ... directed_category;
typedef ... edge_parallel_category;
}:
adjacencyj i s t _ t r a i t s boost/graph/adjacency_List.hpp.
adjacency_1 i st_trai ts.
EdgeLi st
.
: vecS.
VertexLi st
.
: vecS.
Di rected
.
: di rectedS.
DefaultConstructible Assignable.
adjacencyj i st _traits.
adjacencyj ist_traits: :vertex_descriptor
, .
adjacencyj I st_trai ts: :edge_descnptor
14.2. 2 4 7
, .
adjacency_list_traits::directed_category
, (undi rected_tag) -
(di rected_tag).
adjacency_1ist_traits::edge_parallel_category
, (allow_para1 -
lel_edge_tag) (di sail ow_pa rail el _edge_tag).
ad jacencyj i st.
adjacency_matrix_traits boost/graph/adjacency_matrix.hpp.
adjacency_matrix_traits.
Di rected ,
.
: di rectedS.
DefaultConstructible Assignable.
adjacency_matrix_traits.
adjacency_matrix_traits::vertex_descriptor
, .
248 14 BGL
adjacencyjnatnx_traits: :edge_descriptor
, .
adjacency_matrix_traits::directed_category
, (undirected_tag)
(di rected_tag).
adjacency_matrix_traits::edge_parallel_category
,
di sa 11 ow_pa 11 el _edge_tag.
adjacencyjnatrix.
14.2.4. propertyjnap
property_map<Graph. PropertyTag>
property_map ,
.
, PropertyGraph.
( 14.14)
,
property_map. -
, get().
14.14.
< property-map-traits-eg.cpp >
finciude <boost/config.hpp>
linclude <string>
#include <boost/graph/adjacency_li st.hpp>
int mainO {
using namespace boost:
typedef adjacencyjist < lists, lists, directedS,
property < vertex_name_t, std::string > >graph_t;
graph_t g:
graph_traits < graph_t >::vertex_descriptor u = add_vertex(g):
propertyjnap < graph_t. vertex_name_t >::type
namejnap - get(vertex_name. g);
name_map[u] = "Joe":
std::cout namejnap[u] std::endl:
return EXIT_SUCCESS:
}
:
Joe
propertyjnap boost/graph/properties.hpp.
propertyjnap.
14.2. 249
Graph ,
PropertyGraph.
PropertyTag .
.
.
propertyjnap.
property_map<Graph. PropertyTag>::type
,
PropertyTag.
property_map<Graph. PropertyTag>::const_type
,
PropertyTag.
.
.
14.2.5. property
property<PropertyTag. T, NextProperty>
property adjacency_l i st adjacencyjna-
t r i x ,
.
property.
PropertyTag ( -
) .
, -
. BGL -
(
enum)
,
(-
,
get adjacency l i s t ) .
.
250 14 BGL
NextProperty
,
.
: no_property.
property boost/pending/property.hpp.
( 14.15) boost/graph/properties.hpp
14.15.
namespace boost {
// :
enum edge_name_t { edge_name }; //
enum edge_weight_t { edge_weight }: //
enum edge_index_t { edge_index }; //
enum edge_capacity_t { edge_capacity }: //
enum edge__residual_capacity_t { edge_residual_capacity };
//
enum edge_reverse_t { edge_reverse }: //
// :
enum vertex_name_t { vertexjiame }: //
enum vertex_distance_t { vertex_distance }: //
enum vertex_index_t { vertex_index }; //
enum vertex_color_t { vertex_color }; //
enum vertex_degree_t { vertex_degree }: //
enum vertex_out_degree_t { vertex_out_degree };
//
enum vertex_in_degree_t { vertex_in degree }:
//
enum vertex_discover_time_t { vertex_discover_time }:
//
enum vertex__finish_time_t { vertex_finish_time }:
//
// :
enum graph_name_t { graphjiame }: //
BOOST_INSTALL_PROPERTY(vertex, index);
BOOST_INSTALL_PROPERTY(edge. index):
14.3.
14.3.1. edgejist
edge_list<EdgeIterator, ValueType, OiffType>
edge_11st ,
, EdgeListGraph. (value_type)
std:: pai (, , f i rst () second
14.3. 2 5 1
edgej i st.
Edgelterator Inputlterator, val ue_type -
.
ValueType Edgelterator.
: std: :iterator_traits<EdgeIte-
rator>::value_type.
Di ffType Edgelterator.
: std: :iterator_traits<EdgeIte-
rator>: :difference_type.
e d g e j i s t EdgeListGraph.
e d g e j i s t boost/graph/edge_list.hpp.
edgej i st.
graph_traits<edge_list>::vertex_descriptor
, edgej i st.
, first_type std: :pair,
Edgelterator.
graph_traits<edge_list>::edge_descriptor
, edge_l i st.
graph_traits<edge_list>::edge_iterator
, edges (). iterator_category
, Edgelterator.
edgej i st.
edgeJistCEdgelterator f i r s t . Edgelterator last)
,
[first, last).
252 14 BGL
edgejl 1 St.
std:: pai r<edge_iterator. edge_Herator>
edges(const edge_list& g)
, .
vertex_descriptor source(edge_descriptor e.
const edgeJisU g)
.
vertex_descriptor target(edge_descriptor e.
const edge_list& g)
.
14.3.2. reverse_graph
reverse_graph<BidirectionalGraph>
reverse_graph BidirectionalGraph
() , . -
, -
.
14.16 exampl.es/reverse-graph-eg.cpp.
14.16.
typedef adjacencyjist < vecS. vecS. MdirectionalS > Graph;
Graph G(5);
add_edge(0, 2. G): add edged. 1. G); add edged. 3. G):
add_edge(l. 4, G); add""edge(2. 1, G); add_edge(2. 3, G);
add_edge(2. 4. G); add"~edge(3. 1. G): add_edge(3. 4. G);
add edge(4. 0. G); add" edge(4. 1. G);
2 --: 13 4
3 --> 14
4 --> 01
0 --=>
1
1 2 34
2 --: 0
1 2
4 -- = 1 2 3
14.3. 253
reverse_graph.
Bi di rGraph , .
reverse_graph BidirectionalGraph (-
) VertexListGraph PropertyGraph.
reverse_graph boost/graph/reverse_graph.hpp.
reverse_graph.
graph_traits<reverse_graph>::vertex_descriptor
, .
( Graph.)
graph_traits<reverse_graph>::edge_descriptor
, .
( Graph.)
graph_traits<reverse_graph>::vertex_iterator
, vertices .
( VertexListGraph.)
graph_traits<reverse_graph>::edge_iterator
, edges .
( EdgeListGraph.)
graph_trai ts<reverse_graph>::out_edge_i terator
, out_edges().
( IncidenceGraph.)
graph_traits<reverse_graph>::adjacency_iterator
, adjacent_vertices().
( BidirectionalGraph.)
graph_traits<reverse_graph>::directed_category
, -
.
( Graph.)
graph_traits<reverse_graph>::edge_parallel_category
, (-
). :
anow_parallel_edge_tag disallow_parallel_edge_tag. -
sets hash_setS di sal low_paral 1 el_edge_tag,
.
( Graph.)
254 14 BGL
graph_traits<reverse_graph>::traversal_category
, -
. reverse_graph ,
traversal_category .
( Graph.)
graph_traits<reverse_graph>: :vertices_size__type
.
( VertexListGraph.)
graph_traits<reverse_graph>::edge_size_type
.
( EdgeListGraph.)
graph_traits<reverse_graph>::degree_size_type
, .
( IncidenceGraph.)
property_map<reverse_graph. Property>::type
property_map<reverse_graph. Property>::const_type
. -
Property -
VertexProperty EdgeProperty .
( Property Graph.)
reverse_graph.
reverse_graph(BidirectionalGraphs, g)
. () .
reverse_graph.
template <class BidirectionalGraph>
reverse_graph<Bi directional Graph>
make_reverse_graph(BidirectionalGraph& g)
.
std::pair<vertex_iterator, vertex_iterator>
vertices(const reverse_graph& g)
, .
( VertexListGraph.)
std::pair<out_edge_iterator. out_edge_iteretor>
out_edges(vertex_descriptor v, const reverse_graph& g)
,
v . -
.
( IncidenceGraph.)
14.3. 2 5 5
std::pair<in_edge_iterator. in_edge_iterator>
in_edges(vertex_descriptor v. const reverse_graph& g)
, -
v .
.
( BidirectionalGraph.)
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(vertex_descnptor v.
const reverse_graph& g)
,
v .
( AdjacencyGraph.).
vertex_descriptor source(edge_descriptor e,
const reverse_graph& g)
.
( IncidenceGraph.)
vertex_descriptor target(edge_descriptor e.
const reverse_graph& g)
.
( IncidenceGraph.)
degree_size_type out_degree(vertex_descriptor u.
const reverse_graph& g)
, .
( IncidenceGraph.)
degree_size_type in_degree(vertex_descriptor .
const reverse_graph& g)
, . ,
bidi rectionalS.
( BidirectionaLGraph).
vertices_size_type num_vertices(const reverse_graph& g)
g.
( VertexListGraph.)
vertex_descriptor vertex(vertices_size_type n,
const reverse_graph& g)
- .
std: :pair<edge_descriptor. bool>
edge(vertex_descriptor u. vertex_descriptor v,
const reverse_graph& g)
, v.
( AdjacencyMatrix.)
template <class Property>
property_map<reverse_graph. Property>::type
256 14 BGL
get(Property. reverse_graph& g)
template <class Property>
property_map<reverse_graph. Tag>::const_type
get(Property. const reverse_graph& g)
- , Property.
,
VertexProperty .
( PropertyGraph.)
template <class Property, class X>
typename property_traits<property_map<reverse_graph,
Property>::const_type>::value_type
get(Property, const reverse_graph& g. X x)
, -- -
.
template <c1ass Property, class X. class Value>
void put(Property, const reverse_graph& g. X x.
const Va1ue& value)
va 1 ue,
. value typename proper-
ty_traits<property_map<reverse_graph, Property>::type>::value_type
template <class GraphProperties. class GraphProperty>
typename property_value<GraphProperties.
GraphProperty>::type&
get_property(reverse_graph& g. GraphProper~y):
, GraphProperty, -
. property_val ue boost/
pending/property.hpp.
template <class GraphProperties. class GraphProperty>
const typename property_value<GraphProperties,
GraphProperty>::type&
get_property(const reverse_graph& g, GraphProperty);
, GraphProperty, -
. property_val ue boost/
pending/property.hpp.
14.3.3. filtered_graph
filtered_graph<Graph. EdgePredicate. VertexPredicate>
filtered_graph ,
. - , -
. -
, , -
, ,
. filtered_graph
, .
14.3. 257
. fi ltered_graph , -
-
.
, 14.17, -
,
.
14.17. , ,
template <typename EdgeWeightMap>
struct positive_edge_weight { // ?
positive_edge_weight() { }
positive_edge_weight(EdgeWeightMap weight) : m__weight (weight) { }
template <typename Edge>
bool operatorO(const EdgeS e) const {
return 0 < boost::get(m_weight. e ) ;
}
EdgeWeightMap m_weight;
}:
14.18
positive_edge_weight
. (, ), (,) (, ) -
.
14.18. ,
typedef adjacency_1ist<vecS. vecS, directedS.
no_property, property<edge_weight_t, int> > Graph:
typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap;
enum { . , D, E. N };
const char* name = "ABCDE";
Graph g(N):
add_edge(A. B. 2, g); add_edge(A. C. 0. g ) ;
add_edge(C. D. 1, g ) ; add_edge(C. E. 0, g ) ;
add_edge(D, B, 3. g): add_edge(E. C. 0, g ) :
positive_edge_weight<EdgeWeightMap> filter(get(edge_weight. g)):
filtered_graph<Graph, positive_edge_weight<EdgeWeightMap> >
fg(g, filter);
std::cout " : ":
print_edges(fg, name);
-->
258 14 BGL
filtered_graph boost/graph/filtered_graph.hpp.
fil tered_graph.
Graph .
EdgePredi cate , , -
-
.
Predicate. -
.
DefaultConstructible
VertexPredi cate , ,
-
.
Predicate.
.
DefaultConstructible.
: keepal 1 ( )
, filtered_graph<Graph.EP.VP>,
Graph. Graph VertexListGraph, EdgeListGraph, IncidenceGraph, Bidirec-
tionalGraph, AdjacencyGraph PropertyGraph, f i 1 t e r e d _
graph<Graph,EP.VP>.
filtered_graph.
graph_traits<filtered_graph>::vertex_descriptor
, filtered_graph.
( Graph.)
graph_traits<filtered_graph>::edge_descriptor
, fi ltered_graph.
( Graph.)
graph_traits<fi1tered_graph>::vertex_iterctor
, vertices . vertex_iterator
, .
( VertexListGraph.)
graph_trai ts<fi1tered_graph>::edge_i terator
, edges . -
MultiPassInputlterator.
( EdgeListGraph.)
graph_traits<filtered_graph>::out_edge_iterator
14.3. 2 5 9
, out_edges ().
uItiPassln putlterator.
( IncidenceGraph.)
graph_traits<filtered_graph>::i n_edge_i terator
, in_edges().
MultiPassInputlterator.
( BidirectionalGraph.)
graph_traits<fi"ltered_graph>: :adjacency_iterator
, adjacentverticesO.
, .
( AdjacencyGraph.)
graph_traits<filtered_graph>::directed_category
, (undirected_tag) -
(directed_tag).
( Graph.)
graph_traits<filtered_graph>::edge_pa el_category
, (-
).
, edge_pa el _catego .
( Graph.)
graph_traits<filtered_graph>::vertices_size_type
.
( VertexListGraph.)
graph_traits<filtered_graph>::edges_size_type
.
( EdgeListGraph.)
graph_traits<filtered_graph>::degree_size_type
.
( IncidenceGraph.)
property_map<filtered_graph. PropertyTag>::type
property_map<fi1tered_graph. PropertyTag>::const_type
.
, .
( PropertyGraph.)
filtered_graph.
filtered_graph(Graph& g. EdgePredicate ep)
g -
.
filtered_graph(Graphs g. EdgePredicate ep,
VertexPredicate vp)
260 14 BGL
g -
, vp.
, f i 1tered_graph, -
. 1, Graph in_edges(),
.
, filtered_graph , Graph, -
VertexListGraph, EdgeListGraph, IncidenceGraph, BidirectionaLGraph,
AdjacencyGraph, PropertyGraph BidirectionalGraph.
std::pair<vertex_iterator, vertex_iterator>
vertices(const fiHered_graph& g)
,
.
( VertexListGraph.)
std::pair<edge_iterator, edge_iterator>
edges(const filtered_graph& g)
, .
( EdgeListGraph.)
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(vertex_descriptor v.
const f"iltered_graph& g)
,
v .
( AdjacencyGraph.)
std::pair<out_edge_iterator. out_edge_iterator>
out_edges(vertex_descriptor v. const filt3red_graph& g)
,
v . , -
, v.
( IncidenceGraph.)
vertex_descriptor source(edge_descriptor e.
const filtered_graph& g)
.
( IncidenceGraph.)
vertex_descriptor target(edge_descriptor e,
const filtered_graph& g)
.
( IncidenceGraph.)
degree_size_type out_degree(vertex_descriptor u,
const filtered_graph& g)
, .
( IncidenceGraph.)
14.3. 2 6 1
. example/miles_span.cpp, example/roget_components.cpp
example/girth.cpp.
.
VertexListGraph, IncidenceGraph, AdjacencyGraph PropertyGraph. ,
SGB-, -
.
SGB Graph boost/graph/stanford_graph.hpp.
SGB Graph.
graph_traits<Graph*>::vertex_descriptor
, SGB Graph*.
Vertex* ( Vertex typedef
gb_graph.h.)
( Graph.)
graph_traits<Graph*>::edge_descriptor
, SGB Graph*.
boost:: sgb_edge_type. BGL -
boost: :sgb_edge -
: sgb_edge::sgb_edge(Arc* arc, Vertex* source).
( IncidenceGraph.)
graph_traits<Graph*>::vertex_iterator
, verticesO.
RandomAccessIterator.
( VertexListGraph.)
graph_traits<Graph*>::out_edge_iterator
, out_edges (). EdgeLi st=vecS,
MultiPassInputlterator.
( IncidenceGraph.)
graph_traits<Graph*>::adjacency_iterator
, adjacent_vertices().
, out_edge_iterator.
( AdjacencyGraph.)
graph_traits<Graph*>::directed_category
, -
. SGB Graph* , di rected_tag.
( Graph.)
14.3. 263
graph_traits<Graph*>::edge_pa 11 el_category
( -
). Graph* SGB
, 11 ow_pa 11 el _edge_tag.
( Graph.)
graph_traits<Graph*>::traversal_category
Graph* SGB , -
. , -
:
struct sgb_traversal_tag :
public virtual vertex_list_graph_tag,
public virtual incidence_graph_tag,
public virtual adjacency_graph_tag { };
( Graph.)
graph_traits<Graph*>::vertices_size_type
.
( VertexListGraph.)
graph_traits<Graph*>::edges_size_type
.
( EdgeListGraph.)
graph_traits<Graph*>::degree_size_type
.
( IncidenceGraph.)
property_map<Graph*, PropertyTag>::type
property_map<Graph*. PropertyTag>::const_type
. -
Property Tag ,
.
( PropertyGraph.)
SGB Graph .
SGB Graph.
std::pair<vertex_iterator, vertex_iterator>
vertices(const Graph* g)
, .
( VertexListGraph.)
std::pair<edge_iterator. edge_iterator>
edges(const Graph* g)
, .
( EdgeListGraph.)
264 14 BGL
std::pair<adjacency_iterator. adjacency_iterator>
adjacent_vertices(vertex_descriptor v. const Graph* g)
, ,
v .
( AdjacencyGraph.)
std::pair<out_edge_iterator, out_edge_iterator>
out edges(vertex_descriptor v, const Graph* g)
,
v . , -
, v.
( IncidenceGraph.)
vertex_descriptor source(edge_descriptor e,
const Graph* g)
.
( IncidenceGraph.)
vertex_descriptor target(edge_descriptor e.
const Graph* g)
.
( IncidenceGraph.)
degree_size_type out_degree(vertex_descriDtor u.
const Graph* g)
, .
( IncidenceGraph.)
vertices_size_type num_vertices(const Graph* g)
g.
( VertexListGraph.)
edges_snze_type num_edges(const Graph* g)
g.
( EdgeListGraph.)
vertex_descriptor vertex(vertices_size_type n. const Graph* g)
- .
template <typename PropertyTag>
property_map<Graph*, PropertyTag>::type
get(PropertyTag. Graph* g)
template <typename PropertyTag>
property_map<Graph*. PropertyTag>::const_type
get(PropertyTag, const Graph* g)
- , PropertyTag.
( PropertyGraph.)
template <typename PropertyTag. typename X>
typename property_traits<
typename property_map<Graph*. PropertyTag>::const_type
14.3. 265
>: :value_type
get(PropertyTag. const Graph* g, X x)
, .
( PropertyGraph.)
template <typename PropertyTag. typename X.
typename Value>
void put(PropertyTag. const Graph* g. X x.
const Value& value)
val ue. -
. ,
PropertyTag.
( PropertyGraph.)
Vertex Arc SGB -
. BGL-, -
. , -
. -
SGB Graph* get , -
, propertyjnap.
-
, ( 14.19).
14.19. SGB Graph*
// :
template <typename T> u_property:
emplate <typename T> v_property:
template <typename T> w_property;
template <typename T> x_property;
template <typename T> y_property:
template <typename T> z_property:
// :
emplate <typename T> a_property:
template <typename T> b_property:
uti 1,
gb_graph.h SGB. -
: Vertex*, Arc*, Graph*, char* long. -
LvatuePropertyMap.
vertex_i ndex_t ReadablePropertyMap.
edge_l ength_t, -
LvaluePropertyMap, long.
14.20 LEDA ,
BGL-.
14.20. LEDA
#include <boost/graph/leda_graph.hpp>
finclude <iostream>
fundef string // LEDA
int mainO
{
using namespace boost;
typedef GRAPH < std::string. int >graph_t:
graph_t g:
g.new_node("Philoctetes");
g.newjiode("Heracles"):
g.new_node("Al arena");
g.new_node("Eurystheus"):
g.new_node("Amphitryon");
typedef propertyjnap < graph_t. vertex_all_t >::type NodeMap;
NodeMap node_name_map - get(vertex_all. g):
graph_traits < graph_t >::vertex_iterator vi. vi_end;
for (tietvi. vi_end) = vertices(g): vi ! vi_end; ++vi)
std::cout node_name_map[*vi] std::endl:
return EXIT_SUCCESS:
}
:
Philoctetes
Heracles
Alcmena
Eurystheus
Amphitryon
GRAPH.
V , -
LEDA.
, -
LEDA.
VertexListGraph, BidirectionaLGraph AdjacencyGraph. VertexMutablePropertyGraph
EdgeMutabtePropertyGraph vertex_al l_t edge_a1 l_t, -
V LEDA. GRAPH -
tyGraph vertex_i ndex_t edge_i ndex_t, -
(ID) , LEDA ().
14.3. 267
GRAPH boost/graph/leda_graph.hpp.
GRAPH.
graph_traits<GRAPH>::vertex_descriptor
, GRAPH. node
LEDA.
( Graph.)
graph_traits<GRAPH>::edge_descriptor
, GRAPH. edge
LEDA.
( Graph.)
graph_traits<GRAPH>::vertex_iterator
, vertices .
( VertexListGraph.)
graph_traits<GRAPH>::out_edge_iterator
, out_edges().
( IncidenceGraph.)
graph_traits<GRAPH>::in_edge_nterator
, in_edges().
( BidirectionaLGraph.)
graph_traits<GRAPH>::adjacency_iterator
, adjacent_vertices().
( AdjacencyGraph.)
graph_traits<GRAPH>::directed_category
GRAPH LEDA , -
di rected_tag.
( Graph.)
graph_traits<GRAPH>::edge_parallel_category
GRAPH LEDA ,
allow_paral 1 el_edge_tag.
( Graph.)
graph_traits<GRAPH>::traversal_category
, -
, . :
struct Ieda_graph_traversa1_category :
public virtual bidirectional_graph_tag.
268 14 BGL
std::pair<edge_descriptor. bool>
add_edge(vertex_descriptor u. vertex_descriptor v.
const E& ep. GRAPHS g)
{, v)
.
( EdgeMutabLePropertyGraph.)
void remove_edge(vertex_descriptor u. vertex_descriptor v,
GRAPHS g)
{, v) .
( EdgeMutabLeGraph.)
void remove_edge(edge_descriptor e. GRAPHS g)
{, v) . remove_edge (u. v, g) -
: , -
move_edge(u. v. g) {, v).
( EdgeMutableGraph.)
vertex_descriptor add_vertex(GRAPHS g)
.
( VertexMutableGraph.)
vertexjjescriptor add vertex(const VertexPropertiesS p.
GRAPHS g)
( ) -
.
( VertexMutablePropertyGraph.)
void clear_vertex(vertex_descriptor u. GRAPHS g)
, , . -
.
( EdgeMutableGraph.)
void remove_vertex(vertex_descriptor u. GRAPHS g)
.
( VertexMutableGraph.)
template <typename PropertyTag>
property_map<GRAPH, PropertyTag>;:type
get(PropertyTag, GRAPHS g)
- ,
Property Tag.
( PropertyGraph.)
template <typename PropertyTag>
property_map<GRAPH, PropertyTag>::const_type
get(PropertyTag. const GRAPHS g)
- ,
Property Tag.
( PropertyGraph.)
14.3. 2 7 1
14.3.6. std::vector<EdgeList>
std::vector<EdgeLi st>
boost/graph/vector_as_graph.hpp -
std:: vector<std:: 1 i st<i n t .
( 14.21) , -
pri nt_graph() BGL (-
BGL) .
14.21.
( vector-as-graph.cpp >
#include <vector>
#include <list>
#include <boost/graph/vector_as_graph.hpp>
#include <boost/graph/graph_uti1ity.hpp>
int main() {
enum { r, s. t, u, v, w, x, y, N }:
char named - "rstuvwxy":
typedef std::vector < std::list < int > > Graph;
Graph g(N);
g[r].push_back(v); g[s].push_back(r): g[s].push_back(r);
g[s].push_back(w); g[t].push_back(x); g[u].push_back(t):
g[w].push_back(t); g[w].push_back(x): g[x].push_back(y);
g[y].push_back(u);
boost::print_graph(g. name):
return 0:
}
:
--> v
S --> W
t -->
272 14 BGL
--> t
V -->
W --> t X
-->
--> U
vector_as_graph boost/graph/vector_as_graph.hpp.
vector_as_graph.
EdgeLi st Container, val ue_type -
si ze_type std:: vector (-
-
).
VertexListGraph, IncidenceGraph AdjacencyGraph.
vector_as_graph.
graph_traits<std: :vector>: :vertex_descnptor
, .
( Graph.)
graph_traits<std::vector>::edge_descriptor
, .
( Graph.)
graph_traits<std::vector>::vertex_iterator
, vertices().
( VertexListGraph.)
graph_traits<std::vector>::out_edge_iterator
, out edges ().
( IncidenceGraph.)
graph_traits<std::vector>::adjacency_iterator
, adjacent_vertices().
( AdjacencyGraph.)
graph_traits<std::vector>::directed_category
, di -
rected_tag.
( Graph.)
graph_traits<std::vector>::edge_para!lel_category
, -
al 1ow_paral I el_edge_tag.
( Graph.)
14.3. 273
graph_traits<std::vector>::vertices_size_type
, .
( VertexListGraph.)
graph_traits<std::vector>::degree_size_type
, -
.
( IncidenceGraph.)
.
vector_as_graph.
std::pair<vertex_iterator. vertex_iterator>
vertices(const stid::vector& g)
, .
( VertexListGraph.)
std: :pair<adjacency_iterator.
adjacency_iterator> adjacent_vertices(vertex_descriptor v.
const std::vectors g)
, ,
v .
( AdjacencyGraph.)
std::pai r<out_edge_iterator. out_edge_iterator>
out_edges(vertex_descriptor v. const std::vector& g)
,
v . , -
, v.
( IncidenceGraph.)
vertex_descriptor source(edge_descriptor e.
const std::vector& g)
.
( IncidenceGraph.)
vertex_descriptor target(edge_descriptor e.
const std::vectors g)
.
( IncidenceGraph.)
degree_size_type out_degree(vertex_descriptor u.
const std::vector& g)
, .
( IncidenceGraph.)
vertices_size_type num_vertices(const std::vector& g)
.
( VertexListGraph.)
10 . 375
,
. , ,
, , -
, , .
: -
- , , ,
. -
,
^.
:
readable ( ). . -
.
(, )
.
writable ( ). . -
-
, .
read/write ( ). , -
.
, .
lvalue (1-). -
.
, .
.
namespace boost {
struct readable_property_map_tag { };
struct writable_property_map_tag { };
struct read_write_property_map_tag :
public readable_property_map_tag.
public writable_property_map_tag { };
struct lvalue_property_map_tag :
public read_write_property_map_tag { };
15.1.
,
- -
, , , ,
. , -
-
. ,
,
. -
: get(), put() operator[]. 15.2
276 15
,
.
15.2.
template <typename AddressMap>
void foo(AddressMap address)
{
typedef typename boost::property_traits<AddressMap>::
value_type value_type:
typedef typename boost::property_traits<AddressMap>::key_type key_type:
value_type old_address. new_address:
key_type fred = "Fred";
old_address = get(address. fred):
new_address = "384 Fitzpatrick Street"
put(address, fred, new_address):
key_type joe = "Joe":
value_type& joes_address - address[joe]:
joes_address = "325 Cushing Avenue":
}
- -
, - .
. ,
. ,
,
.
:
;
;
key property_traits<PMap>: :key_type;
val property_traits<PMap>: :value_type.
15.1.1. ReadablePropertyMap
ReadablePropertyMap --
, , get().
get -.
CopyConstructibLe
ReadablePropertyMap.
property_traits<PMap>::value_type
.
15.1. 277
property_traits<PMap>::reference
, .
property_traits<PMap>::key_type
-, . -
,
void.
property_traits<PMap>::category
: , readable_property_map_tag.
ReadablePropertyMap .
getCpmap, key)
: reference.
: , key.
15.1.2. WritablePropertyMap
WritabLePropertyMap -,
-, put .
CopyConstructible
WritablePropertyMap.
property_traits<PA>::value_type
.
property_traits<PA>::key_type
-, . -
,
void.
property_traits<PA>::category
: , writable_property_map_tag.
WritablePropertyMap .
put(pmap. key. vai)
: void.
: val , key.
15.1.3. ReadWritePropertyMap
ReadWritePropertyMap ReadablePropertyMap
WritablePropertyMap. ReadWritePropertyMap ,
278 15
15.1.4. LvaluePropertyMap
LvaluePropertyMap -
( , get ()). LvaluePropertyMap -
. Lval uePropertyMap , -
.
ReadablePropertyMap , ReadWritePropertyMap .
LvaluePropertyMap.
property_traits<PMap>::reference
,
val ue_type .
property_traits<PMap>::category
: , lvalue_property_map_tag.
LvaluePropertyMap .
pmap[key]
: reference.
: , .
15.2.
15.2.1. propertyjraits
property_tratis<PropertyMap>
property_traits
. ( -
) property_traits ,
.
namespace boost {
template <typename PA>
struct property_traits {
typedef typename PA::key_type key_type;
typedef typename PA::value_type value_type;
typedef typename PA:reference reference;
typedef typename PA::category category:
};
} // namespace boost
category
, :
15.2. 2 7 9
namespace boost {
struct readable_property_map_tag { }:
struct writable_property_map_tag { }:
struct read_write_property_map_tag : readable_property_map_tag.
writab1e_property_map_tag { };
struct lvalue_property_map_tag : read_write_property_map_tag { };
} // namespace boost
- ,
key_type . -
ty_traits 15.3.
15.3. property_traits
namespace boost {
tempi ate <typename T>
struct property_traits<T*> {
typedef std::ptrdiff_t key_type:
typedef T value_type:
typedef value_type& reference;
typedef lvalue_property_map_tag category;
}:
template <typename T>
void put(T* pa. std: :ptrdiff__t k. const T& val) { pa[k] = val; }
template <typename T>
const T& get(const T* pa, std;;ptrdiff_t k) { return pa[k]: }
property_traits .
PropertyMap
.
property_traits boost/property_map.hpp.
property_traits.
property_traits::key_type
-, .
property_traits::value_type
.
property_traits: reference
.
property_traits::category
.
280 15
15.2.2. iterator_property_map
15.4 .
15.4.
finclude <iostream>
#include <boost/property_map.hpp>
int mainO {
using namespace boost;
double x[] = { 0.2. 4.5. 3.2 }:
iterator_property_map < double *. identity_property_map.
double, doubles > pmap(x);
std::cout "x[l] = " get(pmap, 1) std::endl:
put(pmap. 0. 1.7);
std;:cout "x[0] - " pmap[0] std:;endl:
return 0;
}
:
x[l] - 4.5
x[0] = 1.7
iterator_property_map boost/graph/property_map.hpp.
iterator_property_map.
Iterator .
RandomAccessIterator
IndexMap ,
.
ReadablePropertyMap
.
std::iterator_traits<Iterator>::value_type
15.2. 2 8 1
R .
:
typename std::iterator_traits<Iterator>::reference
LvaluePropertyMap
, LvaluePropertyMap.
iterator_propertyjnap.
iterator_property_map(Iterator iter - IteratorO, IndexMap indexjnap =
IndexMapO)
.
template <typename Key>
reference operator! ](Key k) const;
*(iter + get (indexjnap, k)).
iterator_property_map.
template <typename Iterator, typename IndexMap>
iterator_property_map<Iterator, IndexMap.
typename std: :iterator_traits<Iterator>::value_type.
typename std::iterator_traits<Iterator>::reference>
make_iterator_property_map(Iterator iter,
IndexMap indexjnap)
.
15.2.3.
15.5 BGL.
15.5.
namespace boost {
enum vertex_index_t { vertexjndex = 1 } : \\
enum edge_index_t { edgejindex = 2 } ; \\
enum edgejiamej; { edgejiame = 3 }; \\
enum edge_weight_t { edge_weight = 4 }; \\
enum vertex_name_t { vertex_name = 5 }: \\
enum graph_name_t { graphjiame = 6 }: \\
enum vertexjjistance J : { vertex_distance = 7 } : \\
enum vertex_color_t { vertex_color = 8 }: \\
enum vertex_degree_t { vertex_degree = 9 }; \\
enum vertex_in_degree_t { vertex_in_degree = 10 }:
\\ #
282 15
15.5 {)
enum vertex_out_degree_t { vertex_out_degree = 11 };
\\
enum vertex_discover_time_t { vertex_discover_time = 12 }:
\\
enum vertex_finish__time_t { vertex_finish_time - 1 3 }:
\\
}
namespace boost {
\\
BOOST_INSTALL_PROPERTY(vertex, index);
B00ST_INSTALL_PROPERTY(edge. index);
BOOSTJNSTALL_PROPERTY(edge. name);
15.3.
. -
, .
Stanford GraphBase (SGB) (. -
14.3.4). -
std: :map.
15.6 sgb_vertex_name_map.
put_get_hel per ( boost/property_map.hpp)
. operator^] , a put_get_hel per put
get(). - put_get_hel per -
operator[], char*.
. reference -
, Lval uePropertyMap. -
ReadablePropertyMap. SGB Vertex* -
vertex_descriptor , key_type .
15.6. sgb_vertex_name_map
class sgb_vertex_name_map
: public put_get_helper< char*, sgb_vertex_name_map > {
public:
typedef boost::readable_property_map_tag category:
typedef char* value_type:
typedef char* reference:
typedef Vertex* key_type;
reference operator[ KVertex* v) const { return v->name; }
}:
15.3.2. std::map
-
- , -
. . --
.
std:: map. ,
std: :map , -
. ,
, -
hashjnap. , ,
UniquePai rAssociati veContainer ( 15.7).
15.7. associative_property_map
template <typename UniquePairAssociativeContainer>
class associative_property_map
: public put_get_helper<
typename UniquePairAssociativeContainer::value_type::second_type&.
associative_property_map<UniquePairAssociativeContainer> >
{
typedef UniquePairAssociativeContainer C;
public:
typedef typename C::key_type keyjtype;
typedef typename C::value_type::second_type value_type;
typedef value_type& reference:
typedef lvalue_property_map_tag category:
associative_property_map() : m_c(0) { }
associative_property_map(C& c) : m_c(&c) { }
reference operator[ ](const key_type& k) const {
return (*m_c)[k];
}
,
16.1. Bufer
Buffer () , -
, . Buffer .
- ,
. -
.
:
, Buffer;
;
t .
Buffer, .
::value_type
, . Assignable.
B::size_type
.
b.push(t)
t . b. size .
.
Buffer. , b.top().
b. s i ze () .
: b. empty () .
16.3. MultiPassInputlterator 285
b.topO
( ) .
: b. empty () .
b.sizeO
.
: b. size >= 0.
b.empty()
bool. b. size() == 0.
Buffer .
pushO, pop() sizeO
.
top empty () .
16.2. ColorValue
CoLorValue ,
. BGL
.
EqualityComparable. co1or_traits , -
. , ColorValue.
color_traits<T>::white()
: .
: , .
color_traits<T>::gray()
: .
: , .
color_traits<T>: :()
: .
: , .
16.3. MultiPassInputlterator
MultiPassInputlterator -
In putlterator. ,
, i t l == it2 Hi t l -
, *++itl == *++it2.
286 16 ,
MultiPassInputlterator Forwardlterator. ,
Forwardlterator , reference val ue_type&, MultiPass-
Inputlterator Inputlterator , reference -
value_type.
16.4. Monoid
Monoid .
5, (identity
element). C++ -, -
, , 5",
.
5 Assignable CopyConstructible.
BinaryFunction.
X . a, b X,
S. 1 X, -
( ). ,
.
(, )
: X.
: . .
b
: bool.
: , b 5.
!- b
: bool.
: , b S.
.
(. ) S.
.
((. ). ) (. (. ))
.
(, i ) ==
16.5. mutable_queue
(update). .
, -
Q.update(x). ,
(-) (ID),
mutable_queue
. 0 N, ./V ,
mutable_queue.
mutable_queue.
IndexedType
ID , index(t)
( t IndexedType),
.
Container
RandomAccessContainer. -
, IndexedType.
: std: :vector<IndexedType>.
Compare
BinaryPredicate ( ),
IndexedType .
: std: :less<typename Container: :value_type>.
ID
ReadablePropertyMap, IndexedType
, .
: i dent i ty_property_map.
mutable_queue.
valuejtype
, IndexedType.
size_type
, .
mutable_queue(size_type n. const Compares .
const ID& id = IDO)
. .
template <class Inputlterator>
mutable_queue(lnputlterator first, Inputlterator last,
const Compares const IDS id = IDO)
. std:: vector no
[first, last).
bool empty const
, .
288 16 ,
void pop
.
value_type& top()
.
value_type& front
top().
void push(const value_type& x)
.
void update(const value_type& x)
, -
. ,
, index () == index(x), -
.
16.6.
16.6.1. disjoint_sets
disjoint_sets<RankMap. ParentMap. Fn
idCompress>
di s joi nt_sets
(), (union-
find data structure). 5 = 5,, S2,..., Sk
.
, .
, ParentMap. -
:
.
disjoint_sets.
RankMap ReadWritePropertyMap
, -
ParentMap ReadWritePropertyMap.
-
Fi ndCompres s ,
.
: find_with_full_path_compression
-
kruskal jninimum_spanning_tree(). 16.1
16.6. 2 8 9
disjoint_sets.
disjoint_sets(RankMap . ParentMap p)
.
disjoint_sets(const disjoint_sets& x)
.
template <typename Element>
void make settlement x)
, .
template <typename Element>
void link (Element x. Element y)
, .
template <typename Element>
void union_set(Element x. Element y)
, .
link(find_set(x). find_set(y)).
template <typename Element>
Element find set (Element x)
, .
template <typename Element Iterators
std::size_t count_sets( Element Iterator f i r s t .
Element Iterator last)
.
template <typename Elementlterator>
void compress_sets(ElementIterator f i r s t .
Elementlterator last)
,
.
290 16 ,
0((, )), ,
, (make_set(),
find_set() linkO) . , -
, , .
16.6.2. find_with_path_halving
f1nd_wi th_path_hal vi ng
- ,
, ,
.
template <typename ParentMap. typename Element>
Element operator (ParentMap p. Element x)
1 6 . 6 . 3 . f i n d _ w i t h _ f u Il _ p a t h _ c o m p r e s s i o n
find with_full path_compression
- ,
, ,
.
template <typename ParentMap. typename Element>
Element operatorO(ParentMap p, Element x)
16.7. tie
template <typename Tl. typename T2>
tuple<Tl. T2> tie(Tl& a. T2& b);
Boost Tuple Library ( ), -
, ,
(, , ). tie() -
.
tie() boost/tuple/tupLe.hpp.
tie() verticesO,
std: :pair<vertex_iterator. vertex_iterator>. -
i end.
graph_traits<graph t>: :vertex_iterator i, end:
for(tie(i. end) = vertices(g): i != end: ++i)
// ...
16.2 , ti e() -
std: -.set.
16.8. graph_property_iter_range 2 9 1
16.2. tie().
#include <set>
#include <algorithm>
#include <iostream>
#inc1ude <boost/tuple/tuple.hpp>
int main() {
typedef std::set<int> SetT;
SetT::iterator i. end:
bool inserted:
int vals[5] - { 5. 2. 4. 9. 1 }:
SetT s(vals, vals + 5);
int newj/als[2] - { 3. 9 }:
for (int k = 0: k < 2; ++k) {
// t i e ( ) pair<iterator, bool>
b o o s t : : t i e d .inserted) = s.insert(new_vals[k]);
i f (!inserted)
std::cout * i " ."
std::endl:
else std::cout *i " ." std::endl; ndl:
}
return EXIT_SUCCESS:
}
:
3 .
9 .
16.8. g r a p h _ p r o p e r t y _ i t e r _ r a n g e
graph_property_iter_range<Graph. PropertyTag>
begin/end, -
.
( 16.3) ,
.
.
16.3. graph-property-iter-eg.cpp
< graph-property-iter-eg.cpp > =
#include <string>
#include <boost/graph/adjacencyji st.hpp>
#include <boost/graph/property_iter_range.hpp>
int
mainO
>
292 16 ,
16.3 {)
{
using namespace boost:
typedef adjacencyjlist < lists. vecS. directedS,
property < vertex_name_t. std::string > >graph_t:
graph_t g(3):
const char *vertex_names[] = { "Kubrick". "Clark", "Hal" };
int i = 0;
graph_property_iter_range < graph_t. vertex_name_t >::iterator v. v_end:
for (tie(v, v_end) get_property_iter_range(g. vertex_name):
v != v_end: ++v. ++i)
*v - vertex_names[i]:
tie(v, v_end) - get_property_iter_range(g, vertex_name):
std::copy(v. v_end. std::ostream_iterator < std::string >
Cstd::cout. " ")):
std::cout std::endl;
return 0:
}
:
Kubrick Clark Hal
graph_property_iter_range boost/graph/propertyjter_range.hpp.
graph_property_iter_range.
Graph PropertyGraph.
Property Tag , -
.
graph_property_iter_range.
graph_property_iter_range::iterator-
, ,
.
graph_property_iter_range::const_iterator
, ,
.
graph_property_iter_range::type
std: :pair<iterator, i t e r a t o r s
graph_property_iter_range::const_type
std: :pair<const_iterator. const_iterator>.
He .
16.8. gph_property_iter_range 293
graph_property_iter_range.
tempiate<typename Graph, typename Tag>
typename graph_property_iter_range<Graph, Tag>::type
get_property_iter_range(Graph& graph, const Tag& tag)
, ,
. -
.
tempiate<typename Graph, typename Tag>
typename graph_property_iter_range<Graph. Tag>::const_type
get_property_iter_range(const Graph& graph,
const Tag& tag)
, ,
. -
.
34. D. R. Musser, G. J. Derge, and A. Saini. STL Tutorial and Reference Guide.
Addison-Wesley, 2nd edition, 2001.
35. D. R. Musser and A. A. Stepanov. A library of generic algorithms in ada. In Using
Ada (1987 International Ada Conference), pages 216-225, New York, NY,
Dec. 1987. ACM SIGAda.
36. N. C. Myers. Traits: a new and useful template technique. C++ Report, June 1995,
37. R. Perlman. Fault-tolerant broadcast of routing information. Computer Networks.
December 1983.
38. R. Prim. Shortest connection networks and some generalizations. Bell System
Technical Journal, 36:1389-1401, 1957.
39. J. Siekand A. Lumsdaine. Concept checking: Binding parametric polymorphism
in C++. In First Workshop on C++ Template Programming, Erfurt, Germany,
October 10 2000.
40. A. A. Stepanov and M. Lee. The Standard Template Library. Technical Repon;
X3J16/94-0095, WG21/N0482, ISO Programming Language C++ Project,
May 1994.
41. B. Stroustrup. Design and Evolution of C++. Addison-Wesley, 1994.
42. B. Stroustrup. The C++ Programming Language. Addison Wesley, special edi-
tion, 2000.
43. R. Tarjan. Depth-first search and linear graph algorithms. SIAM Journal on
Computing, l(2):146-160, 1972.
44. R. E. Tarjan. Data Structures and Network Algorithms. Society for Industrial
and Applied Mathematics, 1983.
45. B. L. van der Waerden. Algebra. Frederick Ungar Publishing, 1970.
46. H. Warnsdorff. Des roesselsprungs einfachste und allgemeinste loesung.
Schmalkalden, 1823.
1. ., ., .
. .: , 1979.
2. ., . -
. .: , 1965.
3. . ., . . : -
. : , 1994.
4. . ., . . : , -
. .: -, 2003.
5. . . . .: , 1978.
6. . . .: , 1988.
7. . . .: , 1968.
8. . ., . . . .: , 1966.
9. . . .: , 1973.
C++ STL
1. . C++: -
. : -
, 2002.
2. . -
C++. .: , 2001.
3. ., . C++: .
: , 2003.
298
4. ., ., ., . -
: . .: , 2001
5. . C++ - . .
, 2003.
6. . STL: .
: , 2002
7. . . STL: -
C++. .: , 2004.
8. ., ., ., A, STL
+. - .: -, 2004.
9. . C++. C++ In-Depth. :
, 2003.
10. . C++. .:
, 2001.
11. . C++. .: , 2000.
a
a gdarth
laglo p
i ters 3v2sitior 67
m generic programming 39
aolocrta
h
itmor tyvpsietiors23431
H
hop 90
depth-first search 62 N
disconnected set 119 named parameters 58
disjoint-sets 205
overloaded functions 33
edge 25
edge descriptors 26 P
external property storage 64 policy class 135
p preflow 117
priority queue 129
flow network 117 property interface 230
forward or cross edge 83 property map 27
framework 45 push-relabel algorithm 117
300
R {)
200
refinement 47
202
182
signatures extension 42
207
Standard Template Library, STL 26
- 117
Stanford GraphBase 130
- 117, 209
47
tag dispatching 52
template specialization 50 84
traits class 47, 49, 151
Transmission Control Protocol, TCP 90 LEDA 130
traversal category 219 59
tree edge 83 79
54
109
union-find data structure 288
v
vertex 24 109
vertex descriptors 26 77, 89
77, 89
visitor concepts 164 25
w
WWW, World Wide Web 109
64
6 2
62
33
48
futered_graph 33
25
iterator_property_map 280
knights_tour_graph 125
mutable_queue 287
24
reversegraph 33
32, 216
Stanford GraphBase 282
25
40
25
25
11 25
24
- 92, 188 32
96, 183
192
102,194
13 62
62, 82, 177 77, 89
77, 89, 172
102, 197 52
3 0 1
()
42 Point 43
42 property 70, 79, 249
47 property_map 248
property_traits 275, 278
put_get_helper 282
reverse_graph 252
90 std48
topo_visitor 73
90 47, 49
89 200
102 110
124, 130 45
26, 39
Adjacency Graph 154
58 AdjacencyMatrix 157
48 BellmanFordVisitor 168
- 90 BFS Visitor 165
- 90 BidirectionalGraph 153
26 Buffer 284
230 Color Value 285
28, 45 DFS Visitor 166
28 Dijkstra Visitor 167
28 EdgeListGraph 156
28 EdgeMutableGraph 160
28 EdgeMutablePropertyGraph 164
28 Graph 151
IncidenceGraph 152
LvaluePropertyMap 278
Monoid 286
adjacency_list32,215, 274, 284 MultiPassInputlterator 285
adjacency_list_traits 246 MutableBidirectionalGraph 161
adjacency_matrix 32, 33, 235 MutableEdgeListGraph 162
adjacency_matrix_traits 247 MutablelncidenceGraph 161
array_traits 50 PropertyGraph 162
back_edge_recorder 85 ReadablePropertyMap 276
bacon_number_recorder 81 ReadWritePropertyMap 277
color_traits 285 UniquePairAssociativeContainer 283
ColorPoint 43 VertexListGraph 155
ColorPoint2 44 VertexMutableGraph 159
componentindex 208 VertexMutablePropertyGraph 163
disjoint_sets 288 WritablePropertyMap 277
edgejist 250 27
filtered_graph 256 77, 89
graph_property_iter_range 71, 291 77, 89
g r a p h t r a i t s 53, 243 89
302
91
62
95
- 55
89, 109
95
32
41 , 89
25
42
118, 125
42
205
25
77,83, 89
25
16, 39
25
45
25
182
117
170
25
- 30
69, 83,118,125
25
26
25
26
83
274, 284
83,118,125
27
identify_property_map 36
27
118, 125
compare 30 50
90 32
56 45
55 16, 2(:i
40 20
39, 41
40 Arc 282
31, 67 Vertex 282
84
48
117 52
117
84 39
48 234
54 34
117
118, 125 47
303
()
property_map.hpp 73, 279, 280, 283
adjacency_list.hpp 218, 246 PROTOTYPES 132, 261
adjacency_matrix.hpp 238, 247 relax.hpp 186, 190
bellman-example.cpp 190 reverse-graph-eg.cpp 252
bellman-ford-internet.cpp 92 reverse_graph.hpp 253
bellman_ford_shortest_paths.hpp 189 roget_components.q)[) 262
bfs-example.cpp 174 scc.cpp 112
breadth_first_search.hpp 173 stanford_graph.hpp 33, 132, 244,
261, 262
cc-internet.cpp 110
strong_components.hpp 202
connected_components.hpp 200
topological_sort.hpp 182
container_gen.cpp 233
depth_first_search.hpp 178, 181 topo-sortl.cpp35
dfs-example.cpp 179 topo-sort2.cpp 37
dijkstra-example.cpp 187, 229 topo-sort-with-sgb.cpp 133
dijkstra_shortest_paths.hpp 185 tuple.hpp 290
edge-connectivity .cpp 122 vector_as_graph.hpp 33, 271, 272
edge_list.hpp 251 . DIMACS 210
edge_property.cpp 232 30, 164
edmunds_karp_max_flow.hpp 209
family-tree-eg.cpp 216 accumulate 46
filtered_graph.hpp 258 add_edge 29
gb_graph.h 262, 265 a d d v e r t e x 29
girth.cpp 262 adjacent_vertices 29, 64
graph_archetypes.hpp 55 back_edge 67
graph_concepts.hpp 54 backtracking_search 127
graph-property-iter-eg.cpp 291 bellman_ford_shortest_path 58, 92
graph.cpp 136 breadth_first_search31, 79, 172
graph_traits.hpp 245 compute_loop_extent 86
graphviz.hpp 96, 104 connected_components 110, 200
incremental-components-eg.cpp 206 depth_first_search 34, 38, 82,177
incremental_components.hpp 207, 208 depth_first_visit 82, 88, 181
interior_property_map.cpp 233 dijkstra_shortest_paths 96, 183
johnson_alljpairs_shortest_paths.hpp 192 discover_vertex 67
kevin_bacon.txt 79 edmunds_karp_max_flow 209
kruskal-example.cpp 196 equal 43
kruskal_minimum_spanning_tree.hpp 195 find_loops 84
kruskal-telephone.cpp 104 finish_vertex 67
leda_graph.hpp 33, 131, 136, 244, forward_or_cross_edge 67
266, 267 get 71, 275
miles_span.cpp 262 has_cycle 66, 69
prim-example.cpp 199 has_cycle_dfs 66
prim_minimum_spanning_tree.hpp 198 in_edges29, 148
prim-telephone.cpp 106 incremental_components 207
property.hpp 66, 225, 226, 242, initialize_incremental_components 207
243, 248, 250, 256 is_self_loop 26
property_iter_range.hpp 292 johnson_all_pairs_shortest_paths 192
304
() ()
kruskal minimum spanning tree topo sort dfs 63
104, 194 topological_sort 34, 182
make_iterator_property map 93 tree_edge67, 81
num vertices 56 target 148
number_of_successors 128 vertex_index_map 36
operator 275 visitor 31, 82
out_edges29, 148 warnsdorff 129
prim_minimum_spanning_tree 197 who_owes_who 232
print_trans_delay 27, 28 42
print vertex name 27, 28 14
put 275 33
push relabel max flow 212
read_graphviz 96
same component 207
84
std30, 80 84
sum 40
source 148
strongcomponents 202
51
tie 28, 290 79
topo sort 65, 69 78