Skip to content

Commit

Permalink
error for self loops with coloring. fixes #112 (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennedeg authored May 13, 2022
1 parent cc041b8 commit 5608e49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/traversals/greedy_color.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ Color graph `g` according to an order specified by `seq` using a greedy heuristi
"""
function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where {T <: Integer}
nvg::T = nv(g)
cols = Vector{T}(undef, nvg)
cols = Vector{T}(undef, nvg)
seen = zeros(Bool, nvg + 1)

has_self_loops(g) && throw(ArgumentError("graph must not have self loops"))

for v in seq
seen[v] = true
colors_used = zeros(Bool, nvg)

for w in neighbors(g, v)
if seen[w]
colors_used[cols[w]] = true
Expand All @@ -37,6 +37,8 @@ function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where {T <: Integer
break;
end
end

seen[v] = true
end

return Coloring{T}(maximum(cols), cols)
Expand All @@ -47,8 +49,8 @@ end
Color graph `g` iteratively in the descending order of the degree of the vertices.
"""
function degree_greedy_color(g::AbstractGraph{T}) where {T <: Integer}
seq = convert(Vector{T}, sortperm(degree(g), rev=true))
function degree_greedy_color(g::AbstractGraph{T}) where {T <: Integer}
seq = convert(Vector{T}, sortperm(degree(g), rev=true))
return perm_greedy_color(g, seq)
end

Expand All @@ -59,7 +61,7 @@ end
Color the graph `g` iteratively in a random order using a greedy heuristic
and choose the best coloring out of `reps` such random colorings.
"""
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T <: Integer}
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T <: Integer}

seq = shuffle(vertices(g))
best = perm_greedy_color(g, seq)
Expand All @@ -76,7 +78,7 @@ end
Color graph `g` based on [Greedy Coloring Heuristics](https://en.wikipedia.org/wiki/Greedy_coloring)
The heuristics can be described as choosing a permutation of the vertices and assigning the
The heuristics can be described as choosing a permutation of the vertices and assigning the
lowest color index available iteratively in that order.
If `sort_degree` is true then the permutation is chosen in reverse sorted order of the degree of the vertices.
Expand Down
12 changes: 8 additions & 4 deletions test/traversals/greedy_color.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testset "Greedy Coloring" begin

g3 = star_graph(10)

for g in testgraphs(g3)
Expand All @@ -8,15 +8,15 @@
@test C.num_colors == 2
end
end

g4 = path_graph(20)
g5 = complete_graph(20)

for graph in [g4, g5]
for g in testgraphs(graph)
for op_sort in (true, false)
C = @inferred(greedy_color(g, reps=5, sort_degree=op_sort))

@test C.num_colors <= maximum(degree(g))+1
correct = true
for e in edges(g)
Expand All @@ -26,5 +26,9 @@
end
end
end
end

# non-regression test for #112
g = Graph(1)
add_edge!(g, 1, 1)
@test_throws ArgumentError greedy_color(g)
end

0 comments on commit 5608e49

Please sign in to comment.