algorithm FF fini
This commit is contained in:
parent
5a5093cd5e
commit
be91217e12
5 changed files with 63 additions and 34 deletions
|
@ -1,5 +1,7 @@
|
|||
open Graph
|
||||
|
||||
type path = id list
|
||||
|
||||
(*type record avec id noeud et son cout*)
|
||||
type t_cost={
|
||||
mutable cout:int;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
open Graph
|
||||
|
||||
type path = id list
|
||||
|
||||
type t_cost={
|
||||
mutable cout:int;
|
||||
mutable father:int
|
||||
|
@ -7,4 +9,4 @@ type t_cost={
|
|||
|
||||
val blf: int graph -> id -> id -> t_cost array
|
||||
|
||||
val get_path: int graph -> id -> id -> int list option
|
||||
val get_path: int graph -> id -> id -> path option
|
|
@ -1,7 +1,12 @@
|
|||
open Graph
|
||||
open Tool
|
||||
open BLF
|
||||
|
||||
type path = id list
|
||||
let g_to_string gr = gmap gr string_of_int
|
||||
let g_to_int gr = gmap gr int_of_string
|
||||
|
||||
let find_path (graph : int graph) (forbidden : id list) (id1 : id) (id2 : id) =
|
||||
None
|
||||
|
||||
(* Create a list of pairs (origin,end) from a list of nodes *)
|
||||
let rec create_arcs_from_nodes = function
|
||||
|
@ -14,13 +19,15 @@ let rec create_arcs_from_nodes = function
|
|||
(* Return the minimum value of a path's arcs*)
|
||||
let get_min_label_from_path (graph : int graph) (path : (id * id) list) =
|
||||
let min = Some 999 in
|
||||
List.fold_left
|
||||
let min = List.fold_left
|
||||
(
|
||||
fun acu (id1, id2) ->
|
||||
let label = find_arc graph id1 id2 in
|
||||
if label < acu then label else acu
|
||||
)
|
||||
min path
|
||||
) min path in
|
||||
match min with
|
||||
|None -> 999
|
||||
|Some x -> x
|
||||
|
||||
|
||||
(* Add a value to every arc of a path *)
|
||||
|
@ -31,7 +38,7 @@ let add_value_to_arcs (graph : int graph) (path : (id * id) list) (value : int)
|
|||
add_arc acu id1 id2 value
|
||||
)
|
||||
graph path
|
||||
|
||||
|
||||
|
||||
(* Reverse a path and its arc
|
||||
ex :[(a, b);(b, c)] -> [(b,a);(c, b)] *)
|
||||
|
@ -45,8 +52,8 @@ let rev_arcs (path : (id * id) list) =
|
|||
let get_final_graph (initGraph : int graph) (residualGraph : int graph) =
|
||||
|
||||
(* First get the initial and residual graph as string graphs *)
|
||||
let initGraphString = gmap initGraph string_of_int in
|
||||
let residualGraphString = gmap residualGraph string_of_int in
|
||||
let initGraphString = g_to_string initGraph in
|
||||
let residualGraphString = g_to_string residualGraph in
|
||||
let finalGraph = clone_nodes initGraph in
|
||||
|
||||
(* For every arc in the initial graph, we get its label (aka max_capacity)
|
||||
|
@ -68,30 +75,37 @@ let get_final_graph (initGraph : int graph) (residualGraph : int graph) =
|
|||
)
|
||||
finalGraph
|
||||
|
||||
|
||||
|
||||
(*let fordFulkAlgorithm graph origin end = assert false *)
|
||||
(*
|
||||
let ford_fulk_algorithm graph origin sink =
|
||||
let flow = 0 in
|
||||
While there's a path
|
||||
Find a path
|
||||
let path = xxxxx graph origin end [] in
|
||||
let arcs = create_arcs_from_nodes path in
|
||||
let initGraph = graph in
|
||||
let rec boucle graph origin sink flow =
|
||||
|
||||
let path = get_path graph origin sink in
|
||||
match path with
|
||||
|None -> (flow, graph)
|
||||
|Some x ->
|
||||
(let path = x in
|
||||
let arcs = create_arcs_from_nodes path in
|
||||
|
||||
(*let () = printf "dans boucle\n" in*)
|
||||
|
||||
(* Find the min value of the path *)
|
||||
let min = get_min_label_from_path graph arcs in
|
||||
|
||||
(* Substract the min to every arc of the path *)
|
||||
let graph = add_value_to_arcs graph arcs (-min) in
|
||||
|
||||
Find the min value of the path
|
||||
let min = get_min_label_from_path graph arcs in
|
||||
|
||||
Substract the min to every arc of the path
|
||||
graph = add_value_to_arcs graph arcs (-min) in
|
||||
|
||||
Get the reverse path
|
||||
let reverse = rev_arcs arcs in
|
||||
|
||||
Add the min to every arc of the reverse path
|
||||
graph = add_value_to_arcs graph reverse min in
|
||||
|
||||
Add the min to the flow
|
||||
flow = flow + min
|
||||
|
||||
return the flow
|
||||
*)
|
||||
(* Get the reverse path *)
|
||||
let reverse = rev_arcs arcs in
|
||||
|
||||
(* Add the min to every arc of the reverse path *)
|
||||
let graph = add_value_to_arcs graph reverse min in
|
||||
|
||||
(* Add the min to the flow *)
|
||||
let flow = flow + min in
|
||||
boucle graph origin sink flow) in
|
||||
let (maxFlow, residualGraph) = boucle graph origin sink flow in
|
||||
let finalGraph = get_final_graph initGraph residualGraph in
|
||||
(maxFlow, finalGraph)
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
open Graph
|
||||
open Tool
|
||||
open BLF
|
||||
|
||||
type path = id list
|
||||
|
||||
val g_to_int: string graph -> int graph
|
||||
|
||||
val ford_fulk_algorithm : int graph -> id -> id -> (int * string graph)
|
||||
|
||||
(* for testing purpose *)
|
||||
|
||||
|
|
10
src/ftest.ml
10
src/ftest.ml
|
@ -2,8 +2,13 @@ open Gfile
|
|||
open Tool
|
||||
open FFAlgorithm
|
||||
open BLF
|
||||
open Format
|
||||
|
||||
let () =
|
||||
|
||||
(*/!\ Format de la commande pour lancer le test :
|
||||
./ftest.native [nom_fichier_lecture] [id_source] [id_dest] [nom_fichier_ecriture]
|
||||
ex : ./ftest.native graphs/graph1 0 5 graphs/graph3 *)
|
||||
|
||||
(* Check the number of command-line arguments *)
|
||||
if Array.length Sys.argv <> 5 then
|
||||
|
@ -25,9 +30,12 @@ let () =
|
|||
|
||||
(* Open file *)
|
||||
let graph = from_file infile in
|
||||
let initGraph = g_to_int graph in
|
||||
|
||||
(* Rewrite the graph that has been read. *)
|
||||
let () = write_file outfile graph in
|
||||
let () = export outfile graph in
|
||||
let (flow,finalGraph) = ford_fulk_algorithm initGraph _source _sink in
|
||||
let () = printf "%d\n" flow in
|
||||
let () = export outfile finalGraph in
|
||||
()
|
||||
|
||||
|
|
Loading…
Reference in a new issue