Skip to content

Commit 3ca79fc

Browse files
committed
changed default initialization of data structures
1 parent 760025e commit 3ca79fc

File tree

2 files changed

+75
-60
lines changed

2 files changed

+75
-60
lines changed

src/coloring/acyclic_coloring_mod.jl

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,77 @@ function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
22
color = zeros(Int, nv(g))
33
set = DisjointSets{Int}([])
44

5-
first_visit_to_tree = Array{Tuple{Int, Int}, 1}(undef, ne(g))
6-
first_neighbor = Array{Tuple{Int, Int}, 1}(undef, ne(g))
5+
first_visit_to_tree = Array{Tuple{Int, Int}, 1}()
6+
first_neighbor = Array{Tuple{Int, Int}, 1}()
7+
8+
init_array!(first_visit_to_tree, ne(g))
9+
init_array!(first_neighbor, ne(g))
10+
11+
712

813
forbidden_colors = zeros(Int, nv(g))
914

1015
for v in vertices(g)
11-
println(">>>\nOUTER LOOP")
12-
println(">>> v = $v")
13-
println(">>> first block")
16+
# println(">>>\nOUTER LOOP")
17+
# println(">>> v = $v")
18+
# println(">>> first block")
1419
for w in outneighbors(g, v)
15-
println(">>> w = $w")
20+
# println(">>> w = $w")
1621
if color[w]!=0
17-
wc = color[w]
18-
println(">>> $w has nonzero color = $wc")
19-
println(">>> setting forbidden color[$wc] = $v")
22+
# wc = color[w]
23+
# println(">>> $w has nonzero color = $wc")
24+
# println(">>> setting forbidden color[$wc] = $v")
2025
forbidden_colors[color[w]] = v
2126
end
2227
end
2328

24-
println(">>> second block")
29+
# println(">>> second block")
2530
for w in outneighbors(g, v)
26-
println(">>> w = $w")
31+
# println(">>> w = $w")
2732
if color[w]!=0
28-
wc = color[w]
29-
println(">>> $w has nonzero color = $wc")
33+
# wc = color[w]
34+
# println(">>> $w has nonzero color = $wc")
3035
for x in outneighbors(g, w)
31-
println(">>> x = $x")
32-
wx = color[x]
33-
println(">>> $x has color = $wx")
36+
# println(">>> x = $x")
37+
# wx = color[x]
38+
# println(">>> $x has color = $wx")
3439
if color[x]!=0
35-
println(">>> $wx != 0")
36-
fbc = forbidden_color[color[x]]
37-
println(">>> forbidden color[$wx] = $fbc")
40+
# println(">>> $wx != 0")
41+
# fbc = forbidden_colors[color[x]]
42+
# println(">>> forbidden color[$wx] = $fbc")
3843
if forbidden_colors[color[x]] != v
39-
println(">>> $fbc != $v")
40-
println(">>> calling prevent cycle with $v, $w, $x")
44+
# println(">>> $fbc != $v")
45+
# println(">>> calling prevent cycle with $v, $w, $x")
4146
prevent_cycle!(v, w, x, g, set, first_visit_to_tree, forbidden_colors,color)
4247
end
4348
end
4449
end
4550
end
4651
end
4752

48-
println(">>> third block")
53+
# println(">>> third block")
4954
color[v] = min_index(forbidden_colors, v)
50-
vc = color[v]
51-
println(">>> color of v = $vc")
55+
# vc = color[v]
56+
# println(">>> color of v = $vc")
5257
for w in outneighbors(g, v)
53-
println(">>> w = $w")
58+
# println(">>> w = $w")
5459
if color[w]!=0
55-
println(">>> calling grow star for v = $v, w = $w")
60+
# println(">>> calling grow star for v = $v, w = $w")
5661
grow_star!(v, w, g, set,first_neighbor,color)
5762
end
5863
end
5964

60-
println(">>> fourth block")
65+
# println(">>> fourth block")
6166
for w in outneighbors(g, v)
62-
println(">>> w = $w"
67+
# println(">>> w = $w")
6368
if color[w]!=0
64-
wc = color[w]
65-
println(">>> $w has non zero color = $wc")
69+
# wc = color[w]
70+
# println(">>> $w has non zero color = $wc")
6671
for x in outneighbors(g, w)
67-
wx = color[x]
68-
println(">>> x = $x")
72+
# wx = color[x]
73+
# println(">>> x = $x")
6974
if color[x]!=0 && x!=v
70-
println(">>> $x has nonzero color = $wx")
75+
# println(">>> $x has nonzero color = $wx")
7176
if color[x]==color[v]
7277
merge_trees!(v,w,x,g,set)
7378
end
@@ -79,10 +84,16 @@ function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
7984
return color
8085
end
8186

87+
function init_array!(array, n)
88+
for i in 1:n
89+
push!(array,(0,0))
90+
end
91+
end
92+
8293
function prevent_cycle!(v:: Int, w:: Int, x::Int, g, set, first_visit_to_tree, forbidden_colors,color)
8394
e = find(w, x, g, set)
8495
p, q = first_visit_to_tree[e]
85-
println(">>> first visit to tree : p = $p, q = $q")
96+
# println(">>> first visit to tree : p = $p, q = $q")
8697
if p != v
8798
first_visit_to_tree[e] = (v,w)
8899
elseif q != w
@@ -93,9 +104,9 @@ end
93104
function grow_star!(v, w,g,set,first_neighbor,color)
94105
make_set!(v,w,g,set)
95106
p, q = first_neighbor[color[w]]
96-
wc = color[w]
97-
println(">>> color of w = $wc")
98-
println(">>> first neighbor : p = $p, q = $q")
107+
# wc = color[w]
108+
# println(">>> color of w = $wc")
109+
# println(">>> first neighbor : p = $p, q = $q")
99110
if p != v
100111
first_neighbor[color[w]] = (v,w)
101112
else

test/test_acyclic.jl

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ using LightGraphs
33
using Test
44

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

8+
# println("Starting acyclic coloring test...")
89
#= Test data =#
910
test_graphs = Vector{SimpleGraph}(undef, 0)
1011
test_graphs_dir = Vector{SimpleDiGraph}(undef, 0)
1112

12-
for _ in 1:5
13+
for _ in 1:6
1314
nv = rand(5:20)
1415
ne = rand(1:100)
1516
graph = SimpleGraph(nv)
@@ -94,7 +95,7 @@ for g in test_graphs
9495
end
9596

9697

97-
for i in 1:4
98+
for i in 1:5
9899
g = test_graphs[i]
99100
dg = test_graphs_dir[i]
100101
println("Testing graph $i")
@@ -107,7 +108,7 @@ for i in 1:4
107108
end
108109
println()
109110
out_colors = SparseDiffTools.color_graph(g, SparseDiffTools.AcyclicColoring())
110-
111+
# println(out_colors)
111112
#test condition 1
112113
for v in vertices(g)
113114
color = out_colors[v]
@@ -116,22 +117,25 @@ for i in 1:4
116117
end
117118
end
118119
end
119-
#
120-
# for i in 3:6
121-
# g = test_graphs[i]
122-
# dg = test_graphs_dir[i]
123-
# out_colors = SparseDiffTools.color_graph(g, SparseDiffTools.AcyclicColoring())
124-
#
125-
# #test condition 2
126-
# cycles = simplecycles(dg)
127-
# for c in cycles
128-
# colors = zeros(Int, 0)
129-
# if length(c) > 2
130-
# for v in c
131-
# push!(colors, out_colors[v])
132-
# end
133-
# @test length(unique(colors)) >= 3
134-
# end
135-
# end
136-
#
137-
# end
120+
121+
for i in 3:4
122+
g = test_graphs[i]
123+
dg = test_graphs_dir[i]
124+
# println("testing graph $i")
125+
out_colors = SparseDiffTools.color_graph(g, SparseDiffTools.AcyclicColoring())
126+
# println(out_colors)
127+
#test condition 2
128+
cycles = simplecycles(dg)
129+
for c in cycles
130+
colors = zeros(Int, 0)
131+
if length(c) > 2
132+
for v in c
133+
push!(colors, out_colors[v])
134+
end
135+
@test length(unique(colors)) >= 3
136+
end
137+
end
138+
# println("finished testing graph $i")
139+
end
140+
141+
# println("finished testing...")

0 commit comments

Comments
 (0)