function top = topn( A , num )
%TOPN  determines the n most connected nodes
%
%   T = TOPN( A , np ) returns a vector with the indices of 
%      the  np  most connected nodes, where
%      -  if 0 <= np < 1   np = fraction of network size
%      -  if np > 1  np = number of nodes
%
%      The result is sorted in descending order.
%
%      Default: np = 0.05 = 5% of no. of nodes
%
%  Last change:  10/2/2005 - Florian Knorn

if nargin < 1 || nargin > 2
    error('Please input adjacency matrix and n !');
else
	n = size(A,1);
	if n ~= size(A,2)
		error('Please input SQUARE matrix !');
	end
	if nargin == 1
		num = 0.05;
	end
	if num < 1
		num = ceil(num*n);
	end
	asnans = findnans(A);
	if num < 0 || num > length(asnans)
		error('Hey, 0 < np < networksize!');
	end
end

deleted = find(findnans(A));
temp = setdiff(1:n,deleted)';

A(deleted,:) = []; A(:,deleted) = [];

T = full(sum(A,2));
[d,T] = sort(T,'descend');

if ~isempty(deleted)
	sortme = [T,temp];
	mesorted = sortrows(sortme,1);
	if size(mesorted,1) < num
		error('Hey, 0 < n < networksize!');
	end
	top = mesorted(1:num,2);
else
	top = T(1:num);
end