From ea42a8445374d3d7888095bc96d247061e2e5bb4 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Wed, 10 Jul 2019 12:41:08 +0530 Subject: [PATCH 01/16] bsc patch 1 --- src/coloring/bsc_algo.jl | 120 ++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/src/coloring/bsc_algo.jl b/src/coloring/bsc_algo.jl index 2ea165e1..f12b229b 100644 --- a/src/coloring/bsc_algo.jl +++ b/src/coloring/bsc_algo.jl @@ -1,29 +1,35 @@ +using OffsetArrays +using VertexSafeGraphs """ BSCColor Backtracking Sequential Coloring algorithm """ -function color_graph(G::VSafeGraph,::BSCColor) +function color_graph(G::VSafeGraph) V = nv(G) F = zeros(Int64, V) + F_opt = zeros(Int64, V) freeColors = [Vector{Int64}() for _ in 1:V] #set of free colors for each vertex + colors = zeros(Int64, V) U = zeros(Int64, 0) #stores set of free colors - #F = zeros(Int64, 0) #stores final coloring of vertices - sorted_vertices = order_by_degree(G) + + A = sorted_vertices(G) + #A = OffsetArray(A0, 0:length(A0)-1) + start = 1 optColorNumber = V + 1 - x = sorted_vertices[1] - colors[0] = 0 + x = A[1] + #colors[-1] = 0 push!(U, 1) - freeColors[x] = U + freeColors[x] = copy(U) - while (start >= 1) + while (start >= 0) back = false for i = 1:V if i > start - x = find_uncolored_vertex(sorted_vertices, F) - U = free_colors(x,F,G,optColorNumber) + x = find_uncolored_vertex(A, F) + U = free_colors(x,optColorNumber, colors, A, F, g) sort(U) end if length(U) > 0 @@ -31,7 +37,11 @@ function color_graph(G::VSafeGraph,::BSCColor) F[x] = k deleteat!(U,1) freeColors[x] = copy(U) - l = colors[i-1] + if i-1==0 + l = 0 + else + l = colors[i-1] + end colors[i] = max(k,l) else start = i - 1 @@ -48,13 +58,13 @@ function color_graph(G::VSafeGraph,::BSCColor) else F_opt = F optColorNumber = colors[V-1] - i = least_index(sorted_vertices,optColorNumber,G) + i = least_index(A,F,optColorNumber,G) start = i - 1 if start < 1 break #leave the while loop end - uncolor_all(F, sorted_vertices, start, G) - for i = 0:start + uncolor_all(F, A, start, G) + for i = 1:start x = A[i] U = freeColors[x] U = remove_colors(U, optColorNumber) @@ -77,71 +87,65 @@ end function sorted_vertices(G::VSafeGraph) - V = nv(G) - marked = zeros(Int64,V) - sv = zeros(Int64,0) + g1 = copy(g) + g2 = copy(g) + v = nv(g) + sorted = zeros(Int64, 0) max_degree = -1 max_degree_vertex = -1 - for i = 1:V + while (nv(g2) > 0) max_degree = -1 max_degree_vertex = -1 - for j = 1:V - if j != i - if degree(G,j) > max_degree && marked[j] == 0 - max_degree = degree(G,j) - max_degree_vertex = j - end + for vertex_i in vertices(g2) + if degree(g1, vertex_i) > max_degree + max_degree = degree(g1, vertex_i) + max_degree_vertex = vertex_i end end - push!(sv,max_degree_vertex) - marked[max_degree_vertex] = 1 + push!(sorted, max_degree_vertex) + rem_vertex!(g2, max_degree_vertex) end - return sv + return sorted end #find uncolored vertex of maximal degree of saturation -function find_uncolored_vertex(sv::Array{Int64,1}, G::VSafeGraph) - colors = zeros(Int64,0) - max_colors = -1 - max_color_index = -1 - for i = 1:nv(G) - if F[i] != 0 - for j in inneighbors(G,i) - if F[j] != 0 && F[j] in colors == false - push!(colors, F[j]) - end - end - if length(colors) > max_colors - max_colors = length(colors) - max_color_index = i - end - end - colors = zeros(Int64,0) - end - for i = 1:nv(G) - if A[i] == max_color_index +function find_uncolored_vertex(sv::Array{Int64,1}, F::Array{Int64,1}) + for i in sv + if F[i] == 0 return i end end - end #set of free colors of x, which are < optColorNumber -function free_colors(x::Int64, F::Array{Int64,1}, G::VSafeGraph, max_color::Int64) - colors = zeros(Int64,0) - for color in 1:max_color - present = true - for y in inneighbors(G,x) - if F[y] == color - present = false +function free_colors(x, ocn, colors, A, F, g) + index = -1 + freecolors = zeros(Int64, 0) + for i = 1:length(A) + if A[i] == x + index = i + break + end + end + if index == 1 + colors_used = 0 + else + colors_used = colors[index-1] + end + colors_used += 1 + for c = 1:colors_used + c_allowed = true + for w in inneighbors(g, x) + if F[w] == c + c_allowed = false break end end - if present - push!(colors,color) + if c_allowed && c < ocn + push!(freecolors, c) end end - return colors + freecolors end #least index with F(A[i]) = optColorNumber @@ -165,7 +169,7 @@ function remove_colors(U::Array{Int64,1}, optColorNumber::Int64) modified_U = zeros(Int64,0) for i = 1:length(U) if U[i] < optColorNumber - push!(mmodified_U, U[i]) + push!(modified_U, U[i]) end end return modified_U From 071de9a418261651613f7afcdce94b53c582ec6b Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Wed, 10 Jul 2019 12:55:13 +0530 Subject: [PATCH 02/16] bsc patch 1 --- src/coloring/bsc_algo.jl | 174 +++++++++++++++++++++++---------------- 1 file changed, 101 insertions(+), 73 deletions(-) diff --git a/src/coloring/bsc_algo.jl b/src/coloring/bsc_algo.jl index f12b229b..36eae9f5 100644 --- a/src/coloring/bsc_algo.jl +++ b/src/coloring/bsc_algo.jl @@ -1,92 +1,113 @@ -using OffsetArrays +#BSC 0-based using VertexSafeGraphs -""" - BSCColor - - Backtracking Sequential Coloring algorithm -""" -function color_graph(G::VSafeGraph) - V = nv(G) - F = zeros(Int64, V) - F_opt = zeros(Int64, V) - freeColors = [Vector{Int64}() for _ in 1:V] #set of free colors for each vertex - colors = zeros(Int64, V) - U = zeros(Int64, 0) #stores set of free colors - - - A = sorted_vertices(G) - #A = OffsetArray(A0, 0:length(A0)-1) - - start = 1 - optColorNumber = V + 1 - x = A[1] +using OffsetArrays + +function color_graph(g::VSafeGraph) + #number of vertices + v = nv(g) + + #ordering of vertices of g in non increasing order of degree + A_ = sort_by_degree(g) + A = OffsetArray(A_, 0:length(A_)-1) + + #starting index + start = 0 + + #optimal color number + opt = v + 1 + + #current vertex to be colored + x = A[0] + + #colors[-1] = 0 - push!(U, 1) + colors_ = zeros(Int64, v) + colors = OffsetArray(colors_, 0:length(colors_)-1) + + #U is set of free colors for current vertex + U_ = zeros(Int64, 0) + U = OffsetArray(U_, 0:-1) + push!(U,1) + + #freeColors[x] is set of free colors for vertex x + freeColors_ = [Vector{Int64}() for _ in 1:v] + freeColors = OffsetArray(freeColors_, 0:v-1) freeColors[x] = copy(U) - while (start >= 0) + while(start >= 0) back = false - for i = 1:V + for i = start:v if i > start - x = find_uncolored_vertex(A, F) - U = free_colors(x,optColorNumber, colors, A, F, g) + x = uncolored_vertex_of_max_degree(A, F) + + #set of freeColors for x less than opt + U = set_of_free_colors(x,A,colors,g, F, opt) + + #sort U non decreasing order sort(U) + end + if length(U) > 0 - k = U[1] + k = U[0] F[x] = k - deleteat!(U,1) + + #remove k from U + deleteat!(U, 0) freeColors[x] = copy(U) - if i-1==0 + + if i-1==-1 l = 0 else l = colors[i-1] end + colors[i] = max(k,l) else - start = i - 1 + start = i-1 back = true break end end + if back - if start >= 1 + if start >= 0 x = A[start] - F[x] = 0 #uncolor x + F[x] = 0 U = freeColors[x] end else - F_opt = F - optColorNumber = colors[V-1] - i = least_index(A,F,optColorNumber,G) - start = i - 1 - if start < 1 - break #leave the while loop + Fopt = copy(F) + opt = colors[v-1] + + #least index with F[A[i]] == opt + i = least_index(F,A,opt) + start = i-1 + if start < 0 + break end - uncolor_all(F, A, start, G) - for i = 1:start - x = A[i] + + #uncolor all vertices A[i] where i >= start + uncolor_all!(F,A,start) + for i = 0:start + x = A[i] U = freeColors[x] - U = remove_colors(U, optColorNumber) + + #remove from U all colors > opt + U = remove_from_U(U, opt) freeColors[x] = copy(U) end end end - return F_opt -end - -""" - vertex_degree(G,z) + Fopt +end -Find the degree of the vertex z which belongs to the graph G. -""" function degree(G::VSafeGraph,z::Int64) return length(inneighbors(G,z)) end - -function sorted_vertices(G::VSafeGraph) +function sort_by_degree(g::VSafeGraph) g1 = copy(g) g2 = copy(g) v = nv(g) @@ -108,26 +129,29 @@ function sorted_vertices(G::VSafeGraph) return sorted end -#find uncolored vertex of maximal degree of saturation -function find_uncolored_vertex(sv::Array{Int64,1}, F::Array{Int64,1}) - for i in sv +function uncolored_vertex_of_max_degree(A, F) + for i in A if F[i] == 0 return i end end end -#set of free colors of x, which are < optColorNumber -function free_colors(x, ocn, colors, A, F, g) + + +function set_of_free_colors(x,A,colors,g,F,opt) index = -1 - freecolors = zeros(Int64, 0) - for i = 1:length(A) + + freecolors_ = zeros(Int64, 0) + freecolors = OffsetArray(freecolors_, 0:-1) + + for i in eachindex(A) if A[i] == x index = i break end end - if index == 1 + if index == 0 colors_used = 0 else colors_used = colors[index-1] @@ -146,31 +170,35 @@ function free_colors(x, ocn, colors, A, F, g) end end freecolors + end -#least index with F(A[i]) = optColorNumber -function least_index(A::Array{Int64, 1}, F::Array{Int64,1}, optColorNumber::Int64, G::VSafeGraph) - for i = 1:nv(G) - if F[A[i]] == optColorNumber +#least index i such that F[A[i]] == opt +function least_index(F,A,opt) + for i in eachindex(A) + if F[A[i]] == opt return i end end end -#uncolor all vertices A[i] with i >= start -function uncolor_all(F::Array{Int64,1}, A::Array{Int64,1}, start::Int64, G::VSafeGraph) - for i = start:nv(G) + #uncolor all vertices A[i] where i >= start +function uncolor_all!(F,A,start) + for i = start:length(A)-1 F[A[i]] = 0 end end -#remove from U all colors >= optColorNumber -function remove_colors(U::Array{Int64,1}, optColorNumber::Int64) - modified_U = zeros(Int64,0) - for i = 1:length(U) - if U[i] < optColorNumber - push!(modified_U, U[i]) +#remove from U all colors >= opt +function remove_from_U!(U, opt) + u_ = zeros(In64, 0) + u = OffsetArray(u_, 0:-1) + for i in U + if i < opt + push!(u, i) end end - return modified_U + u end + + From 3100c90f563727ff39d5c8c55164b2332333de72 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Sat, 13 Jul 2019 00:49:26 +0530 Subject: [PATCH 03/16] bsc index 1 based --- src/coloring/bsc_algo.jl | 166 ++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 98 deletions(-) diff --git a/src/coloring/bsc_algo.jl b/src/coloring/bsc_algo.jl index 36eae9f5..3d3e416f 100644 --- a/src/coloring/bsc_algo.jl +++ b/src/coloring/bsc_algo.jl @@ -1,77 +1,68 @@ -#BSC 0-based -using VertexSafeGraphs -using OffsetArrays +#bsc 1-based indexing +using LightGraphs -function color_graph(g::VSafeGraph) - #number of vertices +function color_graph(g::LightGraphs.AbstractGraph) + + #number of vertices in g v = nv(g) - #ordering of vertices of g in non increasing order of degree - A_ = sort_by_degree(g) - A = OffsetArray(A_, 0:length(A_)-1) + #A is order of vertices in non-increasing order of degree + A = sort_by_degree(g) + + #F is the coloring of vertices, 0 means uncolored + #Fopt is the optimal coloring of the graph + F = zeros(Int32, v) + Fopt= zeros(Int32, v) - #starting index - start = 0 + #start index + start = 1 #optimal color number opt = v + 1 #current vertex to be colored - x = A[0] + x = A[1] + #colors[j] = number of colors in A[0]...A[j] + #assume colors[0] = 1 + colors = zeros(Int32, v) - #colors[-1] = 0 - colors_ = zeros(Int64, v) - colors = OffsetArray(colors_, 0:length(colors_)-1) + #set of free colors + U = zeros(Int32, 0) + push!(U, 1) - #U is set of free colors for current vertex - U_ = zeros(Int64, 0) - U = OffsetArray(U_, 0:-1) - push!(U,1) - - #freeColors[x] is set of free colors for vertex x - freeColors_ = [Vector{Int64}() for _ in 1:v] - freeColors = OffsetArray(freeColors_, 0:v-1) + #set of free colors of x + freeColors = [Vector{Int64}() for _ in 1:v] freeColors[x] = copy(U) - while(start >= 0) + while (start >= 1) + back = false for i = start:v if i > start - x = uncolored_vertex_of_max_degree(A, F) - - #set of freeColors for x less than opt - U = set_of_free_colors(x,A,colors,g, F, opt) - - #sort U non decreasing order - sort(U) - + x = uncolored_vertex_of_maximal_degree(A,F) + U = free_colors(x, A, colors, F, g, opt) + sort!(U) end - if length(U) > 0 - k = U[0] + k = U[1] F[x] = k - - #remove k from U - deleteat!(U, 0) + deleteat!(U, 1) freeColors[x] = copy(U) - - if i-1==-1 + if i==1 l = 0 else l = colors[i-1] + colors[i] = max(k, l) end - - colors[i] = max(k,l) else start = i-1 back = true break end end - if back - if start >= 0 + if start >= 1 x = A[start] F[x] = 0 U = freeColors[x] @@ -79,71 +70,53 @@ function color_graph(g::VSafeGraph) else Fopt = copy(F) opt = colors[v-1] - - #least index with F[A[i]] == opt i = least_index(F,A,opt) start = i-1 - if start < 0 + if start < 1 break end - #uncolor all vertices A[i] where i >= start - uncolor_all!(F,A,start) - for i = 0:start + #uncolor all vertices A[i] with i >= start + uncolor_all!(F, A, start) + + #try start+1 instead + for i = 1:start+1 x = A[i] U = freeColors[x] - - #remove from U all colors > opt - U = remove_from_U(U, opt) + #remove colors >= opt from U + U = remove_higher_colors(U, opt) freeColors[x] = copy(U) end end + end - Fopt + F end -function degree(G::VSafeGraph,z::Int64) - return length(inneighbors(G,z)) -end -function sort_by_degree(g::VSafeGraph) - g1 = copy(g) - g2 = copy(g) - v = nv(g) - sorted = zeros(Int64, 0) - max_degree = -1 - max_degree_vertex = -1 - while (nv(g2) > 0) - max_degree = -1 - max_degree_vertex = -1 - for vertex_i in vertices(g2) - if degree(g1, vertex_i) > max_degree - max_degree = degree(g1, vertex_i) - max_degree_vertex = vertex_i - end - end - push!(sorted, max_degree_vertex) - rem_vertex!(g2, max_degree_vertex) - end - return sorted +function sort_by_degree(g::LightGraphs.AbstractGraph) + vs = vertices(g) + degrees = (LightGraphs.degree(g, v) for v in vs) + vertex_pairs = collect(zip(vs, degrees)) + sort!(vertex_pairs, by = p -> p[2], rev = true) + [v[1] for v in vertex_pairs] end -function uncolored_vertex_of_max_degree(A, F) - for i in A - if F[i] == 0 - return i + +function uncolored_vertex_of_maximal_degree(A,F) + for v in A + if F[v] == 0 + return v end end end - -function set_of_free_colors(x,A,colors,g,F,opt) +function free_colors(x, A, colors, F, g, opt) index = -1 - freecolors_ = zeros(Int64, 0) - freecolors = OffsetArray(freecolors_, 0:-1) + freecolors = zeros(Int64, 0) for i in eachindex(A) if A[i] == x @@ -151,11 +124,13 @@ function set_of_free_colors(x,A,colors,g,F,opt) break end end - if index == 0 + + if index == 1 colors_used = 0 else colors_used = colors[index-1] end + colors_used += 1 for c = 1:colors_used c_allowed = true @@ -169,11 +144,11 @@ function set_of_free_colors(x,A,colors,g,F,opt) push!(freecolors, c) end end + freecolors end -#least index i such that F[A[i]] == opt function least_index(F,A,opt) for i in eachindex(A) if F[A[i]] == opt @@ -182,23 +157,18 @@ function least_index(F,A,opt) end end - #uncolor all vertices A[i] where i >= start -function uncolor_all!(F,A,start) - for i = start:length(A)-1 +function uncolor_all!(F, A, start) + for i = start:length(A) F[A[i]] = 0 end end -#remove from U all colors >= opt -function remove_from_U!(U, opt) - u_ = zeros(In64, 0) - u = OffsetArray(u_, 0:-1) - for i in U - if i < opt - push!(u, i) + +function remove_higher_colors(U, opt) + u = zeros(Int32, 0) + for color in U + if color < opt + push!(u, color) end end - u end - - From 43be4fc11cbcc82f40758020e7be03fb408736a0 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Fri, 19 Jul 2019 14:07:00 +0530 Subject: [PATCH 04/16] BSC algorithm --- src/coloring/bsc_algo.jl | 33 +++++++++++++++++++++++++++++++++ test/test_bsc.jl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/test_bsc.jl diff --git a/src/coloring/bsc_algo.jl b/src/coloring/bsc_algo.jl index 3d3e416f..06779226 100644 --- a/src/coloring/bsc_algo.jl +++ b/src/coloring/bsc_algo.jl @@ -94,7 +94,12 @@ function color_graph(g::LightGraphs.AbstractGraph) F end +""" + sort_by_degree() +sort and store the vertices of graph g in +non-increasing order of their degrees +""" function sort_by_degree(g::LightGraphs.AbstractGraph) vs = vertices(g) degrees = (LightGraphs.degree(g, v) for v in vs) @@ -103,7 +108,12 @@ function sort_by_degree(g::LightGraphs.AbstractGraph) [v[1] for v in vertex_pairs] end +""" + uncolored_vertex_of_maximal_degree(A,F) +Returns an uncolored vertex from the graph which has maximum +degree +""" function uncolored_vertex_of_maximal_degree(A,F) for v in A if F[v] == 0 @@ -113,6 +123,12 @@ function uncolored_vertex_of_maximal_degree(A,F) end +""" + free_colors() + +returns set of free colors of x which are less +than optimal color number (opt) +""" function free_colors(x, A, colors, F, g, opt) index = -1 @@ -149,6 +165,12 @@ function free_colors(x, A, colors, F, g, opt) end +""" + least_index() + +returns least index i such that color of vertex +A[i] == opt (optimal color number) +""" function least_index(F,A,opt) for i in eachindex(A) if F[A[i]] == opt @@ -157,13 +179,24 @@ function least_index(F,A,opt) end end +""" + uncolor_all() + +uncolors all vertices A[i] where +i >= start +""" function uncolor_all!(F, A, start) for i = start:length(A) F[A[i]] = 0 end end +""" + remove_higher_colors() +remove all the colors >= opt (optimal color number) +from the set of colors U +""" function remove_higher_colors(U, opt) u = zeros(Int32, 0) for color in U diff --git a/test/test_bsc.jl b/test/test_bsc.jl new file mode 100644 index 00000000..63eb7fbf --- /dev/null +++ b/test/test_bsc.jl @@ -0,0 +1,36 @@ +using LightGraphs + +g0 = SimpleGraph(6) +add_edge!(g0, 1,2) +add_edge!(g0, 1,4) +add_edge!(g0, 1,5) +add_edge!(g0, 3,2) +add_edge!(g0, 3,5) +add_edge!(g0, 3,6) +add_edge!(g0, 4,5) +add_edge!(g0, 5,6) +sv0 = sort_by_degree(g0) + +g1 = SimpleGraph(6) +add_edge!(g1, 2,1) +add_edge!(g1, 3,2) +add_edge!(g1, 4,2) +add_edge!(g1, 5,2) +add_edge!(g1, 6,2) +sv1 = sort_by_degree(g1) + +g2 = SimpleGraph(5) +add_edge!(g2, 1,2) +add_edge!(g2, 1,3) +add_edge!(g2, 1,4) +add_edge!(g2, 4,2) +add_edge!(g2, 5,2) +add_edge!(g2, 3,4) +add_edge!(g2, 4,5) +sv2 = sort_by_degree(g2) + +@testset "sort_by_degree(g)" begin + @test sv0 = [5,1,3,2,4,6] + @test sv1 = [2,1,3,4,5,6] + @test sv2 = [4,1,2,3,5] +end From ca3bb57ce259ec1fbb2246eddaeb35afd0405bbe Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Fri, 19 Jul 2019 17:13:11 +0530 Subject: [PATCH 05/16] remove comments? --- .../{bsc_algo.jl => backtracking_coloring.jl} | 70 ++++++++++++++++--- src/coloring/high_level.jl | 2 +- 2 files changed, 62 insertions(+), 10 deletions(-) rename src/coloring/{bsc_algo.jl => backtracking_coloring.jl} (65%) diff --git a/src/coloring/bsc_algo.jl b/src/coloring/backtracking_coloring.jl similarity index 65% rename from src/coloring/bsc_algo.jl rename to src/coloring/backtracking_coloring.jl index 06779226..17e1c5d6 100644 --- a/src/coloring/bsc_algo.jl +++ b/src/coloring/backtracking_coloring.jl @@ -1,12 +1,16 @@ -#bsc 1-based indexing using LightGraphs -function color_graph(g::LightGraphs.AbstractGraph) +""" + color_graph(g::LightGraphs, ::BacktrackingColor) - #number of vertices in g +Returns a tight, distance-1 coloring of graph g +using the minimum number of colors possible (the +chromatic number of g, χ(g)) +""" +function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) + println("function called") v = nv(g) - #A is order of vertices in non-increasing order of degree A = sort_by_degree(g) #F is the coloring of vertices, 0 means uncolored @@ -16,6 +20,7 @@ function color_graph(g::LightGraphs.AbstractGraph) #start index start = 1 + println("start = $start") #optimal color number opt = v + 1 @@ -30,68 +35,111 @@ function color_graph(g::LightGraphs.AbstractGraph) #set of free colors U = zeros(Int32, 0) push!(U, 1) + print("1. U = ") + println(U) #set of free colors of x freeColors = [Vector{Int64}() for _ in 1:v] freeColors[x] = copy(U) while (start >= 1) - + #println("Running while loop, start = $start") back = false for i = start:v + #println("running for loop, i = $i") if i > start + #println("i > start, entering if block (1)") x = uncolored_vertex_of_maximal_degree(A,F) + #println("x = $x") U = free_colors(x, A, colors, F, g, opt) + #print("U = ") + #println(U) sort!(U) + #println("sorted U = ") + #println(U) end if length(U) > 0 + #println("length of U > 0, entering if block (2)") k = U[1] + #println("k = $k") F[x] = k + #println("set F[$x] = $k") + cp = F[x] + #println("F[$x] = $cp") deleteat!(U, 1) + #print("U = ") + #println(U) freeColors[x] = copy(U) + #print("freeColors[$x] = ") + #println(U) if i==1 l = 0 + #println("l = 0") else l = colors[i-1] - colors[i] = max(k, l) + #println("l = $l") end + colors[i] = max(k, l) + #ck = colors[i] + #println("colors[$i] = $ck") else + #println("else condition, block (3)") start = i-1 + #println("start = $start") back = true + #println("back = true") break end end if back + #println("back is true, if block (4)") if start >= 1 + #println("start = $start >= 1, if block (5)") x = A[start] + #println("x = $x") F[x] = 0 + #println("F[$x] = 0") U = freeColors[x] + #print("U = ") + #println(U) end else + #println("else condition, back is not true block (6)") Fopt = copy(F) + #print("Fopt = ") + #println(Fopt) opt = colors[v-1] + #println("opt = $opt") i = least_index(F,A,opt) + #println("i = $i") start = i-1 + #println("start = $start") if start < 1 + #println("start < 1, so breaking") break end #uncolor all vertices A[i] with i >= start uncolor_all!(F, A, start) - + #println("uncoloring") #try start+1 instead for i = 1:start+1 x = A[i] U = freeColors[x] #remove colors >= opt from U + #print("U = ") + #println(U) + #println("removing from U all colors >= $opt") U = remove_higher_colors(U, opt) + #print("U = ") + #println(U) freeColors[x] = copy(U) end end end - F + Fopt end """ @@ -156,7 +204,7 @@ function free_colors(x, A, colors, F, g, opt) break end end - if c_allowed && c < ocn + if c_allowed && c < opt push!(freecolors, c) end end @@ -198,10 +246,14 @@ remove all the colors >= opt (optimal color number) from the set of colors U """ function remove_higher_colors(U, opt) + if length(U) == 0 + return U + end u = zeros(Int32, 0) for color in U if color < opt push!(u, color) end end + u end diff --git a/src/coloring/high_level.jl b/src/coloring/high_level.jl index ed6c4536..08978e03 100644 --- a/src/coloring/high_level.jl +++ b/src/coloring/high_level.jl @@ -1,6 +1,6 @@ abstract type ColoringAlgorithm end struct GreedyD1Color <: ColoringAlgorithm end -struct BSCColor <: ColoringAlgorithm end +struct BacktrackingColor <: ColoringAlgorithm end struct ContractionColor <: ColoringAlgorithm end struct GreedyStar1Color <: ColoringAlgorithm end struct GreedyStar2Color <: ColoringAlgorithm end From b3839702bbb9b273b92d159765b488d2bb497634 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Fri, 19 Jul 2019 17:31:19 +0530 Subject: [PATCH 06/16] final code for BSC algorithm --- src/coloring/backtracking_coloring.jl | 117 ++++++++++---------------- 1 file changed, 43 insertions(+), 74 deletions(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index 17e1c5d6..dd5cb4d7 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -4,13 +4,13 @@ using LightGraphs color_graph(g::LightGraphs, ::BacktrackingColor) Returns a tight, distance-1 coloring of graph g -using the minimum number of colors possible (the -chromatic number of g, χ(g)) +using the minimum number of colors possible (i.e. +the chromatic number of graph, χ(g)) """ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) - println("function called") v = nv(g) + #A is list of vertices in non-increasing order of degree A = sort_by_degree(g) #F is the coloring of vertices, 0 means uncolored @@ -18,9 +18,7 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) F = zeros(Int32, v) Fopt= zeros(Int32, v) - #start index start = 1 - println("start = $start") #optimal color number opt = v + 1 @@ -33,106 +31,65 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) colors = zeros(Int32, v) #set of free colors - U = zeros(Int32, 0) + U = zeros(Int, 0) push!(U, 1) - print("1. U = ") - println(U) #set of free colors of x freeColors = [Vector{Int64}() for _ in 1:v] freeColors[x] = copy(U) while (start >= 1) - #println("Running while loop, start = $start") + back = false for i = start:v - #println("running for loop, i = $i") if i > start - #println("i > start, entering if block (1)") x = uncolored_vertex_of_maximal_degree(A,F) - #println("x = $x") U = free_colors(x, A, colors, F, g, opt) - #print("U = ") - #println(U) sort!(U) - #println("sorted U = ") - #println(U) end if length(U) > 0 - #println("length of U > 0, entering if block (2)") k = U[1] - #println("k = $k") F[x] = k - #println("set F[$x] = $k") cp = F[x] - #println("F[$x] = $cp") deleteat!(U, 1) - #print("U = ") - #println(U) freeColors[x] = copy(U) - #print("freeColors[$x] = ") - #println(U) if i==1 l = 0 - #println("l = 0") else l = colors[i-1] - #println("l = $l") end colors[i] = max(k, l) - #ck = colors[i] - #println("colors[$i] = $ck") else - #println("else condition, block (3)") start = i-1 - #println("start = $start") back = true - #println("back = true") break end end + if back - #println("back is true, if block (4)") if start >= 1 - #println("start = $start >= 1, if block (5)") x = A[start] - #println("x = $x") F[x] = 0 - #println("F[$x] = 0") U = freeColors[x] - #print("U = ") - #println(U) end else - #println("else condition, back is not true block (6)") Fopt = copy(F) - #print("Fopt = ") - #println(Fopt) opt = colors[v-1] - #println("opt = $opt") i = least_index(F,A,opt) - #println("i = $i") start = i-1 - #println("start = $start") if start < 1 - #println("start < 1, so breaking") break end #uncolor all vertices A[i] with i >= start uncolor_all!(F, A, start) - #println("uncoloring") - #try start+1 instead + for i = 1:start+1 x = A[i] U = freeColors[x] + #remove colors >= opt from U - #print("U = ") - #println(U) - #println("removing from U all colors >= $opt") U = remove_higher_colors(U, opt) - #print("U = ") - #println(U) freeColors[x] = copy(U) end end @@ -140,13 +97,14 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) end Fopt + end """ - sort_by_degree() + sort_by_degree(g::LightGraphs.AbstractGraph) -sort and store the vertices of graph g in -non-increasing order of their degrees +Returns a list of the vertices of graph g sorted +in non-increasing order of their degrees """ function sort_by_degree(g::LightGraphs.AbstractGraph) vs = vertices(g) @@ -157,12 +115,12 @@ function sort_by_degree(g::LightGraphs.AbstractGraph) end """ - uncolored_vertex_of_maximal_degree(A,F) + uncolored_vertex_of_maximal_degree(A::Array{Int,1},F::Array{Int,1}) -Returns an uncolored vertex from the graph which has maximum -degree +Returns an uncolored vertex from the partially +colored graph which has the highest degree """ -function uncolored_vertex_of_maximal_degree(A,F) +function uncolored_vertex_of_maximal_degree(A::Array{Int,1},F::Array{Int,1}) for v in A if F[v] == 0 return v @@ -172,12 +130,22 @@ end """ - free_colors() - -returns set of free colors of x which are less + free_colors(x::Int, + A::Array{Int,1}, + colors::Array{Int,1}, + F::Array{Int64,1}, + g::LightGraphs.AbstractGraph, + opt::Int) + +Returns set of free colors of x which are less than optimal color number (opt) """ -function free_colors(x, A, colors, F, g, opt) +function free_colors(x::Int, + A::Array{Int,1}, + colors::Array{Int,1}, + F::Array{Int64,1}, + g::LightGraphs.AbstractGraph, + opt::Int) index = -1 freecolors = zeros(Int64, 0) @@ -214,12 +182,12 @@ function free_colors(x, A, colors, F, g, opt) end """ - least_index() + least_index(F::Array{Int,1}, A::Array{Int,1}, opt::Int) -returns least index i such that color of vertex -A[i] == opt (optimal color number) +Returns least index i such that color of vertex +A[i] is equal to `opt` (optimal color number) """ -function least_index(F,A,opt) +function least_index(F::Array{Int,1}, A::Array{Int,1}, opt::Int) for i in eachindex(A) if F[A[i]] == opt return i @@ -228,24 +196,25 @@ function least_index(F,A,opt) end """ - uncolor_all() + uncolor_all(F::Array{Int,1}, A::Array{Int,1}, start::Int) -uncolors all vertices A[i] where -i >= start +Uncolors all vertices A[i] where i is +greater than or equal to start """ -function uncolor_all!(F, A, start) +function uncolor_all!(F::Array{Int,1}, A::Array{Int,1}, start::Int) for i = start:length(A) F[A[i]] = 0 end end """ - remove_higher_colors() + remove_higher_colors(U::Array{Int,1}, opt::Int) -remove all the colors >= opt (optimal color number) -from the set of colors U +Remove all the colors which are greater than or +equal to the `opt` (optimal color number) from +the set of colors U """ -function remove_higher_colors(U, opt) +function remove_higher_colors(U::Array{Int,1}, opt::Int) if length(U) == 0 return U end From 1a7d8c3a5310d3898177b92b5b81d437fa1bc477 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Fri, 19 Jul 2019 18:36:15 +0530 Subject: [PATCH 07/16] broke integration test --- src/SparseDiffTools.jl | 1 + src/coloring/backtracking_coloring.jl | 22 ++++-- test/runtests.jl | 1 + test/test_bsc.jl | 109 ++++++++++++++++++++++++-- 4 files changed, 120 insertions(+), 13 deletions(-) diff --git a/src/SparseDiffTools.jl b/src/SparseDiffTools.jl index b2b55c0a..fa9862dc 100644 --- a/src/SparseDiffTools.jl +++ b/src/SparseDiffTools.jl @@ -34,6 +34,7 @@ export contract_color, include("coloring/high_level.jl") +include("coloring/backtracking_coloring.jl") include("coloring/contraction_coloring.jl") include("coloring/greedy_d1_coloring.jl") include("coloring/greedy_star1_coloring.jl") diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index dd5cb4d7..cee1bd18 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -15,8 +15,8 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) #F is the coloring of vertices, 0 means uncolored #Fopt is the optimal coloring of the graph - F = zeros(Int32, v) - Fopt= zeros(Int32, v) + F = zeros(Int, v) + Fopt= zeros(Int, v) start = 1 @@ -28,14 +28,14 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) #colors[j] = number of colors in A[0]...A[j] #assume colors[0] = 1 - colors = zeros(Int32, v) + colors = zeros(Int, v) #set of free colors U = zeros(Int, 0) push!(U, 1) #set of free colors of x - freeColors = [Vector{Int64}() for _ in 1:v] + freeColors = [Vector{Int}() for _ in 1:v] freeColors[x] = copy(U) while (start >= 1) @@ -139,6 +139,16 @@ end Returns set of free colors of x which are less than optimal color number (opt) + +Arguments: + +x: Vertex who's set of free colors is to be calculated +A: List of vertices of graph g sorted in non-increasing order of degree +colors: colors[i] stores the number of distinct colors used in the + coloring of vertices A[0], A[1]... A[i-1] +F: F[i] stores the color of vertex i +g: Graph to be colored +opt: Current optimal number of colors to be used in the coloring of graph g """ function free_colors(x::Int, A::Array{Int,1}, @@ -148,7 +158,7 @@ function free_colors(x::Int, opt::Int) index = -1 - freecolors = zeros(Int64, 0) + freecolors = zeros(Int, 0) for i in eachindex(A) if A[i] == x @@ -218,7 +228,7 @@ function remove_higher_colors(U::Array{Int,1}, opt::Int) if length(U) == 0 return U end - u = zeros(Int32, 0) + u = zeros(Int, 0) for color in U if color < opt push!(u, color) diff --git a/test/runtests.jl b/test/runtests.jl index d698e432..07342a16 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,6 +3,7 @@ using Test @testset "Exact coloring via contraction" begin include("test_contraction.jl") end +@testset "Exact coloring via backtracking" begin include("test_bsc.jl") end @testset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end @testset "Greedy star coloring" begin include("test_greedy_star.jl") end @testset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end diff --git a/test/test_bsc.jl b/test/test_bsc.jl index 63eb7fbf..08305398 100644 --- a/test/test_bsc.jl +++ b/test/test_bsc.jl @@ -1,4 +1,23 @@ +using SparseDiffTools using LightGraphs +using Random +#= + Graph g0 +vertex nuumber followed by color in parentheses + + 6(3) + / \ + / \ + / \ + 5(1)--------3(2) + | \ | + | \ | + | \ | + | 1(2)----2(1) + | / + | / + 4(3) +=# g0 = SimpleGraph(6) add_edge!(g0, 1,2) @@ -9,16 +28,38 @@ add_edge!(g0, 3,5) add_edge!(g0, 3,6) add_edge!(g0, 4,5) add_edge!(g0, 5,6) -sv0 = sort_by_degree(g0) + +#= + Graph g1 + + 1 + | + | + 5----2----3 + / \ + / \ + 6 4 + +=# g1 = SimpleGraph(6) add_edge!(g1, 2,1) add_edge!(g1, 3,2) add_edge!(g1, 4,2) add_edge!(g1, 5,2) add_edge!(g1, 6,2) -sv1 = sort_by_degree(g1) + +#= + Graph g2 + + 1------2 + / | / | + / | / | + / | / | + 3----4------5 + +=# g2 = SimpleGraph(5) add_edge!(g2, 1,2) add_edge!(g2, 1,3) @@ -27,10 +68,64 @@ add_edge!(g2, 4,2) add_edge!(g2, 5,2) add_edge!(g2, 3,4) add_edge!(g2, 4,5) -sv2 = sort_by_degree(g2) -@testset "sort_by_degree(g)" begin - @test sv0 = [5,1,3,2,4,6] - @test sv1 = [2,1,3,4,5,6] - @test sv2 = [4,1,2,3,5] +#test custom graphs first +coloring0 = SparseDiffTools.color_graph(g0, SparseDiffTools.BacktrackingColor()) +coloring1 = SparseDiffTools.color_graph(g1, SparseDiffTools.BacktrackingColor()) +coloring2 = SparseDiffTools.color_graph(g2, SparseDiffTools.BacktrackingColor()) + +for v = 1:nv(g0) + color = coloring0[v] + for j in inneighbors(g0, v) + if coloring0[j] == color + @test false + end + end +end + +for v = 1:nv(g1) + color = coloring1[v] + for j in inneighbors(g1, v) + if coloring1[j] == color + @test false + end + end +end + +for v = 1:nv(g2) + color = coloring2[v] + for j in inneighbors(g2, v) + if coloring2[j] == color + @test false + end + end +end + +test_graphs = Array{SimpleGraph, 1}(undef, 0) +for _ in 1:5 + nv = rand(5:20) + ne = rand(1:100) + graph = SimpleGraph(nv) + for e in 1:ne + v1 = rand(1:nv) + v2 = rand(1:nv) + while v1 == v2 + v2 = rand(1:nv) + end + add_edge!(graph, v1, v2) + end + push!(test_graphs, copy(graph)) +end + + +for i in 1:5 + g = test_graphs[i] + out_colors = SparseDiffTools.color_graph(g,SparseDiffTools.BacktrackingColor()) + + for v = vertices(g) + color = out_colors[v] + for j in inneighbors(g, v) + @test out_colors[j] != color + end + end end From 8564ce3391967f3940f1b845f3f32ee1e10a6214 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Fri, 19 Jul 2019 18:38:43 +0530 Subject: [PATCH 08/16] Update test/runtests.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Mathieu Besançon --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 07342a16..43bc3d1e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,5 +10,5 @@ using Test @testset "AD using color vector" begin include("test_ad.jl") end @testset "Integration test" begin include("test_integration.jl") end @testset "Special matrices" begin include("test_specialmatrices.jl") end -@testset "Jac Vecs and Hes Vecs" begin include("test_jaches_products.jl") end +@testset "Jacobian Vecs and Hessian Vecs" begin include("test_jaches_products.jl") end @testset "Program sparsity computation" begin include("program_sparsity/testall.jl") end From 47d869b29ed13ff4a5594bd409beac3b01607e45 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Mon, 22 Jul 2019 19:30:00 +0530 Subject: [PATCH 09/16] Update src/coloring/backtracking_coloring.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Mathieu Besançon --- src/coloring/backtracking_coloring.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index cee1bd18..8c0a3fdb 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -111,7 +111,7 @@ function sort_by_degree(g::LightGraphs.AbstractGraph) degrees = (LightGraphs.degree(g, v) for v in vs) vertex_pairs = collect(zip(vs, degrees)) sort!(vertex_pairs, by = p -> p[2], rev = true) - [v[1] for v in vertex_pairs] + return [v[1] for v in vertex_pairs] end """ From 04ccded2f215b19a90d4e77433ebbda74bca6cdf Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Mon, 22 Jul 2019 19:30:09 +0530 Subject: [PATCH 10/16] Update src/coloring/backtracking_coloring.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Mathieu Besançon --- src/coloring/backtracking_coloring.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index 8c0a3fdb..db104d3f 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -187,7 +187,7 @@ function free_colors(x::Int, end end - freecolors + return freecolors end From 29e060bc06ab8e1230e5c337123c08d943b4302d Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Mon, 22 Jul 2019 19:30:43 +0530 Subject: [PATCH 11/16] Update src/coloring/backtracking_coloring.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Mathieu Besançon --- src/coloring/backtracking_coloring.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index db104d3f..0ed7aed3 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -234,5 +234,5 @@ function remove_higher_colors(U::Array{Int,1}, opt::Int) push!(u, color) end end - u + return u end From d614ab29b0687de08d6d7a36a31c52198cb3841d Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Mon, 22 Jul 2019 19:42:55 +0530 Subject: [PATCH 12/16] general fixes --- src/coloring/backtracking_coloring.jl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index cee1bd18..47de6401 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -93,11 +93,8 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) freeColors[x] = copy(U) end end - end - - Fopt - + return Fopt end """ @@ -111,7 +108,7 @@ function sort_by_degree(g::LightGraphs.AbstractGraph) degrees = (LightGraphs.degree(g, v) for v in vs) vertex_pairs = collect(zip(vs, degrees)) sort!(vertex_pairs, by = p -> p[2], rev = true) - [v[1] for v in vertex_pairs] + return [v[1] for v in vertex_pairs] end """ @@ -187,7 +184,7 @@ function free_colors(x::Int, end end - freecolors + return freecolors end @@ -234,5 +231,5 @@ function remove_higher_colors(U::Array{Int,1}, opt::Int) push!(u, color) end end - u + return u end From d0af867662af27de4423f2d2a3d0fdfc664c8e0d Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Mon, 22 Jul 2019 19:44:14 +0530 Subject: [PATCH 13/16] general fixes --- src/coloring/backtracking_coloring.jl | 50 +++++++++++++-------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index 47de6401..fb45e5b6 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -15,8 +15,8 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) #F is the coloring of vertices, 0 means uncolored #Fopt is the optimal coloring of the graph - F = zeros(Int, v) - Fopt= zeros(Int, v) + F = zeros(Integer, v) + Fopt= zeros(Integer, v) start = 1 @@ -28,14 +28,14 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) #colors[j] = number of colors in A[0]...A[j] #assume colors[0] = 1 - colors = zeros(Int, v) + colors = zeros(Integer, v) #set of free colors - U = zeros(Int, 0) + U = zeros(Integer, 0) push!(U, 1) #set of free colors of x - freeColors = [Vector{Int}() for _ in 1:v] + freeColors = [Vector{Integer}() for _ in 1:v] freeColors[x] = copy(U) while (start >= 1) @@ -112,12 +112,12 @@ function sort_by_degree(g::LightGraphs.AbstractGraph) end """ - uncolored_vertex_of_maximal_degree(A::Array{Int,1},F::Array{Int,1}) + uncolored_vertex_of_maximal_degree(A::AbstractVector{<:Integer},F::AbstractVector{<:Integer}) Returns an uncolored vertex from the partially colored graph which has the highest degree """ -function uncolored_vertex_of_maximal_degree(A::Array{Int,1},F::Array{Int,1}) +function uncolored_vertex_of_maximal_degree(A::AbstractVector{<:Integer},F::AbstractVector{<:Integer}) for v in A if F[v] == 0 return v @@ -127,12 +127,12 @@ end """ - free_colors(x::Int, - A::Array{Int,1}, - colors::Array{Int,1}, - F::Array{Int64,1}, + free_colors(x::Integer, + A::AbstractVector{<:Integer}, + colors::AbstractVector{<:Integer}, + F::Array{Integer64,1}, g::LightGraphs.AbstractGraph, - opt::Int) + opt::Integer) Returns set of free colors of x which are less than optimal color number (opt) @@ -147,15 +147,15 @@ F: F[i] stores the color of vertex i g: Graph to be colored opt: Current optimal number of colors to be used in the coloring of graph g """ -function free_colors(x::Int, - A::Array{Int,1}, - colors::Array{Int,1}, - F::Array{Int64,1}, +function free_colors(x::Integer, + A::AbstractVector{<:Integer}, + colors::AbstractVector{<:Integer}, + F::Array{Integer64,1}, g::LightGraphs.AbstractGraph, - opt::Int) + opt::Integer) index = -1 - freecolors = zeros(Int, 0) + freecolors = zeros(Integer, 0) for i in eachindex(A) if A[i] == x @@ -189,12 +189,12 @@ function free_colors(x::Int, end """ - least_index(F::Array{Int,1}, A::Array{Int,1}, opt::Int) + least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer) Returns least index i such that color of vertex A[i] is equal to `opt` (optimal color number) """ -function least_index(F::Array{Int,1}, A::Array{Int,1}, opt::Int) +function least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer) for i in eachindex(A) if F[A[i]] == opt return i @@ -203,29 +203,29 @@ function least_index(F::Array{Int,1}, A::Array{Int,1}, opt::Int) end """ - uncolor_all(F::Array{Int,1}, A::Array{Int,1}, start::Int) + uncolor_all(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, start::Integer) Uncolors all vertices A[i] where i is greater than or equal to start """ -function uncolor_all!(F::Array{Int,1}, A::Array{Int,1}, start::Int) +function uncolor_all!(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, start::Integer) for i = start:length(A) F[A[i]] = 0 end end """ - remove_higher_colors(U::Array{Int,1}, opt::Int) + remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer) Remove all the colors which are greater than or equal to the `opt` (optimal color number) from the set of colors U """ -function remove_higher_colors(U::Array{Int,1}, opt::Int) +function remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer) if length(U) == 0 return U end - u = zeros(Int, 0) + u = zeros(Integer, 0) for color in U if color < opt push!(u, color) From 8bdab49f36b635d5dbc21b3f228e6f9785ca662b Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 23 Jul 2019 22:24:37 +0530 Subject: [PATCH 14/16] changed abstract types to concrete --- src/coloring/backtracking_coloring.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index fb45e5b6..cd0e5f92 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -15,8 +15,8 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) #F is the coloring of vertices, 0 means uncolored #Fopt is the optimal coloring of the graph - F = zeros(Integer, v) - Fopt= zeros(Integer, v) + F = zeros(Int, v) + Fopt= zeros(Int, v) start = 1 @@ -28,14 +28,14 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) #colors[j] = number of colors in A[0]...A[j] #assume colors[0] = 1 - colors = zeros(Integer, v) + colors = zeros(Int, v) #set of free colors - U = zeros(Integer, 0) + U = zeros(Int, 0) push!(U, 1) #set of free colors of x - freeColors = [Vector{Integer}() for _ in 1:v] + freeColors = [Vector{Int}() for _ in 1:v] freeColors[x] = copy(U) while (start >= 1) @@ -155,7 +155,7 @@ function free_colors(x::Integer, opt::Integer) index = -1 - freecolors = zeros(Integer, 0) + freecolors = zeros(Int, 0) for i in eachindex(A) if A[i] == x @@ -225,7 +225,7 @@ function remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer) if length(U) == 0 return U end - u = zeros(Integer, 0) + u = zeros(Int, 0) for color in U if color < opt push!(u, color) From 9d5da8fe17e0596c6df4daf588b8bb0bbdcff566 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 23 Jul 2019 22:44:48 +0530 Subject: [PATCH 15/16] fixed tests --- Project.toml | 1 + src/coloring/backtracking_coloring.jl | 4 ++-- test/runtests.jl | 17 ----------------- 3 files changed, 3 insertions(+), 19 deletions(-) diff --git a/Project.toml b/Project.toml index b9c7add4..a1d6b3f6 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Requires = "ae029012-a4dd-5104-9daa-d747884805df" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +SparsityDetection = "684fba80-ace3-11e9-3d08-3bc7ed6f96df" VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f" [compat] diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index cd0e5f92..4f4e8cb3 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -130,7 +130,7 @@ end free_colors(x::Integer, A::AbstractVector{<:Integer}, colors::AbstractVector{<:Integer}, - F::Array{Integer64,1}, + F::Array{Integer,1}, g::LightGraphs.AbstractGraph, opt::Integer) @@ -150,7 +150,7 @@ opt: Current optimal number of colors to be used in the coloring of graph g function free_colors(x::Integer, A::AbstractVector{<:Integer}, colors::AbstractVector{<:Integer}, - F::Array{Integer64,1}, + F::Array{Integer,1}, g::LightGraphs.AbstractGraph, opt::Integer) index = -1 diff --git a/test/runtests.jl b/test/runtests.jl index 9a4fd786..ee0a95c1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,19 +1,3 @@ -<<<<<<< HEAD -using SparseDiffTools -using Test - - -@testset "Exact coloring via contraction" begin include("test_contraction.jl") end -@testset "Exact coloring via backtracking" begin include("test_bsc.jl") end -@testset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end -@testset "Greedy star coloring" begin include("test_greedy_star.jl") end -@testset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end -@testset "AD using color vector" begin include("test_ad.jl") end -@testset "Integration test" begin include("test_integration.jl") end -@testset "Special matrices" begin include("test_specialmatrices.jl") end -@testset "Jacobian Vecs and Hessian Vecs" begin include("test_jaches_products.jl") end -@testset "Program sparsity computation" begin include("program_sparsity/testall.jl") end -======= using SafeTestsets @time @safetestset "Exact coloring via contraction" begin include("test_contraction.jl") end @@ -24,4 +8,3 @@ using SafeTestsets @time @safetestset "Integration test" begin include("test_integration.jl") end @time @safetestset "Special matrices" begin include("test_specialmatrices.jl") end @time @safetestset "Jac Vecs and Hes Vecs" begin include("test_jaches_products.jl") end ->>>>>>> 1b115b8d6dd386c46a0474c736e91aae710afd3d From cfd7490490df011c482282e5af60170041f0ca60 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Wed, 24 Jul 2019 18:37:04 +0530 Subject: [PATCH 16/16] removed redundant dependency --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index a1d6b3f6..b9c7add4 100644 --- a/Project.toml +++ b/Project.toml @@ -12,7 +12,6 @@ LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Requires = "ae029012-a4dd-5104-9daa-d747884805df" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" -SparsityDetection = "684fba80-ace3-11e9-3d08-3bc7ed6f96df" VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f" [compat]