62
53
let fold_booleen f = List.fold_left (fun r x->(f x) or r) false
64
let capacite_restante g e = F.sub (F.max_capacity (G.E.label e)) (E.find flot (G.E.src e, G.E.dst e))
55
let capacite_restante g e =
56
F.sub (F.max_capacity (G.E.label e)) (E.find flot (G.E.src e, G.E.dst e))
66
58
let reste_excedent x = F.compare (V.find excedents x) F.zero > 0
68
60
let flux_et_reflux g x =
70
(fun e s->if F.compare (capacite_restante g e) (F.min_capacity (G.E.label e)) > 0 then e::s else s)
65
(capacite_restante g e) (F.min_capacity (G.E.label e))
75
if F.compare (E.find flot (G.E.src e, G.E.dst e)) (F.min_capacity (G.E.label e)) > 0
76
then (G.E.create (G.E.dst e) (G.E.label e) (G.E.src e))::s else s)
73
(E.find flot (G.E.src e, G.E.dst e)) (F.min_capacity (G.E.label e))
75
then (G.E.create (G.E.dst e) (G.E.label e) (G.E.src e))::s else s)
79
78
let pousser g e l =
80
let x,y = G.E.src e , G.E.dst e in
79
let x, y = G.E.src e, G.E.dst e in
81
80
let ex = V.find excedents x in
82
81
let cxy = capacite_restante g e in
83
if F.compare ex F.zero > 0 &&
84
F.compare cxy (F.min_capacity (G.E.label e)) >0 &&
85
V.find hauteur x = (V.find hauteur y + 1)
87
let d = if F.compare ex cxy <0 then ex else cxy in
88
let fxy = E.find flot (x,y) in
89
let ex = V.find excedents x in
90
let ey = V.find excedents y in
91
E.replace flot (x,y) (F.add fxy d);
92
E.replace flot (y,x) (F.sub F.zero (F.add fxy d));
93
V.replace excedents x (F.sub ex d);
94
V.replace excedents y (F.add ey d);
95
if reste_excedent x then l:=Sv.add x !l;
96
if reste_excedent y then l:=Sv.add y !l;
99
(if F.compare ex F.zero > 0 then l:=Sv.add x !l; false)
82
if F.compare ex F.zero > 0 &&
83
F.compare cxy (F.min_capacity (G.E.label e)) > 0 &&
84
V.find hauteur x = (V.find hauteur y + 1)
86
let d = if F.compare ex cxy < 0 then ex else cxy in
87
let fxy = E.find flot (x,y) in
88
let ex = V.find excedents x in
89
let ey = V.find excedents y in
90
E.replace flot (x,y) (F.add fxy d);
91
E.replace flot (y,x) (F.sub F.zero (F.add fxy d));
92
V.replace excedents x (F.sub ex d);
93
V.replace excedents y (F.add ey d);
94
if reste_excedent x then l:=Sv.add x !l;
95
if reste_excedent y then l:=Sv.add y !l;
98
(if F.compare ex F.zero > 0 then l:=Sv.add x !l;
101
101
let elever g p x =
102
102
let u = flux_et_reflux g x in
104
&& not (G.V.equal x p)
105
&& List.for_all (fun e -> (V.find hauteur (G.E.src e)) <= (V.find hauteur (G.E.dst e))) u
106
&& (let min = List.fold_left (fun m e-> min (V.find hauteur (G.E.dst e)) m) max_int u in
107
V.replace hauteur x (1+min); true)
104
&& not (G.V.equal x p)
107
(fun e -> (V.find hauteur (G.E.src e)) <= (V.find hauteur (G.E.dst e))) u
110
List.fold_left (fun m e -> min (V.find hauteur (G.E.dst e)) m) max_int u
112
V.replace hauteur x (1+min);
109
115
let init_preflot g s p =
110
116
G.iter_vertex (fun x -> V.add excedents x F.zero; V.add hauteur x 0) g;
113
119
let x,y = G.E.src e, G.E.dst e in
114
E.add flot (x,y) (F.flow (G.E.label e));
115
E.add flot (y,x) (F.sub F.zero (F.flow (G.E.label e))))
120
E.add flot (x,y) (F.flow (G.E.label e));
121
E.add flot (y,x) (F.sub F.zero (F.flow (G.E.label e))))
117
123
V.add hauteur s (G.nb_vertex g);
120
126
let y = G.E.dst e in
121
let c = F.max_capacity (G.E.label e) in
123
E.add flot (y,s) (F.sub F.zero c);
127
let c = F.max_capacity (G.E.label e) in
129
E.add flot (y,s) (F.sub F.zero c);
128
134
let maxflow g s p =
131
137
or G.fold_pred_e (fun e r->pousser g e l or r) g x false
133
139
let todo = ref (init_preflot g s p) in
135
(fold_booleen (elever g p) !todo) or
136
(let l = ref Sv.empty in
137
let r = fold_booleen (push_and_pull l) !todo in
138
todo:=Sv.elements !l; r)
141
G.fold_pred_e (fun e f -> F.add (E.find flot (G.E.src e,p)) f) g p F.zero
144
G.fold_pred_e (fun e f -> F.add (F.flow (G.E.label e)) f) g p F.zero
147
let x,y = G.E.src e, G.E.dst e in
148
try E.find flot (x,y)
149
with Not_found -> F.flow (G.E.label e)
151
f, F.sub flot_max flot_init
141
(fold_booleen (elever g p) !todo) or
142
(let l = ref Sv.empty in
143
let r = fold_booleen (push_and_pull l) !todo in
144
todo:=Sv.elements !l; r)
147
G.fold_pred_e (fun e f -> F.add (E.find flot (G.E.src e,p)) f) g p F.zero
150
G.fold_pred_e (fun e f -> F.add (F.flow (G.E.label e)) f) g p F.zero
153
let x,y = G.E.src e, G.E.dst e in
154
try E.find flot (x,y)
155
with Not_found -> F.flow (G.E.label e)
157
f, F.sub flot_max flot_init