-
Notifications
You must be signed in to change notification settings - Fork 45
New coloring methods #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
79116af
[WIP] New coloring methods
pkj-m 64d894e
Update greedy_star1_coloring.jl
pkj-m f62c164
added function documentation
pkj-m 1c72526
Update greedy_star1_coloring.jl
pkj-m 4c5a8c9
Syntax errors
pkj-m 4ef4ff6
fixed logical bug
pkj-m 30323d0
Added greedy star 2 coloring
pkj-m 5c3c702
changed function name
pkj-m c32a1a4
modified distance-1 coloring
pkj-m be50d36
general fixes
pkj-m 85d7b8a
added tests
pkj-m 4a2d0ce
added tests for both functions
pkj-m 0d8904f
general fixes
pkj-m 495916b
added dispatch for color graph
pkj-m 9a2d26f
Merge branch 'master' of https://github.com/JuliaDiffEq/SparseDiffToo…
pkj-m 701e195
added graph from paper in tests
pkj-m 9550cfc
fixed tests
pkj-m e869fa3
added Random
pkj-m a088ea1
Merge branch 'master' into coloring
pkj-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
""" | ||
GreedyD1 Coloring | ||
greedy_d1_coloring | ||
|
||
Find a coloring of a given input graph such that | ||
no two vertices connected by an edge have the same | ||
color using greedy approach. The number of colors | ||
used may be equal or greater than the chromatic | ||
number χ(G) of the graph. | ||
""" | ||
function color_graph(G::VSafeGraph,alg::GreedyD1Color) | ||
V = nv(G) | ||
result = zeros(Int64, V) | ||
function color_graph(g::VSafeGraph, alg::GreedyD1Color) | ||
v = nv(g) | ||
result = zeros(Int64, v) | ||
result[1] = 1 | ||
available = zeros(Int64, V) | ||
for i = 2:V | ||
for j in inneighbors(G, i) | ||
available = BitArray(undef, v) | ||
for i = 2:v | ||
for j in inneighbors(g, i) | ||
if result[j] != 0 | ||
available[result[j]] = 1 | ||
available[result[j]] = true | ||
end | ||
end | ||
for cr = 1:V | ||
if available[cr] == 0 | ||
for cr = 1:v | ||
if available[cr] == false | ||
result[i] = cr | ||
break | ||
end | ||
end | ||
available = zeros(Int64, V) | ||
fill!(available, false) | ||
end | ||
return result | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
greedy_star1_coloring | ||
|
||
Find a coloring of a given input graph such that | ||
no two vertices connected by an edge have the same | ||
color using greedy approach. The number of colors | ||
used may be equal or greater than the chromatic | ||
number `χ(G)` of the graph. | ||
|
||
A star coloring is a special type of distance - 1 coloring, | ||
For a coloring to be called a star coloring, it must satisfy | ||
two conditions: | ||
|
||
1. every pair of adjacent vertices receives distinct colors | ||
(a distance-1 coloring) | ||
|
||
2. For any vertex v, any color that leads to a two-colored path | ||
involving v and three other vertices is impermissible for v. | ||
In other words, every path on four vertices uses at least three | ||
colors. | ||
|
||
Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705. | ||
""" | ||
function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color) | ||
v = nv(g) | ||
color = zeros(Int64, v) | ||
|
||
forbidden_colors = zeros(Int64, v+1) | ||
|
||
for vertex_i = vertices(g) | ||
|
||
for w in inneighbors(g, vertex_i) | ||
if color[w] != 0 | ||
forbidden_colors[color[w]] = vertex_i | ||
end | ||
|
||
for x in inneighbors(g, w) | ||
if color[x] != 0 | ||
if color[w] == 0 | ||
forbidden_colors[color[x]] = vertex_i | ||
else | ||
for y in inneighbors(g, x) | ||
if color[y] != 0 | ||
if y != w && color[y] == color[w] | ||
forbidden_colors[color[x]] = vertex_i | ||
break | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
color[vertex_i] = find_min_color(forbidden_colors, vertex_i) | ||
end | ||
|
||
color | ||
end | ||
|
||
function find_min_color(forbidden_colors::AbstractVector, vertex_i::Integer) | ||
c = 1 | ||
while (forbidden_colors[c] == vertex_i) | ||
c+=1 | ||
end | ||
c | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
greedy_star2_coloring | ||
|
||
Find a coloring of a given input graph such that | ||
no two vertices connected by an edge have the same | ||
color using greedy approach. The number of colors | ||
used may be equal or greater than the chromatic | ||
number `χ(G)` of the graph. | ||
|
||
A star coloring is a special type of distance - 1 coloring, | ||
For a coloring to be called a star coloring, it must satisfy | ||
two conditions: | ||
|
||
1. every pair of adjacent vertices receives distinct colors | ||
(a distance-1 coloring) | ||
|
||
2. For any vertex v, any color that leads to a two-colored path | ||
involving v and three other vertices is impermissible for v. | ||
In other words, every path on four vertices uses at least three | ||
colors. | ||
|
||
Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705. | ||
|
||
TODO: add text explaining the difference between star1 and | ||
star2 | ||
""" | ||
function color_graph(g::LightGraphs.AbstractGraph, :: GreedyStar2Color) | ||
v = nv(g) | ||
color = zeros(Int64, v) | ||
|
||
forbidden_colors = zeros(Int64, v+1) | ||
|
||
for vertex_i = vertices(g) | ||
|
||
for w in inneighbors(g, vertex_i) | ||
if color[w] != 0 | ||
forbidden_colors[color[w]] = vertex_i | ||
end | ||
|
||
for x in inneighbors(g, w) | ||
if color[x] != 0 | ||
if color[w] == 0 | ||
forbidden_colors[color[x]] = vertex_i | ||
else | ||
if color[x] < color[w] | ||
forbidden_colors[color[x]] = vertex_i | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
color[vertex_i] = find_min_color(forbidden_colors, vertex_i) | ||
end | ||
|
||
color | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using SparseDiffTools | ||
using LightGraphs | ||
using Random | ||
|
||
Random.seed!(123) | ||
|
||
#= Test data =# | ||
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 | ||
|
||
#= | ||
Coloring needs to satisfy two conditions: | ||
|
||
1. every pair of adjacent vertices receives distinct colors | ||
(a distance-1 coloring) | ||
|
||
2. For any vertex v, any color that leads to a two-colored path | ||
involving v and three other vertices is impermissible for v. | ||
In other words, every path on four vertices uses at least three | ||
colors. | ||
=# | ||
|
||
|
||
#Sample graph from Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** | ||
|
||
#= | ||
(2) | ||
/ \ | ||
/ \ | ||
(1)----(3)----(4) | ||
|
||
=# | ||
|
||
gx = SimpleGraph(4) | ||
|
||
add_edge!(gx,1,2) | ||
add_edge!(gx,1,3) | ||
add_edge!(gx,2,3) | ||
add_edge!(gx,3,4) | ||
|
||
push!(test_graphs, gx) | ||
|
||
#begin testing | ||
for i in 1:6 | ||
g = test_graphs[i] | ||
|
||
out_colors1 = SparseDiffTools.color_graph(g,SparseDiffTools.GreedyStar1Color()) | ||
out_colors2 = SparseDiffTools.color_graph(g,SparseDiffTools.GreedyStar2Color()) | ||
|
||
#test condition 1 | ||
for v = vertices(g) | ||
color = out_colors1[v] | ||
for j in inneighbors(g, v) | ||
@test out_colors1[j] != color | ||
end | ||
end | ||
|
||
#test condition 2 | ||
for j = vertices(g) | ||
walk = LightGraphs.saw(g, j, 4) | ||
walk_colors = zeros(Int64, 0) | ||
if length(walk) >= 4 | ||
for t in walk | ||
push!(walk_colors, out_colors1[t]) | ||
end | ||
@test length(unique(walk_colors)) >= 3 | ||
end | ||
end | ||
|
||
#test condition 1 | ||
for v = vertices(g) | ||
color = out_colors2[v] | ||
for j in inneighbors(g, v) | ||
@test out_colors2[j] != color | ||
end | ||
end | ||
|
||
#test condition 2 | ||
for j = vertices(g) | ||
walk = LightGraphs.saw(g, j, 4) | ||
walk_colors = zeros(Int64, 0) | ||
if length(walk) >= 4 | ||
for t in walk | ||
push!(walk_colors, out_colors2[t]) | ||
end | ||
@test length(unique(walk_colors)) >= 3 | ||
end | ||
end | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.