How to list all isolated vertices and remove it from the graph?

25 views (last 30 days)
I have a Graph with several isolated vertices. I want to deleted these from the generated output.

Accepted Answer

Steven Lord
Steven Lord on 17 Dec 2021
rng default
A = sprandsym(10, (5/100));
G = graph(A, string(1:10)); % Give the nodes names
plot(G)
isolatedNodes = degree(G) == 0;
G2 = rmnode(G, find(isolatedNodes));
figure
plot(G2)
  2 Comments
Shanmugavelan S
Shanmugavelan S on 17 Dec 2021
Dear Prof.Steven Lord,
Thank you for your help. I tried your valuable suggestions. B
ut after deleting the isolated vertices. In the 2nd graph, vertices label renamed automatically as 1:n
2nd Graph's Output after isolatedNodes = degree(G) == 0;G2= rmnode(G, find(isolatedNodes)); command
Please help me to get an output without affecting the NodeLabels.
Steven Lord
Steven Lord on 17 Dec 2021
Note how I explicitly gave the nodes names rather than using the default numerical IDs.
G = graph(A, string(1:10)); % Give the nodes names

Sign in to comment.

More Answers (1)

Shanmugavelan S
Shanmugavelan S on 18 Dec 2021
Thank you sir for your suggestion. Is there any alternative command to rename nodes with unequal length for labeling (For eg. Vertices are renamed with prime numbers only)

Products

Community Treasure Hunt

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

Start Hunting!