(* get sort from vNS.ml *)

(* let rec tv ls = match ls with [] -> true | [a] -> true
   | a::b::c -> a < b && tv (b::c);; *)
(* (define (tv a) (or (null? a) (let il ((v a)) (let ((t (cdr v)))
  (or (null? t) (and (< (car v) (car t)) (il t))))))) *)

let fi l = let sl = sort (<) l in
  let rec tv ls = match ls with [] -> true | [a] -> true
   | a::b::c -> a < b && tv (b::c) in (tv sl);;

exception Repeated_Vertex;;
let ww vl = (if not (fi vl) then raise Repeated_Vertex;
  sort (fun x y -> match (x, y) with
   ((a, b), (c, d)) -> b < d)
  (let rec numr l n = match l with [] -> [] |
    a::b -> (n, a)::(numr b (n + 1))
  in numr vl 0));;

exception Messed_index;;
let pe vl = let ar = Array.of_list (
   let rec mpa l = match l with
     [] -> [] | (a, b)::c -> (ref false, a)::(mpa c)
     in (mpa (ww vl))) in
 (let rec dp i p = (if i < 0 then p else let next = dp (i-1) in
  (match ar.(i) with (a, n) -> (if !a then (next (not p)) else (  
    (let rec lop v = (match ar.(v) with (w, x) -> (
      (if !w then raise Messed_index); w := true;
          (if v = i then () else lop x))) in (lop n));          
         next p))))
 in (dp ((Array.length ar) - 1) false));;
(* false is even parity *)

(* test
# pe [2; 5; 4; 7; 1];;
- : bool = true
*)