Skip to content

Commit 5bf3b46

Browse files
Merge branch 'master' into min-travis-bump
2 parents 312c45f + a3a812c commit 5bf3b46

11 files changed

+100
-108
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 1.1
87
- 1.3
98
- nightly
109
matrix:

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
1010
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1111
DiffEqDiffTools = "01453d9d-ee7c-5054-8395-0335cb756afa"
1212
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
13+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1314
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
1415
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1516
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

src/coloring/acyclic_coloring.jl

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
4141

4242
color[v] = min_index(forbidden_colors, v)
4343

44-
#grow star for every edge connecting colored vertices v and w
44+
# grow star for every edge connecting colored vertices v and w
4545
for w in outneighbors(g, v)
4646
if color[w] != 0
4747
grow_star!(set, v, w, g, first_neighbor, color)
4848
end
4949
end
5050

51-
#merge the newly formed stars into existing trees if possible
51+
# merge the newly formed stars into existing trees if possible
5252
for w in outneighbors(g, v)
5353
if color[w] != 0
5454
for x in outneighbors(g, w)
@@ -66,14 +66,14 @@ function color_graph(g::LightGraphs.AbstractGraph, ::AcyclicColoring)
6666
end
6767

6868
"""
69-
prevent_cycle(v::Integer,
70-
w::Integer,
71-
x::Integer,
72-
g::LightGraphs.AbstractGraph,
73-
color::AbstractVector{<:Integer},
74-
forbidden_colors::AbstractVector{<:Integer},
75-
first_visit_to_tree::Array{Tuple{Integer, Integer}, 1},
76-
set::DisjointSets{LightGraphs.Edge})
69+
prevent_cycle(v::Integer,
70+
w::Integer,
71+
x::Integer,
72+
g::LightGraphs.AbstractGraph,
73+
color::AbstractVector{<:Integer},
74+
forbidden_colors::AbstractVector{<:Integer},
75+
first_visit_to_tree::Array{Tuple{Integer, Integer}, 1},
76+
set::DisjointSets{LightGraphs.Edge})
7777
7878
Subroutine to avoid generation of 2-colored cycle due to coloring of vertex v,
7979
which is adjacent to vertices w and x in graph g. Disjoint set is used to store
@@ -85,7 +85,7 @@ function prevent_cycle(v::Integer,
8585
g::LightGraphs.AbstractGraph,
8686
color::AbstractVector{<:Integer},
8787
forbidden_colors::AbstractVector{<:Integer},
88-
first_visit_to_tree::AbstractVector{<: Tuple{Integer, Integer}},
88+
first_visit_to_tree::AbstractVector{<:Tuple{Integer, Integer}},
8989
set::DisjointSets{LightGraphs.Edge})
9090

9191
edge = find_edge(g, w, x)
@@ -108,13 +108,14 @@ function min_index(forbidden_colors::AbstractVector{<:Integer}, v::Integer)
108108
end
109109

110110
"""
111-
grow_star!(set::DisjointSets{LightGraphs.Edge},
111+
grow_star!(set::DisjointSets{LightGraphs.Edge},
112112
v::Integer,
113113
w::Integer,
114-
g::LightGraphs.AbstractGraph
115-
first_neighbor::Array{Tuple{Integer, Integer}, 1})
114+
g::LightGraphs.AbstractGraph,
115+
first_neighbor::AbstractVector{<:Tuple{Integer, Integer}},
116+
color::AbstractVector{<: Integer})
116117
117-
Subroutine to grow a 2-colored star after assigning a new color to the
118+
Grow a 2-colored star after assigning a new color to the
118119
previously uncolored vertex v, by comparing it with the adjacent vertex w.
119120
Disjoint set is used to store stars in sets, which are identified through key
120121
edges present in g.
@@ -123,7 +124,7 @@ function grow_star!(set::DisjointSets{LightGraphs.Edge},
123124
v::Integer,
124125
w::Integer,
125126
g::LightGraphs.AbstractGraph,
126-
first_neighbor::AbstractArray{<: Tuple{Integer, Integer}, 1},
127+
first_neighbor::AbstractVector{<:Tuple{Integer, Integer}},
127128
color::AbstractVector{<: Integer})
128129
edge = find_edge(g, v, w)
129130
push!(set, edge)

src/coloring/backtracking_coloring.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
color_graph(g::LightGraphs, ::BacktrackingColor)
2+
color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor)
33
4-
Returns a tight, distance-1 coloring of graph g
4+
Return a tight, distance-1 coloring of graph g
55
using the minimum number of colors possible (i.e.
6-
the chromatic number of graph, χ(g))
6+
the chromatic number of graph, `χ(g)`)
77
"""
88
function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor)
99
v = nv(g)
@@ -181,9 +181,7 @@ function free_colors(x::Integer,
181181
push!(freecolors, c)
182182
end
183183
end
184-
185184
return freecolors
186-
187185
end
188186

189187
"""

src/coloring/contraction_coloring.jl

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
ColorContraction
2+
color_graph(G::VSafeGraph,::ContractionColor)
33
44
Find a coloring of the graph g such that no two vertices connected
55
by an edge have the same color.
@@ -37,7 +37,6 @@ function color_graph(G::VSafeGraph,::ContractionColor)
3737
V = nv(G)
3838
end
3939
return colors
40-
4140
end
4241

4342

@@ -47,8 +46,7 @@ end
4746
Find the vertex in the group nn of vertices belonging to the
4847
graph G which has the highest degree.
4948
"""
50-
function max_degree_vertex(G::VSafeGraph,nn::Array{Int,1})
51-
49+
function max_degree_vertex(G::VSafeGraph, nn::Vector{Int})
5250
max_degree = -1
5351
max_degree_vertex = -1
5452
for v in nn
@@ -59,7 +57,6 @@ function max_degree_vertex(G::VSafeGraph,nn::Array{Int,1})
5957
end
6058
end
6159
return max_degree_vertex
62-
6360
end
6461

6562

@@ -80,17 +77,16 @@ function max_degree_vertex(G::VSafeGraph)
8077
end
8178
end
8279
return max_degree_vertex
83-
8480
end
8581

8682

8783
"""
88-
non_neighbors(G,x)
84+
non_neighbors(G, x)
8985
9086
Find the set of vertices belonging to the graph G which do
9187
not share an edge with the vertex x.
9288
"""
93-
function non_neighbors(G::VSafeGraph, x::Int)
89+
function non_neighbors(G::VSafeGraph, x::Integer)
9490

9591
nn = zeros(Int, 0)
9692
for v in vertices(G)
@@ -102,20 +98,18 @@ function non_neighbors(G::VSafeGraph, x::Int)
10298
end
10399
end
104100
return nn
105-
106101
end
107102

108103

109104
"""
110-
length_common_neighbor(G,z,x)
105+
length_common_neighbor(g, z, x)
111106
112107
Find the number of vertices that share an edge with both the
113-
vertices z and x belonging to the graph G.
108+
vertices z and x belonging to the graph g.
114109
"""
115-
function length_common_neighbor(G::VSafeGraph,z::Int, x::Int)
116-
117-
z_neighbors = inneighbors(G,z)
118-
x_neighbors = inneighbors(G,x)
110+
function length_common_neighbor(g::VSafeGraph, z::Int, x::Int)
111+
z_neighbors = inneighbors(g, z)
112+
x_neighbors = inneighbors(g, x)
119113
common_vertices = indexin(z_neighbors, x_neighbors)
120114
num_common_vertices = 0
121115
for i in common_vertices
@@ -124,36 +118,28 @@ function length_common_neighbor(G::VSafeGraph,z::Int, x::Int)
124118
end
125119
end
126120
return num_common_vertices
127-
128121
end
129122

130123

131124
"""
132-
vertex_degree(G,z)
125+
vertex_degree(g, z)
133126
134-
Find the degree of the vertex z which belongs to the graph G.
127+
Find the degree of the vertex z which belongs to the graph g.
135128
"""
136-
function vertex_degree(G::VSafeGraph,z::Int)
137-
138-
return length(inneighbors(G,z))
139-
140-
end
141-
129+
vertex_degree(G::VSafeGraph,z::Int) = length(inneighbors(G,z))
142130

143131
"""
144-
contract!(G,y,x)
132+
contract!(g, y, x)
145133
146134
Contract the vertex y to x, both of which belong to graph G, that is
147135
delete vertex y and join x with the neighbors of y if they are not
148136
already connected with an edge.
149137
"""
150-
function contract!(G::VSafeGraph,y::Int, x::Int)
151-
152-
for v in inneighbors(G,y)
153-
if has_edge(G,v,x) == false
154-
add_edge!(G,v,x)
138+
function contract!(g::VSafeGraph, y::Int, x::Int)
139+
for v in inneighbors(g, y)
140+
if !has_edge(g, v, x)
141+
add_edge!(g, v, x)
155142
end
156143
end
157-
rem_vertex!(G,y)
158-
144+
rem_vertex!(g, y)
159145
end

src/coloring/greedy_d1_coloring.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
"""
2-
greedy_d1_coloring
2+
color_graph(g::VSafeGraph, alg::GreedyD1Color)
33
44
Find a coloring of a given input graph such that
55
no two vertices connected by an edge have the same
66
color using greedy approach. The number of colors
77
used may be equal or greater than the chromatic
8-
number χ(G) of the graph.
8+
number `χ(G)` of the graph.
99
"""
1010
function color_graph(g::VSafeGraph, alg::GreedyD1Color)
1111
v = nv(g)
1212
result = zeros(Int, v)
1313
result[1] = 1
14-
available = BitArray(undef, v)
14+
available = BitVector(undef, v)
1515
for i = 2:v
1616
for j in inneighbors(g, i)
1717
if result[j] != 0
1818
available[result[j]] = true
1919
end
2020
end
2121
for cr = 1:v
22-
if available[cr] == false
22+
if !available[cr]
2323
result[i] = cr
2424
break
2525
end

src/coloring/greedy_star1_coloring.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
"""
22
greedy_star1_coloring
33
4-
Find a coloring of a given input graph such that
5-
no two vertices connected by an edge have the same
6-
color using greedy approach. The number of colors
7-
used may be equal or greater than the chromatic
8-
number `χ(G)` of the graph.
4+
Find a coloring of a given input graph such that
5+
no two vertices connected by an edge have the same
6+
color using greedy approach. The number of colors
7+
used may be equal or greater than the chromatic
8+
number `χ(G)` of the graph.
99
10-
A star coloring is a special type of distance - 1 coloring,
11-
For a coloring to be called a star coloring, it must satisfy
12-
two conditions:
10+
A star coloring is a special type of distance - 1 coloring,
11+
For a coloring to be called a star coloring, it must satisfy
12+
two conditions:
1313
14-
1. every pair of adjacent vertices receives distinct colors
15-
(a distance-1 coloring)
14+
1. every pair of adjacent vertices receives distinct colors
15+
(a distance-1 coloring)
1616
17-
2. For any vertex v, any color that leads to a two-colored path
18-
involving v and three other vertices is impermissible for v.
19-
In other words, every path on four vertices uses at least three
20-
colors.
17+
2. For any vertex v, any color that leads to a two-colored path
18+
involving v and three other vertices is impermissible for v.
19+
In other words, every path on four vertices uses at least three
20+
colors.
2121
22-
Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705.
22+
Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705.
2323
"""
2424
function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color)
2525
v = nv(g)

src/coloring/greedy_star2_coloring.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
"""
22
greedy_star2_coloring
33
4-
Find a coloring of a given input graph such that
5-
no two vertices connected by an edge have the same
6-
color using greedy approach. The number of colors
7-
used may be equal or greater than the chromatic
8-
number `χ(G)` of the graph.
4+
Find a coloring of a given input graph such that
5+
no two vertices connected by an edge have the same
6+
color using greedy approach. The number of colors
7+
used may be equal or greater than the chromatic
8+
number `χ(G)` of the graph.
99
10-
A star coloring is a special type of distance - 1 coloring,
11-
For a coloring to be called a star coloring, it must satisfy
12-
two conditions:
10+
A star coloring is a special type of distance - 1 coloring,
11+
For a coloring to be called a star coloring, it must satisfy
12+
two conditions:
1313
14-
1. every pair of adjacent vertices receives distinct colors
15-
(a distance-1 coloring)
14+
1. every pair of adjacent vertices receives distinct colors
15+
(a distance-1 coloring)
1616
17-
2. For any vertex v, any color that leads to a two-colored path
18-
involving v and three other vertices is impermissible for v.
19-
In other words, every path on four vertices uses at least three
20-
colors.
17+
2. For any vertex v, any color that leads to a two-colored path
18+
involving v and three other vertices is impermissible for v.
19+
In other words, every path on four vertices uses at least three
20+
colors.
2121
22-
Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705.
22+
Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705.
2323
24-
TODO: add text explaining the difference between star1 and
25-
star2
24+
TODO: add text explaining the difference between star1 and star2
2625
"""
27-
function color_graph(g::LightGraphs.AbstractGraph, :: GreedyStar2Color)
26+
function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar2Color)
2827
v = nv(g)
2928
colorvec = zeros(Int, v)
3029

3130
forbidden_colors = zeros(Int, v+1)
3231

3332
for vertex_i = vertices(g)
34-
3533
for w in inneighbors(g, vertex_i)
3634
if colorvec[w] != 0
3735
forbidden_colors[colorvec[w]] = vertex_i
@@ -49,9 +47,7 @@ function color_graph(g::LightGraphs.AbstractGraph, :: GreedyStar2Color)
4947
end
5048
end
5149
end
52-
5350
colorvec[vertex_i] = find_min_color(forbidden_colors, vertex_i)
5451
end
55-
56-
colorvec
52+
return colorvec
5753
end

src/coloring/high_level.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ struct GreedyStar2Color <: SparseDiffToolsColoringAlgorithm end
77
struct AcyclicColoring <: SparseDiffToolsColoringAlgorithm end
88

99
"""
10-
matrix_colors(A,alg::ColoringAlgorithm = GreedyD1Color())
11-
12-
Returns the colorvec vector for the matrix A using the chosen coloring
13-
algorithm. If a known analytical solution exists, that is used instead.
14-
The coloring defaults to a greedy distance-1 coloring.
10+
matrix_colors(A, alg::ColoringAlgorithm = GreedyD1Color())
1511
12+
Return the colorvec vector for the matrix A using the chosen coloring
13+
algorithm. If a known analytical solution exists, that is used instead.
14+
The coloring defaults to a greedy distance-1 coloring.
1615
"""
1716
function ArrayInterface.matrix_colors(A::AbstractMatrix, alg::SparseDiffToolsColoringAlgorithm = GreedyD1Color(); partition_by_rows::Bool = false)
1817
_A = A isa SparseMatrixCSC ? A : sparse(A) # Avoid the copy

0 commit comments

Comments
 (0)