algorithm FF fini

This commit is contained in:
Kevin Cavailles 2020-11-15 14:17:19 +01:00
parent 5a5093cd5e
commit be91217e12
5 changed files with 63 additions and 34 deletions

View file

@ -1,5 +1,7 @@
open Graph open Graph
type path = id list
(*type record avec id noeud et son cout*) (*type record avec id noeud et son cout*)
type t_cost={ type t_cost={
mutable cout:int; mutable cout:int;

View file

@ -1,5 +1,7 @@
open Graph open Graph
type path = id list
type t_cost={ type t_cost={
mutable cout:int; mutable cout:int;
mutable father:int mutable father:int
@ -7,4 +9,4 @@ type t_cost={
val blf: int graph -> id -> id -> t_cost array 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

View file

@ -1,7 +1,12 @@
open Graph open Graph
open Tool 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 *) (* Create a list of pairs (origin,end) from a list of nodes *)
let rec create_arcs_from_nodes = function 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*) (* Return the minimum value of a path's arcs*)
let get_min_label_from_path (graph : int graph) (path : (id * id) list) = let get_min_label_from_path (graph : int graph) (path : (id * id) list) =
let min = Some 999 in let min = Some 999 in
List.fold_left let min = List.fold_left
( (
fun acu (id1, id2) -> fun acu (id1, id2) ->
let label = find_arc graph id1 id2 in let label = find_arc graph id1 id2 in
if label < acu then label else acu if label < acu then label else acu
) ) min path in
min path match min with
|None -> 999
|Some x -> x
(* Add a value to every arc of a path *) (* 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 add_arc acu id1 id2 value
) )
graph path graph path
(* Reverse a path and its arc (* Reverse a path and its arc
ex :[(a, b);(b, c)] -> [(b,a);(c, b)] *) 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) = let get_final_graph (initGraph : int graph) (residualGraph : int graph) =
(* First get the initial and residual graph as string graphs *) (* First get the initial and residual graph as string graphs *)
let initGraphString = gmap initGraph string_of_int in let initGraphString = g_to_string initGraph in
let residualGraphString = gmap residualGraph string_of_int in let residualGraphString = g_to_string residualGraph in
let finalGraph = clone_nodes initGraph in let finalGraph = clone_nodes initGraph in
(* For every arc in the initial graph, we get its label (aka max_capacity) (* 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 finalGraph
let ford_fulk_algorithm graph origin sink =
(*let fordFulkAlgorithm graph origin end = assert false *)
(*
let flow = 0 in let flow = 0 in
While there's a path let initGraph = graph in
Find a path let rec boucle graph origin sink flow =
let path = xxxxx graph origin end [] in
let arcs = create_arcs_from_nodes path in 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 (* Get the reverse path *)
graph = add_value_to_arcs graph arcs (-min) in let reverse = rev_arcs arcs in
Get the reverse path (* Add the min to every arc of the reverse path *)
let reverse = rev_arcs arcs in let graph = add_value_to_arcs graph reverse min in
Add the min to every arc of the reverse path (* Add the min to the flow *)
graph = add_value_to_arcs graph reverse min in let flow = flow + min in
boucle graph origin sink flow) in
Add the min to the flow let (maxFlow, residualGraph) = boucle graph origin sink flow in
flow = flow + min let finalGraph = get_final_graph initGraph residualGraph in
(maxFlow, finalGraph)
return the flow
*)

View file

@ -1,8 +1,11 @@
open Graph open Graph
open Tool 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 *) (* for testing purpose *)

View file

@ -2,8 +2,13 @@ open Gfile
open Tool open Tool
open FFAlgorithm open FFAlgorithm
open BLF open BLF
open Format
let () = 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 *) (* Check the number of command-line arguments *)
if Array.length Sys.argv <> 5 then if Array.length Sys.argv <> 5 then
@ -25,9 +30,12 @@ let () =
(* Open file *) (* Open file *)
let graph = from_file infile in let graph = from_file infile in
let initGraph = g_to_int graph in
(* Rewrite the graph that has been read. *) (* Rewrite the graph that has been read. *)
let () = write_file outfile graph in 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
() ()