Skip to content

Commit bf44735

Browse files
Merge pull request #52 from pkj-m/bsc
Backtracking Sequential Coloring
2 parents 1b115b8 + cfd7490 commit bf44735

File tree

5 files changed

+368
-173
lines changed

5 files changed

+368
-173
lines changed

src/SparseDiffTools.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export contract_color,
3434

3535

3636
include("coloring/high_level.jl")
37+
include("coloring/backtracking_coloring.jl")
3738
include("coloring/contraction_coloring.jl")
3839
include("coloring/greedy_d1_coloring.jl")
3940
include("coloring/greedy_star1_coloring.jl")

src/coloring/backtracking_coloring.jl

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
using LightGraphs
2+
3+
"""
4+
color_graph(g::LightGraphs, ::BacktrackingColor)
5+
6+
Returns a tight, distance-1 coloring of graph g
7+
using the minimum number of colors possible (i.e.
8+
the chromatic number of graph, χ(g))
9+
"""
10+
function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor)
11+
v = nv(g)
12+
13+
#A is list of vertices in non-increasing order of degree
14+
A = sort_by_degree(g)
15+
16+
#F is the coloring of vertices, 0 means uncolored
17+
#Fopt is the optimal coloring of the graph
18+
F = zeros(Int, v)
19+
Fopt= zeros(Int, v)
20+
21+
start = 1
22+
23+
#optimal color number
24+
opt = v + 1
25+
26+
#current vertex to be colored
27+
x = A[1]
28+
29+
#colors[j] = number of colors in A[0]...A[j]
30+
#assume colors[0] = 1
31+
colors = zeros(Int, v)
32+
33+
#set of free colors
34+
U = zeros(Int, 0)
35+
push!(U, 1)
36+
37+
#set of free colors of x
38+
freeColors = [Vector{Int}() for _ in 1:v]
39+
freeColors[x] = copy(U)
40+
41+
while (start >= 1)
42+
43+
back = false
44+
for i = start:v
45+
if i > start
46+
x = uncolored_vertex_of_maximal_degree(A,F)
47+
U = free_colors(x, A, colors, F, g, opt)
48+
sort!(U)
49+
end
50+
if length(U) > 0
51+
k = U[1]
52+
F[x] = k
53+
cp = F[x]
54+
deleteat!(U, 1)
55+
freeColors[x] = copy(U)
56+
if i==1
57+
l = 0
58+
else
59+
l = colors[i-1]
60+
end
61+
colors[i] = max(k, l)
62+
else
63+
start = i-1
64+
back = true
65+
break
66+
end
67+
end
68+
69+
if back
70+
if start >= 1
71+
x = A[start]
72+
F[x] = 0
73+
U = freeColors[x]
74+
end
75+
else
76+
Fopt = copy(F)
77+
opt = colors[v-1]
78+
i = least_index(F,A,opt)
79+
start = i-1
80+
if start < 1
81+
break
82+
end
83+
84+
#uncolor all vertices A[i] with i >= start
85+
uncolor_all!(F, A, start)
86+
87+
for i = 1:start+1
88+
x = A[i]
89+
U = freeColors[x]
90+
91+
#remove colors >= opt from U
92+
U = remove_higher_colors(U, opt)
93+
freeColors[x] = copy(U)
94+
end
95+
end
96+
end
97+
return Fopt
98+
end
99+
100+
"""
101+
sort_by_degree(g::LightGraphs.AbstractGraph)
102+
103+
Returns a list of the vertices of graph g sorted
104+
in non-increasing order of their degrees
105+
"""
106+
function sort_by_degree(g::LightGraphs.AbstractGraph)
107+
vs = vertices(g)
108+
degrees = (LightGraphs.degree(g, v) for v in vs)
109+
vertex_pairs = collect(zip(vs, degrees))
110+
sort!(vertex_pairs, by = p -> p[2], rev = true)
111+
return [v[1] for v in vertex_pairs]
112+
end
113+
114+
"""
115+
uncolored_vertex_of_maximal_degree(A::AbstractVector{<:Integer},F::AbstractVector{<:Integer})
116+
117+
Returns an uncolored vertex from the partially
118+
colored graph which has the highest degree
119+
"""
120+
function uncolored_vertex_of_maximal_degree(A::AbstractVector{<:Integer},F::AbstractVector{<:Integer})
121+
for v in A
122+
if F[v] == 0
123+
return v
124+
end
125+
end
126+
end
127+
128+
129+
"""
130+
free_colors(x::Integer,
131+
A::AbstractVector{<:Integer},
132+
colors::AbstractVector{<:Integer},
133+
F::Array{Integer,1},
134+
g::LightGraphs.AbstractGraph,
135+
opt::Integer)
136+
137+
Returns set of free colors of x which are less
138+
than optimal color number (opt)
139+
140+
Arguments:
141+
142+
x: Vertex who's set of free colors is to be calculated
143+
A: List of vertices of graph g sorted in non-increasing order of degree
144+
colors: colors[i] stores the number of distinct colors used in the
145+
coloring of vertices A[0], A[1]... A[i-1]
146+
F: F[i] stores the color of vertex i
147+
g: Graph to be colored
148+
opt: Current optimal number of colors to be used in the coloring of graph g
149+
"""
150+
function free_colors(x::Integer,
151+
A::AbstractVector{<:Integer},
152+
colors::AbstractVector{<:Integer},
153+
F::Array{Integer,1},
154+
g::LightGraphs.AbstractGraph,
155+
opt::Integer)
156+
index = -1
157+
158+
freecolors = zeros(Int, 0)
159+
160+
for i in eachindex(A)
161+
if A[i] == x
162+
index = i
163+
break
164+
end
165+
end
166+
167+
if index == 1
168+
colors_used = 0
169+
else
170+
colors_used = colors[index-1]
171+
end
172+
173+
colors_used += 1
174+
for c = 1:colors_used
175+
c_allowed = true
176+
for w in inneighbors(g, x)
177+
if F[w] == c
178+
c_allowed = false
179+
break
180+
end
181+
end
182+
if c_allowed && c < opt
183+
push!(freecolors, c)
184+
end
185+
end
186+
187+
return freecolors
188+
189+
end
190+
191+
"""
192+
least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer)
193+
194+
Returns least index i such that color of vertex
195+
A[i] is equal to `opt` (optimal color number)
196+
"""
197+
function least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer)
198+
for i in eachindex(A)
199+
if F[A[i]] == opt
200+
return i
201+
end
202+
end
203+
end
204+
205+
"""
206+
uncolor_all(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, start::Integer)
207+
208+
Uncolors all vertices A[i] where i is
209+
greater than or equal to start
210+
"""
211+
function uncolor_all!(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, start::Integer)
212+
for i = start:length(A)
213+
F[A[i]] = 0
214+
end
215+
end
216+
217+
"""
218+
remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer)
219+
220+
Remove all the colors which are greater than or
221+
equal to the `opt` (optimal color number) from
222+
the set of colors U
223+
"""
224+
function remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer)
225+
if length(U) == 0
226+
return U
227+
end
228+
u = zeros(Int, 0)
229+
for color in U
230+
if color < opt
231+
push!(u, color)
232+
end
233+
end
234+
return u
235+
end

0 commit comments

Comments
 (0)