annotate toolboxes/FullBNT-1.0.7/graph/mk_adj_mat.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 [A, names] = mk_adj_mat(connections, names, topological)
Daniel@0 2 % MK_ADJ_MAT Make a directed adjacency matrix from a list of connections between named nodes.
Daniel@0 3 %
Daniel@0 4 % A = mk_adj_mat(connections, name)
Daniel@0 5 % This is best explaine by an example:
Daniel@0 6 % names = {'WetGrass', 'Sprinkler', 'Cloudy', 'Rain'};
Daniel@0 7 % connections = {'Cloudy', 'Sprinkler'; 'Cloudy', 'Rain'; 'Sprinkler', 'WetGrass'; 'Rain', 'WetGrass'};
Daniel@0 8 % adds the arcs C -> S, C -> R, S -> W, R -> W. Node 1 is W, 2 is S, 3 is C, 4 is R.
Daniel@0 9 %
Daniel@0 10 % [A, names] = mk_adj_mat(connections, name, 1)
Daniel@0 11 % The last argument of 1 indicates that we should topologically sort the nodes (parents before children).
Daniel@0 12 % In the example, the numbering becomes: node 1 is C, 2 is R, 3 is S, 4 is W
Daniel@0 13 % and the return value of names gets permuted to {'Cloudy', 'Rain', 'Sprinkler', 'WetGrass'}.
Daniel@0 14 % Note that topological sorting the graph is only possible if it has no directed cycles.
Daniel@0 15
Daniel@0 16 if nargin < 3, topological = 0; end
Daniel@0 17
Daniel@0 18 n=length(names);
Daniel@0 19 A=zeros(n);
Daniel@0 20 [nr nc] = size(connections);
Daniel@0 21 for r=1:nr
Daniel@0 22 from = strmatch(connections{r,1}, names, 'exact');
Daniel@0 23 assert(~isempty(from));
Daniel@0 24 to = strmatch(connections{r,2}, names, 'exact');
Daniel@0 25 assert(~isempty(to));
Daniel@0 26 %fprintf(1, 'from %s %d to %s %d\n', connections{r,1}, from, connections{r,2}, to);
Daniel@0 27 A(from,to) = 1;
Daniel@0 28 end
Daniel@0 29
Daniel@0 30 if topological
Daniel@0 31 order = topological_sort(A);
Daniel@0 32 A = A(order, order);
Daniel@0 33 names = names(order);
Daniel@0 34 end
Daniel@0 35
Daniel@0 36