| 1 |
(* |
| 2 |
* This module implements max (s,t) flow. |
| 3 |
* |
| 4 |
* -- Allen |
| 5 |
*) |
| 6 |
|
| 7 |
functor MaxFlow(Num : ABELIAN_GROUP) : MAX_FLOW = |
| 8 |
struct |
| 9 |
structure G = Graph |
| 10 |
structure A = Array |
| 11 |
structure Num = Num |
| 12 |
|
| 13 |
(* |
| 14 |
* Use Goldberg's preflow-push approach. |
| 15 |
* This algorithm is presented in the book by Cormen, Leiserson and Rivest. |
| 16 |
*) |
| 17 |
fun max_flow{graph=G.GRAPH G, s, t, capacity, flows} = |
| 18 |
let val _ = if s = t then raise G.Graph "maxflow" else () |
| 19 |
val N = #capacity G () |
| 20 |
val M = #order G () |
| 21 |
val neighbors = A.array(N,[]) |
| 22 |
val zero = Num.zero |
| 23 |
val dist = A.array(N,0) |
| 24 |
val excess = A.array(N,zero) |
| 25 |
val current = A.array(N,[]) |
| 26 |
|
| 27 |
fun min(a,b) = if Num.<(a,b) then a else b |
| 28 |
fun isZero a = Num.==(a,zero) |
| 29 |
val ~ = Num.~ |
| 30 |
|
| 31 |
fun initialize_preflow() = |
| 32 |
let fun add_edge (e as (u,_,_)) = |
| 33 |
A.update(neighbors,u,e::A.sub(neighbors,u)) |
| 34 |
in #forall_edges G (fn e as (u,v,e') => |
| 35 |
let val c = capacity e |
| 36 |
in if u = s then |
| 37 |
let val f = ref c and f' = ref(~ c) |
| 38 |
in add_edge (u,v,(f,c,f',true,e')); |
| 39 |
add_edge (v,u,(f',zero,f,false,e')); |
| 40 |
A.update(excess,v,Num.+(c,A.sub(excess,v))) |
| 41 |
end |
| 42 |
else |
| 43 |
let val f = ref zero and f' = ref zero |
| 44 |
in add_edge (u,v,(f,c,f',true,e')); |
| 45 |
add_edge (v,u,(f',zero,f,false,e')) |
| 46 |
end |
| 47 |
end); |
| 48 |
A.update(dist,s,M) |
| 49 |
end |
| 50 |
|
| 51 |
(* |
| 52 |
* Push d_f(u,v) = min(e[u],c(u,v)) units of flow from u to v |
| 53 |
* Returns the new e_u |
| 54 |
*) |
| 55 |
fun push(e_u,(u,v,(flow,cap,flow',x,_))) = |
| 56 |
let val c_f = Num.-(cap,!flow) |
| 57 |
val d_f = min(e_u,c_f) |
| 58 |
val e_v = A.sub(excess,v) |
| 59 |
in flow := Num.+(!flow,d_f); |
| 60 |
flow' := ~(!flow); |
| 61 |
A.update(excess,v,Num.+(e_v,d_f)); |
| 62 |
Num.-(e_u,d_f) |
| 63 |
end |
| 64 |
|
| 65 |
(* Lift a vertex |
| 66 |
* dist[v] := 1 + min{ dist[w] | (v,w) \in E_f } |
| 67 |
* Returns the new dist[v] |
| 68 |
*) |
| 69 |
fun lift(v) = |
| 70 |
let fun loop([],d_v) = d_v |
| 71 |
| loop((v,w,(f,c,_,_,_))::es,d_v) = |
| 72 |
if Num.<(!f,c) then loop(es,Int.min(A.sub(dist,w),d_v)) |
| 73 |
else loop(es,d_v) |
| 74 |
val d_v = loop(A.sub(neighbors,v),1000000000) + 1 |
| 75 |
in A.update(dist,v,d_v); |
| 76 |
d_v |
| 77 |
end |
| 78 |
|
| 79 |
(* |
| 80 |
* Push all excess flow thru admissible edges to neighboring vertices |
| 81 |
* until all excess flow has been discharged. |
| 82 |
*) |
| 83 |
fun discharge(v) = |
| 84 |
let val e_v = A.sub(excess,v) |
| 85 |
in if isZero e_v then false |
| 86 |
else |
| 87 |
let fun loop(d_v,e_v,(e as (v,w,(f,c,_,_,_)))::es) = |
| 88 |
if Num.<(!f,c) andalso d_v = A.sub(dist,w) + 1 then |
| 89 |
let val e_v = push(e_v,e) |
| 90 |
in if isZero e_v then (d_v,es) |
| 91 |
else loop(d_v,e_v,es) |
| 92 |
end |
| 93 |
else loop(d_v,e_v,es) |
| 94 |
| loop(_,e_v,[]) = loop(lift(v),e_v,A.sub(neighbors,v)) |
| 95 |
val d_v = A.sub(dist,v) |
| 96 |
val (d_v',es) = loop(d_v,e_v,A.sub(current,v)) |
| 97 |
in A.update(excess,v,zero); (* e[v] must be zero *) |
| 98 |
A.update(current,v,es); |
| 99 |
d_v <> d_v' |
| 100 |
end |
| 101 |
end |
| 102 |
|
| 103 |
fun lift_to_front() = |
| 104 |
(initialize_preflow(); |
| 105 |
iterate([], |
| 106 |
List.foldr(fn ((u,_),L) => |
| 107 |
if u = s orelse u = t then L |
| 108 |
else (A.update(current,u,A.sub(neighbors,u)); u::L)) [] |
| 109 |
(#nodes G ())) |
| 110 |
) |
| 111 |
|
| 112 |
and iterate(_,[]) = () |
| 113 |
| iterate(F,u::B) = |
| 114 |
if discharge(u) then iterate([u],rev F@B) |
| 115 |
else iterate(u::F,B) |
| 116 |
|
| 117 |
in lift_to_front(); |
| 118 |
#forall_nodes G (fn (i,_) => |
| 119 |
app (fn (i,j,(f,_,_,x,e')) => |
| 120 |
if x then flows((i,j,e'),!f) else ()) |
| 121 |
(A.sub(neighbors,i))); |
| 122 |
List.foldr (fn ((_,_,(f,_,_,_,_)),n) => Num.+(!f,n)) zero |
| 123 |
(A.sub(neighbors,s)) |
| 124 |
end |
| 125 |
|
| 126 |
fun min_cost_max_flow{graph=G.GRAPH G, s, t, capacity, cost, flows} = |
| 127 |
raise Graph.Unimplemented |
| 128 |
|
| 129 |
end |