Skip to content

Commit 5608e49

Browse files
authored
error for self loops with coloring. fixes #112 (#113)
1 parent cc041b8 commit 5608e49

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/traversals/greedy_color.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ Color graph `g` according to an order specified by `seq` using a greedy heuristi
1818
"""
1919
function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where {T <: Integer}
2020
nvg::T = nv(g)
21-
cols = Vector{T}(undef, nvg)
21+
cols = Vector{T}(undef, nvg)
2222
seen = zeros(Bool, nvg + 1)
2323

24+
has_self_loops(g) && throw(ArgumentError("graph must not have self loops"))
25+
2426
for v in seq
25-
seen[v] = true
2627
colors_used = zeros(Bool, nvg)
27-
2828
for w in neighbors(g, v)
2929
if seen[w]
3030
colors_used[cols[w]] = true
@@ -37,6 +37,8 @@ function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where {T <: Integer
3737
break;
3838
end
3939
end
40+
41+
seen[v] = true
4042
end
4143

4244
return Coloring{T}(maximum(cols), cols)
@@ -47,8 +49,8 @@ end
4749
4850
Color graph `g` iteratively in the descending order of the degree of the vertices.
4951
"""
50-
function degree_greedy_color(g::AbstractGraph{T}) where {T <: Integer}
51-
seq = convert(Vector{T}, sortperm(degree(g), rev=true))
52+
function degree_greedy_color(g::AbstractGraph{T}) where {T <: Integer}
53+
seq = convert(Vector{T}, sortperm(degree(g), rev=true))
5254
return perm_greedy_color(g, seq)
5355
end
5456

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

6466
seq = shuffle(vertices(g))
6567
best = perm_greedy_color(g, seq)
@@ -76,7 +78,7 @@ end
7678
7779
Color graph `g` based on [Greedy Coloring Heuristics](https://en.wikipedia.org/wiki/Greedy_coloring)
7880
79-
The heuristics can be described as choosing a permutation of the vertices and assigning the
81+
The heuristics can be described as choosing a permutation of the vertices and assigning the
8082
lowest color index available iteratively in that order.
8183
8284
If `sort_degree` is true then the permutation is chosen in reverse sorted order of the degree of the vertices.

test/traversals/greedy_color.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@testset "Greedy Coloring" begin
2-
2+
33
g3 = star_graph(10)
44

55
for g in testgraphs(g3)
@@ -8,15 +8,15 @@
88
@test C.num_colors == 2
99
end
1010
end
11-
11+
1212
g4 = path_graph(20)
1313
g5 = complete_graph(20)
1414

1515
for graph in [g4, g5]
1616
for g in testgraphs(graph)
1717
for op_sort in (true, false)
1818
C = @inferred(greedy_color(g, reps=5, sort_degree=op_sort))
19-
19+
2020
@test C.num_colors <= maximum(degree(g))+1
2121
correct = true
2222
for e in edges(g)
@@ -26,5 +26,9 @@
2626
end
2727
end
2828
end
29-
end
3029

30+
# non-regression test for #112
31+
g = Graph(1)
32+
add_edge!(g, 1, 1)
33+
@test_throws ArgumentError greedy_color(g)
34+
end

0 commit comments

Comments
 (0)