Skip to content
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

Better functionality for PartitionedGraph #45

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Graphs/partitionedgraphs/abstractpartitionedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ abstract type AbstractPartitionEdge{V} <: AbstractNamedEdge{V} end
parent(pe::AbstractPartitionEdge) = not_implemented()
src(pe::AbstractPartitionEdge) = not_implemented()
dst(pe::AbstractPartitionEdge) = not_implemented()
reverse(pe::AbstractPartitionEdge) = not_implemented()

#Don't have the vertices wrapped. But wrap them with source and edge.
15 changes: 10 additions & 5 deletions src/Graphs/partitionedgraphs/abstractpartitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ abstract type AbstractPartitionedGraph{V,PV} <: AbstractNamedGraph{V} end
#Needed for interface
partitioned_graph(pg::AbstractPartitionedGraph) = not_implemented()
unpartitioned_graph(pg::AbstractPartitionedGraph) = not_implemented()
which_partition(pg::AbstractPartitionedGraph, vertex) = not_implemented()
partitioned_vertices(pg::AbstractPartitionedGraph) = not_implemented()
partition_vertex(pg::AbstractPartitionedGraph, vertex) = not_implemented()
partition_vertices(pg::AbstractPartitionedGraph, verts::Vector) = not_implemented()
partition_vertices(pg::AbstractPartitionedGraph) = not_implemented()
copy(pg::AbstractPartitionedGraph) = not_implemented()
delete_from_vertex_map!(pg::AbstractPartitionedGraph, vertex) = not_implemented()
insert_to_vertex_map!(pg::AbstractPartitionedGraph, vertex) = not_implemented()
partition_edge(pg::AbstractPartitionedGraph, edge) = not_implemented()
function partition_edges(pg::AbstractPartitionedGraph, edges::Vector{<:AbstractEdge})
return not_implemented()
end
partition_edges(pg::AbstractPartitionedGraph) = not_implemented()
function edges(pg::AbstractPartitionedGraph, partition_edge::AbstractPartitionEdge)
return not_implemented()
end
Expand All @@ -35,7 +40,7 @@ function has_vertex(pg::AbstractPartitionedGraph, partition_vertex::AbstractPart
return has_vertex(partitioned_graph(pg), parent(partition_vertex))
end

function has_edge(pg::AbstractPartitionedGraph, edge::AbstractPartitionEdge)
function has_edge(pg::AbstractPartitionedGraph, partition_edge::AbstractPartitionEdge)
return has_edge(partitioned_graph(pg), parent(partition_edge))
end

Expand Down Expand Up @@ -123,7 +128,7 @@ function add_vertices!(
end

function rem_vertex!(pg::AbstractPartitionedGraph, vertex)
pv = which_partition(pg, vertex)
pv = partition_vertex(pg, vertex)
delete_from_vertex_map!(pg, pv, vertex)
rem_vertex!(unpartitioned_graph(pg), vertex)
if !haskey(partitioned_vertices(pg), parent(pv))
Expand Down Expand Up @@ -155,7 +160,7 @@ function (pg1::AbstractPartitionedGraph == pg2::AbstractPartitionedGraph)
return false
end
for v in vertices(pg1)
if which_partition(pg1, v) != which_partition(pg2, v)
if partition_vertex(pg1, v) != partition_vertex(pg2, v)
return false
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/Graphs/partitionedgraphs/partitionedge.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
struct PartitionEdge{V,E<:AbstractEdge{V}} <: AbstractPartitionEdge{V}
struct PartitionEdge{V,E<:AbstractEdge{<:V}} <: AbstractPartitionEdge{V}
edge::E
end

parent(pe::PartitionEdge) = getfield(pe, :edge)
src(pe::PartitionEdge) = PartitionVertex(src(parent(pe)))
dst(pe::PartitionEdge) = PartitionVertex(dst(parent(pe)))
PartitionEdge(p::Pair) = PartitionEdge(NamedEdge(first(p) => last(p)))
PartitionEdge(vsrc, vdst) = PartitionEdge(vsrc => vdst)
reverse(pe::PartitionEdge) = PartitionEdge(reverse(parent(pe)))
35 changes: 28 additions & 7 deletions src/Graphs/partitionedgraphs/partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ function PartitionedGraph(partitioned_vertices)
end

function PartitionedGraph(g::AbstractGraph; kwargs...)
partitioned_vertices = partition_vertices(g; kwargs...)
return PartitionedGraph(g, partitioned_vertices)
partitioned_verts = partitioned_vertices(g; kwargs...)
return PartitionedGraph(g, partitioned_verts)
end

#Needed for interface
Expand All @@ -49,17 +49,34 @@ end
function vertices(pg::PartitionedGraph, partition_verts::Vector{<:PartitionVertex})
return unique(reduce(vcat, [vertices(pg, pv) for pv in partition_verts]))
end
function which_partition(pg::PartitionedGraph, vertex)
function partition_vertex(pg::PartitionedGraph, vertex)
return PartitionVertex(which_partition(pg)[vertex])
end

function partition_vertices(pg::PartitionedGraph, verts::Vector)
return unique(partition_vertex(pg, v) for v in verts)
end

function partition_vertices(pg::PartitionedGraph)
return PartitionedVertex.(partitioned_graph(pg))
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
end

function partition_edge(pg::PartitionedGraph, edge::AbstractEdge)
return PartitionEdge(
parent(which_partition(pg, src(edge))) => parent(which_partition(pg, dst(edge)))
parent(partition_vertex(pg, src(edge))) => parent(partition_vertex(pg, dst(edge)))
)
end

function partition_edges(pg::PartitionedGraph, partition_edge::PartitionEdge)
#Lets filter out any self-edges from this. Although this makes it a bit consistent with partition_edge
function partition_edges(pg::PartitionedGraph, edges::Vector{<:AbstractEdge})
return filter(e -> src(e) != dst(e), unique([partition_edge(pg, e) for e in edges]))
end

function partition_edges(pg::PartitionedGraph)
return PartitionEdge.(edges(partitioned_graph(pg)))
end

function edges(pg::PartitionedGraph, partition_edge::PartitionEdge)
psrc_vs = vertices(pg, PartitionVertex(src(partition_edge)))
pdst_vs = vertices(pg, PartitionVertex(dst(partition_edge)))
psrc_subgraph = subgraph(unpartitioned_graph(pg), psrc_vs)
Expand All @@ -69,12 +86,16 @@ function partition_edges(pg::PartitionedGraph, partition_edge::PartitionEdge)
return setdiff(edges(full_subgraph), vcat(edges(psrc_subgraph), edges(pdst_subgraph)))
end

function edges(pg::PartitionedGraph, partition_edges::Vector{<:PartitionEdge})
return unique(reduce(vcat, [edges(pg, pe) for pe in partition_edges]))
end

function copy(pg::PartitionedGraph)
return PartitionedGraph(
copy(unpartitioned_graph(pg)),
copy(partitioned_graph(pg)),
copy_keys_values(partitioned_vertices(pg)),
copy_keys_values(which_partition(pg)),
copy_keys_values(partition_vertex(pg)),
)
end

Expand All @@ -93,7 +114,7 @@ function insert_to_vertex_map!(
end

function delete_from_vertex_map!(pg::PartitionedGraph, vertex)
pv = which_partition(pg, vertex)
pv = partition_vertex(pg, vertex)
return delete_from_vertex_map!(pg, pv, vertex)
end

Expand Down
8 changes: 4 additions & 4 deletions src/Graphs/partitionedgraphs/partitioning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function _npartitions(
return error("Must specify either `npartitions` or `nvertices_per_partition`")
end

function partition_vertices(
function partitioned_vertices(
g::Graph;
npartitions=nothing,
nvertices_per_partition=nothing,
Expand All @@ -84,15 +84,15 @@ function partition_vertices(
return group(v -> 1, collect(vertices(g)))
end

return partition_vertices(
return partitioned_vertices(
Backend(backend), g, _npartitions(g, npartitions, nvertices_per_partition); kwargs...
)
end

function partition_vertices(
function partitioned_vertices(
g::AbstractNamedGraph; npartitions=nothing, nvertices_per_partition=nothing, kwargs...
)
vertex_partitions = partition_vertices(
vertex_partitions = partitioned_vertices(
parent_graph(g); npartitions, nvertices_per_partition, kwargs...
)
#[inv(vertex_to_parent_vertex(g))[v] for v in partitions]
Expand Down
5 changes: 5 additions & 0 deletions src/NamedGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export NamedGraph,
neighbors,
nv,
partitioned_graph,
partition_edge,
partition_edges,
partition_vertex,
partition_vertices,
partitioned_vertices,
path_digraph,
path_graph,
periphery,
Expand Down
8 changes: 4 additions & 4 deletions src/requires/kahypar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ set_partitioning_backend!(Backend"KaHyPar"())
KaHyPar.HyperGraph(g::SimpleGraph) = incidence_matrix(g)

"""
partition_vertices(::Backend"KaHyPar", g::Graph, npartiations::Integer; objective="edge_cut", alg="kway", kwargs...)
partitioned_vertices(::Backend"KaHyPar", g::Graph, npartiations::Integer; objective="edge_cut", alg="kway", kwargs...)

- default_configuration => "cut_kKaHyPar_sea20.ini"
- :edge_cut => "cut_kKaHyPar_sea20.ini"
- :connectivity => "km1_kKaHyPar_sea20.ini"
- imbalance::Number=0.03
"""
function partition_vertices(
function partitioned_vertices(
::Backend"KaHyPar",
g::SimpleGraph,
npartitions::Integer;
Expand All @@ -28,6 +28,6 @@ function partition_vertices(
kahypar_configurations[(objective=objective, alg=alg)],
)
end
partitions = @suppress KaHyPar.partition(g, npartitions; configuration, kwargs...)
return groupfind(partitions .+ 1)
partitioned_verts = @suppress KaHyPar.partition(g, npartitions; configuration, kwargs...)
return groupfind(partitioned_verts .+ 1)
end
24 changes: 4 additions & 20 deletions src/requires/metis.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
set_partitioning_backend!(Backend"Metis"())

"""
partition_vertices(::Backend"Metis", g::AbstractGraph, npartitions::Integer; alg="recursive")
partitioned_vertices(::Backend"Metis", g::AbstractGraph, npartitions::Integer; alg="recursive")

Partition the graph `G` in `n` parts.
The partition algorithm is defined by the `alg` keyword:
- :KWAY: multilevel k-way partitioning
- :RECURSIVE: multilevel recursive bisection
"""
function partition_vertices(
function partitioned_vertices(
::Backend"Metis", g::SimpleGraph, npartitions::Integer; alg="recursive", kwargs...
)
metis_alg = metis_algs[alg]
partitions = Metis.partition(g, npartitions; alg=metis_alg, kwargs...)
return groupfind(Int.(partitions))
partitioned_verts = Metis.partition(g, npartitions; alg=metis_alg, kwargs...)
return groupfind(Int.(partitioned_verts))
end

## #=
## Metis.partition(G, n; alg = :KWAY)
##
## Partition the graph `G` in `n` parts.
## The partition algorithm is defined by the `alg` keyword:
## - :KWAY: multilevel k-way partitioning
## - :RECURSIVE: multilevel recursive bisection
## =#
## function partition(g::Metis.Graph, npartitions::Integer)
## return Metis.partition(g, npartitions; alg=:KWAY)
## end
##
## function partition(g::Graph, npartitions::Integer)
## return partition(Metis.graph(adjacency_matrix(g)), npartitions)
## end
Loading