1function [p,v,d] = cs_fiedler (A)
2%CS_FIEDLER the Fiedler vector of a connected graph.
3%   [p,v,d] = cs_fiedler(A) computes the Fiedler vector v (the eigenvector
4%   corresponding to the 2nd smallest eigenvalue d of the Laplacian of the graph
5%   of A+A').  p is the permutation obtained when v is sorted.  A should be a
6%   connected graph.
7%
8%   Example:
9%       [p,v,d] = cs_fiedler (A) ;
10%
11%   See also CS_SCC, EIGS, SYMRCM, UNMESH.
12
13% Copyright 2006-2012, Timothy A. Davis, http://www.suitesparse.com
14
15n = size (A,1) ;
16if (n < 2)
17    p = 1 ; v = 1 ; d = 0 ; return ;
18end
19opt.disp = 0 ;                      % turn off printing in eigs
20opt.tol = sqrt (eps) ;
21if (~isreal (A))
22    A = spones (A) ;
23end
24S = A | A' | speye (n) ;            % compute the Laplacian of A
25S = diag (sum (S)) - S ;
26[v,d] = eigs (S, 2, 'SA', opt) ;    % find the Fiedler vector v
27v = v (:,2) ;
28d = d (2,2) ;
29[ignore p] = sort (v) ;             % sort it to get p
30