···189189 | _, 0 -> rev xy :: list_of (eulerian_cycle out x)
190190 | 0, _ -> xy :: list_of (eulerian_cycle out y)
191191 | _ ->
192192- (* a bit of a pity to use list concatenation here,
193193- but this does not change the complexity *)
194194- list_of (eulerian_cycle out x) @
195195- xy :: list_of (eulerian_cycle out y)
192192+ let py = eulerian_cycle out y in
193193+ (* caveat: the cycle from y may exhaust edges from x *)
194194+ if out_degree out x = 0 then xy :: list_of py
195195+ else list_of (eulerian_cycle out x) @ xy :: list_of py
196196+ (* a bit of a pity to use list concatenation here,
197197+ but this does not change the complexity *)
196198 ) else (
197199 (* no edge x--y => add one, build a cycle, then remove it *)
198200 let dummy = E.label (snd (any (H.find out x))) in
···216218 let directed _g =
217219 invalid_arg "Eulerian.path (directed graphs not yet supported)"
218220219219- let path g =
220220- if is_directed then directed g else undirected g
221221+ let path =
222222+ if is_directed then directed else undirected
221223222224 let cycle g =
223225 let p, c = path g in
+8-4
tests/test_eulerian.ml
···8989 assert (List.length p = (n+1)*(2*n+1))
9090 done
91919292-(* let () =
9393- * let g, _ = Classic.grid ~n:2 ~m:3 in
9494- * assert (exists_path g);
9595- * assert (not (exists_cycle g)) *)
9292+let () =
9393+ (* +---x---+ tricky one, as the edge x-y
9494+ | | | connects two vertices on a cycle
9595+ +---y---+ *)
9696+ let g, _ = Classic.grid ~n:2 ~m:3 in
9797+ let p, c = Eulerian.path g in
9898+ assert (not c);
9999+ assert (List.length p = 7)
96100