Permalink
Browse files

MST example for Prim's

algorithm
  • Loading branch information...
1 parent a702c33 commit ea8b79da8aa01ef17f798b5bf7dced5325bf00f3 David committed May 3, 2009
Showing with 31 additions and 4 deletions.
  1. +24 −4 mst_prim.m
  2. +7 −0 test/test_examples.m
View
@@ -11,6 +11,21 @@
% [ti tj tv] = mst_prim(...) returns the edges from the matrix and does not
% convert to a sparse matrix structure. This saves a bit of work and is
% required when there are 0 edge weights.
+%
+% Example:
+% load_gaimc_graph('airports'); % A(i,j) = negative travel time
+% A = -A; % convert to travel time.
+% A = max(A,A'); % make the travel times symmetric
+% T = mst_prim(A);
+% gplot(T,xy); % look at the minimum travel time tree in the US
+
+% David Gleich
+% Copyright, Stanford University, 2008-2009
+
+% History:
+% 2009-05-02: Added example
+
+% TODO: Add example
if ~exist('full','var') || isempty(full), full=0; end
if ~exist('target','var') || isempty(full), u=1; end
@@ -19,17 +34,22 @@
else [rp ci ai]=sparse_to_csr(A); check=1;
end
if check && any(ai)<0, error('gaimc:prim', ...
- 'prim''s algorithm cannot handle negative edge weights.'); end
+ 'prim''s algorithm cannot handle negative edge weights.');
+end
if check && ~isequal(A,A'), error('gaimc:prim', ...
- 'prim''s algorithm requires an undirected graph.'); end
+ 'prim''s algorithm requires an undirected graph.');
+end
nverts=length(rp)-1;
d=Inf*ones(nverts,1); T=zeros(nverts,1); L=zeros(nverts,1);
pred=zeros(1,length(rp)-1);
% enter the main dijkstra loop
for iter=1:nverts
if iter==1, root=u;
- else root=mod(u+iter-1,nverts)+1; if L(v)>0, continue; end, end
+ else
+ root=mod(u+iter-1,nverts)+1;
+ if L(v)>0, continue; end
+ end
n=1; T(n)=root; L(root)=n; % oops, n is now the size of the heap
d(root) = 0;
while n>0
@@ -114,4 +134,4 @@
else
varargout = {ti, tj, tv};
end
-
+
View
@@ -5,6 +5,13 @@
%% dfs
load_gaimc_graph('dfs_example.mat') % use the dfs example from Boost
d = dfs(A,1)
+
+%% mst_prim
+load_gaimc_graph('airports'); % A(i,j) = negative travel time
+A = -A; % convert to travel time.
+A = max(A,A'); % make the travel times symmetric
+T = mst_prim(A);
+gplot(T,xy); % look at the minimum travel time tree in the US
%% scomponents
% scomponents

0 comments on commit ea8b79d

Please sign in to comment.