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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2021-1-24
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by