|
| 1 | +""" |
| 2 | + color_graph(g::LightGraphs.AbstractGraphs, ::AcyclicColoring) |
| 3 | +
|
| 4 | +Returns a coloring vector following the acyclic coloring rules (1) the coloring |
| 5 | +corresponds to a distance-1 coloring, and (2) vertices in every cycle of the |
| 6 | +graph are assigned at least three distinct colors. This variant of coloring is |
| 7 | +called acyclic since every subgraph induced by vertices assigned any two colors |
| 8 | +is a collection of trees—and hence is acyclic. |
| 9 | +
|
| 10 | +Reference: Gebremedhin AH, Manne F, Pothen A. **New Acyclic and Star Coloring Algorithms with Application to Computing Hessians** |
| 11 | +""" |
| 12 | +function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring) |
| 13 | + |
| 14 | + color = zeros(Int, nv(g)) |
| 15 | + forbidden_colors = zeros(Int, nv(g)) |
| 16 | + |
| 17 | + set = DisjointSets{LightGraphs.Edge}([]) |
| 18 | + |
| 19 | + first_visit_to_tree = Array{Tuple{Int, Int}, 1}(undef, ne(g)) |
| 20 | + first_neighbor = Array{Tuple{Int, Int}, 1}(undef, nv(g)) |
| 21 | + |
| 22 | + for v in vertices(g) |
| 23 | + #enforces the first condition of acyclic coloring |
| 24 | + for w in outneighbors(g, v) |
| 25 | + if color[w] != 0 |
| 26 | + forbidden_colors[color[w]] = v |
| 27 | + end |
| 28 | + end |
| 29 | + #enforces the second condition of acyclic coloring |
| 30 | + for w in outneighbors(g, v) |
| 31 | + if color[w] != 0 #colored neighbor |
| 32 | + for x in outneighbors(g, w) |
| 33 | + if color[x] != 0 #colored x |
| 34 | + if forbidden_colors[color[x]] != v |
| 35 | + prevent_cycle(v, w, x, g, color, forbidden_colors, first_visit_to_tree, set) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + color[v] = min_index(forbidden_colors, v) |
| 43 | + |
| 44 | + #grow star for every edge connecting colored vertices v and w |
| 45 | + for w in outneighbors(g, v) |
| 46 | + if color[w] != 0 |
| 47 | + grow_star!(set, v, w, g, first_neighbor, color) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + #merge the newly formed stars into existing trees if possible |
| 52 | + for w in outneighbors(g, v) |
| 53 | + if color[w] != 0 |
| 54 | + for x in outneighbors(g, w) |
| 55 | + if color[x] != 0 && x != v |
| 56 | + if color[x] == color[v] |
| 57 | + merge_trees!(set, v, w, x, g) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + return color |
| 66 | +end |
| 67 | + |
| 68 | +""" |
| 69 | + prevent_cycle(v::Integer, |
| 70 | + w::Integer, |
| 71 | + x::Integer, |
| 72 | + g::LightGraphs.AbstractGraph, |
| 73 | + color::AbstractVector{<:Integer}, |
| 74 | + forbidden_colors::AbstractVector{<:Integer}, |
| 75 | + first_visit_to_tree::Array{Tuple{Integer, Integer}, 1}, |
| 76 | + set::DisjointSets{LightGraphs.Edge}) |
| 77 | +
|
| 78 | +Subroutine to avoid generation of 2-colored cycle due to coloring of vertex v, |
| 79 | +which is adjacent to vertices w and x in graph g. Disjoint set is used to store |
| 80 | +the induced 2-colored subgraphs/trees where the id of set is a key edge of g |
| 81 | +""" |
| 82 | +function prevent_cycle(v::Integer, |
| 83 | + w::Integer, |
| 84 | + x::Integer, |
| 85 | + g::LightGraphs.AbstractGraph, |
| 86 | + color::AbstractVector{<:Integer}, |
| 87 | + forbidden_colors::AbstractVector{<:Integer}, |
| 88 | + first_visit_to_tree::AbstractVector{<: Tuple{Integer, Integer}}, |
| 89 | + set::DisjointSets{LightGraphs.Edge}) |
| 90 | + |
| 91 | + edge = find_edge(g, w, x) |
| 92 | + e = find_root(set, edge) |
| 93 | + p, q = first_visit_to_tree[edge_index(g, e)] |
| 94 | + if p != v |
| 95 | + first_visit_to_tree[edge_index(g, e)] = (v, w) |
| 96 | + elseif q != w |
| 97 | + forbidden_colors[color[x]] = v |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +""" |
| 102 | + min_index(forbidden_colors::AbstractVector{<:Integer}, v::Integer) |
| 103 | +
|
| 104 | +Returns min{i > 0 such that forbidden_colors[i] != v} |
| 105 | +""" |
| 106 | +function min_index(forbidden_colors::AbstractVector{<:Integer}, v::Integer) |
| 107 | + return findfirst(!isequal(v), forbidden_colors) |
| 108 | +end |
| 109 | + |
| 110 | +""" |
| 111 | + grow_star!(set::DisjointSets{LightGraphs.Edge}, |
| 112 | + v::Integer, |
| 113 | + w::Integer, |
| 114 | + g::LightGraphs.AbstractGraph |
| 115 | + first_neighbor::Array{Tuple{Integer, Integer}, 1}) |
| 116 | +
|
| 117 | +Subroutine to grow a 2-colored star after assigning a new color to the |
| 118 | +previously uncolored vertex v, by comparing it with the adjacent vertex w. |
| 119 | +Disjoint set is used to store stars in sets, which are identified through key |
| 120 | +edges present in g. |
| 121 | +""" |
| 122 | +function grow_star!(set::DisjointSets{LightGraphs.Edge}, |
| 123 | + v::Integer, |
| 124 | + w::Integer, |
| 125 | + g::LightGraphs.AbstractGraph, |
| 126 | + first_neighbor::AbstractArray{<: Tuple{Integer, Integer}, 1}, |
| 127 | + color::AbstractVector{<: Integer}) |
| 128 | + edge = find_edge(g, v, w) |
| 129 | + push!(set, edge) |
| 130 | + p, q = first_neighbor[color[w]] |
| 131 | + if p != v |
| 132 | + first_neighbor[color[w]] = (v, w) |
| 133 | + else |
| 134 | + edge1 = find_edge(g, v, w) |
| 135 | + edge2 = find_edge(g, p, q) |
| 136 | + e1 = find_root(set, edge1) |
| 137 | + e2 = find_root(set, edge2) |
| 138 | + union!(set, e1, e2) |
| 139 | + end |
| 140 | + return nothing |
| 141 | +end |
| 142 | + |
| 143 | + |
| 144 | +""" |
| 145 | + merge_trees!(v::Integer, |
| 146 | + w::Integer, |
| 147 | + x::Integer, |
| 148 | + g::LightGraphs.AbstractGraph, |
| 149 | + set::DisjointSets{LightGraphs.Edge}) |
| 150 | +
|
| 151 | +Subroutine to merge trees present in the disjoint set which have a |
| 152 | +common edge. |
| 153 | +""" |
| 154 | +function merge_trees!(set::DisjointSets{LightGraphs.Edge}, |
| 155 | + v::Integer, |
| 156 | + w::Integer, |
| 157 | + x::Integer, |
| 158 | + g::LightGraphs.AbstractGraph) |
| 159 | + edge1 = find_edge(g, v, w) |
| 160 | + edge2 = find_edge(g, w, x) |
| 161 | + e1 = find_root(set, edge1) |
| 162 | + e2 = find_root(set, edge2) |
| 163 | + if (e1 != e2) |
| 164 | + union!(set, e1, e2) |
| 165 | + end |
| 166 | +end |
| 167 | + |
| 168 | + |
| 169 | +""" |
| 170 | + find_edge(g::LightGraphs.AbstractGraph, v::Integer, w::Integer) |
| 171 | +
|
| 172 | +Returns an edge object of the type LightGraphs.Edge which represents the |
| 173 | +edge connecting vertices v and w of the undirected graph g |
| 174 | +""" |
| 175 | +function find_edge(g::LightGraphs.AbstractGraph, |
| 176 | + v::Integer, |
| 177 | + w::Integer) |
| 178 | + for e in edges(g) |
| 179 | + if (src(e) == v && dst(e) == w) || (src(e) == w && dst(e) == v) |
| 180 | + return e |
| 181 | + end |
| 182 | + end |
| 183 | + throw(ArgumentError("$v and $w are not connected in graph g")) |
| 184 | +end |
| 185 | + |
| 186 | +""" |
| 187 | + edge_index(g::LightGraphs.AbstractGraph, e::LightGraphs.Edge) |
| 188 | +
|
| 189 | +Returns an Integer value which uniquely identifies the edge e in graph |
| 190 | +g. Used as an index in main function to avoid custom arrays with non- |
| 191 | +numerical indices. |
| 192 | +""" |
| 193 | +function edge_index(g::LightGraphs.AbstractGraph, |
| 194 | + e::LightGraphs.Edge) |
| 195 | + for (i, edge) in enumerate(edges(g)) |
| 196 | + if edge == e |
| 197 | + return i |
| 198 | + end |
| 199 | + end |
| 200 | + throw(ArgumentError("Edge $e is not present in graph g")) |
| 201 | +end |
0 commit comments