(* get sort from vNS.ml *)

let en = 
let rec numr l n = match l with [] -> [] |
    a::b -> (n, a)::(numr b (n + 1))
  in fun l -> numr l 0;;

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

let fi l = let sl = sort (<) (l ) 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) (en vl));;

let rec mpa l = match l with
     [] -> []
   | (a, b)::c -> (ref false, a)::(mpa c);;

exception Messed_index;;
let pe vl = let ar = Array.of_list (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 *)

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