Le module Graph contient désormais un type arc.
(Ce qui rend la fonction fold plus lisible) Mise à jour des fichiers de graphe. Passage à dune au lieu de ocamlbuild.
This commit is contained in:
parent
336ab07955
commit
c1c2f561dd
11 changed files with 125 additions and 100 deletions
22
Makefile
22
Makefile
|
|
@ -1,7 +1,14 @@
|
|||
.PHONY: all build format edit demo clean
|
||||
|
||||
src?=1
|
||||
dst?=2
|
||||
|
||||
all: build
|
||||
|
||||
build:
|
||||
@echo "\n==== COMPILING ====\n"
|
||||
ocamlbuild ftest.native
|
||||
@echo "\n ⚙ COMPILING ⚙\n"
|
||||
dune build src/ftest.exe
|
||||
ls src/*.exe > /dev/null && ln -fs src/*.exe .
|
||||
|
||||
format:
|
||||
ocp-indent --inplace src/*
|
||||
|
|
@ -10,11 +17,12 @@ edit:
|
|||
code . -n
|
||||
|
||||
demo: build
|
||||
@echo "\n==== EXECUTING ====\n"
|
||||
./ftest.native graphs/graph1 1 2 outfile
|
||||
@echo "\n==== RESULT ==== (content of outfile) \n"
|
||||
@echo "\n 🏁 EXECUTING 🏁\n"
|
||||
./ftest.exe graphs/graph1 $(src) $(dst) outfile
|
||||
@echo "\n 🥁 RESULT (content of outfile) 🥁\n"
|
||||
@cat outfile
|
||||
|
||||
clean:
|
||||
-rm -rf _build/
|
||||
-rm ftest.native
|
||||
find -L . -name "*~" -delete
|
||||
rm -f *.exe
|
||||
dune clean
|
||||
|
|
|
|||
3
_tags
3
_tags
|
|
@ -1,3 +0,0 @@
|
|||
<src/**>: include
|
||||
|
||||
|
||||
6
dune-project
Normal file
6
dune-project
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(lang dune 2.0)
|
||||
|
||||
(name maxflow)
|
||||
|
||||
|
||||
|
||||
1
ftest.exe
Symbolic link
1
ftest.exe
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
src/ftest.exe
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
%% Test graph #1
|
||||
|
||||
%% Nodes
|
||||
|
||||
n 88 209 % This is node #0, with its coordinates (which are not used by the algorithms).
|
||||
n 408 183
|
||||
n 269 491
|
||||
n 261 297
|
||||
n 401 394
|
||||
n 535 294 % This is node #5.
|
||||
|
||||
|
||||
%% Edges
|
||||
|
||||
e 3 1 11 % An edge from 3 to 1, labeled "11".
|
||||
e 3 2 2
|
||||
e 1 5 21
|
||||
e 4 5 14
|
||||
e 1 4 1
|
||||
e 0 1 7
|
||||
e 0 3 10
|
||||
e 3 4 5
|
||||
e 2 4 12
|
||||
e 0 2 8
|
||||
|
|
@ -1,13 +1,18 @@
|
|||
% This is a graph.
|
||||
%% Test graph #1
|
||||
|
||||
n 20 300 0
|
||||
%% Nodes
|
||||
|
||||
n 20 300 0 % This is node #0, with its coordinates (which are not used by the algorithms).
|
||||
n 200 300 1
|
||||
n 200 200 2
|
||||
n 200 400 3
|
||||
n 380 300 4
|
||||
n 380 200 5
|
||||
n 380 200 5 % This is node #5.
|
||||
|
||||
e 0 2 0 8
|
||||
|
||||
%% Edges (arcs)
|
||||
|
||||
e 0 2 0 8 % An arc from 0 to 2, labeled "8". The second 0 is useless.
|
||||
e 0 3 1 10
|
||||
e 0 1 2 7
|
||||
e 2 4 3 12
|
||||
|
|
@ -16,6 +21,6 @@ e 3 2 5 2
|
|||
e 3 1 6 11
|
||||
e 1 4 7 1
|
||||
e 1 5 8 21
|
||||
e 4 5 9 14
|
||||
e 4 5 9 14 % An arc from 4 to 5 labeled 14. The 9 is useless.
|
||||
|
||||
% End of graph
|
||||
|
|
|
|||
13
src/dune
Normal file
13
src/dune
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
(executable
|
||||
(name ftest)
|
||||
;; (libraries )
|
||||
|
||||
;; Disable some warnings
|
||||
;; (flags (:standard -w -6))
|
||||
|
||||
;; Link to executable in main dir
|
||||
(promote (until-clean))
|
||||
|
||||
)
|
||||
|
||||
BIN
src/ftest.exe
Executable file
BIN
src/ftest.exe
Executable file
Binary file not shown.
71
src/gfile.ml
71
src/gfile.ml
|
|
@ -1,23 +1,37 @@
|
|||
open Graph
|
||||
open Printf
|
||||
|
||||
|
||||
type path = string
|
||||
|
||||
(* Format of text files:
|
||||
% This is a comment
|
||||
|
||||
% A node with its coordinates (which are not used).
|
||||
n 88.8 209.7
|
||||
n 408.9 183.0
|
||||
% A node with its coordinates (which are not used), and its id.
|
||||
n 88.8 209.7 0
|
||||
n 408.9 183.0 1
|
||||
|
||||
% The first node has id 0, the next is 1, and so on.
|
||||
|
||||
% Edges: e source dest label
|
||||
e 3 1 11
|
||||
e 0 2 8
|
||||
% Edges: e source dest label id (the edge id is not used).
|
||||
e 3 1 11 0
|
||||
e 0 2 8 1
|
||||
|
||||
*)
|
||||
|
||||
(* Compute arbitrary position for a node. Center is 300,300 *)
|
||||
let iof = int_of_float
|
||||
let foi = float_of_int
|
||||
|
||||
let index_i id = iof (sqrt (foi id *. 1.1))
|
||||
|
||||
let compute_x id = 20 + 180 * index_i id
|
||||
|
||||
let compute_y id =
|
||||
let i0 = index_i id in
|
||||
let delta = id - (i0 * i0 * 10 / 11) in
|
||||
let sgn = if delta mod 2 = 0 then -1 else 1 in
|
||||
|
||||
300 + sgn * (delta / 2) * 100
|
||||
|
||||
|
||||
let write_file path graph =
|
||||
|
||||
(* Open a write-file. *)
|
||||
|
|
@ -27,20 +41,20 @@ let write_file path graph =
|
|||
fprintf ff "%% This is a graph.\n\n" ;
|
||||
|
||||
(* Write all nodes (with fake coordinates) *)
|
||||
n_iter_sorted graph (fun id -> fprintf ff "n %.1f 1.0\n" (float_of_int id)) ;
|
||||
n_iter_sorted graph (fun id -> fprintf ff "n %d %d %d\n" (compute_x id) (compute_y id) id) ;
|
||||
fprintf ff "\n" ;
|
||||
|
||||
(* Write all arcs *)
|
||||
e_iter graph (fun id1 id2 lbl -> fprintf ff "e %d %d %s\n" id1 id2 lbl) ;
|
||||
|
||||
let _ = e_fold graph (fun count arc -> fprintf ff "e %d %d %d %s\n" arc.src arc.tgt count arc.lbl ; count + 1) 0 in
|
||||
|
||||
fprintf ff "\n%% End of graph\n" ;
|
||||
|
||||
|
||||
close_out ff ;
|
||||
()
|
||||
|
||||
(* Reads a line with a node. *)
|
||||
let read_node id graph line =
|
||||
try Scanf.sscanf line "n %f %f" (fun _ _ -> new_node graph id)
|
||||
let read_node graph line =
|
||||
try Scanf.sscanf line "n %f %f %d" (fun _ _ id -> new_node graph id)
|
||||
with e ->
|
||||
Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
|
||||
failwith "from_file"
|
||||
|
|
@ -51,8 +65,8 @@ let ensure graph id = if node_exists graph id then graph else new_node graph id
|
|||
|
||||
(* Reads a line with an arc. *)
|
||||
let read_arc graph line =
|
||||
try Scanf.sscanf line "e %d %d %s"
|
||||
(fun id1 id2 label -> new_arc (ensure (ensure graph id1) id2) id1 id2 label)
|
||||
try Scanf.sscanf line "e %d %d %_d %s@%%"
|
||||
(fun src tgt lbl -> new_arc (ensure (ensure graph src) tgt) { src ; tgt ; lbl } )
|
||||
with e ->
|
||||
Printf.printf "Cannot read arc in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
|
||||
failwith "from_file"
|
||||
|
|
@ -68,34 +82,33 @@ let from_file path =
|
|||
|
||||
let infile = open_in path in
|
||||
|
||||
(* Read all lines until end of file.
|
||||
* n is the current node counter. *)
|
||||
let rec loop n graph =
|
||||
(* Read all lines until end of file. *)
|
||||
let rec loop graph =
|
||||
try
|
||||
let line = input_line infile in
|
||||
|
||||
(* Remove leading and trailing spaces. *)
|
||||
let line = String.trim line in
|
||||
|
||||
let (n2, graph2) =
|
||||
let graph2 =
|
||||
(* Ignore empty lines *)
|
||||
if line = "" then (n, graph)
|
||||
if line = "" then graph
|
||||
|
||||
(* The first character of a line determines its content : n or e. *)
|
||||
else match line.[0] with
|
||||
| 'n' -> (n+1, read_node n graph line)
|
||||
| 'e' -> (n, read_arc graph line)
|
||||
| 'n' -> read_node graph line
|
||||
| 'e' -> read_arc graph line
|
||||
|
||||
(* It should be a comment, otherwise we complain. *)
|
||||
| _ -> (n, read_comment graph line)
|
||||
| _ -> read_comment graph line
|
||||
in
|
||||
loop n2 graph2
|
||||
loop graph2
|
||||
|
||||
with End_of_file -> graph (* Done *)
|
||||
in
|
||||
|
||||
let final_graph = loop 0 empty_graph in
|
||||
|
||||
let final_graph = loop empty_graph in
|
||||
|
||||
close_in infile ;
|
||||
final_graph
|
||||
|
||||
|
||||
|
|
|
|||
29
src/graph.ml
29
src/graph.ml
|
|
@ -1,9 +1,12 @@
|
|||
type id = int
|
||||
|
||||
type 'a out_arcs = (id * 'a) list
|
||||
type 'a arc =
|
||||
{ src: id ;
|
||||
tgt: id ;
|
||||
lbl: 'a }
|
||||
|
||||
(* A graph is just a list of pairs: a node & its outgoing arcs. *)
|
||||
type 'a graph = (id * 'a out_arcs) list
|
||||
type 'a graph = (id * 'a arc list) list
|
||||
|
||||
exception Graph_error of string
|
||||
|
||||
|
|
@ -15,27 +18,23 @@ let out_arcs gr id =
|
|||
try List.assoc id gr
|
||||
with Not_found -> raise (Graph_error ("Node " ^ string_of_int id ^ " does not exist in this graph."))
|
||||
|
||||
let find_arc gr id1 id2 =
|
||||
let out = out_arcs gr id1 in
|
||||
try Some (List.assoc id2 out)
|
||||
with Not_found -> None
|
||||
let find_arc gr id1 id2 = List.find_opt (fun arc -> arc.tgt = id2) (out_arcs gr id1)
|
||||
|
||||
let new_node gr id =
|
||||
if node_exists gr id then raise (Graph_error ("Node " ^ string_of_int id ^ " already exists in the graph."))
|
||||
else (id, []) :: gr
|
||||
|
||||
let new_arc gr id1 id2 lbl =
|
||||
let new_arc gr arc =
|
||||
|
||||
(* Existing out-arcs *)
|
||||
let outa = out_arcs gr id1 in
|
||||
let outa = out_arcs gr arc.src in
|
||||
|
||||
(* Update out-arcs.
|
||||
* remove_assoc does not fail if id2 is not bound. *)
|
||||
let outb = (id2, lbl) :: List.remove_assoc id2 outa in
|
||||
(* Update out-arcs. *)
|
||||
let outb = arc :: List.filter (fun a -> a.tgt <> arc.tgt) outa in
|
||||
|
||||
(* Replace out-arcs in the graph. *)
|
||||
let gr2 = List.remove_assoc id1 gr in
|
||||
(id1, outb) :: gr2
|
||||
let gr2 = List.remove_assoc arc.src gr in
|
||||
(arc.src, outb) :: gr2
|
||||
|
||||
let n_iter gr f = List.iter (fun (id, _) -> f id) gr
|
||||
|
||||
|
|
@ -43,7 +42,7 @@ let n_iter_sorted gr f = n_iter (List.sort compare gr) f
|
|||
|
||||
let n_fold gr f acu = List.fold_left (fun acu (id, _) -> f acu id) acu gr
|
||||
|
||||
let e_iter gr f = List.iter (fun (id1, out) -> List.iter (fun (id2, x) -> f id1 id2 x) out) gr
|
||||
let e_iter gr f = List.iter (fun (_, out) -> List.iter f out) gr
|
||||
|
||||
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
|
||||
let e_fold gr f acu = List.fold_left (fun acu (_, out) -> List.fold_left (fun acu arc -> f acu arc) acu out) acu gr
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,22 @@
|
|||
|
||||
(* Type of a directed graph in which arcs have labels of type 'a. *)
|
||||
(* Type of a directed graph in which arcs have labels of type 'a.
|
||||
* This type is deliberately abstract. *)
|
||||
type 'a graph
|
||||
|
||||
(* Each node has a unique identifier (a number). *)
|
||||
type id = int
|
||||
|
||||
(* Type of an arc (edge) with labels of type 'a *)
|
||||
type 'a arc =
|
||||
{ (* Source *)
|
||||
src: id ;
|
||||
|
||||
(* Target *)
|
||||
tgt: id ;
|
||||
|
||||
(* Label *)
|
||||
lbl: 'a }
|
||||
|
||||
exception Graph_error of string
|
||||
|
||||
|
||||
|
|
@ -17,11 +29,11 @@ val empty_graph: 'a graph
|
|||
* @raise Graph_error if the id already exists. *)
|
||||
val new_node: 'a graph -> id -> 'a graph
|
||||
|
||||
(* new_arc gr id1 id2 lbl : adds an arc from node id1 to node id2 with label lbl
|
||||
* Both nodes must already exist in the graph.
|
||||
(* new_arc gr arc : adds an arc.
|
||||
* Both arc nodes must already exist in the graph.
|
||||
* If the arc already exists, its label is replaced by lbl.
|
||||
* @raise Graph_error if node id1 or id2 does not exist in the graph. *)
|
||||
val new_arc: 'a graph -> id -> id -> 'a -> 'a graph
|
||||
* @raise Graph_error if node src or tgt does not exist in the graph. *)
|
||||
val new_arc: 'a graph -> 'a arc -> 'a graph
|
||||
|
||||
|
||||
(************** GETTERS *****************)
|
||||
|
|
@ -29,17 +41,13 @@ val new_arc: 'a graph -> id -> id -> 'a -> 'a graph
|
|||
(* node_exists gr id indicates if the node with identifier id exists in graph gr. *)
|
||||
val node_exists: 'a graph -> id -> bool
|
||||
|
||||
(* Type of lists of outgoing arcs of a node.
|
||||
* An arc is represented by a pair of the destination identifier and the arc label. *)
|
||||
type 'a out_arcs = (id * 'a) list
|
||||
|
||||
(* Find the out_arcs of a node.
|
||||
* @raise Graph_error if the id is unknown in the graph. *)
|
||||
val out_arcs: 'a graph -> id -> 'a out_arcs
|
||||
val out_arcs: 'a graph -> id -> 'a arc list
|
||||
|
||||
(* find_arc gr id1 id2 finds an arc between id1 and id2 and returns its label. Returns None if the arc does not exist.
|
||||
(* find_arc gr id1 id2 finds an arc between id1 and id2. Returns None if the arc does not exist.
|
||||
* @raise Graph_error if id1 is unknown. *)
|
||||
val find_arc: 'a graph -> id -> id -> 'a option
|
||||
val find_arc: 'a graph -> id -> id -> 'a arc option
|
||||
|
||||
|
||||
(************** COMBINATORS, ITERATORS **************)
|
||||
|
|
@ -54,10 +62,9 @@ val n_iter_sorted: 'a graph -> (id -> unit) -> unit
|
|||
val n_fold: 'a graph -> ('b -> id -> 'b) -> 'b -> 'b
|
||||
|
||||
|
||||
(* Iter on all arcs (edges) *)
|
||||
val e_iter: 'a graph -> (id -> id -> 'a -> unit) -> unit
|
||||
|
||||
(* Fold on all arcs (edges) *)
|
||||
val e_fold: 'a graph -> ('b -> id -> id -> 'a -> 'b) -> 'b -> 'b
|
||||
(* Iter on all arcs (edges). *)
|
||||
val e_iter: 'a graph -> ('a arc -> unit) -> unit
|
||||
|
||||
(* Fold on all arcs (edges). *)
|
||||
val e_fold: 'a graph -> ('b -> 'a arc -> 'b) -> 'b -> 'b
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue