rlacroix 1 year ago
parent
commit
76715cf142

+ 8
- 0
.idea/.gitignore View File

@@ -0,0 +1,8 @@
1
+# Default ignored files
2
+/shelf/
3
+/workspace.xml
4
+# Editor-based HTTP Client requests
5
+/httpRequests/
6
+# Datasource local storage ignored files
7
+/dataSources/
8
+/dataSources.local.xml

+ 6
- 0
.idea/misc.xml View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectRootManager">
4
+    <output url="file://$PROJECT_DIR$/out" />
5
+  </component>
6
+</project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/.idea/ocaml-maxflow-project.iml" filepath="$PROJECT_DIR$/.idea/ocaml-maxflow-project.iml" />
6
+    </modules>
7
+  </component>
8
+</project>

+ 9
- 0
.idea/ocaml-maxflow-project.iml View File

@@ -0,0 +1,9 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module type="JAVA_MODULE" version="4">
3
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+    <exclude-output />
5
+    <content url="file://$MODULE_DIR$" />
6
+    <orderEntry type="inheritedJdk" />
7
+    <orderEntry type="sourceFolder" forTests="false" />
8
+  </component>
9
+</module>

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="" vcs="Git" />
5
+  </component>
6
+</project>

+ 17
- 0
_build/_digests View File

@@ -0,0 +1,17 @@
1
+"Rule: ocaml dependencies mli (%=src/gfile )": "7\182\192g\020\238\188\b\211\164{\014\157\135\011\003"
2
+"Rule: ocaml dependencies ml (%=src/ftest )": "'P\029\247\232H;\197;\234_.z\180e9"
3
+"Rule: ocaml: ml & cmi -> cmx & o (%=src/gfile )": "\185\022\131\212\239\224%\026)\232P\180\192Y\211\025"
4
+"Resource: /home/chepycou/Documents/cours/4-IR/functionalProgramming/Projet/repoGit/ocaml-maxflow-project/src/ftest.ml": "\007\196\023\203\192\230\143\1569\199\162\136\173\005;S"
5
+"Rule: ocaml: mli -> cmi (%=src/graph )": "ZJ:\176\2527A6\005k\191<Z\212@\144"
6
+"Resource: /home/chepycou/Documents/cours/4-IR/functionalProgramming/Projet/repoGit/ocaml-maxflow-project/src/graph.mli": "\031\217\135\022\208\254\154\148\252)\210\130\174\215X\031"
7
+"Rule: ocaml: cmx* & o* -> native (%=src/ftest )": "\135\153\250\007\138U\234`\236\163^\171A\192\165\191"
8
+"Rule: ocaml: ml & cmi -> cmx & o (%=src/ftest )": "\245\229K\\\218\250\197(,\245;\1710]\011\128"
9
+"Resource: /home/chepycou/Documents/cours/4-IR/functionalProgramming/Projet/repoGit/ocaml-maxflow-project/src/graph.ml": "\005\175.\2458\176E\202]\255\235=9\015\000\237"
10
+"Rule: ocaml: mli -> cmi (%=src/gfile )": "\2028\135\152o,Y9\219\178\241C\150A*\209"
11
+"Rule: ocaml: ml -> cmo & cmi (%=src/ftest )": "\207\216\168\169\164\213\209`\157N=/\002\tQ\157"
12
+"Resource: /home/chepycou/Documents/cours/4-IR/functionalProgramming/Projet/repoGit/ocaml-maxflow-project/src/gfile.ml": "\1966\177\141\185,fpl\1987gT\128b\028"
13
+"Rule: ocaml: ml & cmi -> cmx & o (%=src/graph )": "\202s\156\178Lm\165*\224l\179\219N\006\193\000"
14
+"Rule: ocaml dependencies ml (%=src/graph )": "\137kk#\140\236\011\255?\132T\225M\018\169\224"
15
+"Rule: ocaml dependencies mli (%=src/graph )": "\b\217\022sE\156(F\165M\014)D\182\178e"
16
+"Resource: /home/chepycou/Documents/cours/4-IR/functionalProgramming/Projet/repoGit/ocaml-maxflow-project/src/gfile.mli": "\203\249\163Ct\234\0067d}\142\011m8T`"
17
+"Rule: ocaml dependencies ml (%=src/gfile )": "U4(<\t=rQ\240c\0278\231N\1949"

+ 26
- 0
_build/_log View File

@@ -0,0 +1,26 @@
1
+### Starting build.
2
+# Target: src/ftest.ml.depends, tags: { extension:ml, file:src/ftest.ml, include, ocaml, ocamldep, quiet, traverse }
3
+/home/chepycou/.opam/4.14.0/bin/ocamldep.opt -modules src/ftest.ml > src/ftest.ml.depends # cached
4
+# Target: src/gfile.mli.depends, tags: { extension:mli, file:src/gfile.mli, include, ocaml, ocamldep, quiet, traverse }
5
+/home/chepycou/.opam/4.14.0/bin/ocamldep.opt -modules src/gfile.mli > src/gfile.mli.depends # cached
6
+# Target: src/graph.mli.depends, tags: { extension:mli, file:src/graph.mli, include, ocaml, ocamldep, quiet, traverse }
7
+/home/chepycou/.opam/4.14.0/bin/ocamldep.opt -modules src/graph.mli > src/graph.mli.depends # cached
8
+# Target: src/graph.cmi, tags: { byte, compile, extension:mli, file:src/graph.mli, include, interf, ocaml, quiet, traverse }
9
+/home/chepycou/.opam/4.14.0/bin/ocamlc.opt -c -I src -o src/graph.cmi src/graph.mli # cached
10
+# Target: src/gfile.cmi, tags: { byte, compile, extension:mli, file:src/gfile.mli, include, interf, ocaml, quiet, traverse }
11
+/home/chepycou/.opam/4.14.0/bin/ocamlc.opt -c -I src -o src/gfile.cmi src/gfile.mli # cached
12
+# Target: src/ftest.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:src/ftest.cmo, file:src/ftest.ml, implem, include, ocaml, quiet, traverse }
13
+/home/chepycou/.opam/4.14.0/bin/ocamlc.opt -c -I src -o src/ftest.cmo src/ftest.ml # cached
14
+# Target: src/gfile.ml.depends, tags: { extension:ml, file:src/gfile.ml, include, ocaml, ocamldep, quiet, traverse }
15
+/home/chepycou/.opam/4.14.0/bin/ocamldep.opt -modules src/gfile.ml > src/gfile.ml.depends # cached
16
+# Target: src/graph.ml.depends, tags: { extension:ml, file:src/graph.ml, include, ocaml, ocamldep, quiet, traverse }
17
+/home/chepycou/.opam/4.14.0/bin/ocamldep.opt -modules src/graph.ml > src/graph.ml.depends # cached
18
+# Target: src/graph.cmx, tags: { compile, extension:cmx, extension:ml, file:src/graph.cmx, file:src/graph.ml, implem, include, native, ocaml, quiet, traverse }
19
+/home/chepycou/.opam/4.14.0/bin/ocamlopt.opt -c -I src -o src/graph.cmx src/graph.ml # cached
20
+# Target: src/gfile.cmx, tags: { compile, extension:cmx, extension:ml, file:src/gfile.cmx, file:src/gfile.ml, implem, include, native, ocaml, quiet, traverse }
21
+/home/chepycou/.opam/4.14.0/bin/ocamlopt.opt -c -I src -o src/gfile.cmx src/gfile.ml # cached
22
+# Target: src/ftest.cmx, tags: { compile, extension:cmx, extension:ml, file:src/ftest.cmx, file:src/ftest.ml, implem, include, native, ocaml, quiet, traverse }
23
+/home/chepycou/.opam/4.14.0/bin/ocamlopt.opt -c -I src -o src/ftest.cmx src/ftest.ml # cached
24
+# Target: src/ftest.native, tags: { dont_link_with, extension:native, file:src/ftest.native, include, link, native, ocaml, program, quiet, traverse }
25
+/home/chepycou/.opam/4.14.0/bin/ocamlopt.opt -I src src/graph.cmx src/gfile.cmx src/ftest.cmx -o src/ftest.native # cached
26
+# Compilation successful.

+ 1
- 0
_build/ocamlc.where View File

@@ -0,0 +1 @@
1
+/home/chepycou/.opam/4.14.0/lib/ocaml

BIN
_build/src/ftest.cmi View File


BIN
_build/src/ftest.cmo View File


BIN
_build/src/ftest.cmx View File


+ 35
- 0
_build/src/ftest.ml View File

@@ -0,0 +1,35 @@
1
+open Gfile
2
+    
3
+let () =
4
+
5
+  (* Check the number of command-line arguments *)
6
+  if Array.length Sys.argv <> 5 then
7
+    begin
8
+      Printf.printf
9
+        "\n ✻  Usage: %s infile source sink outfile\n\n%s%!" Sys.argv.(0)
10
+        ("    🟄  infile  : input file containing a graph\n" ^
11
+         "    🟄  source  : identifier of the source vertex (used by the ford-fulkerson algorithm)\n" ^
12
+         "    🟄  sink    : identifier of the sink vertex (ditto)\n" ^
13
+         "    🟄  outfile : output file in which the result should be written.\n\n") ;
14
+      exit 0
15
+    end ;
16
+
17
+
18
+  (* Arguments are : infile(1) source-id(2) sink-id(3) outfile(4) *)
19
+  
20
+  let infile = Sys.argv.(1)
21
+  and outfile = Sys.argv.(4)
22
+  
23
+  (* These command-line arguments are not used for the moment. *)
24
+  and _source = int_of_string Sys.argv.(2)
25
+  and _sink = int_of_string Sys.argv.(3)
26
+  in
27
+
28
+  (* Open file *)
29
+  let graph = from_file infile in
30
+
31
+  (* Rewrite the graph that has been read. *)
32
+  let () = write_file outfile graph in
33
+
34
+  ()
35
+

+ 1
- 0
_build/src/ftest.ml.depends View File

@@ -0,0 +1 @@
1
+src/ftest.ml: Array Gfile Printf Sys

BIN
_build/src/ftest.native View File


BIN
_build/src/ftest.o View File


BIN
_build/src/gfile.cmi View File


BIN
_build/src/gfile.cmx View File


+ 101
- 0
_build/src/gfile.ml View File

@@ -0,0 +1,101 @@
1
+open Graph
2
+open Printf
3
+
4
+type path = string
5
+
6
+(* Format of text files:
7
+   % This is a comment
8
+
9
+   % A node with its coordinates (which are not used).
10
+   n 88.8 209.7
11
+   n 408.9 183.0
12
+
13
+   % The first node has id 0, the next is 1, and so on.
14
+
15
+   % Edges: e source dest label
16
+   e 3 1 11
17
+   e 0 2 8
18
+
19
+*)
20
+
21
+let write_file path graph =
22
+
23
+  (* Open a write-file. *)
24
+  let ff = open_out path in
25
+
26
+  (* Write in this file. *)
27
+  fprintf ff "%% This is a graph.\n\n" ;
28
+
29
+  (* Write all nodes (with fake coordinates) *)
30
+  n_iter_sorted graph (fun id -> fprintf ff "n %.1f 1.0\n" (float_of_int id)) ;
31
+  fprintf ff "\n" ;
32
+
33
+  (* Write all arcs *)
34
+  e_iter graph (fun id1 id2 lbl -> fprintf ff "e %d %d %s\n" id1 id2 lbl) ;
35
+
36
+  fprintf ff "\n%% End of graph\n" ;
37
+
38
+  close_out ff ;
39
+  ()
40
+
41
+(* Reads a line with a node. *)
42
+let read_node id graph line =
43
+  try Scanf.sscanf line "n %f %f" (fun _ _ -> new_node graph id)
44
+  with e ->
45
+    Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
46
+    failwith "from_file"
47
+
48
+(* Ensure that the given node exists in the graph. If not, create it. 
49
+ * (Necessary because the website we use to create online graphs does not generate correct files when some nodes have been deleted.) *)
50
+let ensure graph id = if node_exists graph id then graph else new_node graph id
51
+
52
+(* Reads a line with an arc. *)
53
+let read_arc graph line =
54
+  try Scanf.sscanf line "e %d %d %s"
55
+        (fun id1 id2 label -> new_arc (ensure (ensure graph id1) id2) id1 id2 label)
56
+  with e ->
57
+    Printf.printf "Cannot read arc in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
58
+    failwith "from_file"
59
+
60
+(* Reads a comment or fail. *)
61
+let read_comment graph line =
62
+  try Scanf.sscanf line " %%" graph
63
+  with _ ->
64
+    Printf.printf "Unknown line:\n%s\n%!" line ;
65
+    failwith "from_file"
66
+
67
+let from_file path =
68
+
69
+  let infile = open_in path in
70
+
71
+  (* Read all lines until end of file. 
72
+   * n is the current node counter. *)
73
+  let rec loop n graph =
74
+    try
75
+      let line = input_line infile in
76
+
77
+      (* Remove leading and trailing spaces. *)
78
+      let line = String.trim line in
79
+
80
+      let (n2, graph2) =
81
+        (* Ignore empty lines *)
82
+        if line = "" then (n, graph)
83
+
84
+        (* The first character of a line determines its content : n or e. *)
85
+        else match line.[0] with
86
+          | 'n' -> (n+1, read_node n graph line)
87
+          | 'e' -> (n, read_arc graph line)
88
+
89
+          (* It should be a comment, otherwise we complain. *)
90
+          | _ -> (n, read_comment graph line)
91
+      in      
92
+      loop n2 graph2
93
+
94
+    with End_of_file -> graph (* Done *)
95
+  in
96
+
97
+  let final_graph = loop 0 empty_graph in
98
+
99
+  close_in infile ;
100
+  final_graph
101
+

+ 1
- 0
_build/src/gfile.ml.depends View File

@@ -0,0 +1 @@
1
+src/gfile.ml: Graph Printexc Printf Scanf String

+ 18
- 0
_build/src/gfile.mli View File

@@ -0,0 +1,18 @@
1
+(* Read a graph from a file,
2
+ * Write a graph to a file. *)
3
+
4
+open Graph
5
+
6
+type path = string
7
+
8
+(* Values are read as strings. *)
9
+val from_file: path -> string graph
10
+
11
+(* Similarly, we write only a string graph.
12
+ * If necessary, use gmap (to be written by you) to prepare the input graph. *)
13
+val write_file: path -> string graph -> unit
14
+
15
+
16
+(* The format of files is compatible with the files generated by:
17
+   https://algorithms.discrete.ma.tum.de/graph-algorithms/flow-ford-fulkerson/index_en.html
18
+*)

+ 1
- 0
_build/src/gfile.mli.depends View File

@@ -0,0 +1 @@
1
+src/gfile.mli: Graph

BIN
_build/src/gfile.o View File


BIN
_build/src/graph.cmi View File


BIN
_build/src/graph.cmx View File


+ 49
- 0
_build/src/graph.ml View File

@@ -0,0 +1,49 @@
1
+type id = int
2
+
3
+type 'a out_arcs = (id * 'a) list
4
+
5
+(* A graph is just a list of pairs: a node & its outgoing arcs. *)
6
+type 'a graph = (id * 'a out_arcs) list
7
+
8
+exception Graph_error of string
9
+
10
+let empty_graph = []
11
+
12
+let node_exists gr id = List.mem_assoc id gr
13
+
14
+let out_arcs gr id =
15
+  try List.assoc id gr
16
+  with Not_found -> raise (Graph_error ("Node " ^ string_of_int id ^ " does not exist in this graph."))
17
+
18
+let find_arc gr id1 id2 =
19
+  let out = out_arcs gr id1 in
20
+  try Some (List.assoc id2 out)
21
+  with Not_found -> None
22
+
23
+let new_node gr id =
24
+  if node_exists gr id then raise (Graph_error ("Node " ^ string_of_int id ^ " already exists in the graph."))
25
+  else (id, []) :: gr
26
+
27
+let new_arc gr id1 id2 lbl =
28
+
29
+  (* Existing out-arcs *)
30
+  let outa = out_arcs gr id1 in
31
+
32
+  (* Update out-arcs.
33
+   * remove_assoc does not fail if id2 is not bound.  *)
34
+  let outb = (id2, lbl) :: List.remove_assoc id2 outa in
35
+  
36
+  (* Replace out-arcs in the graph. *)
37
+  let gr2 = List.remove_assoc id1 gr in
38
+  (id1, outb) :: gr2
39
+
40
+let n_iter gr f = List.iter (fun (id, _) -> f id) gr
41
+
42
+let n_iter_sorted gr f = n_iter (List.sort compare gr) f
43
+
44
+let n_fold gr f acu = List.fold_left (fun acu (id, _) -> f acu id) acu gr
45
+
46
+let e_iter gr f = List.iter (fun (id1, out) -> List.iter (fun (id2, x) -> f id1 id2 x) out) gr
47
+
48
+let e_fold gr f acu = List.fold_left (fun acu (id1, out) -> List.fold_left (fun acu (id2, x) -> f acu id1 id2 x) acu out) acu gr
49
+

+ 1
- 0
_build/src/graph.ml.depends View File

@@ -0,0 +1 @@
1
+src/graph.ml: List

+ 63
- 0
_build/src/graph.mli View File

@@ -0,0 +1,63 @@
1
+
2
+(* Type of a directed graph in which arcs have labels of type 'a. *)
3
+type 'a graph
4
+
5
+(* Each node has a unique identifier (a number). *)
6
+type id = int
7
+
8
+exception Graph_error of string
9
+
10
+
11
+(**************  CONSTRUCTORS  **************)
12
+
13
+(* The empty graph. *)
14
+val empty_graph: 'a graph
15
+
16
+(* Add a new node with the given identifier.
17
+ * @raise Graph_error if the id already exists. *)
18
+val new_node: 'a graph -> id -> 'a graph
19
+
20
+(* new_arc gr id1 id2 lbl  : adds an arc from node id1 to node id2 with label lbl
21
+ * Both nodes must already exist in the graph.
22
+ * If the arc already exists, its label is replaced by lbl. 
23
+ * @raise Graph_error if node id1 or id2 does not exist in the graph. *)
24
+val new_arc: 'a graph -> id -> id -> 'a -> 'a graph
25
+
26
+
27
+(**************  GETTERS  *****************)
28
+
29
+(* node_exists gr id  indicates if the node with identifier id exists in graph gr. *)
30
+val node_exists: 'a graph -> id -> bool
31
+
32
+(* Type of lists of outgoing arcs of a node. 
33
+ * An arc is represented by a pair of the destination identifier and the arc label. *)
34
+type 'a out_arcs = (id * 'a) list
35
+
36
+(* Find the out_arcs of a node.
37
+ * @raise Graph_error if the id is unknown in the graph. *)
38
+val out_arcs: 'a graph -> id -> 'a out_arcs
39
+
40
+(* find_arc gr id1 id2  finds an arc between id1 and id2 and returns its label. Returns None if the arc does not exist. 
41
+* @raise Graph_error if id1 is unknown. *)
42
+val find_arc: 'a graph -> id -> id -> 'a option
43
+
44
+
45
+(**************  COMBINATORS, ITERATORS  **************)
46
+
47
+(* Iterate on all nodes, in no special order. *)
48
+val n_iter: 'a graph -> (id -> unit) -> unit
49
+
50
+(* Like n_iter, but the nodes are sorted. *)
51
+val n_iter_sorted: 'a graph -> (id -> unit) -> unit
52
+  
53
+(* Fold on all (unsorted) nodes. You must remember what List.fold_left does. *)
54
+val n_fold: 'a graph -> ('b -> id -> 'b) -> 'b -> 'b
55
+
56
+
57
+(* Iter on all arcs (edges) *)
58
+val e_iter: 'a graph -> (id -> id -> 'a -> unit) -> unit
59
+
60
+(* Fold on all arcs (edges) *)
61
+val e_fold: 'a graph -> ('b -> id -> id -> 'a -> 'b) -> 'b -> 'b
62
+
63
+

+ 1
- 0
_build/src/graph.mli.depends View File

@@ -0,0 +1 @@
1
+src/graph.mli:

BIN
_build/src/graph.o View File


+ 1
- 0
ftest.native View File

@@ -0,0 +1 @@
1
+/home/chepycou/Documents/cours/4-IR/functionalProgramming/Projet/repoGit/ocaml-maxflow-project/_build/src/ftest.native

Loading…
Cancel
Save