From 4d08e292c2667ac04b42be371155736cadb8ef5f Mon Sep 17 00:00:00 2001 From: Arthur Bit-Monnot Date: Fri, 24 Nov 2023 15:56:29 +0100 Subject: [PATCH 01/15] ci: change to use latest ubuntu version with ocaml 4.14 --- .github/workflows/workflow.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 645d1dc..1832485 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -11,11 +11,10 @@ jobs: matrix: os: # - macos-latest -# - ubuntu-latest + - ubuntu-latest # - windows-latest - - ubuntu-18.04 ocaml-version: - - 4.11.0 + - 4.14.0 # - 4.10.1 # - 4.09.1 # - 4.08.1 From b7d19974e25ff9fe41ae6c1116d8eda31c2e7a26 Mon Sep 17 00:00:00 2001 From: Arthur Bit-Monnot Date: Fri, 24 Nov 2023 15:56:29 +0100 Subject: [PATCH 02/15] ci: change to use latest ubuntu version with ocaml 4.14 --- .github/workflows/workflow.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 645d1dc..c77357b 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -11,11 +11,10 @@ jobs: matrix: os: # - macos-latest -# - ubuntu-latest + - ubuntu-latest # - windows-latest - - ubuntu-18.04 ocaml-version: - - 4.11.0 + - 4.14.0 # - 4.10.1 # - 4.09.1 # - 4.08.1 @@ -39,7 +38,7 @@ jobs: with: ocaml-version: ${{ matrix.ocaml-version }} - - run: opam install ocamlbuild + - run: opam install dune - name: Build project run: eval $(opam env) && make build - name: Run demo From 16094291a07506ec13164d6193e1ada82ef1cacb Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 27 Nov 2023 11:14:01 +0100 Subject: [PATCH 03/15] Add method for creating a flow graph --- src/tools.ml | 6 +++--- src/tools.mli | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/tools.ml b/src/tools.ml index d10d942..6ab9c3c 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -1,13 +1,13 @@ open Graph -let clone_nodes gr= n_fold gr new_node empty_graph +let clone_nodes gr = n_fold gr new_node empty_graph let gmap gr f = e_fold gr (fun g arc -> (new_arc g {src=arc.src; tgt=arc.tgt; lbl=f arc.lbl})) (clone_nodes gr) let add_arc graph id1 id2 n = - let arc =find_arc graph id1 id2 in + let arc = find_arc graph id1 id2 in match arc with | None -> new_arc graph {src=id1; tgt=id2; lbl=n} | Some arc -> new_arc graph {src=arc.src; tgt=arc.tgt; lbl=arc.lbl+n} - +let create_flow_graph gr = e_fold gr (fun g arc -> (new_arc g {src=arc.src; tgt=arc.tgt; lbl=0})) (clone_nodes gr) diff --git a/src/tools.mli b/src/tools.mli index 8e6fb3d..5cfc926 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -3,3 +3,5 @@ open Graph val clone_nodes: 'a graph -> 'b graph val gmap: 'a graph -> ('a -> 'b) -> 'b graph val add_arc: int graph -> id -> id -> int -> int graph + +val create_flow_graph: 'a graph -> int graph \ No newline at end of file From 5e92d25ce1cdec46ef80cd69569b2b71ec628dec Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 27 Nov 2023 12:09:28 +0100 Subject: [PATCH 04/15] Work on flow graph methods --- src/tools.ml | 18 +++++++++++++++++- src/tools.mli | 11 ++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/tools.ml b/src/tools.ml index 6ab9c3c..508cd75 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -1,6 +1,7 @@ open Graph let clone_nodes gr = n_fold gr new_node empty_graph + let gmap gr f = e_fold gr (fun g arc -> (new_arc g {src=arc.src; tgt=arc.tgt; lbl=f arc.lbl})) (clone_nodes gr) @@ -10,4 +11,19 @@ let add_arc graph id1 id2 n = | None -> new_arc graph {src=id1; tgt=id2; lbl=n} | Some arc -> new_arc graph {src=arc.src; tgt=arc.tgt; lbl=arc.lbl+n} -let create_flow_graph gr = e_fold gr (fun g arc -> (new_arc g {src=arc.src; tgt=arc.tgt; lbl=0})) (clone_nodes gr) +let create_flow_graph graph = gmap graph (fun _ -> 0) + +let update_arc_flow graph src_id dest_id flow = + match (find_arc graph src_id dest_id) with + | Some arc -> arc.lbl <- arc.lbl + flow + | None -> () + +(*let update_flow_graph graph node_ids new_flow = + let mylength_acu_better l = + let rec loop acu l = match l with + | Empty -> acu + | Cell(_, tail) -> loop (acu + 1) tail + in + loop 0 l ;;*) + +(*let find_max_possible_flow graph node_ids = e_fold graph (fun acu arc -> if List.mem arc.src myList) 0*) \ No newline at end of file diff --git a/src/tools.mli b/src/tools.mli index 5cfc926..cad274e 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -1,7 +1,16 @@ open Graph val clone_nodes: 'a graph -> 'b graph + val gmap: 'a graph -> ('a -> 'b) -> 'b graph + val add_arc: int graph -> id -> id -> int -> int graph -val create_flow_graph: 'a graph -> int graph \ No newline at end of file +(* try not to lose genericity *) +val create_flow_graph: 'a graph -> int graph + +val update_arc_flow: int graph -> int -> int -> int -> int graph + +(*val update_flow_graph: int graph -> int list -> int -> int graph*) + +(*val find_max_possible_flow: int graph -> int list -> int*) \ No newline at end of file From 93074093078bd2ad1f6959fae6532a28f9ba8150 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 27 Nov 2023 12:15:51 +0100 Subject: [PATCH 05/15] Work on flow graph methods --- src/tools.ml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tools.ml b/src/tools.ml index 508cd75..575d906 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -13,10 +13,12 @@ let add_arc graph id1 id2 n = let create_flow_graph graph = gmap graph (fun _ -> 0) -let update_arc_flow graph src_id dest_id flow = +(*let update_arc_flow graph src_id dest_id flow = match (find_arc graph src_id dest_id) with | Some arc -> arc.lbl <- arc.lbl + flow - | None -> () + | None -> ()*) + +let update_arc_flow graph src_id dest_id flow = gmap graph (fun arc -> if arc.src = src_id && arc.tgt = dest_id then (arc.lbl + flow)) (*let update_flow_graph graph node_ids new_flow = let mylength_acu_better l = From 9f1c416d199122ab567f7e346782e0b5629d872a Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 13:56:55 +0100 Subject: [PATCH 06/15] Implement a function that updates flow graph --- src/tools.ml | 34 +++++++++++++++++++--------------- src/tools.mli | 5 +---- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/tools.ml b/src/tools.ml index 575d906..e0ac489 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -12,20 +12,24 @@ let add_arc graph id1 id2 n = | Some arc -> new_arc graph {src=arc.src; tgt=arc.tgt; lbl=arc.lbl+n} let create_flow_graph graph = gmap graph (fun _ -> 0) - -(*let update_arc_flow graph src_id dest_id flow = - match (find_arc graph src_id dest_id) with - | Some arc -> arc.lbl <- arc.lbl + flow - | None -> ()*) - -let update_arc_flow graph src_id dest_id flow = gmap graph (fun arc -> if arc.src = src_id && arc.tgt = dest_id then (arc.lbl + flow)) - -(*let update_flow_graph graph node_ids new_flow = - let mylength_acu_better l = - let rec loop acu l = match l with - | Empty -> acu - | Cell(_, tail) -> loop (acu + 1) tail + +let update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph = + fun original_graph condition increment -> + let new_graph = + e_fold + original_graph + (fun acc_graph arc -> + (* Perform some transformation or condition on the arc and accumulate the result in the new graph *) + let modified_arc = + if condition arc then + { arc with lbl = arc.lbl + increment } + else + arc + in + new_arc acc_graph modified_arc) + empty_graph in - loop 0 l ;;*) + new_graph + -(*let find_max_possible_flow graph node_ids = e_fold graph (fun acu arc -> if List.mem arc.src myList) 0*) \ No newline at end of file + \ No newline at end of file diff --git a/src/tools.mli b/src/tools.mli index cad274e..de80fdb 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -9,8 +9,5 @@ val add_arc: int graph -> id -> id -> int -> int graph (* try not to lose genericity *) val create_flow_graph: 'a graph -> int graph -val update_arc_flow: int graph -> int -> int -> int -> int graph +val update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph -(*val update_flow_graph: int graph -> int list -> int -> int graph*) - -(*val find_max_possible_flow: int graph -> int list -> int*) \ No newline at end of file From a5d44a7fc8e8ce87fc7424cf3b836bdb1e3524cc Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 16:07:38 +0100 Subject: [PATCH 07/15] Fix mistake in update_flow_graph --- graphs/flow_graph1.txt | 21 +++++++++++++++++++++ src/ftest.ml | 9 +++++++-- src/tools.ml | 3 +-- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 graphs/flow_graph1.txt diff --git a/graphs/flow_graph1.txt b/graphs/flow_graph1.txt new file mode 100644 index 0000000..03a7ecc --- /dev/null +++ b/graphs/flow_graph1.txt @@ -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 5 +e 0 3 1 5 +e 0 1 2 5 +e 2 4 3 5 +e 3 4 4 5 +e 3 2 5 5 +e 3 1 6 5 +e 1 4 7 5 +e 1 5 8 5 +e 4 5 9 5 + +% End of graph diff --git a/src/ftest.ml b/src/ftest.ml index faec057..8f82245 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -28,10 +28,15 @@ let () = (* Open file *) let graph = from_file infile in - let g = clone_nodes graph in + (*let g = clone_nodes graph in*) + + let flow_graph_1 = create_flow_graph graph in + let flow_graph_2 = update_flow_graph flow_graph_1 (fun _ -> true) 5 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 g in + let () = write_file outfile string_flow_graph in () diff --git a/src/tools.ml b/src/tools.ml index e0ac489..48a766a 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -19,7 +19,6 @@ let update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph = e_fold original_graph (fun acc_graph arc -> - (* Perform some transformation or condition on the arc and accumulate the result in the new graph *) let modified_arc = if condition arc then { arc with lbl = arc.lbl + increment } @@ -27,7 +26,7 @@ let update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph = arc in new_arc acc_graph modified_arc) - empty_graph + original_graph in new_graph From 4e069579f3e4ac846c6a9e926509f1cf87feedd9 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 16:39:47 +0100 Subject: [PATCH 08/15] Implement method that checks if arc is in the found path --- graphs/flow_graph1.txt | 14 +++++++------- src/ftest.ml | 13 ++++++------- src/tools.ml | 14 ++++++++++++++ src/tools.mli | 2 ++ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/graphs/flow_graph1.txt b/graphs/flow_graph1.txt index dc2e634..cae1dd4 100644 --- a/graphs/flow_graph1.txt +++ b/graphs/flow_graph1.txt @@ -8,14 +8,14 @@ n 380 300 4 n 380 200 5 e 0 2 0 4 -e 0 3 1 4 -e 0 1 2 4 +e 0 3 1 0 +e 0 1 2 0 e 2 4 3 4 -e 3 4 4 4 -e 3 2 5 4 -e 3 1 6 4 -e 1 4 7 4 -e 1 5 8 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 % End of graph diff --git a/src/ftest.ml b/src/ftest.ml index 1e807bf..5d7a660 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -29,10 +29,12 @@ let () = (* Open file *) let graph = from_file infile in - (*let g = clone_nodes graph in*) let flow_graph_1 = create_flow_graph graph in - let flow_graph_2 = update_flow_graph flow_graph_1 (fun _ -> true) 4 in + + let path : int list = [0; 2; 4; 5] in + + let flow_graph_2 = update_flow_graph flow_graph_1 (fun arc -> check_if_arc_is_in_path arc path) 1 in let string_flow_graph = gmap flow_graph_2 (fun x -> string_of_int x) in @@ -40,16 +42,13 @@ let () = 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 () + in () - (* Rewrite the graph that has been read. *) - (* let () = write_file outfile g in - - () *) diff --git a/src/tools.ml b/src/tools.ml index 48a766a..ceabd34 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -29,6 +29,20 @@ let update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph = original_graph in new_graph + +let check_if_arc_is_in_path : int arc -> int list -> bool = + fun arc path -> + let rec is_arc_in_path path = + match path with + | [] | [_] -> false (* A path must have at least two nodes to have an arc *) + | node1 :: node2 :: rest -> + if (node1, node2) = (arc.src, arc.tgt) || (node1, node2) = (arc.tgt, arc.src) then + true + else + is_arc_in_path (node2 :: rest) + in + is_arc_in_path path + \ No newline at end of file diff --git a/src/tools.mli b/src/tools.mli index de80fdb..f3f1647 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -11,3 +11,5 @@ val create_flow_graph: 'a graph -> int graph val update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph +val check_if_arc_is_in_path : int arc -> int list -> bool + From 2442e5786bb4f574f326607578e069bc54c06159 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 17:01:10 +0100 Subject: [PATCH 09/15] 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 + + + From 4d00af25eed5c7be7b8afcb0c1e52a6bffe13145 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 17:19:59 +0100 Subject: [PATCH 10/15] Make function that finds max flow also take current flow into account --- graphs/{flow_graph1.txt => flow_graph1_1.txt} | 0 graphs/flow_graph1_2.txt | 21 ++++++++++ graphs/flow_graph1_3.txt | 21 ++++++++++ src/ftest.ml | 42 +++++++++++++++---- src/tools.ml | 25 +++++------ src/tools.mli | 2 +- 6 files changed, 90 insertions(+), 21 deletions(-) rename graphs/{flow_graph1.txt => flow_graph1_1.txt} (100%) create mode 100644 graphs/flow_graph1_2.txt create mode 100644 graphs/flow_graph1_3.txt diff --git a/graphs/flow_graph1.txt b/graphs/flow_graph1_1.txt similarity index 100% rename from graphs/flow_graph1.txt rename to graphs/flow_graph1_1.txt diff --git a/graphs/flow_graph1_2.txt b/graphs/flow_graph1_2.txt new file mode 100644 index 0000000..2a65445 --- /dev/null +++ b/graphs/flow_graph1_2.txt @@ -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 diff --git a/graphs/flow_graph1_3.txt b/graphs/flow_graph1_3.txt new file mode 100644 index 0000000..dc7c217 --- /dev/null +++ b/graphs/flow_graph1_3.txt @@ -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 diff --git a/src/ftest.ml b/src/ftest.ml index bbc815d..77198f3 100644 --- a/src/ftest.ml +++ b/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 diff --git a/src/tools.ml b/src/tools.ml index b0a70d3..ffe055c 100644 --- a/src/tools.ml +++ b/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 diff --git a/src/tools.mli b/src/tools.mli index 5a39631..e2bd480 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -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 From 8228b64eed9a75cb3bfd6c6f40c6e4e6c747c8e0 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 17:28:23 +0100 Subject: [PATCH 11/15] Make function that finds max flow also take backward arches into account --- src/tools.ml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/tools.ml b/src/tools.ml index ffe055c..faf4459 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -51,14 +51,23 @@ let find_max_flow_on_path : int graph -> int graph -> int list -> int option = | node1 :: node2 :: rest -> 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 arc1_rev_opt = find_arc graph node2 node1 in + let arc2_rev_opt = find_arc flow_graph node2 node1 in + match (arc1_opt, arc2_opt, arc1_rev_opt, arc2_rev_opt) with + | (Some arc1, Some arc2, None, None) -> let rest_difference_opt = find_max_flow_on_path (node2 :: rest) in begin 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, Some arc1_rev, Some arc2_rev) -> + let rest_difference_opt = find_max_flow_on_path (node2 :: rest) in + begin + match rest_difference_opt with + | Some rest_difference -> Some (min (arc2_rev.lbl - arc1_rev.lbl) rest_difference) + | None -> Some (arc2_rev.lbl - arc1_rev.lbl) + end | _ -> None in find_max_flow_on_path path From 1f6c4eaec5c15270ab0e0fc196286b5c1a1ef26f Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 17:52:26 +0100 Subject: [PATCH 12/15] Make function that finds max flow also take backward arches into account --- graphs/flow_graph_ana.txt | 20 ++++++++++++++++++++ graphs/graph_ana.txt | 25 +++++++++++++++++++++++++ src/ftest.ml | 4 ++-- src/tools.ml | 8 +++++--- src/tools.mli | 1 - 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 graphs/flow_graph_ana.txt create mode 100644 graphs/graph_ana.txt diff --git a/graphs/flow_graph_ana.txt b/graphs/flow_graph_ana.txt new file mode 100644 index 0000000..68b7f14 --- /dev/null +++ b/graphs/flow_graph_ana.txt @@ -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 10 +e 4 2 1 0 +e 3 4 2 6 +e 2 5 3 4 +e 1 4 4 12 +e 1 3 5 0 +e 1 2 6 4 +e 0 3 7 6 +e 0 1 8 8 + +% End of graph diff --git a/graphs/graph_ana.txt b/graphs/graph_ana.txt new file mode 100644 index 0000000..6cd0632 --- /dev/null +++ b/graphs/graph_ana.txt @@ -0,0 +1,25 @@ +%% Test graph #1 + +%% Nodes + +n 20 300 0 % S This is node #0, with its coordinates (which are not used by the algorithms). +n 200 300 1 % A +n 200 200 2 % B +n 200 400 3 % C +n 380 300 4 % D +n 380 200 5 % T This is node #5. + + +%% Edges (arcs) + +e 0 1 0 10 % An arc from 0 to 2, labeled "8". The second 0 is useless. +e 0 3 1 10 +e 1 2 2 4 +e 1 3 3 2 +e 1 4 4 8 +e 2 5 5 10 +e 3 4 6 9 +e 4 2 7 6 +e 4 5 8 10 + +% End of graph diff --git a/src/ftest.ml b/src/ftest.ml index 77198f3..695a3fd 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -35,7 +35,7 @@ let () = (* iteration 1 *) - let path_1 : int list = [0; 2; 4; 5] in + let path_1 : int list = [0; 1; 4; 5] in Printf.printf "Path 1: [%s]\n" (String.concat "; " (List.map string_of_int path_1)) ; let new_flow_optional_1 : int option = find_max_flow_on_path graph flow_graph_0 path_1 in @@ -57,7 +57,7 @@ let () = (* iteration 3 *) - let path_3 : int list = [0; 1; 4; 5] in + let path_3 : int list = [0; 3; 4; 1; 2; 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 diff --git a/src/tools.ml b/src/tools.ml index faf4459..cf0c2b2 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -61,17 +61,19 @@ let find_max_flow_on_path : int graph -> int graph -> int list -> int option = | Some rest_difference -> Some (min (arc1.lbl - arc2.lbl) rest_difference) | None -> Some (arc1.lbl - arc2.lbl) end - | (None, None, Some arc1_rev, Some arc2_rev) -> + | (None, None, Some _, Some arc2_rev) -> let rest_difference_opt = find_max_flow_on_path (node2 :: rest) in begin match rest_difference_opt with - | Some rest_difference -> Some (min (arc2_rev.lbl - arc1_rev.lbl) rest_difference) - | None -> Some (arc2_rev.lbl - arc1_rev.lbl) + | Some rest_difference -> Some (min (arc2_rev.lbl) rest_difference) + | None -> Some (arc2_rev.lbl) end | _ -> None in find_max_flow_on_path path + + \ No newline at end of file diff --git a/src/tools.mli b/src/tools.mli index e2bd480..01113c3 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -16,4 +16,3 @@ val check_if_arc_is_in_path : int arc -> int list -> bool val find_max_flow_on_path : int graph -> int graph -> int list -> int option - From 7fd4e25758a46585333d7134e43b4d2ce2ef20da Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 18:02:29 +0100 Subject: [PATCH 13/15] Fix label change on backward traversing --- graphs/flow_graph_ana.txt | 2 +- src/ftest.ml | 6 +++--- src/tools.ml | 21 ++++++++++++++++++--- src/tools.mli | 4 +++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/graphs/flow_graph_ana.txt b/graphs/flow_graph_ana.txt index 68b7f14..5dcf2d4 100644 --- a/graphs/flow_graph_ana.txt +++ b/graphs/flow_graph_ana.txt @@ -11,7 +11,7 @@ e 4 5 0 10 e 4 2 1 0 e 3 4 2 6 e 2 5 3 4 -e 1 4 4 12 +e 1 4 4 4 e 1 3 5 0 e 1 2 6 4 e 0 3 7 6 diff --git a/src/ftest.ml b/src/ftest.ml index 695a3fd..4b3db1c 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -42,7 +42,7 @@ let () = let new_flow_1 : int = Option.value new_flow_optional_1 ~default:0 in Printf.printf "Flow 1: %d\n" new_flow_1 ; - 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 + let flow_graph_1 = update_flow_graph flow_graph_0 (fun arc -> check_if_arc_is_in_path arc path_1) (fun arc -> check_if_backward_arc_is_in_path arc path_1) new_flow_1 in (* iteration 2 *) @@ -53,7 +53,7 @@ let () = 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 + let flow_graph_2 = update_flow_graph flow_graph_1 (fun arc -> check_if_arc_is_in_path arc path_2) (fun arc -> check_if_backward_arc_is_in_path arc path_2) new_flow_2 in (* iteration 3 *) @@ -64,7 +64,7 @@ let () = 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 + let flow_graph_3 = update_flow_graph flow_graph_2 (fun arc -> check_if_arc_is_in_path arc path_3) (fun arc -> check_if_backward_arc_is_in_path arc path_3) new_flow_3 in (* save result to file *) diff --git a/src/tools.ml b/src/tools.ml index cf0c2b2..e7e79f7 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -13,8 +13,8 @@ let add_arc graph id1 id2 n = let create_flow_graph graph = gmap graph (fun _ -> 0) -let update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph = - fun original_graph condition increment -> +let update_flow_graph : int graph -> (int arc -> bool) -> (int arc -> bool) -> int -> int graph = + fun original_graph condition backward_condition increment -> let new_graph = e_fold original_graph @@ -22,6 +22,8 @@ let update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph = let modified_arc = if condition arc then { arc with lbl = arc.lbl + increment } + else if backward_condition arc then + { arc with lbl = arc.lbl - increment } else arc in @@ -36,7 +38,20 @@ let check_if_arc_is_in_path : int arc -> int list -> bool = match path with | [] | [_] -> false (* A path must have at least two nodes to have an arc *) | node1 :: node2 :: rest -> - if (node1, node2) = (arc.src, arc.tgt) || (node1, node2) = (arc.tgt, arc.src) then + if (node1, node2) = (arc.src, arc.tgt) then + true + else + is_arc_in_path (node2 :: rest) + in + is_arc_in_path path + +let check_if_backward_arc_is_in_path : int arc -> int list -> bool = + fun arc path -> + let rec is_arc_in_path path = + match path with + | [] | [_] -> false (* A path must have at least two nodes to have an arc *) + | node1 :: node2 :: rest -> + if (node2, node1) = (arc.src, arc.tgt) then true else is_arc_in_path (node2 :: rest) diff --git a/src/tools.mli b/src/tools.mli index 01113c3..0a889a4 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -9,10 +9,12 @@ val add_arc: int graph -> id -> id -> int -> int graph (* try not to lose genericity *) val create_flow_graph: 'a graph -> int graph -val update_flow_graph : int graph -> (int arc -> bool) -> int -> int graph +val update_flow_graph : int graph -> (int arc -> bool) -> (int arc -> bool) -> int -> int graph val check_if_arc_is_in_path : int arc -> int list -> bool +val check_if_backward_arc_is_in_path : int arc -> int list -> bool + val find_max_flow_on_path : int graph -> int graph -> int list -> int option From a709226236faa129512a532995b5303fd2b57350 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 18:07:47 +0100 Subject: [PATCH 14/15] Implement while function --- src/tools.ml | 7 +++++++ src/tools.mli | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/tools.ml b/src/tools.ml index e7e79f7..1ecc663 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -87,6 +87,13 @@ let find_max_flow_on_path : int graph -> int graph -> int list -> int option = in find_max_flow_on_path path +let rec while_loop condition action x = + if condition x then begin + action x; (* Execute the action *) + while_loop condition action x (* Recursive call for the next iteration *) + end else + () + diff --git a/src/tools.mli b/src/tools.mli index 0a889a4..b969085 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -17,4 +17,6 @@ val check_if_backward_arc_is_in_path : int arc -> int list -> bool val find_max_flow_on_path : int graph -> int graph -> int list -> int option +val while_loop : ('a -> bool) -> ('a -> unit) -> 'a -> unit + From 0b4747665cc44f2bc5242dbcdc936b20431a9c00 Mon Sep 17 00:00:00 2001 From: AnaGrskovic Date: Mon, 4 Dec 2023 18:18:21 +0100 Subject: [PATCH 15/15] Comment out while loop for now --- src/tools.ml | 4 ++-- src/tools.mli | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools.ml b/src/tools.ml index 1ecc663..de61d1f 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -87,12 +87,12 @@ let find_max_flow_on_path : int graph -> int graph -> int list -> int option = in find_max_flow_on_path path -let rec while_loop condition action x = +(*let rec while_loop condition action x = if condition x then begin action x; (* Execute the action *) while_loop condition action x (* Recursive call for the next iteration *) end else - () + ()*) diff --git a/src/tools.mli b/src/tools.mli index b969085..35a03d4 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -17,6 +17,6 @@ val check_if_backward_arc_is_in_path : int arc -> int list -> bool val find_max_flow_on_path : int graph -> int graph -> int list -> int option -val while_loop : ('a -> bool) -> ('a -> unit) -> 'a -> unit +(*val while_loop : ('a -> bool) -> ('a -> unit) -> 'a -> unit*)