int * int graph changes

This commit is contained in:
Leonie Gallois 2020-12-06 13:55:35 +01:00
parent a746f65f34
commit 7b770156bc
3 changed files with 21 additions and 21 deletions

View file

@ -15,20 +15,20 @@ let rec create_arcs_from_nodes = function
(* Return the minimum value of a path's edge*) (* Return the minimum value of a path's edge*)
let get_min_label_from_path (graph : float graph) (path : (id * id) list) = let get_min_label_from_path (graph : (int * int) graph) (path : (id * id) list) =
let min = 999999999.0 in let min = 999999999 in
List.fold_left List.fold_left
( (
fun acu (id1, id2) -> fun acu (id1, id2) ->
let label = ( match find_arc graph id1 id2 with let label = ( match find_arc graph id1 id2 with
|None -> 999999999 |None -> 999999999
|Some x -> x) in |Some (cout,capa) -> cout) in
if label < acu then label else acu if label < acu then label else acu
) min path ) min path
(* Add a value to every egde of a path *) (* Add a value to every egde of a path *)
let add_value_to_arcs (graph : float graph) (path : (id * id) list) (value : float) = let add_value_to_arcs (graph : (int * int) graph) (path : (id * id) list) (value : int) =
List.fold_left List.fold_left
( (
fun acu (id1, id2) -> fun acu (id1, id2) ->
@ -46,7 +46,7 @@ let rev_arcs (path : (id * id) list) =
(* Get the final graph after the FFalgorithm (* Get the final graph after the FFalgorithm
The label of every arc becomes "x" where x The label of every arc becomes "x" where x
is the value of the opposite arc on the residual graph*) is the value of the opposite arc on the residual graph*)
let get_final_graph (initGraph : float graph) (residualGraph : float graph) = let get_final_graph (initGraph : (int * int) graph) (residualGraph : (int * int) graph) =
(* First get the initial and residual graph as string graphs *) (* First get the initial and residual graph as string graphs *)
let initGraphString = initGraph in let initGraphString = initGraph in
@ -56,23 +56,23 @@ let get_final_graph (initGraph : float graph) (residualGraph : float graph) =
(* For every arc in the initial graph we get the label of (* For every arc in the initial graph we get the label of
the opposite arc in the residual graph. the opposite arc in the residual graph.
If it exists then the arc of the final graph gets the label "x", If it exists then the arc of the final graph gets the label "x",
"0.0" otherwise*) "0" otherwise*)
e_fold initGraph e_fold initGraph
( (
fun acu id1 id2 x -> fun acu id1 id2 (cout_x,capa_x) ->
let label_rev_arc = match find_arc residualGraphString id2 id1 with let label_rev_arc = match find_arc residualGraphString id2 id1 with
|None -> 0.0 |None -> 0
|Some x -> (match find_arc initGraphString id2 id1 with |Some (cout_x,capa_x) -> (match find_arc initGraphString id2 id1 with
|None -> x |None -> cout_x
|Some y -> Float.sub x y ) in |Some (cout_y, capa_y) -> Int.sub cout_x cout_y ) in
let label_rev_arc = if (label_rev_arc > 0.0) then (string_of_float label_rev_arc) else "0" in let label_rev_arc = if (label_rev_arc > 0) then ((string_of_int label_rev_arc),(string_of_int capa_x )) else "cout:0,capa:"^(string_of_int capa_x) in
new_arc acu id1 id2 label_rev_arc new_arc acu id1 id2 (label_rev_arc, capa_x)
) )
finalGraph finalGraph
let ford_fulk_algorithm (graph : float graph) (origin : id) (sink : id) = let ford_fulk_algorithm (graph : (int * int) graph) (origin : id) (sink : id) =
let flow = 0.0 in let flow = 0 in
let initGraph = graph in let initGraph = graph in
let rec boucle graph origin sink flow = let rec boucle graph origin sink flow =
@ -88,7 +88,7 @@ let ford_fulk_algorithm (graph : float graph) (origin : id) (sink : id) =
let min = get_min_label_from_path graph arcs in let min = get_min_label_from_path graph arcs in
(* Substract the min to every arc of the path *) (* Substract the min to every arc of the path *)
let graph = add_value_to_arcs graph arcs (Float.neg min) in let graph = add_value_to_arcs graph arcs (Int.neg min) in
(* Get the reverse path *) (* Get the reverse path *)
let reverse = rev_arcs arcs in let reverse = rev_arcs arcs in
@ -97,7 +97,7 @@ let ford_fulk_algorithm (graph : float graph) (origin : id) (sink : id) =
let graph = add_value_to_arcs graph reverse min in let graph = add_value_to_arcs graph reverse min in
(* Add the min to the flow *) (* Add the min to the flow *)
let flow = Float.add flow min in let flow = Int.add flow min in
boucle graph origin sink flow) in boucle graph origin sink flow) in
let (maxFlow, residualGraph) = boucle graph origin sink flow in let (maxFlow, residualGraph) = boucle graph origin sink flow in
let finalGraph = get_final_graph initGraph residualGraph in let finalGraph = get_final_graph initGraph residualGraph in

View file

@ -12,11 +12,11 @@ let gmap gr f =
let new_graph = clone_nodes gr in let new_graph = clone_nodes gr in
e_fold gr (fun acu id1 id2 x -> new_arc acu id1 id2 (f x)) new_graph e_fold gr (fun acu id1 id2 x -> new_arc acu id1 id2 (f x)) new_graph
let add_arc g id1 id2 n = let add_arc g id1 id2 cout =
let f = find_arc g id1 id2 in let f = find_arc g id1 id2 in
match f with match f with
|None->new_arc g id1 id2 n |None->new_arc g id1 id2 (cout,1)
|Some x->new_arc g id1 id2 (Int.add n x) |Some x->new_arc g id1 id2 ((Int.add cout x),1)
let get_max_id graph = let get_max_id graph =
n_fold graph (fun acu id -> max id acu) 0 n_fold graph (fun acu id -> max id acu) 0

View file

@ -6,6 +6,6 @@ val clone_nodes: 'a graph -> 'b graph
(* Apply a function f to every label of the graph's arcs *) (* Apply a function f to every label of the graph's arcs *)
val gmap: 'a graph -> ('a -> 'b) -> 'b graph val gmap: 'a graph -> ('a -> 'b) -> 'b graph
val add_arc: (int * int) graph -> id -> id -> float -> (int * int) graph val add_arc: (int * int) graph -> id -> id -> int -> (int * int) graph
val get_max_id : 'a graph -> id val get_max_id : 'a graph -> id