How to segment a graph from a node?

1 view (last 30 days)
Bin Qi
Bin Qi on 2 Mar 2021
I have a graph object.
EndNodes Weight
1 2 67
2 4 54
3 5 67
4 5 268
4 10 1
5 8 38
6 9 45
7 8 134
8 9 54
And I wish to segment it into many subgraphes from node 4 which has a degree of 3. So I want the subgraphes to be:
EndNodes Weight
1 2 67
2 4 54
EndNodes Weight
3 5 67
4 5 268
5 8 38
6 9 45
7 8 134
8 9 54
EndNodes Weight
4 10 1
How can I achieve this?

Answers (1)

Alamanda Ponappa Poovaya
Alamanda Ponappa Poovaya on 26 Mar 2021
Hi, I understand you want to segment your graphs in a particular way. Firstly, I would suggest you give a name to each of your nodes and not rely on the integer names, as the subgraph function which is part of the solution resets numerical node ids but will preserve named ids. I have added this modification in the code below, which takes the same graph object you mentioned in your question and does this operation. I have also plotted the graphs so you can see that it is as expected
s = [1 2 3 4 4 5 6 7 8];
t = [2 4 5 5 10 8 9 8 9];
w = [67 54 67 268 1 38 45 134 54];
n = {'n1','n2','n3','n4','n5','n6','n7','n8','n9','n10'}; % naming the nodes
G = graph(s,t,w,n);
n4 = neighbors(G,4);
H = rmedge(G,[4,4,4],n4); % split into 3 graphs
s1 = dfsearch(H,n4(1)); % find all components of the subgraphs starting at neighbours of 4
s2 = dfsearch(H,n4(2));
s3 = dfsearch(H,n4(3));
subgraph1 = subgraph(G,[4 ;s1]); % including 4 in the subgraphs
subgraph2 = subgraph(G,[4; s2]);
subgraph3 = subgraph(G,[4; s3]);
subplot(2,2,1);
plot(G,'EdgeLabel',G.Edges.Weight)
subplot(2,2,2);
plot(subgraph1,'EdgeLabel',subgraph1.Edges.Weight)
subplot(2,2,3);
plot(subgraph2,'EdgeLabel',subgraph2.Edges.Weight)
subplot(2,2,4);
plot(subgraph3,'EdgeLabel',subgraph3.Edges.Weight)
Links to function docs:

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!