diff --git a/outfile.svg b/outfile.svg new file mode 100644 index 0000000..e8ccf8f --- /dev/null +++ b/outfile.svg @@ -0,0 +1,119 @@ + + + + + + +finite_state_machine + + + +0 + +0 + + + +1 + +1 + + + +0->1 + + +7 + + + +2 + +2 + + + +0->2 + + +8 + + + +3 + +3 + + + +0->3 + + +10 + + + +4 + +4 + + + +1->4 + + +1 + + + +5 + +5 + + + +1->5 + + +21 + + + +2->4 + + +12 + + + +3->1 + + +11 + + + +3->2 + + +2 + + + +3->4 + + +5 + + + +4->5 + + +14 + + + diff --git a/src/ftest.ml b/src/ftest.ml index 4de8c1f..2891c80 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -24,7 +24,7 @@ let () = let graph = from_file infile in (* Rewrite the graph that has been read. *) - let () = write_file outfile graph in + let () = export outfile graph in () diff --git a/src/gfile.ml b/src/gfile.ml index 9599cc3..a50060b 100644 --- a/src/gfile.ml +++ b/src/gfile.ml @@ -18,6 +18,27 @@ type path = string *) +let export path graph = + (* Open a write-file. *) + let ff = open_out path in + + (* Write in this file. *) + fprintf ff "digraph finite_state_machine { + rankdir=LR; + size=\"8,5\" + node [shape = circle];\n"; + + (* Write all nodes *) + n_iter_sorted graph (fun id -> fprintf ff " %d " id) ; + fprintf ff ";\n" ; + + (* Write all arcs *) + e_iter graph (fun id1 id2 lbl -> fprintf ff " %d -> %d [ label = \"%s\"];\n" id1 id2 lbl) ; + + fprintf ff "}\n" ; + close_out ff ; + () + let write_file path graph = (* Open a write-file. *) 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/tools.ml b/src/tools.ml index 960a82a..c14e058 100644 --- a/src/tools.ml +++ b/src/tools.ml @@ -1,7 +1,16 @@ -(* Yes, we have to repeat open Graph. *) open Graph -(* assert false is of type ∀α.α, so the type-checker is happy. *) -let clone_nodes gr = assert false -let gmap gr f = assert false -let add_arc gr f = assert false \ No newline at end of file + +let rec clone_nodes gr = + n_fold gr (fun g id -> new_node g id) empty_graph + +let gmap gr f = + e_fold gr ( + fun g id_s id_d cost -> new_arc g id_s id_d (f cost) + ) (clone_nodes gr) + +let add_arc gr id1 id2 n = + let lbl = find_arc gr id1 id2 in + match lbl with + | None -> new_arc gr id1 id2 n + | Some cost -> new_arc gr id1 id2 (n + cost) \ No newline at end of file