This repository was archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpush_relabel.jl
94 lines (81 loc) · 3.66 KB
/
push_relabel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@testset "Push relabel" begin
# Construct DiGraph
flow_graph = lg.DiGraph(8)
# Load custom dataset
flow_edges = [
(1, 2, 10), (1, 3, 5), (1, 4, 15), (2, 3, 4), (2, 5, 9),
(2, 6, 15), (3, 4, 4), (3, 6, 8), (4, 7, 16), (5, 6, 15),
(5, 8, 10), (6, 7, 15), (6, 8, 10), (7, 3, 6), (7, 8, 10)
]
capacity_matrix = zeros(Int, 8, 8)
for e in flow_edges
u, v, f = e
lg.add_edge!(flow_graph, u, v)
capacity_matrix[u, v] = f
end
for g in testdigraphs(flow_graph)
residual_graph = @inferred(LightGraphsFlows.residual(g))
# Test enqueue_vertex
Q = Array{Int,1}()
excess = [0, 1, 0, 1]
active = [false, false, true, true]
@test @inferred(LightGraphsFlows.enqueue_vertex!(Q, 1, active, excess)) == nothing
@test @inferred(LightGraphsFlows.enqueue_vertex!(Q, 3, active, excess)) == nothing
@test @inferred(LightGraphsFlows.enqueue_vertex!(Q, 4, active, excess)) == nothing
@test length(Q) == 0
@test @inferred(LightGraphsFlows.enqueue_vertex!(Q, 2, active, excess)) == nothing
@test length(Q) == 1
# Test push_flow
Q = Array{Int,1}()
excess = [15, 1, 1, 0, 0, 0, 0, 0]
height = [8, 0, 0, 0, 0, 0, 0, 0]
active = [true, false, false, false, false, false, false, true]
flow_matrix = zeros(Int, 8, 8)
@test @inferred(LightGraphsFlows.push_flow!(residual_graph, 1, 2, capacity_matrix, flow_matrix, excess, height, active, Q)) == nothing
@test length(Q) == 1
@test flow_matrix[1, 2] == 10
@test @inferred(LightGraphsFlows.push_flow!(residual_graph, 2, 3, capacity_matrix, flow_matrix, excess, height, active, Q)) == nothing
@test length(Q) == 1
@test flow_matrix[2, 3] == 0
# Test gap
Q = Array{Int,1}()
excess = [15, 1, 1, 0, 0, 0, 0, 0]
height = [8, 2, 2, 1, 3, 3, 4, 5]
active = [true, false, false, false, false, false, false, true]
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(LightGraphsFlows.gap!(residual_graph, 1, excess, height, active, count, Q)) == nothing
@test length(Q) == 2
# Test relabel
Q = Array{Int,1}()
excess = [15, 1, 1, 0, 0, 0, 0, 0]
height = [8, 1, 1, 1, 1, 1, 1, 0]
active = [true, false, false, false, false, false, false, true]
count = [1, 6, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
flow_matrix = zeros(Int, 8, 8)
@test @inferred(LightGraphsFlows.relabel!(residual_graph, 2, capacity_matrix, flow_matrix, excess, height, active, count, Q)) == nothing
@test length(Q) == 1
# Test discharge
Q = Array{Int,1}()
excess = [50, 1, 1, 0, 0, 0, 0, 0]
height = [8, 0, 0, 0, 0, 0, 0, 0]
active = [true, false, false, false, false, false, false, true]
count = [7, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
flow_matrix = zeros(Int, 8, 8)
@test @inferred(LightGraphsFlows.discharge!(residual_graph, 1, capacity_matrix, flow_matrix, excess, height, active, count, Q)) == nothing
@test length(Q) == 3
# Test with default distances
@test LightGraphsFlows.push_relabel(residual_graph, 1, 8, LightGraphsFlows.DefaultCapacity(residual_graph))[1] == 3
# Test with capacity matrix
@test LightGraphsFlows.push_relabel(residual_graph, 1, 8, capacity_matrix)[1] == 28
end
# Non regression test added for #448
M448 = [0 1 0 0 1 1
1 0 0 0 1 0
0 0 0 1 0 0
0 0 0 0 0 0
1 0 1 0 0 1
0 0 0 0 1 0]
g448 = lg.DiGraph(M448)
@test maximum_flow(g448, 1, 2, M448, algorithm=PushRelabelAlgorithm())[1] == 1
end