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