math 381 - function [mst, cost] = prim(A) % % % % % % User...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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 n-1 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(A-A','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 = n-1; 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/22/2010 for the course MATH 11853 taught by Professor Aravkin,aleksandry during the Summer '10 term at University of Washington.

Page1 / 2

math 381 - function [mst, cost] = prim(A) % % % % % % User...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online