Skip to content

Commit f96bb9b

Browse files
committed
made changes
1 parent db6bf70 commit f96bb9b

File tree

2 files changed

+67
-83
lines changed

2 files changed

+67
-83
lines changed

src/coloring/acyclic_coloring.jl

Lines changed: 66 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,26 @@ Reference: Gebremedhin AH, Manne F, Pothen A. **New Acyclic and Star Coloring Al
1111
"""
1212
function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
1313
color = zeros(Int, nv(g))
14-
set = DisjointSets{Int}([])
14+
two_colored_forest = DisjointSets{Int}(())
1515

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))
2118

2219
forbidden_colors = zeros(Int, nv(g))
2320

2421
for v in vertices(g)
2522
for w in outneighbors(g, v)
26-
if color[w]!=0
23+
if color[w] != 0
2724
forbidden_colors[color[w]] = v
2825
end
2926
end
3027

3128
for w in outneighbors(g, v)
32-
if color[w]!=0
29+
if color[w] != 0
3330
for x in outneighbors(g, w)
34-
if color[x]!=0
31+
if color[x] != 0
3532
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)
3734
end
3835
end
3936
end
@@ -43,17 +40,17 @@ function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
4340
color[v] = min_index(forbidden_colors, v)
4441

4542
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)
4845
end
4946
end
5047

5148
for w in outneighbors(g, v)
52-
if color[w]!=0
49+
if color[w] != 0
5350
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)
5754
end
5855
end
5956
end
@@ -65,28 +62,29 @@ end
6562

6663

6764
"""
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})
7673
7774
Subroutine to avoid generation of 2-colored cycle due to coloring of vertex v,
7875
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'
8078
"""
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,
8282
w::Integer,
8383
x::Integer,
8484
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},
8886
color::AbstractVector{<:Integer})
89-
e = find(w, x, g, set)
87+
e = find(w, x, g, two_colored_forest)
9088
p, q = first_visit_to_tree[e]
9189

9290
if p != v
@@ -98,75 +96,75 @@ end
9896

9997

10098
"""
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})
107105
108106
Grow a 2-colored star after assigning a new color to the
109107
previously uncolored vertex v, by comparing it with the adjacent vertex w.
110108
Disjoint set is used to store stars in sets, which are identified through key
111109
edges present in g.
112110
"""
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,
114114
w::Integer,
115115
g::LightGraphs.AbstractGraph,
116-
set::DisjointSets{<:Integer},
117-
first_neighbor::Array{<: Tuple{Integer,Integer},1},
118116
color::AbstractVector{<:Integer})
119-
make_set!(v,w,g,set)
117+
insert_new_tree!(two_colored_forest,v,w,g)
120118
p, q = first_neighbor[color[w]]
121119

122120
if p != v
123121
first_neighbor[color[w]] = (v,w)
124122
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)
128126
end
129127
end
130128

131129

132130
"""
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)
138136
139137
Subroutine to merge trees present in the disjoint set which have a
140138
common edge.
141139
"""
142-
function merge_trees!(v::Integer,
140+
function merge_trees!(two_colored_forest::DisjointSets{<:Integer},
141+
v::Integer,
143142
w::Integer,
144143
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)
149147
if e1 != e2
150-
union!(set, e1, e2)
148+
union!(two_colored_forest, e1, e2)
151149
end
152150
end
153151

154152

155153
"""
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)
160158
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
162160
of the edge connecting v and w in the graph g
163161
"""
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)
168166
edge_index = find_edge_index(v,w,g)
169-
push!(set,edge_index)
167+
push!(two_colored_forest,edge_index)
170168
end
171169

172170

@@ -184,17 +182,17 @@ end
184182
find(w::Integer,
185183
x::Integer,
186184
g::LightGraphs.AbstractGraph,
187-
set::DisjointSets{<:Integer})
185+
two_colored_forest::DisjointSets{<:Integer})
188186
189187
Returns the root of the disjoint set to which the edge connecting vertices w and x
190188
in the graph g belongs to
191189
"""
192190
function find(w::Integer,
193191
x::Integer,
194192
g::LightGraphs.AbstractGraph,
195-
set::DisjointSets{<:Integer})
193+
two_colored_forest::DisjointSets{<:Integer})
196194
edge_index = find_edge_index(w, x, g)
197-
return find_root(set, edge_index)
195+
return find_root(two_colored_forest, edge_index)
198196
end
199197

200198

@@ -208,24 +206,10 @@ function find_edge_index(v::Integer, w::Integer, g::LightGraphs.AbstractGraph)
208206
pos = 1
209207
for i in edges(g)
210208

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)
212210
return pos
213211
end
214212
pos = pos + 1
215213
end
216214
throw(ArgumentError("$v and $w are not connected in the graph"))
217215
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

test/test_acyclic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using LightGraphs
33
using Test
44

55
using Random
6-
Random.seed!(45)
6+
Random.seed!(123)
77

88
# println("Starting acyclic coloring test...")
99
#= Test data =#

0 commit comments

Comments
 (0)