
\begin{tt}
\begin{verbatim}

module declad_eg5b.
export min_spanning_tree2(bfff,ffff).

/*  Selects (in a non-deterministic manner) minimum cost spanning trees.
   	min_spanning_tree2(root, start, end, cost)
    is true if (start, end, cost) is an edge in a selected spanning 
    tree rooted at root.   Can also be queried with root free.

    At each step, the set of edges of the form (from,to) with "from" in
    the current spanning tree is examined.  Of these, the edges with the
    least cost are chosen and added to the tree, subject to the
    aggregate selection (which ensures that each node has only one
    immediate predecessor in a tree).  Also, the chosen edges are
    discarded from "edge" and "chosen" so that more expensive edges 
    (which may be needed to reach other nodes) are considered 
    in the next step.

    Assumes a base relation edge(start, end, cost) 

*/ 

min_spanning_tree2(R,nil,R,0) :- is_source(R).
min_spanning_tree2(R,X,Y,C), del edge(X,Y,C) :- 
	min_spanning_tree2(R,Z,X,C1), chosen(R,X,Y,C).

is_source(R) :- edge(R, _, _).

@aggregate_selection min_spanning_tree2 (R,X,Y,C) (R,Y) any(X,C).

end_module.

module declad5_eg5b.
export chosen[bbff].

% Whenever "edge" is modified, we want the join in the body to match
% ALL tuples in spanning_tree with ALL remaining edge tuples.

chosen(R,X,Y,C) :- min_spanning_tree2(R,_,X,_), edge(X,Y,C).

@aggregate_selection chosen (R,X,Y,C) (R,X,Y) min(C).

end_module.


\end{verbatim}
\end{tt}
