annotate toolboxes/FullBNT-1.0.7/graph/minspan.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [t,nk] = minspan(IJC)
Daniel@0 2 %MINSPAN Minimum weight spanning tree using Kruskal algorithm.
Daniel@0 3 %[t,nk] = minspan(IJC)
Daniel@0 4 % IJC = n x 3 matrix arc list [i j c] of arc heads, tails, and costs
Daniel@0 5 % t = n-element logical vector, where
Daniel@0 6 % t(i) = 1, if IJC(i,:) arc in spanning tree
Daniel@0 7 % t(i) = k, if IJC(i,:) arc in component k of forest
Daniel@0 8 % nk = number of components
Daniel@0 9
Daniel@0 10 % Copyright (c) 1998-2001 by Michael G. Kay
Daniel@0 11 % Matlog Version 5 22-Aug-2001
Daniel@0 12
Daniel@0 13 % Input Error Checking ******************************************************
Daniel@0 14 [n,cIJC] = size(IJC);
Daniel@0 15 if cIJC ~= 3
Daniel@0 16 error('''IJC'' must be a 3-column matrix.')
Daniel@0 17 elseif n < 1
Daniel@0 18 error('''IJC'' must have at least one row.')
Daniel@0 19 elseif any(IJC(:,1) < 1) | any(any(~isint(IJC(:,[1 2]))))
Daniel@0 20 error('Invalid arc index in IJC.')
Daniel@0 21 end
Daniel@0 22 % End (Input Error Checking) ************************************************
Daniel@0 23
Daniel@0 24 i = IJC(:,1); j = abs(IJC(:,2));
Daniel@0 25 m = max(max([i j]));
Daniel@0 26
Daniel@0 27 sidxIJ = argsort(IJC(:,3));
Daniel@0 28 i = i(sidxIJ); j = j(sidxIJ);
Daniel@0 29
Daniel@0 30 t = logical(zeros(n,1));
Daniel@0 31 k = 1; % Current arc
Daniel@0 32 nt = 0; % Number of arcs in spanning tree
Daniel@0 33 v = (1:m)'; % Arc labels
Daniel@0 34
Daniel@0 35 while nt < m - 1 & k <= n
Daniel@0 36 if (v(i(k)) ~= v(j(k)))
Daniel@0 37 v(v==v(j(k))) = v(i(k));
Daniel@0 38 t(k) = 1;
Daniel@0 39 nt = nt + 1;
Daniel@0 40 end
Daniel@0 41 k = k + 1;
Daniel@0 42 end
Daniel@0 43
Daniel@0 44 idxIJ = invperm(sidxIJ);
Daniel@0 45 t = t(idxIJ); i = i(idxIJ); j = j(idxIJ);
Daniel@0 46
Daniel@0 47 c = unique(v(unique([i; j]))); % Unique labels of arc vertices
Daniel@0 48 nk = length(c);
Daniel@0 49 if ~any(t), nk = 0; end % Self-loop not a component
Daniel@0 50
Daniel@0 51 if nk > 1
Daniel@0 52 for k = 1:nk
Daniel@0 53 t(t~=0 & v(i)==c(k)) = k; % Relabel to consecutive component numbers
Daniel@0 54 end
Daniel@0 55 end
Daniel@0 56