1 | <HTML> |
---|
2 | <!-- |
---|
3 | -- Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000 |
---|
4 | -- |
---|
5 | -- Permission to use, copy, modify, distribute and sell this software |
---|
6 | -- and its documentation for any purpose is hereby granted without fee, |
---|
7 | -- provided that the above copyright notice appears in all copies and |
---|
8 | -- that both that copyright notice and this permission notice appear |
---|
9 | -- in supporting documentation. Silicon Graphics makes no |
---|
10 | -- representations about the suitability of this software for any |
---|
11 | -- purpose. It is provided "as is" without express or implied warranty. |
---|
12 | --> |
---|
13 | <Head> |
---|
14 | <Title>Boost Graph Library: Constructing Graph Algorithms</Title> |
---|
15 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
---|
16 | ALINK="#ff0000"> |
---|
17 | <IMG SRC="../../../boost.png" |
---|
18 | ALT="C++ Boost" width="277" height="86"> |
---|
19 | |
---|
20 | <BR Clear> |
---|
21 | |
---|
22 | <H1>Constructing graph algorithms with BGL</H1> |
---|
23 | |
---|
24 | <P> |
---|
25 | The <I>main</I> goal of BGL is not to provide a nice graph class, or |
---|
26 | to provide a comprehensive set of reusable graph algorithms (though |
---|
27 | these are goals). The main goal of BGL is to encourage others to |
---|
28 | write reusable graph algorithms. By reusable we mean maximally |
---|
29 | reusable. Generic programming is a methodology for making algorithms |
---|
30 | maximally reusable, and in this section we will discuss how to apply |
---|
31 | generic programming to constructing graph algorithms. |
---|
32 | |
---|
33 | <P> |
---|
34 | To illustrate the generic programming process we will step though the |
---|
35 | construction of a graph coloring algorithm. The graph coloring problem |
---|
36 | (or more specifically, the vertex coloring problem) is to label each |
---|
37 | vertex in a graph <i>G</i> with a color such that no two adjacent |
---|
38 | vertices are labeled with the same color and such that the minimum |
---|
39 | number of colors are used. In general, the graph coloring problem is |
---|
40 | NP-complete, and therefore it is impossible to find an optimal |
---|
41 | solution in a reasonable amount of time. However, there are many |
---|
42 | algorithms that use heuristics to find colorings that are close to the |
---|
43 | minimum. |
---|
44 | |
---|
45 | <P> |
---|
46 | The particular algorithm we present here is based on the linear time |
---|
47 | <TT>SEQ</TT> subroutine that is used in the estimation of sparse |
---|
48 | Jacobian and Hessian matrices [<A |
---|
49 | HREF="bibliography.html#curtis74:_jacob">9</A>,<A |
---|
50 | HREF="bibliography.html#coleman84:_estim_jacob">7</A>,<A |
---|
51 | HREF="bibliography.html#coleman85:_algor">6</A>]. This algorithm |
---|
52 | visits all of the vertices in the graph according to the order defined |
---|
53 | by the input order. At each vertex the algorithm marks the colors of |
---|
54 | the adjacent vertices, and then chooses the smallest unmarked color |
---|
55 | for the color of the current vertex. If all of the colors are already |
---|
56 | marked, a new color is created. A color is considered marked if its |
---|
57 | mark number is equal to the current vertex number. This saves the |
---|
58 | trouble of having to reset the marks for each vertex. The |
---|
59 | effectiveness of this algorithm is highly dependent on the input |
---|
60 | vertex order. There are several ordering algorithms, including the |
---|
61 | <I>largest-first</I> [<A HREF="bibliography.html#welsch67">31</A>], |
---|
62 | <I>smallest-last</I> [<a |
---|
63 | href="bibliography.html#matula72:_graph_theory_computing">29</a>], and |
---|
64 | <I>incidence degree</I> [<a |
---|
65 | href="bibliography.html#brelaz79:_new">32</a>] algorithms, which |
---|
66 | improve the effectiveness of this coloring algorithm. |
---|
67 | |
---|
68 | <P> |
---|
69 | The first decision to make when constructing a generic graph algorithm |
---|
70 | is to decide what graph operations are necessary for implementing the |
---|
71 | algorithm, and which graph concepts the operations map to. In this |
---|
72 | algorithm we will need to traverse through all of the vertices to |
---|
73 | intialize the vertex colors. We also need to access the adjacent |
---|
74 | vertices. Therefore, we will choose the <a |
---|
75 | href="./VertexListGraph.html">VertexListGraph</a> concept because it |
---|
76 | is the minimum concept that includes these operations. The graph type |
---|
77 | will be parameterized in the template function for this algorithm. We |
---|
78 | do not restrict the graph type to a particular graph class, such as |
---|
79 | the BGL <a href="./adjacency_list.html"><TT>adjacency_list</TT></a>, |
---|
80 | for this would drastically limit the reusability of the algorithm (as |
---|
81 | most algorithms written to date are). We do restrict the graph type to |
---|
82 | those types that model <a |
---|
83 | href="./VertexListGraph.html">VertexListGraph</a>. This is enforced by |
---|
84 | the use of those graph operations in the algorithm, and furthermore by |
---|
85 | our explicit requirement added as a concept check with |
---|
86 | <TT>function_requires()</TT> (see Section <A |
---|
87 | HREF="../../concept_check/concept_check.htm">Concept |
---|
88 | Checking</A> for more details about concept checking). |
---|
89 | |
---|
90 | <P> |
---|
91 | Next we need to think about what vertex or edge properties will be |
---|
92 | used in the algorithm. In this case, the only property is vertex |
---|
93 | color. The most flexible way to specify access to vertex color is to |
---|
94 | use the propery map interface. This gives the user of the |
---|
95 | algorithm the ability to decide how they want to store the properties. |
---|
96 | Since we will need to both read and write the colors we specify the |
---|
97 | requirements as <a |
---|
98 | href="../../property_map/ReadWritePropertyMap.html">ReadWritePropertyMap</a>. The |
---|
99 | <TT>key_type</TT> of the color map must be the |
---|
100 | <TT>vertex_descriptor</TT> from the graph, and the <TT>value_type</TT> |
---|
101 | must be some kind of integer. We also specify the interface for the |
---|
102 | <TT>order</TT> parameter as a property map, in this case a <a |
---|
103 | href="../../property_map/ReadablePropertyMap.html">ReadablePropertyMap</a>. For |
---|
104 | order, the <TT>key_type</TT> is an integer offset and the |
---|
105 | <TT>value_type</TT> is a <TT>vertex_descriptor</TT>. Again we enforce |
---|
106 | these requirements with concept checks. The return value of this |
---|
107 | algorithm is the number of colors that were needed to color the graph, |
---|
108 | hence the return type of the function is the graph's |
---|
109 | <TT>vertices_size_type</TT>. The following code shows the interface for our |
---|
110 | graph algorithm as a template function, the concept checks, and some |
---|
111 | typedefs. The implementation is straightforward, the only step not |
---|
112 | discussed above is the color initialization step, where we set the |
---|
113 | color of all the vertices to ``uncolored''. |
---|
114 | |
---|
115 | <P> |
---|
116 | <PRE> |
---|
117 | namespace boost { |
---|
118 | template <class VertexListGraph, class Order, class Color> |
---|
119 | typename graph_traits<VertexListGraph>::vertices_size_type |
---|
120 | sequential_vertex_color_ting(const VertexListGraph& G, |
---|
121 | Order order, Color color) |
---|
122 | { |
---|
123 | typedef graph_traits<VertexListGraph> GraphTraits; |
---|
124 | typedef typename GraphTraits::vertex_descriptor vertex_descriptor; |
---|
125 | typedef typename GraphTraits::vertices_size_type size_type; |
---|
126 | typedef typename property_traits<Color>::value_type ColorType; |
---|
127 | typedef typename property_traits<Order>::value_type OrderType; |
---|
128 | |
---|
129 | function_requires< VertexListGraphConcept<VertexListGraph> >(); |
---|
130 | function_requires< ReadWritePropertyMapConcept<Color, vertex_descriptor> >(); |
---|
131 | function_requires< IntegerConcept<ColorType> >(); |
---|
132 | function_requires< size_type, ReadablePropertyMapConcept<Order> >(); |
---|
133 | typedef typename same_type<OrderType, vertex_descriptor>::type req_same; |
---|
134 | |
---|
135 | size_type max_color = 0; |
---|
136 | const size_type V = num_vertices(G); |
---|
137 | std::vector<size_type> |
---|
138 | mark(V, numeric_limits_max(max_color)); |
---|
139 | |
---|
140 | typename GraphTraits::vertex_iterator v, vend; |
---|
141 | for (tie(v, vend) = vertices(G); v != vend; ++v) |
---|
142 | color[*v] = V - 1; // which means "not colored" |
---|
143 | |
---|
144 | for (size_type i = 0; i < V; i++) { |
---|
145 | vertex_descriptor current = order[i]; |
---|
146 | |
---|
147 | // mark all the colors of the adjacent vertices |
---|
148 | typename GraphTraits::adjacency_iterator ai, aend; |
---|
149 | for (tie(ai, aend) = adjacent_vertices(current, G); ai != aend; ++ai) |
---|
150 | mark[color[*ai]] = i; |
---|
151 | |
---|
152 | // find the smallest color unused by the adjacent vertices |
---|
153 | size_type smallest_color = 0; |
---|
154 | while (smallest_color < max_color && mark[smallest_color] == i) |
---|
155 | ++smallest_color; |
---|
156 | |
---|
157 | // if all the colors are used up, increase the number of colors |
---|
158 | if (smallest_color == max_color) |
---|
159 | ++max_color; |
---|
160 | |
---|
161 | color[current] = smallest_color; |
---|
162 | } |
---|
163 | return max_color; |
---|
164 | } |
---|
165 | } // namespace boost |
---|
166 | </PRE> |
---|
167 | |
---|
168 | <P> |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | <br> |
---|
173 | <HR> |
---|
174 | <TABLE> |
---|
175 | <TR valign=top> |
---|
176 | <TD nowrap>Copyright © 2000-2001</TD><TD> |
---|
177 | <A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, |
---|
178 | Indiana University (<A |
---|
179 | HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br> |
---|
180 | <A HREF="../../../people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br> |
---|
181 | <A HREF=http://www.osl.iu.edu/~lums>Andrew Lumsdaine</A>, |
---|
182 | Indiana University (<A |
---|
183 | HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) |
---|
184 | </TD></TR></TABLE> |
---|
185 | |
---|
186 | </BODY> |
---|
187 | </HTML> |
---|