How to prevent the function rmnode from refreshing the nodes' labels?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Waseem AL Aqqad
el 24 de En. de 2021
Editada: Waseem AL Aqqad
el 2 de Feb. de 2021
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 comentarios
Respuesta aceptada
Walter Roberson
el 24 de En. de 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 comentario
Waseem AL Aqqad
el 24 de En. de 2021
Editada: Waseem AL Aqqad
el 2 de Feb. de 2021
Más respuestas (0)
Ver también
Categorías
Más información sobre Graph and Network Algorithms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!