From 79116af4328fdfc5590cfaac9f830eefb3ce34ea Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 04:32:43 +0530 Subject: [PATCH 01/17] [WIP] New coloring methods Implemented greedy star-1 coloring. --- src/coloring/greedy_d1_coloring.jl | 2 +- src/coloring/greedy_star1_coloring.jl | 57 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/coloring/greedy_star1_coloring.jl diff --git a/src/coloring/greedy_d1_coloring.jl b/src/coloring/greedy_d1_coloring.jl index d85acbfa..fa9f61c8 100644 --- a/src/coloring/greedy_d1_coloring.jl +++ b/src/coloring/greedy_d1_coloring.jl @@ -7,7 +7,7 @@ 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) +function color_graph(G::VSafeGraph, alg::GreedyD1Color) V = nv(G) result = zeros(Int64, V) result[1] = 1 diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl new file mode 100644 index 00000000..8a7fccfb --- /dev/null +++ b/src/coloring/greedy_star1_coloring.jl @@ -0,0 +1,57 @@ +""" + + 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. +""" +function greedy_star1_coloring(G::VertexSafeGraph, alg::GreedyStar1Coloring) + V = nv(G), color = zeros(Int64, V) + forbiddenColors = zeros(Int64, V+1) + + for vertex_i = 1:V + for w in outneigbors(vertex_i) + + if color[w] != 0 + forbiddenColors[color[w]] = vertex_i + end + + for x in outneighbors(w) + if color[x] != 0 + if color[w] != 0 + forbiddenColors[color[x]] = vertex_i + else + + for y in outneighbors(x) + if y != w && color[y] == color[w] + forbiddenColors[color[x]] = vertex_i + break + end + end + + end + end + end + end + + color[vertex_i] = find_min_color(forbiddenColors, vertex_i) + + end + color +end + +function find_min_color(forbiddenColors::AbstractVector, vertex_i::Integer) + + c = 1 + while (forbiddenColors[c] == vertex_i) + c+=1 + end + + c +end + + +"""hello world this is a test message to checkout branching in git""" From 64d894ef4ab62f4ed95923e59635f600d1033218 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 04:34:55 +0530 Subject: [PATCH 02/17] Update greedy_star1_coloring.jl --- src/coloring/greedy_star1_coloring.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index 8a7fccfb..084ab7d7 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -1,5 +1,4 @@ """ - greedy_star1_coloring Find a coloring of a given input graph such that @@ -52,6 +51,3 @@ function find_min_color(forbiddenColors::AbstractVector, vertex_i::Integer) c end - - -"""hello world this is a test message to checkout branching in git""" From f62c164813a105eacb76574525ef0e04aa9b0cb2 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 04:56:18 +0530 Subject: [PATCH 03/17] added function documentation --- src/coloring/greedy_star1_coloring.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index 084ab7d7..46cd8d06 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -6,6 +6,18 @@ 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. """ function greedy_star1_coloring(G::VertexSafeGraph, alg::GreedyStar1Coloring) V = nv(G), color = zeros(Int64, V) From 1c72526a3b955b927a2534d3a87070052c71745d Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 05:27:20 +0530 Subject: [PATCH 04/17] Update greedy_star1_coloring.jl --- src/coloring/greedy_star1_coloring.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index 46cd8d06..6429ad6b 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -18,6 +18,8 @@ involving v and three other vertices is impermissible for v. In other words, every path on four vertices uses at least three colors. + + reference: What Color is your Jacobian?, pg 662 """ function greedy_star1_coloring(G::VertexSafeGraph, alg::GreedyStar1Coloring) V = nv(G), color = zeros(Int64, V) From 4c5a8c96c4d07c0a2ebd7a507acaab438d0f0a3d Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 05:51:05 +0530 Subject: [PATCH 05/17] Syntax errors --- src/coloring/greedy_star1_coloring.jl | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index 6429ad6b..34aebdf5 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -8,60 +8,60 @@ 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 + 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 + 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: What Color is your Jacobian?, pg 662 """ -function greedy_star1_coloring(G::VertexSafeGraph, alg::GreedyStar1Coloring) - V = nv(G), color = zeros(Int64, V) +function greedy_star1_coloring(G::VSafeGraph) + V = nv(G) + color = zeros(Int64, V) + forbiddenColors = zeros(Int64, V+1) for vertex_i = 1:V - for w in outneigbors(vertex_i) + for w in inneighbors(G, vertex_i) if color[w] != 0 forbiddenColors[color[w]] = vertex_i end - for x in outneighbors(w) + for x in inneighbors(G, w) if color[x] != 0 if color[w] != 0 forbiddenColors[color[x]] = vertex_i else - - for y in outneighbors(x) - if y != w && color[y] == color[w] - forbiddenColors[color[x]] = vertex_i - break + for y in inneighbors(G, x) + if color[y] != 0 + if y != w && color[y] == color[w] + forbiddenColors[color[x]] = vertex_i + break + end end end - end end end end color[vertex_i] = find_min_color(forbiddenColors, vertex_i) - end + color end function find_min_color(forbiddenColors::AbstractVector, vertex_i::Integer) - c = 1 while (forbiddenColors[c] == vertex_i) c+=1 end - c end From 4ef4ff633b1ee48b5fdafc749054a213cb885357 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 06:19:04 +0530 Subject: [PATCH 06/17] fixed logical bug --- src/coloring/greedy_star1_coloring.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index 34aebdf5..e2133673 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -36,7 +36,7 @@ function greedy_star1_coloring(G::VSafeGraph) for x in inneighbors(G, w) if color[x] != 0 - if color[w] != 0 + if color[w] == 0 forbiddenColors[color[x]] = vertex_i else for y in inneighbors(G, x) From 30323d01048da0a15ea99d352b31d447c58cb1ea Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 06:24:29 +0530 Subject: [PATCH 07/17] Added greedy star 2 coloring --- src/coloring/greedy_star2_coloring.jl | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/coloring/greedy_star2_coloring.jl diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl new file mode 100644 index 00000000..66a8be9e --- /dev/null +++ b/src/coloring/greedy_star2_coloring.jl @@ -0,0 +1,65 @@ +""" + 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: What Color is your Jacobian?, pg 663 + + TODO: add text explaining the difference between star1 and + star2 +""" +function greedy_star1_coloring(G::VSafeGraph) + V = nv(G) + color = zeros(Int64, V) + + forbiddenColors = zeros(Int64, V+1) + + for vertex_i = 1:V + + for w in inneighbors(G, vertex_i) + if color[w] != 0 + forbiddenColors[color[w]] = vertex_i + end + + for x in inneighbors(G, w) + if color[x] != 0 + if color[w] == 0 + forbiddenColors[color[x]] = vertex_i + else + if color[x] < color[w] + forbiddenColors[color[x]] = vertex_i + end + end + end + end + end + + color[vertex_i] = find_min_color(forbiddenColors, vertex_i) + end + + color +end + +function find_min_color(forbiddenColors::AbstractVector, vertex_i::Integer) + c = 1 + while (forbiddenColors[c] == vertex_i) + c+=1 + end + c +end From 5c3c702cfcfd6f212030eb01de21f7a4d742f6ee Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 06:26:20 +0530 Subject: [PATCH 08/17] changed function name --- src/coloring/greedy_star2_coloring.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl index 66a8be9e..2341859b 100644 --- a/src/coloring/greedy_star2_coloring.jl +++ b/src/coloring/greedy_star2_coloring.jl @@ -24,7 +24,7 @@ TODO: add text explaining the difference between star1 and star2 """ -function greedy_star1_coloring(G::VSafeGraph) +function greedy_star2_coloring(G::VSafeGraph) V = nv(G) color = zeros(Int64, V) From c32a1a45f8895fa88c19965d6f91386f160d44a9 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 17:08:43 +0530 Subject: [PATCH 09/17] modified distance-1 coloring --- src/coloring/greedy_d1_coloring.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/coloring/greedy_d1_coloring.jl b/src/coloring/greedy_d1_coloring.jl index fa9f61c8..487eced1 100644 --- a/src/coloring/greedy_d1_coloring.jl +++ b/src/coloring/greedy_d1_coloring.jl @@ -1,5 +1,5 @@ """ - 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 @@ -7,24 +7,24 @@ 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{1}(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 From be50d36022a85f58e5786322e8ecbdea22c0ab4a Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 22:46:56 +0530 Subject: [PATCH 10/17] general fixes --- src/coloring/greedy_star1_coloring.jl | 20 ++++++++++---------- src/coloring/greedy_star2_coloring.jl | 18 +++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index e2133673..fc6cf299 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -5,7 +5,7 @@ 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. + 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 @@ -19,27 +19,27 @@ In other words, every path on four vertices uses at least three colors. - reference: What Color is your Jacobian?, pg 662 + 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 greedy_star1_coloring(G::VSafeGraph) - V = nv(G) - color = zeros(Int64, V) +function greedy_star1_coloring(g::LightGraphs.AbstractGraph) + v = nv(g) + color = zeros(Int64, v) - forbiddenColors = zeros(Int64, V+1) + forbiddenColors = zeros(Int64, v+1) - for vertex_i = 1:V + for vertex_i = vertices(g) - for w in inneighbors(G, vertex_i) + for w in inneighbors(g, vertex_i) if color[w] != 0 forbiddenColors[color[w]] = vertex_i end - for x in inneighbors(G, w) + for x in inneighbors(g, w) if color[x] != 0 if color[w] == 0 forbiddenColors[color[x]] = vertex_i else - for y in inneighbors(G, x) + for y in inneighbors(g, x) if color[y] != 0 if y != w && color[y] == color[w] forbiddenColors[color[x]] = vertex_i diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl index 2341859b..731220c1 100644 --- a/src/coloring/greedy_star2_coloring.jl +++ b/src/coloring/greedy_star2_coloring.jl @@ -5,7 +5,7 @@ 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. + 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 @@ -19,25 +19,25 @@ In other words, every path on four vertices uses at least three colors. - reference: What Color is your Jacobian?, pg 663 + 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 greedy_star2_coloring(G::VSafeGraph) - V = nv(G) - color = zeros(Int64, V) +function greedy_star2_coloring(G::LightGraphs.AbstractGraph) + v = nv(g) + color = zeros(Int64, v) - forbiddenColors = zeros(Int64, V+1) + forbiddenColors = zeros(Int64, v+1) - for vertex_i = 1:V + for vertex_i = vertices(g) - for w in inneighbors(G, vertex_i) + for w in inneighbors(g, vertex_i) if color[w] != 0 forbiddenColors[color[w]] = vertex_i end - for x in inneighbors(G, w) + for x in inneighbors(g, w) if color[x] != 0 if color[w] == 0 forbiddenColors[color[x]] = vertex_i From 85d7b8a7dab92dfa609fd7210915dc2d24f2a205 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 2 Jul 2019 23:56:36 +0530 Subject: [PATCH 11/17] added tests --- test/runtests.jl | 3 +- test/test_greedy_star.jl | 63 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/test_greedy_star.jl diff --git a/test/runtests.jl b/test/runtests.jl index 2577c9a5..e06b3172 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,9 +4,10 @@ using Test @testset "Exact coloring via contraction" begin include("test_contraction.jl") end @testset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end +@testset "Greedy star coloring" begin include("test_greedy_star.jl") end @testset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end @testset "AD using color vector" begin include("test_ad.jl") end @testset "Integration test" begin include("test_integration.jl") end @testset "Special matrices" begin include("test_specialmatrices.jl") end @testset "Jac Vecs and Hes Vecs" begin include("test_jaches_products.jl") end -@testset "Jacobian sparsity computation" begin include("program_sparsity/testall.jl") end \ No newline at end of file +@testset "Jacobian sparsity computation" begin include("program_sparsity/testall.jl") end diff --git a/test/test_greedy_star.jl b/test/test_greedy_star.jl new file mode 100644 index 00000000..0813cca4 --- /dev/null +++ b/test/test_greedy_star.jl @@ -0,0 +1,63 @@ +using SparseDiffTools +using LightGraphs +using Random + +Random.seed!(123) + +#= Test data =# +test_graphs = Array{VSafeGraph, 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. +=# + +for i in 1:5 + g = test_graphs[i] + + out_colors1 = SparseDiffTools.greedy_star1_coloring(g) + out_colors2 = SparseDiffTools.greedy_star2_coloring(g) + + #test condition 1 + for v = vertices(g) + color = out_colors1[v] + for j in inneighbors(g, v) + @test out_color[j] != color + end + end + + #test condition 2 + for j = vertices(g) + walk = saw(g, j, 4) + walk_colors = zeros(Int64, 0) + if length(saw) >= 4 + for t in walk + push!(walk_colors, out_colors1[t]) + end + @test unique(walk_colors) >= 3 + end + end + +end From 4a2d0cee475d9e8ff860e57088719f79d15daa81 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Wed, 3 Jul 2019 01:28:59 +0530 Subject: [PATCH 12/17] added tests for both functions --- src/SparseDiffTools.jl | 4 ++++ src/coloring/greedy_star2_coloring.jl | 10 +--------- test/test_greedy_star.jl | 28 +++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/SparseDiffTools.jl b/src/SparseDiffTools.jl index b2c60b7b..9ba7751f 100644 --- a/src/SparseDiffTools.jl +++ b/src/SparseDiffTools.jl @@ -13,6 +13,8 @@ import Core: SSAValue export contract_color, greedy_d1, + greedy_star1_coloring, + greedy_star2_coloring, matrix2graph, matrix_colors, forwarddiff_color_jacobian!, @@ -33,6 +35,8 @@ export contract_color, include("coloring/high_level.jl") include("coloring/contraction_coloring.jl") include("coloring/greedy_d1_coloring.jl") +include("coloring/greedy_star1_coloring.jl") +include("coloring/greedy_star2_coloring.jl") include("coloring/matrix2graph.jl") include("differentiation/compute_jacobian_ad.jl") include("differentiation/jaches_products.jl") diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl index 731220c1..3831df7a 100644 --- a/src/coloring/greedy_star2_coloring.jl +++ b/src/coloring/greedy_star2_coloring.jl @@ -24,7 +24,7 @@ TODO: add text explaining the difference between star1 and star2 """ -function greedy_star2_coloring(G::LightGraphs.AbstractGraph) +function greedy_star2_coloring(g::LightGraphs.AbstractGraph) v = nv(g) color = zeros(Int64, v) @@ -55,11 +55,3 @@ function greedy_star2_coloring(G::LightGraphs.AbstractGraph) color end - -function find_min_color(forbiddenColors::AbstractVector, vertex_i::Integer) - c = 1 - while (forbiddenColors[c] == vertex_i) - c+=1 - end - c -end diff --git a/test/test_greedy_star.jl b/test/test_greedy_star.jl index 0813cca4..7a71ac20 100644 --- a/test/test_greedy_star.jl +++ b/test/test_greedy_star.jl @@ -5,7 +5,7 @@ using Random Random.seed!(123) #= Test data =# -test_graphs = Array{VSafeGraph, 1}(undef, 0) +test_graphs = Array{SimpleGraph, 1}(undef, 0) for _ in 1:5 nv = rand(5:20) @@ -44,7 +44,7 @@ for i in 1:5 for v = vertices(g) color = out_colors1[v] for j in inneighbors(g, v) - @test out_color[j] != color + @test out_colors1[j] != color end end @@ -52,11 +52,31 @@ for i in 1:5 for j = vertices(g) walk = saw(g, j, 4) walk_colors = zeros(Int64, 0) - if length(saw) >= 4 + if length(walk) >= 4 for t in walk push!(walk_colors, out_colors1[t]) end - @test unique(walk_colors) >= 3 + @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 = 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 From 0d8904f9c5f7d8b6017a58dc2180f59cc46d792e Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Wed, 3 Jul 2019 23:19:10 +0530 Subject: [PATCH 13/17] general fixes --- src/coloring/greedy_d1_coloring.jl | 2 +- src/coloring/greedy_star1_coloring.jl | 14 +++++++------- src/coloring/greedy_star2_coloring.jl | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/coloring/greedy_d1_coloring.jl b/src/coloring/greedy_d1_coloring.jl index 487eced1..41719b74 100644 --- a/src/coloring/greedy_d1_coloring.jl +++ b/src/coloring/greedy_d1_coloring.jl @@ -11,7 +11,7 @@ function color_graph(g::VSafeGraph, alg::GreedyD1Color) v = nv(g) result = zeros(Int64, v) result[1] = 1 - available = BitArray{1}(undef, v) + available = BitArray(undef, v) for i = 2:v for j in inneighbors(g, i) if result[j] != 0 diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index fc6cf299..10d9b4c2 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -25,24 +25,24 @@ function greedy_star1_coloring(g::LightGraphs.AbstractGraph) v = nv(g) color = zeros(Int64, v) - forbiddenColors = zeros(Int64, v+1) + forbidden_colors = zeros(Int64, v+1) for vertex_i = vertices(g) for w in inneighbors(g, vertex_i) if color[w] != 0 - forbiddenColors[color[w]] = vertex_i + forbidden_colors[color[w]] = vertex_i end for x in inneighbors(g, w) if color[x] != 0 if color[w] == 0 - forbiddenColors[color[x]] = vertex_i + forbidden_colors[color[x]] = vertex_i else for y in inneighbors(g, x) if color[y] != 0 if y != w && color[y] == color[w] - forbiddenColors[color[x]] = vertex_i + forbidden_colors[color[x]] = vertex_i break end end @@ -52,15 +52,15 @@ function greedy_star1_coloring(g::LightGraphs.AbstractGraph) end end - color[vertex_i] = find_min_color(forbiddenColors, vertex_i) + color[vertex_i] = find_min_color(forbidden_colors, vertex_i) end color end -function find_min_color(forbiddenColors::AbstractVector, vertex_i::Integer) +function find_min_color(forbidden_colors::AbstractVector, vertex_i::Integer) c = 1 - while (forbiddenColors[c] == vertex_i) + while (forbidden_colors[c] == vertex_i) c+=1 end c diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl index 3831df7a..770a654c 100644 --- a/src/coloring/greedy_star2_coloring.jl +++ b/src/coloring/greedy_star2_coloring.jl @@ -28,29 +28,29 @@ function greedy_star2_coloring(g::LightGraphs.AbstractGraph) v = nv(g) color = zeros(Int64, v) - forbiddenColors = zeros(Int64, v+1) + forbidden_colors = zeros(Int64, v+1) for vertex_i = vertices(g) for w in inneighbors(g, vertex_i) if color[w] != 0 - forbiddenColors[color[w]] = vertex_i + forbidden_colors[color[w]] = vertex_i end for x in inneighbors(g, w) if color[x] != 0 if color[w] == 0 - forbiddenColors[color[x]] = vertex_i + forbidden_colors[color[x]] = vertex_i else if color[x] < color[w] - forbiddenColors[color[x]] = vertex_i + forbidden_colors[color[x]] = vertex_i end end end end end - color[vertex_i] = find_min_color(forbiddenColors, vertex_i) + color[vertex_i] = find_min_color(forbidden_colors, vertex_i) end color From 495916b618c4d49bd7dba6a83a7642e18c346202 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Thu, 4 Jul 2019 03:07:55 +0530 Subject: [PATCH 14/17] added dispatch for color graph --- src/coloring/greedy_star1_coloring.jl | 2 +- src/coloring/greedy_star2_coloring.jl | 2 +- src/coloring/high_level.jl | 2 ++ test/test_greedy_star.jl | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index 10d9b4c2..fa545501 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -21,7 +21,7 @@ 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 greedy_star1_coloring(g::LightGraphs.AbstractGraph) +function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color) v = nv(g) color = zeros(Int64, v) diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl index 770a654c..1820e21a 100644 --- a/src/coloring/greedy_star2_coloring.jl +++ b/src/coloring/greedy_star2_coloring.jl @@ -24,7 +24,7 @@ TODO: add text explaining the difference between star1 and star2 """ -function greedy_star2_coloring(g::LightGraphs.AbstractGraph) +function color_graph(g::LightGraphs.AbstractGraph, :: GreedyStar2Color) v = nv(g) color = zeros(Int64, v) diff --git a/src/coloring/high_level.jl b/src/coloring/high_level.jl index 94da3609..66cb3db0 100644 --- a/src/coloring/high_level.jl +++ b/src/coloring/high_level.jl @@ -2,6 +2,8 @@ abstract type ColoringAlgorithm end struct GreedyD1Color <: ColoringAlgorithm end struct BSCColor <: ColoringAlgorithm end struct ContractionColor <: ColoringAlgorithm end +struct GreedyStar1Color <: ColoringAlgorithm end +struct GreedyStar2Color <: ColoringAlgorithm end """ matrix_colors(A,alg::ColoringAlgorithm = GreedyD1Color()) diff --git a/test/test_greedy_star.jl b/test/test_greedy_star.jl index 7a71ac20..f9c78f18 100644 --- a/test/test_greedy_star.jl +++ b/test/test_greedy_star.jl @@ -37,8 +37,8 @@ colors. for i in 1:5 g = test_graphs[i] - out_colors1 = SparseDiffTools.greedy_star1_coloring(g) - out_colors2 = SparseDiffTools.greedy_star2_coloring(g) + out_colors1 = SparseDiffTools.color_graph(g,SparseDiffTools.GreedyStar1Color()) + out_colors2 = SparseDiffTools.color_graph(g,SparseDiffTools.GreedyStar2Color()) #test condition 1 for v = vertices(g) From 701e19528f2336671c80c1934e8da74bb98324d8 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Mon, 8 Jul 2019 07:55:57 +0530 Subject: [PATCH 15/17] added graph from paper in tests --- test/test_greedy_star.jl | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/test/test_greedy_star.jl b/test/test_greedy_star.jl index f9c78f18..ce640f45 100644 --- a/test/test_greedy_star.jl +++ b/test/test_greedy_star.jl @@ -25,7 +25,7 @@ end #= Coloring needs to satisfy two conditions: -1. every pair of adjacent vertices receives distinct colors +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 @@ -34,7 +34,29 @@ In other words, every path on four vertices uses at least three colors. =# -for i in 1:5 + +#Sample graph from Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** + +#= + (2) + / \ + / \ + (1)----(3)----(4) + +=# + +inner = SimpleGraph(4) +g = VSafeGraph(inner) + +add_edge!(g,1,2) +add_edge!(g,1,3) +add_edge!(g,2,3) +add_edge!(g,3,4) + +push!(test_graphs, g) + +#begin testing +for i in 1:6 g = test_graphs[i] out_colors1 = SparseDiffTools.color_graph(g,SparseDiffTools.GreedyStar1Color()) @@ -50,7 +72,7 @@ for i in 1:5 #test condition 2 for j = vertices(g) - walk = saw(g, j, 4) + walk = LightGraphs.saw(g, j, 4) walk_colors = zeros(Int64, 0) if length(walk) >= 4 for t in walk @@ -70,7 +92,7 @@ for i in 1:5 #test condition 2 for j = vertices(g) - walk = saw(g, j, 4) + walk = LighGraphs.saw(g, j, 4) walk_colors = zeros(Int64, 0) if length(walk) >= 4 for t in walk From 9550cfc501015563419dedeea1b1762866aa0a46 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 9 Jul 2019 22:22:40 +0530 Subject: [PATCH 16/17] fixed tests --- test/test_greedy_star.jl | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/test_greedy_star.jl b/test/test_greedy_star.jl index ce640f45..807d5bea 100644 --- a/test/test_greedy_star.jl +++ b/test/test_greedy_star.jl @@ -45,15 +45,14 @@ colors. =# -inner = SimpleGraph(4) -g = VSafeGraph(inner) +gx = SimpleGraph(4) -add_edge!(g,1,2) -add_edge!(g,1,3) -add_edge!(g,2,3) -add_edge!(g,3,4) +add_edge!(gx,1,2) +add_edge!(gx,1,3) +add_edge!(gx,2,3) +add_edge!(gx,3,4) -push!(test_graphs, g) +push!(test_graphs, gx) #begin testing for i in 1:6 @@ -92,7 +91,7 @@ for i in 1:6 #test condition 2 for j = vertices(g) - walk = LighGraphs.saw(g, j, 4) + walk = LightGraphs.saw(g, j, 4) walk_colors = zeros(Int64, 0) if length(walk) >= 4 for t in walk From e869fa330d82c2d4575f90d47fc69c233f9a74c7 Mon Sep 17 00:00:00 2001 From: Pankaj Mishra Date: Tue, 9 Jul 2019 22:43:29 +0530 Subject: [PATCH 17/17] added Random --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index c2fd76fc..8e1a263c 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,7 @@ DiffEqDiffTools = "01453d9d-ee7c-5054-8395-0335cb756afa" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"