|
| 1 | +""" |
| 2 | + greedy_star1_coloring |
| 3 | +
|
| 4 | + Find a coloring of a given input graph such that |
| 5 | + no two vertices connected by an edge have the same |
| 6 | + color using greedy approach. The number of colors |
| 7 | + used may be equal or greater than the chromatic |
| 8 | + number `χ(G)` of the graph. |
| 9 | +
|
| 10 | + A star coloring is a special type of distance - 1 coloring, |
| 11 | + For a coloring to be called a star coloring, it must satisfy |
| 12 | + two conditions: |
| 13 | +
|
| 14 | + 1. every pair of adjacent vertices receives distinct colors |
| 15 | + (a distance-1 coloring) |
| 16 | +
|
| 17 | + 2. For any vertex v, any color that leads to a two-colored path |
| 18 | + involving v and three other vertices is impermissible for v. |
| 19 | + In other words, every path on four vertices uses at least three |
| 20 | + colors. |
| 21 | +
|
| 22 | + Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705. |
| 23 | +""" |
| 24 | +function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color) |
| 25 | + v = nv(g) |
| 26 | + color = zeros(Int64, v) |
| 27 | + |
| 28 | + forbidden_colors = zeros(Int64, v+1) |
| 29 | + |
| 30 | + for vertex_i = vertices(g) |
| 31 | + |
| 32 | + for w in inneighbors(g, vertex_i) |
| 33 | + if color[w] != 0 |
| 34 | + forbidden_colors[color[w]] = vertex_i |
| 35 | + end |
| 36 | + |
| 37 | + for x in inneighbors(g, w) |
| 38 | + if color[x] != 0 |
| 39 | + if color[w] == 0 |
| 40 | + forbidden_colors[color[x]] = vertex_i |
| 41 | + else |
| 42 | + for y in inneighbors(g, x) |
| 43 | + if color[y] != 0 |
| 44 | + if y != w && color[y] == color[w] |
| 45 | + forbidden_colors[color[x]] = vertex_i |
| 46 | + break |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + color[vertex_i] = find_min_color(forbidden_colors, vertex_i) |
| 56 | + end |
| 57 | + |
| 58 | + color |
| 59 | +end |
| 60 | + |
| 61 | +function find_min_color(forbidden_colors::AbstractVector, vertex_i::Integer) |
| 62 | + c = 1 |
| 63 | + while (forbidden_colors[c] == vertex_i) |
| 64 | + c+=1 |
| 65 | + end |
| 66 | + c |
| 67 | +end |
0 commit comments