about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/graph/mk_adj_mat.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/mk_adj_mat.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/mk_adj_mat.m')
-rw-r--r--sourcecodes/bnt-master/graph/mk_adj_mat.m36
1 files changed, 36 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/graph/mk_adj_mat.m b/sourcecodes/bnt-master/graph/mk_adj_mat.m
new file mode 100644
index 00000000..09f73d4a
--- /dev/null
+++ b/sourcecodes/bnt-master/graph/mk_adj_mat.m
@@ -0,0 +1,36 @@
+function [A, names] = mk_adj_mat(connections, names, topological)
+% MK_ADJ_MAT Make a directed adjacency matrix from a list of connections between named nodes.
+%
+% A = mk_adj_mat(connections, name)
+% This is best explaine by an example:
+%   names = {'WetGrass', 'Sprinkler', 'Cloudy', 'Rain'}; 
+%   connections = {'Cloudy', 'Sprinkler'; 'Cloudy', 'Rain'; 'Sprinkler', 'WetGrass'; 'Rain', 'WetGrass'}; 
+% adds the arcs C -> S, C -> R, S -> W, R -> W. Node 1 is W, 2 is S, 3 is C, 4 is R.
+%
+% [A, names] = mk_adj_mat(connections, name, 1)
+% The last argument of 1 indicates that we should topologically sort the nodes (parents before children).
+% In the example, the numbering becomes: node 1 is C, 2 is R, 3 is S, 4 is W
+% and the return value of names gets permuted to {'Cloudy', 'Rain', 'Sprinkler', 'WetGrass'}.
+% Note that topological sorting the graph is only possible if it has no directed cycles.
+
+if nargin < 3, topological = 0; end
+  
+n=length(names);
+A=zeros(n);
+[nr nc] = size(connections);
+for r=1:nr
+  from = strmatch(connections{r,1}, names, 'exact');
+  assert(~isempty(from));
+  to = strmatch(connections{r,2}, names, 'exact');
+  assert(~isempty(to));
+  %fprintf(1, 'from %s %d to %s %d\n', connections{r,1}, from, connections{r,2}, to);
+  A(from,to) = 1;
+end
+
+if topological
+  order = topological_sort(A); 
+  A = A(order, order); 
+  names = names(order); 
+end
+
+