Make function that finds max flow also take current flow into account
This commit is contained in:
parent
2442e5786b
commit
4d00af25ee
6 changed files with 90 additions and 21 deletions
21
graphs/flow_graph1_2.txt
Normal file
21
graphs/flow_graph1_2.txt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
% This is a graph.
|
||||
|
||||
n 20 300 0
|
||||
n 200 300 1
|
||||
n 200 200 2
|
||||
n 200 400 3
|
||||
n 380 300 4
|
||||
n 380 200 5
|
||||
|
||||
e 0 2 0 8
|
||||
e 0 3 1 5
|
||||
e 0 1 2 0
|
||||
e 2 4 3 8
|
||||
e 3 4 4 5
|
||||
e 3 2 5 0
|
||||
e 3 1 6 0
|
||||
e 1 4 7 0
|
||||
e 1 5 8 0
|
||||
e 4 5 9 13
|
||||
|
||||
% End of graph
|
||||
21
graphs/flow_graph1_3.txt
Normal file
21
graphs/flow_graph1_3.txt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
% This is a graph.
|
||||
|
||||
n 20 300 0
|
||||
n 200 300 1
|
||||
n 200 200 2
|
||||
n 200 400 3
|
||||
n 380 300 4
|
||||
n 380 200 5
|
||||
|
||||
e 4 5 0 14
|
||||
e 1 5 1 0
|
||||
e 1 4 2 1
|
||||
e 3 1 3 0
|
||||
e 3 2 4 0
|
||||
e 3 4 5 5
|
||||
e 2 4 6 8
|
||||
e 0 1 7 1
|
||||
e 0 3 8 5
|
||||
e 0 2 9 8
|
||||
|
||||
% End of graph
|
||||
42
src/ftest.ml
42
src/ftest.ml
|
|
@ -31,18 +31,44 @@ let () =
|
|||
let string_graph = from_file infile in
|
||||
let graph = gmap string_graph (fun x -> int_of_string x) in
|
||||
|
||||
let flow_graph_1 = create_flow_graph graph in
|
||||
let flow_graph_0 = create_flow_graph graph in
|
||||
|
||||
let path : int list = [0; 2; 4; 5] in
|
||||
Printf.printf "Path: [%s]\n" (String.concat "; " (List.map string_of_int path)) ;
|
||||
(* iteration 1 *)
|
||||
|
||||
let new_flow_optional : int option = find_smallest_label_on_path graph path in
|
||||
let new_flow : int = Option.value new_flow_optional ~default:0 in
|
||||
Printf.printf "Flow : %d\n" new_flow ;
|
||||
let path_1 : int list = [0; 2; 4; 5] in
|
||||
Printf.printf "Path 1: [%s]\n" (String.concat "; " (List.map string_of_int path_1)) ;
|
||||
|
||||
let flow_graph_2 = update_flow_graph flow_graph_1 (fun arc -> check_if_arc_is_in_path arc path) new_flow in
|
||||
let new_flow_optional_1 : int option = find_max_flow_on_path graph flow_graph_0 path_1 in
|
||||
let new_flow_1 : int = Option.value new_flow_optional_1 ~default:0 in
|
||||
Printf.printf "Flow 1: %d\n" new_flow_1 ;
|
||||
|
||||
let string_flow_graph = gmap flow_graph_2 (fun x -> string_of_int x) in
|
||||
let flow_graph_1 = update_flow_graph flow_graph_0 (fun arc -> check_if_arc_is_in_path arc path_1) new_flow_1 in
|
||||
|
||||
(* iteration 2 *)
|
||||
|
||||
let path_2 : int list = [0; 3; 4; 5] in
|
||||
Printf.printf "Path 2: [%s]\n" (String.concat "; " (List.map string_of_int path_2)) ;
|
||||
|
||||
let new_flow_optional_2 : int option = find_max_flow_on_path graph flow_graph_1 path_2 in
|
||||
let new_flow_2 : int = Option.value new_flow_optional_2 ~default:0 in
|
||||
Printf.printf "Flow 2: %d\n" new_flow_2 ;
|
||||
|
||||
let flow_graph_2 = update_flow_graph flow_graph_1 (fun arc -> check_if_arc_is_in_path arc path_2) new_flow_2 in
|
||||
|
||||
(* iteration 3 *)
|
||||
|
||||
let path_3 : int list = [0; 1; 4; 5] in
|
||||
Printf.printf "Path 3: [%s]\n" (String.concat "; " (List.map string_of_int path_3)) ;
|
||||
|
||||
let new_flow_optional_3 : int option = find_max_flow_on_path graph flow_graph_2 path_3 in
|
||||
let new_flow_3 : int = Option.value new_flow_optional_3 ~default:0 in
|
||||
Printf.printf "Flow 3: %d\n" new_flow_3 ;
|
||||
|
||||
let flow_graph_3 = update_flow_graph flow_graph_2 (fun arc -> check_if_arc_is_in_path arc path_3) new_flow_3 in
|
||||
|
||||
(* save result to file *)
|
||||
|
||||
let string_flow_graph = gmap flow_graph_3 (fun x -> string_of_int x) in
|
||||
|
||||
(* Rewrite the graph that has been read. *)
|
||||
let () = write_file outfile string_flow_graph in
|
||||
|
|
|
|||
25
src/tools.ml
25
src/tools.ml
|
|
@ -43,24 +43,25 @@ let check_if_arc_is_in_path : int arc -> int list -> bool =
|
|||
in
|
||||
is_arc_in_path path
|
||||
|
||||
let find_smallest_label_on_path : int graph -> int list -> int option =
|
||||
fun graph path ->
|
||||
let rec find_smallest_label path =
|
||||
let find_max_flow_on_path : int graph -> int graph -> int list -> int option =
|
||||
fun graph flow_graph path ->
|
||||
let rec find_max_flow_on_path path =
|
||||
match path with
|
||||
| [] | [_] -> None (* A path must have at least two nodes to have arcs *)
|
||||
| node1 :: node2 :: rest ->
|
||||
let arc_opt = find_arc graph node1 node2 in
|
||||
match arc_opt with
|
||||
| Some arc ->
|
||||
let rest_label_opt = find_smallest_label (node2 :: rest) in
|
||||
let arc1_opt = find_arc graph node1 node2 in
|
||||
let arc2_opt = find_arc flow_graph node1 node2 in
|
||||
match (arc1_opt, arc2_opt) with
|
||||
| (Some arc1, Some arc2) ->
|
||||
let rest_difference_opt = find_max_flow_on_path (node2 :: rest) in
|
||||
begin
|
||||
match rest_label_opt with
|
||||
| Some rest_label -> Some (min arc.lbl rest_label)
|
||||
| None -> Some arc.lbl
|
||||
match rest_difference_opt with
|
||||
| Some rest_difference -> Some (min (arc1.lbl - arc2.lbl) rest_difference)
|
||||
| None -> Some (arc1.lbl - arc2.lbl)
|
||||
end
|
||||
| None -> None
|
||||
| _ -> None
|
||||
in
|
||||
find_smallest_label path
|
||||
find_max_flow_on_path path
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ val update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph
|
|||
|
||||
val check_if_arc_is_in_path : int arc -> int list -> bool
|
||||
|
||||
val find_smallest_label_on_path : int graph -> int list -> int option
|
||||
val find_max_flow_on_path : int graph -> int graph -> int list -> int option
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue