add tools functons

This commit is contained in:
Arnaud Vergnet 2020-11-04 12:08:22 +01:00
parent 26f16658cf
commit 31c05c0f32
5 changed files with 156 additions and 7 deletions

119
outfile.svg Normal file
View file

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.43.0 (0)
-->
<!-- Title: finite_state_machine Pages: 1 -->
<svg width="388pt" height="155pt"
viewBox="0.00 0.00 388.00 155.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 151)">
<title>finite_state_machine</title>
<polygon fill="white" stroke="transparent" points="-4,4 -4,-151 384,-151 384,4 -4,4"/>
<!-- 0 -->
<g id="node1" class="node">
<title>0</title>
<ellipse fill="none" stroke="black" cx="18" cy="-72" rx="18" ry="18"/>
<text text-anchor="middle" x="18" y="-68.3" font-family="Times,serif" font-size="14.00">0</text>
</g>
<!-- 1 -->
<g id="node2" class="node">
<title>1</title>
<ellipse fill="none" stroke="black" cx="190" cy="-129" rx="18" ry="18"/>
<text text-anchor="middle" x="190" y="-125.3" font-family="Times,serif" font-size="14.00">1</text>
</g>
<!-- 0&#45;&gt;1 -->
<g id="edge3" class="edge">
<title>0&#45;&gt;1</title>
<path fill="none" stroke="black" d="M34.06,-80.87C40.15,-84.27 47.3,-88.04 54,-91 91.02,-107.38 100.76,-111.03 140,-121 147.07,-122.8 154.83,-124.28 162.04,-125.46"/>
<polygon fill="black" stroke="black" points="161.66,-128.94 172.07,-126.97 162.7,-122.02 161.66,-128.94"/>
<text text-anchor="middle" x="104" y="-119.8" font-family="Times,serif" font-size="14.00">7</text>
</g>
<!-- 2 -->
<g id="node3" class="node">
<title>2</title>
<ellipse fill="none" stroke="black" cx="190" cy="-18" rx="18" ry="18"/>
<text text-anchor="middle" x="190" y="-14.3" font-family="Times,serif" font-size="14.00">2</text>
</g>
<!-- 0&#45;&gt;2 -->
<g id="edge1" class="edge">
<title>0&#45;&gt;2</title>
<path fill="none" stroke="black" d="M32.43,-61.03C45.34,-51.14 65.8,-37.1 86,-30 110.59,-21.36 140.3,-18.65 161.49,-17.93"/>
<polygon fill="black" stroke="black" points="161.79,-21.43 171.71,-17.72 161.64,-14.43 161.79,-21.43"/>
<text text-anchor="middle" x="104" y="-33.8" font-family="Times,serif" font-size="14.00">8</text>
</g>
<!-- 3 -->
<g id="node4" class="node">
<title>3</title>
<ellipse fill="none" stroke="black" cx="104" cy="-72" rx="18" ry="18"/>
<text text-anchor="middle" x="104" y="-68.3" font-family="Times,serif" font-size="14.00">3</text>
</g>
<!-- 0&#45;&gt;3 -->
<g id="edge2" class="edge">
<title>0&#45;&gt;3</title>
<path fill="none" stroke="black" d="M36.4,-72C47.74,-72 62.77,-72 75.68,-72"/>
<polygon fill="black" stroke="black" points="75.9,-75.5 85.9,-72 75.9,-68.5 75.9,-75.5"/>
<text text-anchor="middle" x="61" y="-75.8" font-family="Times,serif" font-size="14.00">10</text>
</g>
<!-- 4 -->
<g id="node5" class="node">
<title>4</title>
<ellipse fill="none" stroke="black" cx="276" cy="-77" rx="18" ry="18"/>
<text text-anchor="middle" x="276" y="-73.3" font-family="Times,serif" font-size="14.00">4</text>
</g>
<!-- 1&#45;&gt;4 -->
<g id="edge8" class="edge">
<title>1&#45;&gt;4</title>
<path fill="none" stroke="black" d="M205.55,-119.22C211.8,-115.08 219.21,-110.23 226,-106 234.08,-100.96 243.01,-95.61 251.02,-90.89"/>
<polygon fill="black" stroke="black" points="253.02,-93.78 259.88,-85.7 249.48,-87.73 253.02,-93.78"/>
<text text-anchor="middle" x="233" y="-109.8" font-family="Times,serif" font-size="14.00">1</text>
</g>
<!-- 5 -->
<g id="node6" class="node">
<title>5</title>
<ellipse fill="none" stroke="black" cx="362" cy="-108" rx="18" ry="18"/>
<text text-anchor="middle" x="362" y="-104.3" font-family="Times,serif" font-size="14.00">5</text>
</g>
<!-- 1&#45;&gt;5 -->
<g id="edge9" class="edge">
<title>1&#45;&gt;5</title>
<path fill="none" stroke="black" d="M208.07,-127.11C233.69,-124.24 283.64,-118.53 326,-113 328.56,-112.67 331.22,-112.31 333.88,-111.94"/>
<polygon fill="black" stroke="black" points="334.51,-115.39 343.92,-110.52 333.53,-108.45 334.51,-115.39"/>
<text text-anchor="middle" x="276" y="-124.8" font-family="Times,serif" font-size="14.00">21</text>
</g>
<!-- 2&#45;&gt;4 -->
<g id="edge4" class="edge">
<title>2&#45;&gt;4</title>
<path fill="none" stroke="black" d="M207.14,-24.75C217.04,-29.24 229.75,-35.66 240,-43 245.51,-46.95 251,-51.75 255.94,-56.49"/>
<polygon fill="black" stroke="black" points="253.64,-59.14 263.18,-63.75 258.6,-54.2 253.64,-59.14"/>
<text text-anchor="middle" x="233" y="-46.8" font-family="Times,serif" font-size="14.00">12</text>
</g>
<!-- 3&#45;&gt;1 -->
<g id="edge7" class="edge">
<title>3&#45;&gt;1</title>
<path fill="none" stroke="black" d="M119.25,-81.67C132.04,-90.35 151.05,-103.25 165.99,-113.39"/>
<polygon fill="black" stroke="black" points="164.44,-116.56 174.68,-119.28 168.37,-110.77 164.44,-116.56"/>
<text text-anchor="middle" x="147" y="-106.8" font-family="Times,serif" font-size="14.00">11</text>
</g>
<!-- 3&#45;&gt;2 -->
<g id="edge6" class="edge">
<title>3&#45;&gt;2</title>
<path fill="none" stroke="black" d="M117.76,-60.27C124.18,-54.68 132.22,-48.12 140,-43 147.49,-38.07 156.07,-33.4 163.94,-29.46"/>
<polygon fill="black" stroke="black" points="165.59,-32.55 173.06,-25.04 162.54,-26.25 165.59,-32.55"/>
<text text-anchor="middle" x="147" y="-46.8" font-family="Times,serif" font-size="14.00">2</text>
</g>
<!-- 3&#45;&gt;4 -->
<g id="edge5" class="edge">
<title>3&#45;&gt;4</title>
<path fill="none" stroke="black" d="M122.24,-72.51C151.84,-73.38 212.57,-75.16 247.89,-76.2"/>
<polygon fill="black" stroke="black" points="247.87,-79.7 257.97,-76.5 248.08,-72.71 247.87,-79.7"/>
<text text-anchor="middle" x="190" y="-77.8" font-family="Times,serif" font-size="14.00">5</text>
</g>
<!-- 4&#45;&gt;5 -->
<g id="edge10" class="edge">
<title>4&#45;&gt;5</title>
<path fill="none" stroke="black" d="M293.2,-82.98C305.17,-87.39 321.7,-93.5 335.44,-98.57"/>
<polygon fill="black" stroke="black" points="334.26,-101.86 344.85,-102.04 336.68,-95.3 334.26,-101.86"/>
<text text-anchor="middle" x="319" y="-97.8" font-family="Times,serif" font-size="14.00">14</text>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.9 KiB

View file

@ -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
()

View file

@ -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. *)

View file

@ -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

View file

@ -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
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)