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