From ea8b79da8aa01ef17f798b5bf7dced5325bf00f3 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 2 May 2009 22:45:32 -0700 Subject: [PATCH] MST example for Prim's algorithm --- mst_prim.m | 28 ++++++++++++++++++++++++---- test/test_examples.m | 7 +++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/mst_prim.m b/mst_prim.m index 98b88d4..4f2c7b6 100644 --- a/mst_prim.m +++ b/mst_prim.m @@ -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,9 +34,11 @@ 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); @@ -29,7 +46,10 @@ % 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 - \ No newline at end of file + diff --git a/test/test_examples.m b/test/test_examples.m index 7171c34..a9cde07 100644 --- a/test/test_examples.m +++ b/test/test_examples.m @@ -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