clone_nodes, gmap and export functions
This commit is contained in:
parent
1ec2eed82f
commit
2f6f51711a
5 changed files with 47 additions and 4 deletions
|
@ -1,4 +1,5 @@
|
||||||
open Gfile
|
open Gfile
|
||||||
|
open Tool
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
|
|
||||||
|
@ -24,7 +25,7 @@ let () =
|
||||||
let graph = from_file infile in
|
let graph = from_file infile in
|
||||||
|
|
||||||
(* Rewrite the graph that has been read. *)
|
(* 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
|
||||||
()
|
()
|
||||||
|
|
||||||
|
|
20
src/gfile.ml
20
src/gfile.ml
|
@ -35,6 +35,8 @@ let write_file path graph =
|
||||||
|
|
||||||
fprintf ff "\n%% End of graph\n" ;
|
fprintf ff "\n%% End of graph\n" ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
close_out ff ;
|
close_out ff ;
|
||||||
()
|
()
|
||||||
|
|
||||||
|
@ -44,7 +46,7 @@ let read_node id graph line =
|
||||||
with e ->
|
with e ->
|
||||||
Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
|
Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ;
|
||||||
failwith "from_file"
|
failwith "from_file"
|
||||||
|
|
||||||
(* Ensure that the given node exists in the graph. If not, create it.
|
(* 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.) *)
|
* (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
|
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 ;
|
close_in infile ;
|
||||||
final_graph
|
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 ;
|
||||||
|
()
|
||||||
|
|
|
@ -12,7 +12,7 @@ val from_file: path -> string graph
|
||||||
* If necessary, use gmap (to be written by you) to prepare the input graph. *)
|
* If necessary, use gmap (to be written by you) to prepare the input graph. *)
|
||||||
val write_file: path -> string graph -> unit
|
val write_file: path -> string graph -> unit
|
||||||
|
|
||||||
|
val export: path -> string graph -> unit
|
||||||
|
|
||||||
(* The format of files is compatible with the files generated by:
|
(* 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
|
https://www-m9.ma.tum.de/graph-algorithms/flow-ford-fulkerson/index_en.html
|
||||||
|
|
15
src/tool.ml
Normal file
15
src/tool.ml
Normal file
|
@ -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
|
9
src/tool.mli
Normal file
9
src/tool.mli
Normal file
|
@ -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
|
Loading…
Reference in a new issue