Skip to content

Commit

Permalink
Patch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Jan 29, 2024
1 parent 71ec796 commit 65c7db7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 65 deletions.
13 changes: 7 additions & 6 deletions src/flows/mincut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Compute the min-cut between `source` and `target` for the given graph.
First computes the maxflow using `algorithm` and then builds the partition of the residual graph
Returns a triplet `(part1, part2, flow)` with the partition containing the source, the partition containing the target (the rest) and the min-cut(max-flow) value
"""
function mincut_flow(
flow_graph::Graphs.DiGraph, # the input graph
function mincut_flow end
@traitfn function mincut_flow(
flow_graph::::IsDirected, # the input graph
source::Integer, # the source vertex
target::Integer, # the target vertex
capacity_matrix::AbstractMatrix, # edge flow capacities
Expand All @@ -15,11 +16,11 @@ function mincut_flow(
flow, flow_matrix = maximum_flow(flow_graph, source, target, capacity_matrix, algorithm)
residual_matrix = spzeros(Graphs.nv(flow_graph), Graphs.nv(flow_graph))
for edge in Graphs.edges(flow_graph)
residual_matrix[edge.src, edge.dst] = max(
0.0, capacity_matrix[edge.src, edge.dst] - flow_matrix[edge.src, edge.dst]
residual_matrix[src(edge), dst(edge)] = max(
0.0, capacity_matrix[src(edge), dst(edge)] - flow_matrix[src(edge), dst(edge)]
)
residual_matrix[edge.dst, edge.src] = max(
0.0, capacity_matrix[edge.dst, edge.src] - flow_matrix[edge.dst, edge.src]
residual_matrix[dst(edge), src(edge)] = max(
0.0, capacity_matrix[dst(edge), src(edge)] - flow_matrix[dst(edge), src(edge)]
)
end
part1 = typeof(source)[]
Expand Down
44 changes: 20 additions & 24 deletions test/flows/boykov_kolmogorov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
C[u, t] = C[t, u] = Inf
end

for G_gen in test_generic_graphs(G)
for G_gen in [G] # TODO: generic graphs
# now we are ready to start the flow
flow, _, labels = maximum_flow(
G_gen, s, t, C; algorithm=BoykovKolmogorovAlgorithm()
Expand All @@ -81,7 +81,7 @@
# the final cut represents the two disconnected
# subimages filled with water of different color
COLOR = reshape(labels[1:(end - 2)], sz)
@test COLOR == [
@test COLOR == eltype(COLOR)[
1 1 1 1 0 0 0 0 2
1 1 1 1 0 0 0 0 2
1 1 1 1 0 0 0 0 2
Expand All @@ -92,18 +92,16 @@
1 0 0 0 0 2 2 2 2
1 0 0 0 0 2 2 2 2
]
end

# now let's create a bridge connecting the two
# subimages to allow flow from source to target
for (I1, I2) in
[[(4, 4), (5, 4)], [(5, 4), (5, 5)], [(5, 5), (5, 6)], [(5, 6), (6, 6)]]
u = LinearIndices(sz)[I1...]
v = LinearIndices(sz)[I2...]
C[u, v] = C[v, u] = 1
end
# now let's create a bridge connecting the two
# subimages to allow flow from source to target
for (I1, I2) in
[[(4, 4), (5, 4)], [(5, 4), (5, 5)], [(5, 5), (5, 6)], [(5, 6), (6, 6)]]
u = LinearIndices(sz)[I1...]
v = LinearIndices(sz)[I2...]
C[u, v] = C[v, u] = 1
end

for G_gen in test_generic_graphs(G)
flow, _, labels = maximum_flow(
G_gen, s, t, C; algorithm=BoykovKolmogorovAlgorithm()
)
Expand All @@ -114,7 +112,7 @@

# the final cut is unchanged compared to the previous one
COLOR = reshape(labels[1:(end - 2)], sz)
@test COLOR == [
@test COLOR == eltype(COLOR)[
1 1 1 1 0 0 0 0 2
1 1 1 1 0 0 0 0 2
1 1 1 1 0 0 0 0 2
Expand All @@ -125,18 +123,16 @@
1 0 0 0 0 2 2 2 2
1 0 0 0 0 2 2 2 2
]
end

# finally let's create a second bridge to increase
# the maximum flow from one to two units
for (I1, I2) in
[[(4, 4), (4, 5)], [(4, 5), (5, 5)], [(5, 5), (6, 5)], [(6, 5), (6, 6)]]
u = LinearIndices(sz)[I1...]
v = LinearIndices(sz)[I2...]
C[u, v] = C[v, u] = 1
end
# finally let's create a second bridge to increase
# the maximum flow from one to two units
for (I1, I2) in
[[(4, 4), (4, 5)], [(4, 5), (5, 5)], [(5, 5), (6, 5)], [(6, 5), (6, 6)]]
u = LinearIndices(sz)[I1...]
v = LinearIndices(sz)[I2...]
C[u, v] = C[v, u] = 1
end

for G_gen in test_generic_graphs(G)
flow, _, labels = maximum_flow(
G_gen, s, t, C; algorithm=BoykovKolmogorovAlgorithm()
)
Expand All @@ -147,7 +143,7 @@
# the final cut is slightly different
# near the corners of the two subimages
COLOR = reshape(labels[1:(end - 2)], sz)
@test COLOR == [
@test COLOR == eltype(COLOR)[
1 1 1 1 0 0 0 0 2
1 1 1 1 0 0 0 0 2
1 1 1 1 0 0 0 0 2
Expand Down
15 changes: 8 additions & 7 deletions test/flows/edmonds_karp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
(7, 8, 10),
]

capacity_matrix = zeros(Int, 8, 8)
for e in flow_edges
u, v, f = e
Graphs.add_edge!(flow_graph, u, v)
capacity_matrix[u, v] = f
end

for fg in test_generic_graphs(flow_graph)
capacity_matrix = zeros(Int, 8, 8)
for e in flow_edges
u, v, f = e
Graphs.add_edge!(fg, u, v)
capacity_matrix[u, v] = f
end
residual_graph = @inferred(Graphs.residual(fg))

# Test with default distances
Expand Down Expand Up @@ -55,7 +56,7 @@
function test_find_path_disconnected(
residual_graph, s, t, flow_matrix, capacity_matrix
)
h = copy(residual_graph)
h = SimpleDiGraph(copy(residual_graph))
for dst in collect(Graphs.neighbors(residual_graph, s))
Graphs.rem_edge!(residual_graph, s, dst)
end
Expand Down
13 changes: 7 additions & 6 deletions test/flows/maximum_flow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@

for (nvertices, flow_edges, s, t, fdefault, fcustom, frestrict, caprestrict) in graphs
flow_graph = Graphs.DiGraph(nvertices)
capacity_matrix = zeros(Int, nvertices, nvertices)
for e in flow_edges
u, v, f = e
Graphs.add_edge!(flow_graph, u, v)
capacity_matrix[u, v] = f
end

for g in test_generic_graphs(flow_graph)
capacity_matrix = zeros(Int, nvertices, nvertices)
for e in flow_edges
u, v, f = e
Graphs.add_edge!(g, u, v)
capacity_matrix[u, v] = f
end

# Test DefaultCapacity
d = @inferred(Graphs.DefaultCapacity(g))
Expand Down
30 changes: 16 additions & 14 deletions test/flows/mincut.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
@testset "Mincut" begin
cap1 = [
0.0 2.0 2.0 0.0 0.0
0.0 0.0 0.0 0.0 3.0
0.0 1.0 0.0 3.0 0.0
0.0 0.0 0.0 0.0 1.0
0.0 0.0 0.0 0.0 0.0
]
cap2 = [
0.0 3.0 2.0 0.0 0.0
0.0 0.0 0.0 0.0 3.0
0.0 1.0 0.0 3.0 0.0
0.0 0.0 0.0 0.0 1.5
0.0 0.0 0.0 0.0 0.0
]

g = Graphs.complete_digraph(5)

for g_gen in test_generic_graphs(g)
cap1 = [
0.0 2.0 2.0 0.0 0.0
0.0 0.0 0.0 0.0 3.0
0.0 1.0 0.0 3.0 0.0
0.0 0.0 0.0 0.0 1.0
0.0 0.0 0.0 0.0 0.0
]
(part1, part2, value) = Graphs.mincut_flow(
g_gen, 1, 5, cap1, Graphs.PushRelabelAlgorithm()
)
@test value 4.0
@test part1 == [1]
@test sort(part2) == collect(2:5)
cap2 = [
0.0 3.0 2.0 0.0 0.0
0.0 0.0 0.0 0.0 3.0
0.0 1.0 0.0 3.0 0.0
0.0 0.0 0.0 0.0 1.5
0.0 0.0 0.0 0.0 0.0
]
(part1, part2, value) = Graphs.mincut_flow(
g_gen, 1, 5, cap2, Graphs.PushRelabelAlgorithm()
)
Expand Down
13 changes: 7 additions & 6 deletions test/flows/multiroute_flow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@

for (nvertices, flow_edges, s, t, froutes, breakpts, ffloat) in graphs
flow_graph = Graphs.DiGraph(nvertices)
capacity_matrix = zeros(Int, nvertices, nvertices)
for e in flow_edges
u, v, f = e
Graphs.add_edge!(flow_graph, u, v)
capacity_matrix[u, v] = f
end

for g in test_generic_graphs(flow_graph)
capacity_matrix = zeros(Int, nvertices, nvertices)
for e in flow_edges
u, v, f = e
Graphs.add_edge!(g, u, v)
capacity_matrix[u, v] = f
end
# Test ExtendedMultirouteFlowAlgorithm when the number of routes is either
# Noninteger or 0 (the algorithm returns the breaking points)
@test multiroute_flow(g, s, t, capacity_matrix) == breakpts
Expand Down
5 changes: 3 additions & 2 deletions test/flows/push_relabel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@
count = [0, 1, 2, 2, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
flow_matrix = zeros(Int, 8, 8)

@test @inferred(Graphs.gap!(residual_graph, 1, excess, height, active, count, Q)) ==
nothing
@test @inferred(
Graphs.push_relabel_gap!(residual_graph, 1, excess, height, active, count, Q)
) == nothing
@test length(Q) == 2

# Test relabel
Expand Down

0 comments on commit 65c7db7

Please sign in to comment.