Skip to content

Commit

Permalink
Add hierholzer.ml
Browse files Browse the repository at this point in the history
  • Loading branch information
pdnm committed Oct 11, 2020
1 parent 3ad1c89 commit 40cfe2b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ch4/hierholzer.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
open Scanf
open Printf

type graph = int array array (* Adjacency List representation *)

let hierholzer (adj : graph) (start : int) : int list =
let n = Array.length adj in
let idx = Array.make n 0 in
let rec iter path = function
| [] -> path
| u :: stack' as stack ->
if idx.(u) < Array.length adj.(u) then begin
let v = adj.(u).(idx.(u)) in
idx.(u) <- idx.(u) + 1;
iter path (v :: stack)
end else
iter (u :: path) stack' in
iter [] [start]


let () =
let adj = [|
[1; 6];
[2];
[3; 4];
[0];
[5];
[0; 2];
[5];
|] |> Array.map Array.of_list in
let answer = hierholzer adj 0 in
answer |> List.iter (fun u -> printf "%c " (char_of_int (int_of_char 'A' + u)));
printf "\n"

0 comments on commit 40cfe2b

Please sign in to comment.