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

4 ビュー (過去 30 日間)
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 1 月 24 日
編集済み: Waseem AL Aqqad 2021 年 2 月 2 日
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.
  1 件のコメント
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 1 月 24 日
編集済み: Waseem AL Aqqad 2021 年 2 月 2 日
I decided to go with option two and it worked perfectly. THANK you very much.
eid = outedges(G,attack);
G=rmedge(G,eid);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by