How to prevent the function rmnode from refreshing the nodes' labels?
조회 수: 3 (최근 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:
- The specific node should not belong to the largest component
- 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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!