merge with flow-graph

This commit is contained in:
Achraf 2023-12-11 08:00:31 +01:00
commit 2cad93c7ff
8 changed files with 218 additions and 6 deletions

View file

@ -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

21
graphs/flow_graph1_1.txt Normal file
View 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 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

21
graphs/flow_graph1_2.txt Normal file
View 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
View 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

20
graphs/flow_graph_ana.txt Normal file
View file

@ -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

25
graphs/graph_ana.txt Normal file
View file

@ -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

View file

@ -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
()*)

View file

@ -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*)