Skip to content

Commit 3767b03

Browse files
JoeyT1994mtfishman
andauthored
Edge coloring (#98)
Co-authored-by: Matt Fishman <[email protected]>
1 parent e1592bd commit 3767b03

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

Project.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NamedGraphs"
22
uuid = "678767b0-92e7-4007-89e4-4527a8725b19"
33
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
4-
version = "0.6.7"
4+
version = "0.6.8"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -11,6 +11,8 @@ Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1111
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1212
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
1313
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
14+
SimpleGraphAlgorithms = "41400c72-0c58-5c16-8579-4ecbce768449"
15+
SimpleGraphConverter = "205b04f2-f585-4877-a239-566270b3f673"
1416
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
1517
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1618
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
@@ -26,6 +28,7 @@ SymRCM = "286e6d88-80af-4590-acc9-0001b223b9bd"
2628
NamedGraphsGraphsFlowsExt = "GraphsFlows"
2729
NamedGraphsKaHyParExt = "KaHyPar"
2830
NamedGraphsMetisExt = "Metis"
31+
NamedGraphsSimpleGraphAlgorithmsExt = "SimpleGraphAlgorithms"
2932
NamedGraphsSymRCMExt = "SymRCM"
3033

3134
[compat]
@@ -38,6 +41,8 @@ KaHyPar = "0.3.1"
3841
LinearAlgebra = "1.7"
3942
Metis = "1.4"
4043
PackageExtensionCompat = "1"
44+
SimpleGraphAlgorithms = "0.6.0"
45+
SimpleGraphConverter = "0.1.0"
4146
SimpleTraits = "0.9"
4247
SparseArrays = "1.7"
4348
SplitApplyCombine = "1.2.2"
@@ -49,6 +54,8 @@ julia = "1.7"
4954
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
5055
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
5156
Metis = "2679e427-3c69-5b7f-982b-ece356f1e94b"
57+
SimpleGraphAlgorithms = "41400c72-0c58-5c16-8579-4ecbce768449"
58+
SimpleGraphConverter = "205b04f2-f585-4877-a239-566270b3f673"
5259
SymRCM = "286e6d88-80af-4590-acc9-0001b223b9bd"
5360
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5461

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module NamedGraphsSimpleGraphAlgorithmsExt
2+
using Graphs: edgetype
3+
using NamedGraphs: AbstractNamedGraph, position_graph, vertices
4+
using NamedGraphs.GraphsExtensions: GraphsExtensions
5+
using SimpleGraphAlgorithms: SimpleGraphAlgorithms
6+
using SimpleGraphConverter: UndirectedGraph
7+
8+
function SimpleGraphAlgorithms.edge_color(g::AbstractNamedGraph, k::Int64)
9+
pg, vs = position_graph(g), collect(vertices(g))
10+
ec_dict = SimpleGraphAlgorithms.edge_color(UndirectedGraph(pg), k)
11+
# returns k vectors of edges which each contain the colored/commuting edges
12+
return [
13+
[edgetype(g)(vs[first(first(e))], vs[last(first(e))]) for e in ec_dict if last(e) == i]
14+
for i in 1:k
15+
]
16+
end
17+
end

test/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Metis = "2679e427-3c69-5b7f-982b-ece356f1e94b"
99
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
1010
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1111
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
12+
SimpleGraphAlgorithms = "41400c72-0c58-5c16-8579-4ecbce768449"
13+
SimpleGraphConverter = "205b04f2-f585-4877-a239-566270b3f673"
1214
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
1315
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1416
SymRCM = "286e6d88-80af-4590-acc9-0001b223b9bd"
@@ -24,6 +26,8 @@ KaHyPar = "0.3.1"
2426
Metis = "1.5.0"
2527
NamedGraphs = "0.6.6"
2628
SafeTestsets = "0.1.0"
29+
SimpleGraphAlgorithms = "0.6.0"
30+
SimpleGraphConverter = "0.1.0"
2731
Suppressor = "0.2.8"
2832
SymRCM = "0.2.2"
2933
Pkg = "1.10.0"

test/test_edge_coloring.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@eval module $(gensym())
2+
using Graphs: degree, dst, edges, ne, src, vertices
3+
using NamedGraphs.NamedGraphGenerators:
4+
named_comb_tree, named_grid, named_hexagonal_lattice_graph
5+
using SimpleGraphConverter
6+
using SimpleGraphAlgorithms: edge_color
7+
using Test: @test, @testset
8+
9+
@testset "EdgeColoring" begin
10+
g = named_grid((4, 4); periodic=true)
11+
12+
#For bipartite graphs, a coloring can always be achieved, in linear time,
13+
#using the maximum degree of the graph
14+
k = maximum([degree(g, v) for v in vertices(g)])
15+
colored_edges = edge_color(g, k)
16+
#Test all edges are present in the coloring
17+
@test issetequal(reduce(vcat, colored_edges), edges(g))
18+
#Test all colors have same number of edges in this case
19+
@test all([length(es) == ne(g) / k for es in colored_edges])
20+
#Test every vertex appears only once in each group (no overlapping edges in the coloring)
21+
@test all([
22+
unique(vcat(src.(es), dst.(es))) == vcat(src.(es), dst.(es)) for es in colored_edges
23+
])
24+
end
25+
end

0 commit comments

Comments
 (0)