This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
/
boykov_kolmogorov.jl
200 lines (174 loc) · 6.7 KB
/
boykov_kolmogorov.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
boykov_kolmogorov_impl(residual_graph, source, target, capacity_matrix)
Compute the max-flow/min-cut between `source` and `target` for `residual_graph`
using the Boykov-Kolmogorov algorithm.
Return the maximum flow in the network, the flow matrix and the partition
`{S,T}` in the form of a vector of 0's, 1's and 2's.
### References
- BOYKOV, Y.; KOLMOGOROV, V., 2004. An Experimental Comparison of
Min-Cut/Max-Flow Algorithms for Energy Minimization in Vision.
### Author
- Júlio Hoffimann Mendes (juliohm@stanford.edu)
"""
function boykov_kolmogorov_impl end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function boykov_kolmogorov_impl(
residual_graph::AG::lg.IsDirected, # the input graph
source::Integer, # the source vertex
target::Integer, # the target vertex
capacity_matrix::AbstractMatrix{T} # edge flow capacities
) where {T, U, AG<:lg.AbstractGraph{U}}
n = lg.nv(residual_graph)
flow = 0
flow_matrix = zeros(T, n, n)
TREE = zeros(U, n)
TREE[source] = U(1)
TREE[target] = U(2)
PARENT = zeros(U, n)
A = [source, target]
O = Vector{U}()
while true
# growth stage
path = find_path!(residual_graph, source, target, flow_matrix, capacity_matrix, PARENT, TREE, A)
isempty(path) && break
# augmentation stage
flow += augment!(path, flow_matrix, capacity_matrix, PARENT, TREE, O)
# adoption stage
adopt!(residual_graph, source, target, flow_matrix, capacity_matrix, PARENT, TREE, A, O)
end
return flow, flow_matrix, TREE
end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function find_path!(
residual_graph::AG::lg.IsDirected, # the input graph
source::Integer, # the source vertex
target::Integer, # the target vertex
flow_matrix::AbstractMatrix, # the current flow matrix
capacity_matrix::AbstractMatrix, # edge flow capacities
PARENT::Vector, # parent table
TREE::Vector, # tree table
A::Vector # active set
) where {T, AG<:lg.AbstractGraph{T}}
tree_cap(p, q) = TREE[p] == one(T) ? capacity_matrix[p, q] - flow_matrix[p, q] :
capacity_matrix[q, p] - flow_matrix[q, p]
while !isempty(A)
p = last(A)
for q in lg.neighbors(residual_graph, p)
if tree_cap(p, q) > 0
if TREE[q] == zero(T)
TREE[q] = TREE[p]
PARENT[q] = p
pushfirst!(A, q)
end
if TREE[q] ≠ zero(T) && TREE[q] ≠ TREE[p]
# p -> source
path_to_source = [p]
while PARENT[p] ≠ zero(T)
p = PARENT[p]
push!(path_to_source, p)
end
# q -> target
path_to_target = [q]
while PARENT[q] ≠ zero(T)
q = PARENT[q]
push!(path_to_target, q)
end
# source -> target
path = [reverse!(path_to_source); path_to_target]
if path[1] == source && path[end] == target
return path
elseif path[1] == target && path[end] == source
return reverse!(path)
end
end
end
end
pop!(A)
end
return Vector{T}()
end
function augment!(
path::AbstractVector, # path from source to target
flow_matrix::AbstractMatrix, # the current flow matrix
capacity_matrix::AbstractMatrix, # edge flow capacities
PARENT::Vector, # parent table
TREE::Vector, # tree table
O::Vector # orphan set
)
T = eltype(path)
# bottleneck capacity
Δ = Inf
for i = 1:(length(path) - 1)
p, q = path[i:(i + 1)]
cap = capacity_matrix[p, q] - flow_matrix[p, q]
cap < Δ && (Δ = cap)
end
# update residual graph
for i = 1:(length(path) - 1)
p, q = path[i:(i + 1)]
flow_matrix[p, q] += Δ
flow_matrix[q, p] -= Δ
# create orphans
if flow_matrix[p, q] == capacity_matrix[p, q]
if TREE[p] == TREE[q] == one(T)
PARENT[q] = zero(T)
pushfirst!(O, q)
end
if TREE[p] == TREE[q] == 2
PARENT[p] = zero(T)
pushfirst!(O, p)
end
end
end
return Δ
end
@traitfn function adopt!(
residual_graph::AG::lg.IsDirected, # the input graph
source::Integer, # the source vertex
target::Integer, # the target vertex
flow_matrix::AbstractMatrix, # the current flow matrix
capacity_matrix::AbstractMatrix, # edge flow capacities
PARENT::Vector, # parent table
TREE::Vector, # tree table
A::Vector, # active set
O::Vector # orphan set
) where {T, AG<:lg.AbstractGraph{T}}
tree_cap(p, q) = TREE[p] == 1 ? capacity_matrix[p, q] - flow_matrix[p, q] :
capacity_matrix[q, p] - flow_matrix[q, p]
while !isempty(O)
p = pop!(O)
# try to find parent that is not an orphan
parent_found = false
for q in lg.neighbors(residual_graph, p)
if TREE[q] == TREE[p] && tree_cap(q, p) > 0
# check if "origin" is either source or target
o = q
while PARENT[o] ≠ 0
o = PARENT[o]
end
if o == source || o == target
parent_found = true
PARENT[p] = q
break
end
end
end
if !parent_found
# scan all neighbors and make the orphan a free node
for q in lg.neighbors(residual_graph, p)
if TREE[q] == TREE[p]
if tree_cap(q, p) > 0
pushfirst!(A, q)
end
if PARENT[q] == p
PARENT[q] = zero(T)
pushfirst!(O, q)
end
end
end
TREE[p] = zero(T)
B = setdiff(A, p)
resize!(A, length(B))[:] = B
end
end
end