Skip to content

Commit 692615c

Browse files
committed
Add hierholzer.ml
1 parent e2c622f commit 692615c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ch4/hierholzer.ml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
open Scanf
2+
open Printf
3+
4+
type graph = int array array (* Adjacency List representation *)
5+
6+
let hierholzer (adj : graph) (start : int) : int list =
7+
let n = Array.length adj in
8+
let idx = Array.make n 0 in
9+
let rec iter path = function
10+
| [] -> path
11+
| u :: stack' as stack ->
12+
if idx.(u) < Array.length adj.(u) then begin
13+
let v = adj.(u).(idx.(u)) in
14+
idx.(u) <- idx.(u) + 1;
15+
iter path (v :: stack)
16+
end else
17+
iter (u :: path) stack' in
18+
iter [] [start]
19+
20+
21+
let () =
22+
let adj = [|
23+
[1; 6];
24+
[2];
25+
[3; 4];
26+
[0];
27+
[5];
28+
[0; 2];
29+
[5];
30+
|] |> Array.map Array.of_list in
31+
let answer = hierholzer adj 0 in
32+
answer |> List.iter (fun u -> printf "%c " (char_of_int (int_of_char 'A' + u)));
33+
printf "\n"

0 commit comments

Comments
 (0)