function [A , rmme] = attack( A , pn , alredrmvd)
%ATTACK  removes the n most connected nodes
%
%    ATTACK(A,pn)  "removes" the  most connected nodes (i.e. 
%       with highest degrees) nodes from the network given by
%       its adj. matrix  A  by replacing corresp. rows and cols
%       with NaNs.
%       The number of removed nodes depends on  pn:
%       - if 0 < pn < 1  pn is the fraction of removed nodes
%       - if pn >= 1  pn is directly the number of nodes removed
%
%  Last change:  10/2/2005 - Florian Knorn

%  The vector  alredrmvd  contains nodes that have already been
%  removed. (This may be used in conjunction with other func-
%  tions that need to keep track of the removed nodes.)

if nargin < 2 || nargin > 3
    error('Please provide at least adjacency matrix A and number n !');
else
	n = size(A,1);
	if pn < 0
		error('n should be >= 0 !');
	elseif pn > n
		error('pn cannot be greater then the total number of nodes !');
	end
	if nargin <= 2
		alredrmvd = [];
	end
end

if pn < 1 % pn is a percentage -> convert...
	pn = ceil(n*pn);
end

if pn > n
	error('Cannot remove more nodes then actually exist !');
end

rmme = topn(A,pn);

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

% needed to report orig. indices of the removed nodes 
origind = 1:n+length(alredrmvd); % get [1 2 3 ... orig_n]
origind(alredrmvd) = []; % get surivors names
rmme = origind(rmme);	 % report removed nodes