-
Notifications
You must be signed in to change notification settings - Fork 0
/
drca.jl
149 lines (132 loc) · 3.71 KB
/
drca.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Pkg
Pkg.add(["Colors", "Plots", "GraphPlot", "DataStructures", "Compose", "Graphs", "Cairo"])
using GraphPlot
using Plots
using Compose
using Graphs
using Cairo
using DataStructures
mutable struct vertexData
c::Int64
nColors::Vector{Int64}
end
function localColorSelection(id, lD, colors)
localData = lD[id]
if localData.c == 0
colorCandidate = rand(setdiff(colors, localData.nColors))
localData.c = colorCandidate
end
end
function localCandidateBroadcast(id, lD, graph)
for neigh in neighbors(graph, id)
push!(lD[neigh].nColors, lD[id].c)
end
end
function localCleanUp(id, lD, graph, uncolored)
if lD[id].c ∈ lD[id].nColors
for neigh in neighbors(graph, id)
deleteat!(lD[neigh].nColors, findfirst(isequal(lD[id].c), lD[neigh].nColors))
end
lD[id].c = 0
return
end
setdiff!(uncolored, id)
end
function simulate(g, Δ)
colors = Set(1:Δ+1)
println(colors)
uncolored::Set{Int64} = Set(collect(vertices(g)))
localData = Vector{vertexData}(undef, nv(g))
for i in 1:nv(g)
localData[i] = vertexData(0, Vector{Int64}())
end
while !isempty(uncolored)
for id in uncolored
localColorSelection(id, localData, colors)
end
for id in uncolored
localCandidateBroadcast(id, localData, g)
end
tmpuncolored = deepcopy(uncolored)
for id in tmpuncolored
localCleanUp(id, localData, g, uncolored)
end
end
return map(x -> x.c, localData)
end
function test_coloring(g, coloring, Δ)
for v in vertices(g)
for neigh in neighbors(g, v)
if (coloring[v] == coloring[neigh]) || (coloring[v] > Δ + 1) || (coloring[neigh] > Δ + 1)
@assert false, "Shit"
end
end
end
end
function generate_random_unweighted_graph(num_vertices, num_edges, Δ)
g = SimpleGraph(num_vertices)
candidateVertices = Set(1:num_vertices)
while ne(g) < num_edges
if length(candidateVertices) == 1
break
end
u, v = rand(candidateVertices, 2)
if length(neighbors(g, u)) == Δ
setdiff!(candidateVertices, u)
continue
elseif length(neighbors(g, v)) == Δ
setdiff!(candidateVertices, v)
continue
end
if length(candidateVertices) == 2 && u != v && has_edge(g, u, v)
break
end
if u != v && !has_edge(g, u, v)
add_edge!(g, u, v)
end
end
return g
end
function visualize_graph(g, name, colormap)
realColors = distinguishable_colors(length(colormap), colorant"blue")
# realColors = theme_palette(:auto).colors.colors
vertex_colors = [realColors[colormap[v]] for v in vertices(g)]
nodelabels = 1:nv(g)
draw(PDF(name, 16cm, 16cm), gplot(g, NODELABELSIZE=10 / sqrt(nv(g)), nodelabel=nodelabels, nodefillc=vertex_colors))
end
max_num_vertices, max_Δ = 800, 8
several = 100
for i in 1:several
num_vertices = rand(max_Δ:max_num_vertices)
Δ = rand(2:2:max_Δ)
num_edges = rand(num_vertices:num_vertices*Δ/2)
println("Generating Graph")
graph = random_regular_graph(num_vertices, Δ)
if is_directed(graph)
@assert false "Created directed Graph!!!"
end
println("Starting simulation")
coloring = simulate(graph, Δ)
println(coloring)
test_coloring(graph, coloring, Δ)
if i == several
visualize_graph(graph, "graph.pdf", coloring)
end
end
for i in 1:several
num_vertices = rand(max_Δ:max_num_vertices)
Δ = rand(2:max_Δ)
num_edges = rand(num_vertices:num_vertices*Δ/2)
println("Generating Graph")
graph = generate_random_unweighted_graph(num_vertices, num_edges, Δ)
if is_directed(graph)
@assert false "Created directed Graph!!!"
end
println("Starting simulation")
coloring = simulate(graph, Δ)
println(coloring)
test_coloring(graph, coloring, Δ)
if i == several
visualize_graph(graph, "graph.pdf", coloring)
end
end