To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / graph / scc.m @ 8:b5b38998ef3b
History | View | Annotate | Download (1.36 KB)
| 1 |
function [c,v] = scc(a,tol) |
|---|---|
| 2 |
|
| 3 |
% Finds the strongly connected sets of vertices |
| 4 |
% in the DI-rected G-raph of A |
| 5 |
% c = 0-1 matrix displaying accessibility |
| 6 |
% v = displays the equivalent classes |
| 7 |
% |
| 8 |
% v(i,j) is the j'th member of the i'th equiv class (0 padded) |
| 9 |
% |
| 10 |
% http://www.math.wsu.edu/math/faculty/tsat/matlab.html |
| 11 |
|
| 12 |
[m,n] = size(a); |
| 13 |
if m~=n 'Not a Square Matrix', break, end |
| 14 |
b=abs(a); o=ones(size(a)); x=zeros(1,n); |
| 15 |
msg='The Matrix is Irreducible !'; v='Connected Directed Graph !'; |
| 16 |
if (nargin==1) tol=n*eps*norm(a,'inf'); end |
| 17 |
|
| 18 |
% Create a companion matrix |
| 19 |
c = b>tol*o; |
| 20 |
if (c==o) |
| 21 |
% msg, break |
| 22 |
v = 1:length(a); |
| 23 |
return |
| 24 |
end |
| 25 |
|
| 26 |
|
| 27 |
% Compute accessibility in at most n-step paths |
| 28 |
for k=1:n |
| 29 |
for j=1:n |
| 30 |
for i=1:n |
| 31 |
% If index i accesses j, where can you go ? |
| 32 |
if c(i,j) > 0 c(i,:) = c(i,:)+c(j,:); end |
| 33 |
end |
| 34 |
end |
| 35 |
end |
| 36 |
% Create a 0-1 matrix with the above information |
| 37 |
c>zeros(size(a)); c=ans; if (c==o) msg, break, end |
| 38 |
|
| 39 |
% Identify equivalence classes |
| 40 |
d=c.*c'+eye(size(a)); d>zeros(size(a)); d=ans; |
| 41 |
v=zeros(size(a)); |
| 42 |
for i=1:n find(d(i,:)); ans(n)=0; v(i,:)=ans; end |
| 43 |
|
| 44 |
% Eliminate displaying of identical rows |
| 45 |
i=1; |
| 46 |
while(i<n) |
| 47 |
for k=i+1:n |
| 48 |
if v(k,1) == v(i,1) |
| 49 |
v(k,:)=x; |
| 50 |
end |
| 51 |
end |
| 52 |
i=i+1; |
| 53 |
end |
| 54 |
j=1; |
| 55 |
for i=1:n |
| 56 |
if v(i,1)>0 |
| 57 |
h(j,:)=v(i,:); |
| 58 |
j=j+1; |
| 59 |
end |
| 60 |
end |
| 61 |
v=h; |
| 62 |
|
| 63 |
|
| 64 |
|