mst_kruskal(g(NumVertices,Edges),MST) :-
  length(SetProxies,NumVertices),
  sort(Edges,SortedEdges),
  add_edges(SortedEdges,SetProxies,[],MST).

add_edges([],_,MST,MST).
add_edges([edge(Weight,U,V)|SortedEdges],SetProxies,Accum,MST) :-
  nth(U,SetProxies,UProxy),
  nth(V,SetProxies,VProxy),
  ( UProxy == VProxy -> add_edges(SortedEdges,SetProxies,Accum,MST)
  ; UProxy = VProxy, 
    add_edges(SortedEdges,SetProxies,[edge(Weight,U,V)|Accum],MST)
  ).

nth(1,[Head|_],Head).
nth(N,[_|Tail],Elt) :-
  N > 1,
  NewN is N - 1,
  nth(NewN,Tail,Elt).

  