function [mst, cost] = prim(A)
% User supplies adjacency matrix A.
This program uses Prim's algorithm
% to find a minimum spanning tree.
The edges of the minimum spanning
% tree are returned in array mst (of size n1 by 2), and the total cost
% is returned in variable cost.
The program prints out intermediate
% results and pauses so that user can see what is happening.
To continue
% after a pause, hit any key.
[n,n] = size(A);
% The matrix is n by n, where n = #
nodes.
A, n, pause,
if norm(AA','fro') ~= 0 ,
% If adjacency matrix is not symmetric,
disp(' Error:
Adjacency matrix must be symmetric ') % print error message and
quit.
return,
end;
% Start with node 1 and keep track of which nodes are in tree and which are not.
intree = [1];
number_in_tree = 1;
number_of_edges = 0;
notintree = [2:n]';
number_notin_tree = n1;
in = intree(1:number_in_tree),
% Print which nodes are in tree and
which
out = notintree(1:number_notin_tree), pause, % are not.
% Iterate until all n nodes are in tree.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '10
 Aravkin,AleksandrY
 Math, Graph Theory, adjacency matrix, Spanning tree, mincost

Click to edit the document details