about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/graph/minspan.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/graph/minspan.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/graph/minspan.m')
-rw-r--r--sourcecodes/bnt-master/graph/minspan.m56
1 files changed, 56 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/graph/minspan.m b/sourcecodes/bnt-master/graph/minspan.m
new file mode 100644
index 00000000..23d396fb
--- /dev/null
+++ b/sourcecodes/bnt-master/graph/minspan.m
@@ -0,0 +1,56 @@
+function [t,nk] = minspan(IJC)
+%MINSPAN Minimum weight spanning tree using Kruskal algorithm.
+%[t,nk] = minspan(IJC)
+%   IJC = n x 3 matrix arc list [i j c] of arc heads, tails, and costs
+%     t = n-element logical vector, where 
+%         t(i) = 1, if IJC(i,:) arc in spanning tree
+%         t(i) = k, if IJC(i,:) arc in component k of forest
+%    nk = number of components
+
+% Copyright (c) 1998-2001 by Michael G. Kay
+% Matlog Version 5 22-Aug-2001
+
+% Input Error Checking ******************************************************
+[n,cIJC] = size(IJC);
+if cIJC ~= 3
+   error('''IJC'' must be a 3-column matrix.')
+elseif n < 1
+   error('''IJC'' must have at least one row.')
+elseif any(IJC(:,1) < 1) | any(any(~isint(IJC(:,[1 2]))))
+   error('Invalid arc index in IJC.')
+end
+% End (Input Error Checking) ************************************************
+
+i = IJC(:,1); j = abs(IJC(:,2));
+m = max(max([i j]));
+
+sidxIJ = argsort(IJC(:,3));
+i = i(sidxIJ); j = j(sidxIJ);
+
+t = logical(zeros(n,1));
+k = 1;            % Current arc
+nt = 0;           % Number of arcs in spanning tree
+v = (1:m)';       % Arc labels
+
+while nt < m - 1 & k <= n
+   if (v(i(k)) ~= v(j(k)))
+      v(v==v(j(k))) = v(i(k));
+      t(k) = 1;
+      nt = nt + 1;
+   end
+   k = k + 1;
+end
+
+idxIJ = invperm(sidxIJ);
+t = t(idxIJ); i = i(idxIJ); j = j(idxIJ);
+
+c = unique(v(unique([i; j])));   % Unique labels of arc vertices
+nk = length(c);
+if ~any(t), nk = 0; end          % Self-loop not a component
+
+if nk > 1
+   for k = 1:nk
+      t(t~=0 & v(i)==c(k)) = k;  % Relabel to consecutive component numbers
+   end
+end
+