From 2442e5786bb4f574f326607578e069bc54c06159 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 17:01:10 +0100 Subject: [PATCH] Implement function that finds the lowest flow in path --- graphs/flow_graph1.txt | 20 ++++++++++---------- src/ftest.ml | 18 +++++++++++++----- src/tools.ml | 19 +++++++++++++++++++ src/tools.mli | 4 ++++ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/graphs/flow_graph1.txt b/graphs/flow_graph1.txt index cae1dd4..43e3420 100644 --- a/graphs/flow_graph1.txt +++ b/graphs/flow_graph1.txt @@ -7,15 +7,15 @@ n 200 400 3 n 380 300 4 n 380 200 5 -e 0 2 0 4 -e 0 3 1 0 -e 0 1 2 0 -e 2 4 3 4 -e 3 4 4 0 -e 3 2 5 0 -e 3 1 6 0 -e 1 4 7 0 -e 1 5 8 0 -e 4 5 9 4 +e 4 5 0 8 +e 1 5 1 0 +e 1 4 2 0 +e 3 1 3 0 +e 3 2 4 0 +e 3 4 5 0 +e 2 4 6 8 +e 0 1 7 0 +e 0 3 8 0 +e 0 2 9 8 % End of graph diff --git a/src/ftest.ml b/src/ftest.ml index 5d7a660..bbc815d 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -1,6 +1,6 @@ open Gfile open Tools -open Fulkerson +(*open Fulkerson*) let () = @@ -28,26 +28,34 @@ let () = in (* Open file *) - let graph = from_file infile in + 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 path : int list = [0; 2; 4; 5] in + Printf.printf "Path: [%s]\n" (String.concat "; " (List.map string_of_int path)) ; - let flow_graph_2 = update_flow_graph flow_graph_1 (fun arc -> check_if_arc_is_in_path arc path) 1 in + 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 flow_graph_2 = update_flow_graph flow_graph_1 (fun arc -> check_if_arc_is_in_path arc path) new_flow in let string_flow_graph = gmap flow_graph_2 (fun x -> string_of_int x) in (* Rewrite the graph that has been read. *) let () = write_file outfile string_flow_graph in + () + - (* let g = clone_nodes graph in *) + (*(* let g = clone_nodes graph in *) let () = aff (dfs graph 0 [] 3) ; Printf.printf "\n\n" ; hash - in () + in () *) diff --git a/src/tools.ml b/src/tools.ml index ceabd34..b0a70d3 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -43,6 +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 = + 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 + begin + match rest_label_opt with + | Some rest_label -> Some (min arc.lbl rest_label) + | None -> Some arc.lbl + end + | None -> None + in + find_smallest_label path + \ No newline at end of file diff --git a/src/tools.mli b/src/tools.mli index f3f1647..5a39631 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -13,3 +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 + + +