@@ -11,29 +11,26 @@ Reference: Gebremedhin AH, Manne F, Pothen A. **New Acyclic and Star Coloring Al
11
11
"""
12
12
function color_graph (g:: LightGraphs.AbstractGraph , :: AcyclicColoring )
13
13
color = zeros (Int, nv (g))
14
- set = DisjointSets {Int} ([] )
14
+ two_colored_forest = DisjointSets {Int} (() )
15
15
16
- first_visit_to_tree = Array {Tuple{Int, Int}, 1} ()
17
- first_neighbor = Array {Tuple{Int, Int}, 1} ()
18
-
19
- init_array! (first_visit_to_tree, ne (g))
20
- init_array! (first_neighbor, ne (g))
16
+ first_visit_to_tree = fill ((0 ,0 ), ne (g))
17
+ first_neighbor = fill ((0 ,0 ), ne (g))
21
18
22
19
forbidden_colors = zeros (Int, nv (g))
23
20
24
21
for v in vertices (g)
25
22
for w in outneighbors (g, v)
26
- if color[w]!= 0
23
+ if color[w] != 0
27
24
forbidden_colors[color[w]] = v
28
25
end
29
26
end
30
27
31
28
for w in outneighbors (g, v)
32
- if color[w]!= 0
29
+ if color[w] != 0
33
30
for x in outneighbors (g, w)
34
- if color[x]!= 0
31
+ if color[x] != 0
35
32
if forbidden_colors[color[x]] != v
36
- prevent_cycle! (v, w, x, g, set, first_visit_to_tree, forbidden_colors ,color)
33
+ prevent_cycle! (first_visit_to_tree,forbidden_colors, v, w, x, g, two_colored_forest ,color)
37
34
end
38
35
end
39
36
end
@@ -43,17 +40,17 @@ function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
43
40
color[v] = min_index (forbidden_colors, v)
44
41
45
42
for w in outneighbors (g, v)
46
- if color[w]!= 0
47
- grow_star! (v, w, g, set,first_neighbor, color)
43
+ if color[w] != 0
44
+ grow_star! (two_colored_forest,first_neighbor, v, w, g, color)
48
45
end
49
46
end
50
47
51
48
for w in outneighbors (g, v)
52
- if color[w]!= 0
49
+ if color[w] != 0
53
50
for x in outneighbors (g, w)
54
- if color[x]!= 0 && x!= v
55
- if color[x]== color[v]
56
- merge_trees! (v,w,x,g,set )
51
+ if color[x] != 0 && x != v
52
+ if color[x] == color[v]
53
+ merge_trees! (two_colored_forest, v,w,x,g)
57
54
end
58
55
end
59
56
end
65
62
66
63
67
64
"""
68
- prevent_cycle(v:: Integer,
69
- w:: Integer,
70
- x ::Integer,
71
- g::LightGraphs.AbstractGraph ,
72
- color::AbstractVector{<: Integer} ,
73
- forbidden_colors::AbstractVector{<:Integer} ,
74
- first_visit_to_tree::Array{Tuple{ Integer, Integer}, 1 },
75
- set::DisjointSets{LightGraphs.Edge })
65
+ prevent_cycle!(first_visit_to_tree::AbstractVector{<:Tuple{ Integer,Integer}} ,
66
+ forbidden_colors::AbstractVector{<: Integer} ,
67
+ v ::Integer,
68
+ w::Integer ,
69
+ x:: Integer,
70
+ g::LightGraphs.AbstractGraph ,
71
+ two_colored_forest::DisjointSets{<: Integer},
72
+ color::AbstractVector{<:Integer })
76
73
77
74
Subroutine to avoid generation of 2-colored cycle due to coloring of vertex v,
78
75
which is adjacent to vertices w and x in graph g. Disjoint set is used to store
79
- the induced 2-colored subgraphs/trees where the id of set is a key edge of g
76
+ the induced 2-colored subgraphs/trees where the id of set is an integer
77
+ representing an edge of graph 'g'
80
78
"""
81
- function prevent_cycle! (v:: Integer ,
79
+ function prevent_cycle! (first_visit_to_tree:: AbstractVector{<:Tuple{Integer,Integer}} ,
80
+ forbidden_colors:: AbstractVector{<:Integer} ,
81
+ v:: Integer ,
82
82
w:: Integer ,
83
83
x:: Integer ,
84
84
g:: LightGraphs.AbstractGraph ,
85
- set:: DisjointSets{<:Integer} ,
86
- first_visit_to_tree:: Array{<:Tuple{Integer,Integer},1} ,
87
- forbidden_colors:: AbstractVector{<:Integer} ,
85
+ two_colored_forest:: DisjointSets{<:Integer} ,
88
86
color:: AbstractVector{<:Integer} )
89
- e = find (w, x, g, set )
87
+ e = find (w, x, g, two_colored_forest )
90
88
p, q = first_visit_to_tree[e]
91
89
92
90
if p != v
98
96
99
97
100
98
"""
101
- grow_star!(set ::DisjointSets{LightGraphs.Edge },
102
- v:: Integer,
103
- w ::Integer,
104
- g::LightGraphs.AbstractGraph ,
105
- first_neighbor::AbstractVector{<:Tuple{Integer, Integer}} ,
106
- color::AbstractVector{<: Integer})
99
+ grow_star!(two_colored_forest ::DisjointSets{<:Integer },
100
+ first_neighbor::AbstractVector{<: Tuple{ Integer,Integer}} ,
101
+ v ::Integer,
102
+ w::Integer ,
103
+ g::LightGraphs.AbstractGraph ,
104
+ color::AbstractVector{<:Integer})
107
105
108
106
Grow a 2-colored star after assigning a new color to the
109
107
previously uncolored vertex v, by comparing it with the adjacent vertex w.
110
108
Disjoint set is used to store stars in sets, which are identified through key
111
109
edges present in g.
112
110
"""
113
- function grow_star! (v:: Integer ,
111
+ function grow_star! (two_colored_forest:: DisjointSets{<:Integer} ,
112
+ first_neighbor:: AbstractVector{<: Tuple{Integer,Integer}} ,
113
+ v:: Integer ,
114
114
w:: Integer ,
115
115
g:: LightGraphs.AbstractGraph ,
116
- set:: DisjointSets{<:Integer} ,
117
- first_neighbor:: Array{<: Tuple{Integer,Integer},1} ,
118
116
color:: AbstractVector{<:Integer} )
119
- make_set! ( v,w,g,set )
117
+ insert_new_tree! (two_colored_forest, v,w,g)
120
118
p, q = first_neighbor[color[w]]
121
119
122
120
if p != v
123
121
first_neighbor[color[w]] = (v,w)
124
122
else
125
- e1 = find (v,w,g,set )
126
- e2 = find (p,q,g,set )
127
- union! (set , e1, e2)
123
+ e1 = find (v,w,g,two_colored_forest )
124
+ e2 = find (p,q,g,two_colored_forest )
125
+ union! (two_colored_forest , e1, e2)
128
126
end
129
127
end
130
128
131
129
132
130
"""
133
- merge_trees!(v:: Integer,
134
- w ::Integer,
135
- x ::Integer,
136
- g::LightGraphs.AbstractGraph ,
137
- set::DisjointSets{ LightGraphs.Edge} )
131
+ merge_trees!(two_colored_forest::DisjointSets{<: Integer} ,
132
+ v ::Integer,
133
+ w ::Integer,
134
+ x::Integer ,
135
+ g:: LightGraphs.AbstractGraph )
138
136
139
137
Subroutine to merge trees present in the disjoint set which have a
140
138
common edge.
141
139
"""
142
- function merge_trees! (v:: Integer ,
140
+ function merge_trees! (two_colored_forest:: DisjointSets{<:Integer} ,
141
+ v:: Integer ,
143
142
w:: Integer ,
144
143
x:: Integer ,
145
- g:: LightGraphs.AbstractGraph ,
146
- set:: DisjointSets{<:Integer} )
147
- e1 = find (v,w,g,set)
148
- e2 = find (w,x,g,set)
144
+ g:: LightGraphs.AbstractGraph )
145
+ e1 = find (v,w,g,two_colored_forest)
146
+ e2 = find (w,x,g,two_colored_forest)
149
147
if e1 != e2
150
- union! (set , e1, e2)
148
+ union! (two_colored_forest , e1, e2)
151
149
end
152
150
end
153
151
154
152
155
153
"""
156
- make_set!(v:: Integer,
157
- w ::Integer,
158
- g::LightGraphs.AbstractGraph ,
159
- set::DisjointSets{<:Integer} )
154
+ insert_new_tree!(two_colored_forest::DisjointSets{<: Integer} ,
155
+ v ::Integer,
156
+ w::Integer ,
157
+ g::LightGraphs.AbstractGraph )
160
158
161
- creates a new singleton set in the disjoint set 'set ' consisting
159
+ creates a new singleton set in the disjoint set 'two_colored_forest ' consisting
162
160
of the edge connecting v and w in the graph g
163
161
"""
164
- function make_set! (v :: Integer ,
165
- w :: Integer ,
166
- g :: LightGraphs.AbstractGraph ,
167
- set :: DisjointSets{<:Integer} )
162
+ function insert_new_tree! (two_colored_forest :: DisjointSets{<: Integer} ,
163
+ v :: Integer ,
164
+ w :: Integer ,
165
+ g :: LightGraphs.AbstractGraph )
168
166
edge_index = find_edge_index (v,w,g)
169
- push! (set ,edge_index)
167
+ push! (two_colored_forest ,edge_index)
170
168
end
171
169
172
170
@@ -184,17 +182,17 @@ end
184
182
find(w::Integer,
185
183
x::Integer,
186
184
g::LightGraphs.AbstractGraph,
187
- set ::DisjointSets{<:Integer})
185
+ two_colored_forest ::DisjointSets{<:Integer})
188
186
189
187
Returns the root of the disjoint set to which the edge connecting vertices w and x
190
188
in the graph g belongs to
191
189
"""
192
190
function find (w:: Integer ,
193
191
x:: Integer ,
194
192
g:: LightGraphs.AbstractGraph ,
195
- set :: DisjointSets{<:Integer} )
193
+ two_colored_forest :: DisjointSets{<:Integer} )
196
194
edge_index = find_edge_index (w, x, g)
197
- return find_root (set , edge_index)
195
+ return find_root (two_colored_forest , edge_index)
198
196
end
199
197
200
198
@@ -208,24 +206,10 @@ function find_edge_index(v::Integer, w::Integer, g::LightGraphs.AbstractGraph)
208
206
pos = 1
209
207
for i in edges (g)
210
208
211
- if (src (i)== v && dst (i)== w) || (src (i)== w && dst (i)== v)
209
+ if (src (i) == v && dst (i) == w) || (src (i) == w && dst (i) == v)
212
210
return pos
213
211
end
214
212
pos = pos + 1
215
213
end
216
214
throw (ArgumentError (" $v and $w are not connected in the graph" ))
217
215
end
218
-
219
-
220
- """
221
- init_array(array::AbstractVector{<:Tuple{Integer, Integer}},
222
- n::Integer)
223
-
224
- Helper function to initialize the data structures with tuple (0,0)
225
- """
226
- function init_array! (array:: Array{<: Tuple{Integer,Integer},1} ,
227
- n:: Integer )
228
- for i in 1 : n
229
- push! (array,(0 ,0 ))
230
- end
231
- end
0 commit comments