{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# math 381 - function[mst cost = prim(A User supplies...

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

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.

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.

{[ snackBarMessage ]}

### Page1 / 2

math 381 - function[mst cost = prim(A User supplies...

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

View Full Document
Ask a homework question - tutors are online