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 diff --git a/graphs/flow_graph1_1.txt b/graphs/flow_graph1_1.txt new file mode 100644 index 0000000..43e3420 --- /dev/null +++ b/graphs/flow_graph1_1.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 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/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/graphs/flow_graph_ana.txt b/graphs/flow_graph_ana.txt new file mode 100644 index 0000000..5dcf2d4 --- /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 4 +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/tools.ml b/src/tools.ml index d10d942..de61d1f 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -1,13 +1,101 @@ 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 graph = gmap graph (fun _ -> 0) + +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 + (fun acc_graph arc -> + 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 + new_arc acc_graph modified_arc) + 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) 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) + in + is_arc_in_path 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 arc1_opt = find_arc graph node1 node2 in + let arc2_opt = find_arc flow_graph node1 node2 in + 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 _, 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) rest_difference) + | None -> Some (arc2_rev.lbl) + end + | _ -> None + 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 + ()*) + + + + + + \ No newline at end of file diff --git a/src/tools.mli b/src/tools.mli index 8e6fb3d..35a03d4 100644 --- a/src/tools.mli +++ b/src/tools.mli @@ -1,5 +1,22 @@ 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 + +(* try not to lose genericity *) +val create_flow_graph: 'a graph -> 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 + +(*val while_loop : ('a -> bool) -> ('a -> unit) -> 'a -> unit*) + +