How to prevent the function rmnode from refreshing the nodes' labels?

4 views (last 30 days)
I have a graph G which consists of 20 nodes, and I'm selecting a random node and remove it from the graph using rmnode. At each time my code check if all other nodes (one node at a time) after removing that node satisfies these two conditions:
  1. The specific node should not belong to the largest component
  2. The specific node does not have neighbors.
if one node satisfies these two conditions it should be removed.
How can I let rmnode not to refreshes the nodes' labels each time?
G= WattsStrogatz(20,2,0.2);
% so=[1 1 1 2 2 2 2 3 3 3 5];
% ta=[2 3 4 3 4 5 6 6 7 5 7];
% G=graph(so,ta);
G=minspantree(G);
Hf=Cascading_Failure(G,1);
function Hf=Cascading_Failure(G,rmv)
N=numnodes(G);
subplot(2,2,1);
p=plot(G);
x=p.XData;
y=p.YData;
attack=randsample(N,rmv);
Gf= rmnode(G,attack);
x(attack)=[];
y(attack)=[];
subplot(2,2,2);
plot(Gf,'XData',x,'YData',y)
[bin,binsize]=conncomp(Gf);
comp=length(binsize);
m=mode(bin);
o=find(bin==m); % members of largest component
for i=1:length(attack)
distance=distances(G,attack(i)); % distance vector ( Check the nodes based on their distance to node 'attack' )
[~, idx]=sort(distance,'ascend');
for j=2:length(distance)
if ~ismember(idx(j),o) && isempty(neighbors(Gf,idx(j)))
Gf=rmnode(Gf,idx(j));
x(idx(j))=[];
y(idx(j))=[];
figure; plot(Gf,'XData',x,'YData',y)
end
end
end
Hf=Gf;
end

Accepted Answer

Walter Roberson
Walter Roberson on 24 Jan 2021
If you use graph() or digraph() objects, then Nodes is a table, and you can add additional properties to the table including persistent labeling.
Consider too that instead of removing a node, that it can sometimes be as effective to remove all edges leading to it and from it, which has the advantage that the node numbers do not change.
  1 Comment
Waseem AL Aqqad
Waseem AL Aqqad on 24 Jan 2021
Edited: Waseem AL Aqqad on 2 Feb 2021
I decided to go with option two and it worked perfectly. THANK you very much.
eid = outedges(G,attack);
G=rmedge(G,eid);

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!