|
| 1 | +--- |
| 2 | +layout: c-manual |
| 3 | +title: igraph Reference Manual |
| 4 | +mainheader: igraph Reference Manual |
| 5 | +lead: For using the igraph C library |
| 6 | +vmenu: true |
| 7 | +doctype: html/ |
| 8 | +langversion: 0.10.16 |
| 9 | +--- |
| 10 | + |
| 11 | +{% raw %} |
| 12 | +<div class="navigation-header mb-4" width="100%" summary="Navigation header"><div class="btn-group"> |
| 13 | +<a accesskey="p" class="btn btn-light" href="igraph-Isomorphism.html"><i class="fa fa-chevron-left"></i> |
| 14 | + Previous |
| 15 | + </a><a accesskey="h" class="btn btn-light" href="index.html"><i class="fa fa-home"></i> |
| 16 | + Home |
| 17 | + </a><a accesskey="n" class="btn btn-light" href="igraph-Motifs.html"><i class="fa fa-chevron-right"></i> |
| 18 | + Next |
| 19 | + </a> |
| 20 | +</div></div> |
| 21 | +<div class="chapter"> |
| 22 | +<div class="titlepage"><div><div><h1 class="title"> |
| 23 | +<a name="igraph-Coloring"></a>Chapter 18. Graph coloring</h1></div></div></div> |
| 24 | +<div class="toc"><dl class="toc"> |
| 25 | +<dt><span class="section"><a href="igraph-Coloring.html#igraph_vertex_coloring_greedy">1. <code class="function">igraph_vertex_coloring_greedy</code> — Computes a vertex coloring using a greedy algorithm.</a></span></dt> |
| 26 | +<dt><span class="section"><a href="igraph-Coloring.html#igraph_coloring_greedy_t">2. <code class="function">igraph_coloring_greedy_t</code> — Ordering heuristics for greedy graph coloring.</a></span></dt> |
| 27 | +<dt><span class="section"><a href="igraph-Coloring.html#igraph_is_perfect">3. <code class="function">igraph_is_perfect</code> — Checks if the graph is perfect.</a></span></dt> |
| 28 | +</dl></div> |
| 29 | +<div class="section"> |
| 30 | +<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 31 | +<a name="igraph_vertex_coloring_greedy"></a>1. <code class="function">igraph_vertex_coloring_greedy</code> — Computes a vertex coloring using a greedy algorithm.</h2></div></div></div> |
| 32 | +<a class="indexterm" name="id-1.19.2.2"></a><p> |
| 33 | +</p> |
| 34 | +<div class="informalexample"><pre class="programlisting"> |
| 35 | +igraph_error_t igraph_vertex_coloring_greedy(const igraph_t *graph, igraph_vector_int_t *colors, igraph_coloring_greedy_t heuristic); |
| 36 | +</pre></div> |
| 37 | +<p> |
| 38 | +</p> |
| 39 | +<p> |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +This function assigns a "color"—represented as a non-negative integer—to |
| 44 | +each vertex of the graph in such a way that neighboring vertices never have |
| 45 | +the same color. The obtained coloring is not necessarily minimal. |
| 46 | + |
| 47 | +</p> |
| 48 | +<p> |
| 49 | +Vertices are colored greedily, one by one, always choosing the smallest color |
| 50 | +index that differs from that of already colored neighbors. Vertices are picked |
| 51 | +in an order determined by the speified heuristic. |
| 52 | +Colors are represented by non-negative integers 0, 1, 2, ... |
| 53 | + |
| 54 | +</p> |
| 55 | +<p><b>Arguments: </b> |
| 56 | +</p> |
| 57 | +<div class="variablelist"><table border="0" class="variablelist"> |
| 58 | +<colgroup> |
| 59 | +<col align="left" valign="top"> |
| 60 | +<col> |
| 61 | +</colgroup> |
| 62 | +<tbody> |
| 63 | +<tr> |
| 64 | +<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td> |
| 65 | +<td><p> |
| 66 | + The input graph. |
| 67 | +</p></td> |
| 68 | +</tr> |
| 69 | +<tr> |
| 70 | +<td><p><span class="term"><em class="parameter"><code>colors</code></em>:</span></p></td> |
| 71 | +<td><p> |
| 72 | + Pointer to an initialized integer vector. The vertex colors will be stored here. |
| 73 | +</p></td> |
| 74 | +</tr> |
| 75 | +<tr> |
| 76 | +<td><p><span class="term"><em class="parameter"><code>heuristic</code></em>:</span></p></td> |
| 77 | +<td><p> |
| 78 | + The vertex ordering heuristic to use during greedy coloring. |
| 79 | + See <a class="link" href="igraph-Coloring.html#igraph_coloring_greedy_t" title="2. igraph_coloring_greedy_t — Ordering heuristics for greedy graph coloring."><code class="function">igraph_coloring_greedy_t</code></a> for more information.</p></td> |
| 80 | +</tr> |
| 81 | +</tbody> |
| 82 | +</table></div> |
| 83 | +<p> |
| 84 | + |
| 85 | + |
| 86 | +</p> |
| 87 | +<p><b>Returns: </b></p> |
| 88 | +<div class="variablelist"><table border="0" class="variablelist"> |
| 89 | +<colgroup> |
| 90 | +<col align="left" valign="top"> |
| 91 | +<col> |
| 92 | +</colgroup> |
| 93 | +<tbody><tr> |
| 94 | +<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td> |
| 95 | +<td><p> |
| 96 | + Error code. |
| 97 | + </p></td> |
| 98 | +</tr></tbody> |
| 99 | +</table></div> |
| 100 | +<p> |
| 101 | + |
| 102 | +</p> |
| 103 | +<div class="hideshow" onClick="toggle(this, event)"> |
| 104 | +<div class="example"> |
| 105 | +<a name="id-1.19.2.9.1"></a><p class="title"><b>Example 18.1. File <code class="code">examples/simple/igraph_coloring.c</code></b></p> |
| 106 | +<div class="example-contents"> |
| 107 | +<pre class="programlisting"><span class="strong"><strong>#include</strong></span> <igraph.h> |
| 108 | + |
| 109 | +int <span class="strong"><strong>main</strong></span>(void) { |
| 110 | + igraph_t graph; |
| 111 | + igraph_vector_int_t colors; |
| 112 | + |
| 113 | + <span class="emphasis"><em>/* Setting a seed makes the result of erdos_renyi_game_gnm deterministic. */</em></span> |
| 114 | + <span class="strong"><strong><a class="link" href="igraph-Random.html#igraph_rng_seed" title="3.3. igraph_rng_seed — Seeds a random number generator.">igraph_rng_seed</a></strong></span>(<span class="strong"><strong><a class="link" href="igraph-Random.html#igraph_rng_default" title="2.1. igraph_rng_default — Query the default random number generator.">igraph_rng_default</a></strong></span>(), 42); |
| 115 | + |
| 116 | + <span class="emphasis"><em>/* IGRAPH_UNDIRECTED and IGRAPH_NO_LOOPS are both equivalent to 0/FALSE, but</em></span> |
| 117 | +<span class="emphasis"><em> communicate intent better in this context. */</em></span> |
| 118 | + <span class="strong"><strong><a class="link" href="igraph-Generators.html#igraph_erdos_renyi_game_gnm" title="2.3. igraph_erdos_renyi_game_gnm — Generates a random (Erdős-Rényi) graph with a fixed number of edges.">igraph_erdos_renyi_game_gnm</a></strong></span>(&graph, 1000, 10000, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); |
| 119 | + |
| 120 | + <span class="emphasis"><em>/* As with all igraph functions, the vector in which the result is returned must</em></span> |
| 121 | +<span class="emphasis"><em> be initialized in advance. */</em></span> |
| 122 | + <span class="strong"><strong>igraph_vector_int_init</strong></span>(&colors, 0); |
| 123 | + <span class="strong"><strong><a class="link" href="igraph-Coloring.html#igraph_vertex_coloring_greedy" title="1. igraph_vertex_coloring_greedy — Computes a vertex coloring using a greedy algorithm.">igraph_vertex_coloring_greedy</a></strong></span>(&graph, &colors, IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS); |
| 124 | + |
| 125 | + <span class="emphasis"><em>/* Verify that the colouring is valid, i.e. no two adjacent vertices have the same colour. */</em></span> |
| 126 | + { |
| 127 | + igraph_integer_t i; |
| 128 | + <span class="emphasis"><em>/* Store the edge count to avoid the overhead from igraph_ecount in the for loop. */</em></span> |
| 129 | + igraph_integer_t no_of_edges = <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_ecount" title="4.2.2. igraph_ecount — The number of edges in a graph.">igraph_ecount</a></strong></span>(&graph); |
| 130 | + <span class="strong"><strong>for</strong></span> (i = 0; i < no_of_edges; ++i) { |
| 131 | + <span class="strong"><strong>if</strong></span> ( <span class="strong"><strong><a class="link" href="igraph-Data-structures.html#VECTOR" title="2.4.1. VECTOR — Accessing an element of a vector.">VECTOR</a></strong></span>(colors)[ <span class="strong"><strong><a class="link" href="igraph-Basic.html#IGRAPH_FROM" title="4.2.6. IGRAPH_FROM — The source vertex of an edge.">IGRAPH_FROM</a></strong></span>(&graph, i) ] == <span class="strong"><strong><a class="link" href="igraph-Data-structures.html#VECTOR" title="2.4.1. VECTOR — Accessing an element of a vector.">VECTOR</a></strong></span>(colors)[ <span class="strong"><strong><a class="link" href="igraph-Basic.html#IGRAPH_TO" title="4.2.7. IGRAPH_TO — The target vertex of an edge.">IGRAPH_TO</a></strong></span>(&graph, i) ] ) { |
| 132 | + <span class="strong"><strong>printf</strong></span>("Inconsistent coloring! Vertices %" IGRAPH_PRId " and %" IGRAPH_PRId " are adjacent but have the same color.\n", |
| 133 | + <span class="strong"><strong><a class="link" href="igraph-Basic.html#IGRAPH_FROM" title="4.2.6. IGRAPH_FROM — The source vertex of an edge.">IGRAPH_FROM</a></strong></span>(&graph, i), <span class="strong"><strong><a class="link" href="igraph-Basic.html#IGRAPH_TO" title="4.2.7. IGRAPH_TO — The target vertex of an edge.">IGRAPH_TO</a></strong></span>(&graph, i)); |
| 134 | + <span class="strong"><strong>abort</strong></span>(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + <span class="emphasis"><em>/* Destroy data structure when we are done. */</em></span> |
| 140 | + <span class="strong"><strong>igraph_vector_int_destroy</strong></span>(&colors); |
| 141 | + <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="4.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&graph); |
| 142 | + |
| 143 | + <span class="strong"><strong>return</strong></span> 0; |
| 144 | +} |
| 145 | +</pre> |
| 146 | +<p></p> |
| 147 | +</div> |
| 148 | +</div> |
| 149 | +<br class="example-break"> |
| 150 | +</div> |
| 151 | +<p> |
| 152 | +</p> |
| 153 | +</div> |
| 154 | +<div class="section"> |
| 155 | +<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 156 | +<a name="igraph_coloring_greedy_t"></a>2. <code class="function">igraph_coloring_greedy_t</code> — Ordering heuristics for greedy graph coloring.</h2></div></div></div> |
| 157 | +<a class="indexterm" name="id-1.19.3.2"></a><p> |
| 158 | +</p> |
| 159 | +<pre class="programlisting"> |
| 160 | +typedef enum { |
| 161 | + IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS = 0, |
| 162 | + IGRAPH_COLORING_GREEDY_DSATUR = 1 |
| 163 | +} igraph_coloring_greedy_t; |
| 164 | +</pre> |
| 165 | +<p> |
| 166 | +</p> |
| 167 | +<p> |
| 168 | + |
| 169 | + |
| 170 | +Ordering heuristics for <a class="link" href="igraph-Coloring.html#igraph_vertex_coloring_greedy" title="1. igraph_vertex_coloring_greedy — Computes a vertex coloring using a greedy algorithm."><code class="function">igraph_vertex_coloring_greedy()</code></a>. |
| 171 | + |
| 172 | +</p> |
| 173 | +<p><b>Values: </b> |
| 174 | +</p> |
| 175 | +<div class="variablelist"><table border="0" class="variablelist"> |
| 176 | +<colgroup> |
| 177 | +<col align="left" valign="top"> |
| 178 | +<col> |
| 179 | +</colgroup> |
| 180 | +<tbody> |
| 181 | +<tr> |
| 182 | +<td><p><span class="term"><code class="constant">IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS</code>:</span></p></td> |
| 183 | +<td><p> |
| 184 | + Choose the vertex with largest number of already colored neighbors. |
| 185 | +</p></td> |
| 186 | +</tr> |
| 187 | +<tr> |
| 188 | +<td><p><span class="term"><code class="constant">IGRAPH_COLORING_GREEDY_DSATUR</code>:</span></p></td> |
| 189 | +<td><p> |
| 190 | + Choose the vertex with largest number of unique colors in its neighborhood, i.e. its |
| 191 | + "saturation degree". When multiple vertices have the same saturation degree, choose |
| 192 | + the one with the most not yet colored neighbors. Added in igraph 0.10.4. This heuristic |
| 193 | + is known as "DSatur", and was proposed in |
| 194 | + Daniel Brélaz: New methods to color the vertices of a graph, |
| 195 | + Commun. ACM 22, 4 (1979), 251–256. <a class="ulink" href="https://doi.org/10.1145/359094.359101" target="_top">https://doi.org/10.1145/359094.359101</a></p></td> |
| 196 | +</tr> |
| 197 | +</tbody> |
| 198 | +</table></div> |
| 199 | +<p> |
| 200 | + |
| 201 | + |
| 202 | +</p> |
| 203 | +</div> |
| 204 | +<div class="section"> |
| 205 | +<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 206 | +<a name="igraph_is_perfect"></a>3. <code class="function">igraph_is_perfect</code> — Checks if the graph is perfect.</h2></div></div></div> |
| 207 | +<a class="indexterm" name="id-1.19.4.2"></a><p> |
| 208 | +</p> |
| 209 | +<div class="informalexample"><pre class="programlisting"> |
| 210 | +igraph_error_t igraph_is_perfect(const igraph_t *graph, igraph_bool_t *perfect); |
| 211 | +</pre></div> |
| 212 | +<p> |
| 213 | +</p> |
| 214 | +<p> |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +A perfect graph is an undirected graph in which the chromatic number of every induced |
| 219 | +subgraph equals the order of the largest clique of that subgraph. |
| 220 | +The chromatic number of a graph G is the smallest number of colors needed to |
| 221 | +color the vertices of G so that no two adjacent vertices share the same color. |
| 222 | + |
| 223 | +</p> |
| 224 | +<p> |
| 225 | +Warning: This function may create the complement of the graph internally, |
| 226 | +which consumes a lot of memory. For moderately sized graphs, consider |
| 227 | +decomposing them into biconnected components and running the check separately |
| 228 | +on each component. |
| 229 | + |
| 230 | +</p> |
| 231 | +<p> |
| 232 | +This implementation is based on the strong perfect graph theorem which was |
| 233 | +conjectured by Claude Berge and proved by Maria Chudnovsky, Neil Robertson, |
| 234 | +Paul Seymour, and Robin Thomas. |
| 235 | + |
| 236 | +</p> |
| 237 | +<p><b>Arguments: </b> |
| 238 | +</p> |
| 239 | +<div class="variablelist"><table border="0" class="variablelist"> |
| 240 | +<colgroup> |
| 241 | +<col align="left" valign="top"> |
| 242 | +<col> |
| 243 | +</colgroup> |
| 244 | +<tbody> |
| 245 | +<tr> |
| 246 | +<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td> |
| 247 | +<td><p> |
| 248 | + The input graph. It is expected to be undirected and simple. |
| 249 | +</p></td> |
| 250 | +</tr> |
| 251 | +<tr> |
| 252 | +<td><p><span class="term"><em class="parameter"><code>perfect</code></em>:</span></p></td> |
| 253 | +<td><p> |
| 254 | + Pointer to an integer, the result will be stored here. |
| 255 | +</p></td> |
| 256 | +</tr> |
| 257 | +</tbody> |
| 258 | +</table></div> |
| 259 | +<p> |
| 260 | +</p> |
| 261 | +<p><b>Returns: </b></p> |
| 262 | +<div class="variablelist"><table border="0" class="variablelist"> |
| 263 | +<colgroup> |
| 264 | +<col align="left" valign="top"> |
| 265 | +<col> |
| 266 | +</colgroup> |
| 267 | +<tbody><tr> |
| 268 | +<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td> |
| 269 | +<td><p> |
| 270 | + Error code. |
| 271 | + </p></td> |
| 272 | +</tr></tbody> |
| 273 | +</table></div> |
| 274 | +<p> |
| 275 | + |
| 276 | +Time complexity: worst case exponenital, often faster in practice. |
| 277 | + |
| 278 | +</p> |
| 279 | +</div> |
| 280 | +</div> |
| 281 | +<table class="navigation-footer" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="0"><tr valign="middle"> |
| 282 | +<td align="left"><a accesskey="p" href="igraph-Isomorphism.html"><b>← Chapter 17. Graph isomorphism</b></a></td> |
| 283 | +<td align="right"><a accesskey="n" href="igraph-Motifs.html"><b>Chapter 19. Graph motifs, dyad census and triad census →</b></a></td> |
| 284 | +</tr></table> |
| 285 | +{% endraw %} |
0 commit comments