functor KRUSKAL( structure G: sig type Edge type Vertex type Graph val ends : Edge -> Vertex * Vertex (* graph *) val edges: Graph -> Edge list end structure PQ: QueueSig (* a priority queue of edges *) structure F: SetSig (* a set of edges *) structure P: sig type Part eqtype Item val empty: Part val union: Part -> (Item*Item) -> Part val find : Part -> Item -> Item end sharing type F.Item = PQ.Item = G.Edge and type P.Item = G.Vertex ) = struct type Graph = G.Graph and Forest = F.Set fun span (g: Graph) : Forest = let fun grow edgeQ forest p = if PQ.isEmpty edgeQ then forest else let val (e,q) = PQ.deq edgeQ val (u,v) = G.ends e in if P.find p u = P.find p v then grow q forest p else grow q (F.insert(e, forest)) (P.union p (u,v)) end in grow (PQ.menq (G.edges g, PQ.empty)) F.empty P.empty end end;