Skip to content

Commit f308313

Browse files
matbesanconsbromberger
authored andcommitted
Taking coloring seriously (#1213)
* coloring fix * style consistency, doc verbs * parallel part
1 parent f08851c commit f308313

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

docs/make.jl

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ makedocs(
2222
"Operators" => "operators.md",
2323
"Plotting Graphs" => "plotting.md",
2424
"Path and Traversal" => "pathing.md",
25+
"Coloring" => "coloring.md",
2526
"Distance" => "distance.md",
2627
"Centrality Measures" => "centrality.md",
2728
"Linear Algebra" => "linalg.md",

docs/src/coloring.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Coloring
2+
3+
*LightGraphs.jl* defines a structure and basic interface for coloring algorithms.
4+
Since coloring is a hard problem in the general case, users can extend the behavior
5+
and define their own function taking a graph as input and returning the `Coloring` structure.
6+
7+
```@autodocs
8+
Modules = [LightGraphs]
9+
Pages = [
10+
"traversals/greedy_color.jl",
11+
]
12+
```
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where T <: Integer
1+
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T <: Integer}
22
best = @distributed (LightGraphs.best_color) for i in 1:reps
33
seq = shuffle(vertices(g))
44
LightGraphs.perm_greedy_color(g, seq)
55
end
66

7-
return convert(LightGraphs.coloring{T}, best)
7+
return convert(LightGraphs.Coloring{T}, best)
88
end
99

1010
greedy_color(g::AbstractGraph{U}; sort_degree::Bool=false, reps::Integer=1) where {U <: Integer} =
11-
sort_degree ? LightGraphs.degree_greedy_color(g) : Parallel.random_greedy_color(g, reps)
11+
sort_degree ? LightGraphs.degree_greedy_color(g) : Parallel.random_greedy_color(g, reps)

src/traversals/greedy_color.jl

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
"""
2-
struct coloring{T}
2+
struct Coloring{T}
33
4-
Store number of colors used and mapping from vertex to color
4+
Store the number of colors used and mapping from vertex to color
55
"""
6-
struct coloring{T <: Integer} <: Any
6+
struct Coloring{T <: Integer}
77
num_colors::T
88
colors::Vector{T}
99
end
1010

11-
best_color(c1::coloring, c2::coloring) = c1.num_colors < c2.num_colors ? c1 : c2
11+
best_color(c1::Coloring, c2::Coloring) = c1.num_colors < c2.num_colors ? c1 : c2
1212

1313
"""
1414
perm_greedy_color(g, seq)
1515
1616
Color graph `g` according to an order specified by `seq` using a greedy heuristic.
17-
seq[i] = v imples that vertex v is the i<sup>th</sup> vertex to be colored.
17+
`seq[i] = v` implies that vertex v is the ``i^{th}`` vertex to be colored.
1818
"""
19-
function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where T <: Integer
19+
function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where {T <: Integer}
2020
nvg::T = nv(g)
2121
cols = Vector{T}(undef, nvg)
2222
seen = zeros(Bool, nvg + 1)
@@ -39,28 +39,27 @@ function perm_greedy_color(g::AbstractGraph, seq::Vector{T}) where T <: Integer
3939
end
4040
end
4141

42-
return coloring{T}(maximum(cols), cols)
42+
return Coloring{T}(maximum(cols), cols)
4343
end
4444

4545
"""
4646
degree_greedy_color(g)
4747
4848
Color graph `g` iteratively in the descending order of the degree of the vertices.
4949
"""
50-
function degree_greedy_color(g::AbstractGraph{T}) where T <: Integer
50+
function degree_greedy_color(g::AbstractGraph{T}) where {T <: Integer}
5151
seq = convert(Vector{T}, sortperm(degree(g), rev=true))
5252
return perm_greedy_color(g, seq)
5353
end
5454

5555

5656
"""
57-
random_greedy_color(g, reps=1)
57+
random_greedy_color(g, reps)
5858
59-
Color graph `g` iteratively in a random order using a greedy heruistic
60-
and choose the best coloring out of `reps` such random coloring.
59+
Color the graph `g` iteratively in a random order using a greedy heuristic
60+
and choose the best coloring out of `reps` such random colorings.
6161
"""
62-
function random_greedy_color(g::AbstractGraph{T},
63-
reps::Integer) where T <: Integer
62+
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T <: Integer}
6463

6564
seq = shuffle(vertices(g))
6665
best = perm_greedy_color(g, seq)
@@ -87,5 +86,4 @@ If `sort_degree` is false then `reps` colorings are obtained based on random per
8786
colors is chosen.
8887
"""
8988
greedy_color(g::AbstractGraph{U}; sort_degree::Bool=false, reps::Integer=1) where {U <: Integer} =
90-
sort_degree ? degree_greedy_color(g) : random_greedy_color(g, reps)
91-
89+
sort_degree ? degree_greedy_color(g) : random_greedy_color(g, reps)

0 commit comments

Comments
 (0)