1function [p, q, r, s] = ccspy (A, bipartite, res)
2%CCSPY plot the connected components of a matrix.
3%
4%   Example:
5%       [p, q, r, s] = ccspy (A, bipartite, res)
6%
7%   If A is square, [p,q,r,s] = ccspy(A) finds a permutation p so that A(p,q)
8%   is permuted into block upper triangular form.  In this case, r=s, p=q and
9%   the kth diagonal block is given by A (t,t) where t = r(k):r(k+1)-1.
10%   The diagonal of A is ignored.
11%
12%   If A is not square (or for [p,q,r,s] = ccspy(A,1)), then the connected
13%   components of the bipartite graph of A are found.  A(p,q) is permuted into
14%   block diagonal form, where the diagonal blocks are rectangular.  The kth
15%   block is given by A(r(k):r(k+1)-1,s(k):s(k+1)-1).  A can be rectangular.
16%
17%   It then plots the result via cspy, drawing a greenbox around each component.
18%   A 3rd input argument (res) controls the resolution (see cspy for a
19%   description of the res parameter).
20%
21%   See also CSPY, CS_DMPERM, DMPERM, CS_SCC, CS_SCC2, CS_DMSPY.
22
23% Copyright 2006-2012, Timothy A. Davis, http://www.suitesparse.com
24
25if (~issparse (A))
26    A = sparse (A) ;
27end
28[m n] = size (A) ;
29if (nargin < 3)
30    res = 256 ;
31end
32if (nargin < 2)
33    bipartite = [ ] ;
34end
35if (isempty (bipartite))
36    bipartite = (m ~= n) ;
37end
38
39% find the strongly connected components
40[p1 q r s] = cs_scc2 (A, bipartite) ;
41if (nargout > 0)
42    p = p1 ;
43end
44
45nb = length (r)-1 ;
46
47% plot the result
48S = A (p1,q) ;
49if (res == 0)
50    spy (S) ;
51    e = 1 ;
52else
53    e = cspy (S,res) ;
54end
55hold on
56
57title (sprintf ('%d-by-%d, strongly connected commponents: %d\n', m, n, nb)) ;
58
59if (~bipartite)
60    plot ([.5 .5 n+.5 n+.5], [.5 .5 n+.5 n+.5], 'r') ;
61end
62
63drawboxes (nb, e, r, s) ;
64
65drawbox (1,m+1,1,n+1,'k',1,e) ;
66hold off
67