From 2f6f51711a34bb08210c4a049b25ed115c061a0f Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 4 Nov 2020 22:03:27 +0100 Subject: [PATCH] clone_nodes, gmap and export functions --- src/ftest.ml | 5 +++-- src/gfile.ml | 20 +++++++++++++++++++- src/gfile.mli | 2 +- src/tool.ml | 15 +++++++++++++++ src/tool.mli | 9 +++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/tool.ml create mode 100644 src/tool.mli diff --git a/src/ftest.ml b/src/ftest.ml index 4de8c1f..7bdc64f 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -1,4 +1,5 @@ open Gfile +open Tool let () = @@ -24,7 +25,7 @@ let () = let graph = from_file infile in (* Rewrite the graph that has been read. *) - let () = write_file outfile graph in - + let () = write_file outfile (gmap graph (fun lbl -> string_of_int (int_of_string lbl *2) ) ) in + let () = export outfile graph in () diff --git a/src/gfile.ml b/src/gfile.ml index 9599cc3..abdc387 100644 --- a/src/gfile.ml +++ b/src/gfile.ml @@ -35,6 +35,8 @@ let write_file path graph = fprintf ff "\n%% End of graph\n" ; + + close_out ff ; () @@ -44,7 +46,7 @@ let read_node id graph line = with e -> Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ; failwith "from_file" - + (* Ensure that the given node exists in the graph. If not, create it. * (Necessary because the website we use to create online graphs does not generate correct files when some nodes have been deleted.) *) let ensure graph id = if node_exists graph id then graph else new_node graph id @@ -99,3 +101,19 @@ let from_file path = close_in infile ; final_graph + +(* Write the graph in a .dot file*) +let export path graph = + (* Open a write-file. *) + let ff = open_out (path^".dot") in + + (* Write in this file. *) + fprintf ff "digraph graphique1 {\n\tsize=\"20\"\n\tnode [shape = circle];\n"; + + (* Write all arcs *) + e_iter graph (fun id1 id2 lbl -> fprintf ff "\t%d -> %d [ label = \"%s\" ];\n" id1 id2 lbl) ; + + fprintf ff "}\n" ; + + close_out ff ; + () diff --git a/src/gfile.mli b/src/gfile.mli index 81a2c14..f94c5e4 100644 --- a/src/gfile.mli +++ b/src/gfile.mli @@ -12,7 +12,7 @@ val from_file: path -> string graph * If necessary, use gmap (to be written by you) to prepare the input graph. *) val write_file: path -> string graph -> unit - +val export: path -> string graph -> unit (* The format of files is compatible with the files generated by: https://www-m9.ma.tum.de/graph-algorithms/flow-ford-fulkerson/index_en.html diff --git a/src/tool.ml b/src/tool.ml new file mode 100644 index 0000000..ee487cd --- /dev/null +++ b/src/tool.ml @@ -0,0 +1,15 @@ +(* Yes, we have to repeat open Graph. *) +open Graph + +(* assert false is of type ∀α.α, so the type-checker is happy. *) + + +let clone_nodes gr = n_fold gr new_node empty_graph + + +(* Clone the nodes first then clone every arc but change their label by applying f*) +let gmap gr f = + let new_graph = clone_nodes gr in + e_fold gr (fun acu id1 id2 x -> new_arc acu id1 id2 (f x)) new_graph + +let add_arc g id1 id2 n = assert false diff --git a/src/tool.mli b/src/tool.mli new file mode 100644 index 0000000..0ca3797 --- /dev/null +++ b/src/tool.mli @@ -0,0 +1,9 @@ +open Graph + +(* Clone a graph by keeping only its nodes *) +val clone_nodes: 'a graph -> 'b graph + +(* Apply a function f to every label of the graph's arcs *) +val gmap: 'a graph -> ('a -> 'b) -> 'b graph + +val add_arc: int graph -> id -> id -> int -> int graph \ No newline at end of file