comparison toolboxes/FullBNT-1.0.7/GraphViz/make_layout.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 [x, y] = layout_dag(adj)
2 % MAKE_LAYOUT Creates a layout from an adjacency matrix
3 %
4 % [X, Y] = MAKE_LAYOUT(ADJ)
5 %
6 % Inputs :
7 % ADJ = adjacency matrix (source, sink)
8 %
9 % Outputs :
10 % X, Y : Positions of nodes
11 %
12 % Usage Example : [X, Y] = make_layout(adj);
13 %
14 %
15 % Note : Uses some very simple heuristics, so any other
16 % algorithm would create a nicer layout
17 %
18 % See also
19
20 % Uses :
21
22 % Change History :
23 % Date Time Prog Note
24 % 13-Apr-2000 8:25 PM ATC Created under MATLAB 5.3.1.29215a (R11.1)
25
26 % ATC = Ali Taylan Cemgil,
27 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
28 % e-mail : cemgil@mbfys.kun.nl
29
30 N = size(adj,1);
31 tps = toposort(adj);
32
33 if ~isempty(tps), % is directed ?
34 level = zeros(1,N);
35 for i=tps,
36 idx = find(adj(:,i));
37 if ~isempty(idx),
38 l = max(level(idx));
39 level(i)=l+1;
40 end;
41 end;
42 else
43 level = poset(adj,1)'-1;
44 end;
45
46 y = (level+1)./(max(level)+2);
47 y = 1-y;
48 x = zeros(size(y));
49 for i=0:max(level),
50 idx = find(level==i);
51 offset = (rem(i,2)-0.5)/10;
52 x(idx) = (1:length(idx))./(length(idx)+1)+offset;
53 end;
54
55 %%%%%%%
56
57 function [depth] = poset(adj, root)
58 % POSET Identify a partial ordering among the nodes of a graph
59 %
60 % [DEPTH] = POSET(ADJ,ROOT)
61 %
62 % Inputs :
63 % ADJ : Adjacency Matrix
64 % ROOT : Node to start with
65 %
66 % Outputs :
67 % DEPTH : Depth of the Node
68 %
69 % Usage Example : [depth] = poset(adj,12);
70 %
71 %
72 % Note : All Nodes must be connected
73 % See also
74
75 % Uses :
76
77 % Change History :
78 % Date Time Prog Note
79 % 17-Jun-1998 12:01 PM ATC Created under MATLAB 5.1.0.421
80
81 % ATC = Ali Taylan Cemgil,
82 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
83 % e-mail : cemgil@mbfys.kun.nl
84
85 adj = adj+adj';
86
87 N = size(adj,1);
88 depth = zeros(N,1);
89 depth(root) = 1;
90 queue = root;
91
92 while 1,
93 if isempty(queue),
94 if all(depth), break;
95 else
96 root = find(depth==0);
97 root = root(1);
98 depth(root) = 1;
99 queue = root;
100 end;
101 end;
102 r = queue(1); queue(1) = [];
103 idx = find(adj(r,:));
104 idx2 = find(~depth(idx));
105 idx = idx(idx2);
106 queue = [queue idx];
107 depth(idx) = depth(r)+1;
108 end;
109
110 %%%%%%%%%
111
112 function [seq] = toposort(adj)
113 % TOPOSORT A Topological ordering of nodes in a directed graph
114 %
115 % [SEQ] = TOPOSORT(ADJ)
116 %
117 % Inputs :
118 % ADJ : Adjacency Matrix.
119 % ADJ(i,j)==1 ==> there exists a directed edge
120 % from i to j
121 %
122 % Outputs :
123 % SEQ : A topological ordered sequence of nodes.
124 % empty matrix if graph contains cycles.
125 %
126 % Usage Example :
127 % N=5;
128 % [l,u] = lu(rand(N));
129 % adj = ~diag(ones(1,N)) & u>0.5;
130 % seq = toposort(adj);
131 %
132 %
133 % Note :
134 % See also
135
136 % Uses :
137
138 % Change History :
139 % Date Time Prog Note
140 % 18-May-1998 4:44 PM ATC Created under MATLAB 5.1.0.421
141
142 % ATC = Ali Taylan Cemgil,
143 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
144 % e-mail : cemgil@mbfys.kun.nl
145
146 N = size(adj);
147 indeg = sum(adj,1);
148 outdeg = sum(adj,2);
149 seq = [];
150
151 for i=1:N,
152 % Find nodes with indegree 0
153 idx = find(indeg==0);
154 % If can't find than graph contains a cycle
155 if isempty(idx),
156 seq = [];
157 break;
158 end;
159 % Remove the node with the max number of connections
160 [dummy idx2] = max(outdeg(idx));
161 indx = idx(idx2);
162 seq = [seq, indx];
163 indeg(indx)=-1;
164 idx = find(adj(indx,:));
165 indeg(idx) = indeg(idx)-1;
166 end;
167
168
169
170