From 83c6b455bee33e090a0ba90cbbd2f344954fe15c Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 11 Dec 2023 16:20:59 +0100 Subject: [PATCH] Implement and call iterate function --- graphs/flow_graph_ana.tx | 20 ++++++++++++++++ src/ftest.ml | 50 +++++++++++++++++++++++++++++----------- src/fulkerson.ml | 14 ++++++++++- 3 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 graphs/flow_graph_ana.tx diff --git a/graphs/flow_graph_ana.tx b/graphs/flow_graph_ana.tx new file mode 100644 index 0000000..5a7f028 --- /dev/null +++ b/graphs/flow_graph_ana.tx @@ -0,0 +1,20 @@ +% 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 0 +e 4 2 1 0 +e 3 4 2 0 +e 2 5 3 4 +e 1 4 4 0 +e 1 3 5 0 +e 1 2 6 4 +e 0 3 7 0 +e 0 1 8 4 + +% End of graph diff --git a/src/ftest.ml b/src/ftest.ml index 05bbe97..cf23f4c 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -1,5 +1,5 @@ open Gfile -(* open Tools *) +open Tools open Fulkerson let () = @@ -20,25 +20,47 @@ let () = (* Arguments are : infile(1) source-id(2) sink-id(3) outfile(4) *) let infile = Sys.argv.(1) - (* and outfile = Sys.argv.(4) *) + and outfile = Sys.argv.(4) (* These command-line arguments are not used for the moment. *) - and _source = int_of_string Sys.argv.(2) - and _sink = int_of_string Sys.argv.(3) + and source = int_of_string Sys.argv.(2) + and target = int_of_string Sys.argv.(3) in - (* Open file *) - let graph = from_file infile in - (* let g = clone_nodes graph in *) - let () = aff (find_path graph 0 4) ; - Printf.printf "\n\n" ; - in () - (* export outfile graph *) + Printf.printf "Source: %d\n" source ; + Printf.printf "Target: %d\n" target ; - + (* Open file *) + let string_graph = from_file infile in + let graph = gmap string_graph (fun x -> int_of_string x) in + + let flow_graph = create_flow_graph graph in + + (* iteration 1 *) + + let flow_graph = iterate graph flow_graph source target in + + (* iteration 2 *) + + let flow_graph = iterate graph flow_graph source target in + + (* iteration 3 *) + + let flow_graph = iterate graph flow_graph source target in + + (* save result to file *) + + let string_flow_graph = gmap flow_graph (fun x -> string_of_int x) in (* Rewrite the graph that has been read. *) - (* let () = write_file outfile g in + let () = write_file outfile string_flow_graph in - () *) + () + + + (*(* let g = clone_nodes graph in *) + let () = aff (dfs graph 0 [] 3) ; + Printf.printf "\n\n" ; + hash + in () *) \ No newline at end of file diff --git a/src/fulkerson.ml b/src/fulkerson.ml index 1bcf9f7..23d565c 100644 --- a/src/fulkerson.ml +++ b/src/fulkerson.ml @@ -38,4 +38,16 @@ let rec aff nodes = | [] -> () | x::rest -> Printf.printf "%d " x ; aff rest - \ No newline at end of file +let iterate graph flow_graph src tgt = + let path : int list = find_path graph src tgt in + Printf.printf "Path: [%s]\n" (String.concat "; " (List.map string_of_int path)) ; + + let new_flow_optional : int option = find_max_flow_on_path graph flow_graph path in + let new_flow : int = Option.value new_flow_optional ~default:0 in + Printf.printf "Flow: %d\n" new_flow ; + + update_flow_graph flow_graph (fun arc -> check_if_arc_is_in_path arc path) (fun arc -> check_if_backward_arc_is_in_path arc path) new_flow + + + + \ No newline at end of file