Hi, is it possible to extract a subgraph by selecting the edges, instead of nodes? If Yes, how?
From the Matlab docs, I can see it is possible by selecting the nodes, but not the edges:
H = subgraph(G,nodeIDs)
H = subgraph(G,idx)

 채택된 답변

Wan Ji
Wan Ji 2021년 8월 20일
편집: Wan Ji 2021년 8월 21일

1 개 추천

Note that all the endnodes of some given edges may be repeated, as commented by @Sim. Unique is a good way to solve such problem, to the example provided by Sim, now the code is modified as
clc; close all; clear all;
s = [1 1 1 1 2 2 2 2 2 2 2 2 2 2 15 15 15 15 15];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
x = [1 2 0 0 0 1 7 8 9 10 1 2 4 3 5 6 7 8 9 10];
y = [0 1 0 1 2 2 0 1 2 3 4 5 3 5 6 3 4 6 7 8];
G = graph(s,t);
G.Nodes.X = x(:);
G.Nodes.Y = y(:);
EdgeNumber = [1 2 15] ; % the no. of egde
EndNodes = G.Edges.EndNodes(EdgeNumber,:)
H = subgraph(G, unique(EndNodes));
figure
hold on
plot(G,'XData',x,'YData',y,'EdgeColor','blue','LineWidth',1)
h = plot(H,'XData',H.Nodes.X,'YData',H.Nodes.Y,'EdgeColor','red','LineWidth',2);
h.NodeLabel = {};
It is found that this method includes one edge more, i.e. the edge (2,15) in G. This is OK, because what the method provide is the subgraph connected with these 5 nodes.
IF YOU WANT TO get subgraph really by edges. Then
s = [1 1 1 1 2 2 2 2 2 2 2 2 2 2 15 15 15 15 15];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
x = [1 2 0 0 0 1 7 8 9 10 1 2 4 3 5 6 7 8 9 10];
y = [0 1 0 1 2 2 0 1 2 3 4 5 3 5 6 3 4 6 7 8];
G = graph(s,t);
G.Nodes.X = x(:);
G.Nodes.Y = y(:);
EdgeNumber = [1 2 15] ; % the no. of egde
EndNodes = G.Edges.EndNodes(EdgeNumber,:);
[q, ~, ic]= unique(EndNodes);
p = 1:1:numel(q);
st = p(reshape(ic,size(EndNodes)));
sub_Graph_by_Egde = graph(st(:,1), st(:,2));
sub_Graph_by_Egde.Nodes.X = G.Nodes.X(q,:);
sub_Graph_by_Egde.Nodes.Y = G.Nodes.Y(q,:);
figure
hold on
plot(G,'XData',x,'YData',y,'EdgeColor','blue','LineWidth',1)
h = plot(sub_Graph_by_Egde,'XData',sub_Graph_by_Egde.Nodes.X,...
'YData',sub_Graph_by_Egde.Nodes.Y,'EdgeColor','red','LineWidth',2);
I think I have solved your problem

댓글 수: 3

Sim
Sim 2021년 8월 20일
편집: Sim 2021년 8월 20일
Cool, many thanks @Wan Ji !
However, this works only with 1 edge:
clc; close all; clear all;
s = [1 1 1 1 2 2 2 2 2 2 2 2 2 2 15 15 15 15 15];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
x = [1 2 0 0 0 1 7 8 9 10 1 2 4 3 5 6 7 8 9 10];
y = [0 1 0 1 2 2 0 1 2 3 4 5 3 5 6 3 4 6 7 8];
G = graph(s,t);
G.Nodes.X = x(:); G.Nodes.Y = y(:);
EdgeNumber = [10] ; % the no. of egde
EndNodes = G.Edges.EndNodes(EdgeNumber,:)
H = subgraph(G, EndNodes);
figure
hold on
plot(G,'XData',x,'YData',y,'EdgeColor','blue','LineWidth',1)
h = plot(H,'XData',H.Nodes.X,'YData',H.Nodes.Y,'EdgeColor','red','LineWidth',2);
h.NodeLabel = {};
Which gives me this result:
EndNodes =
2 11
How to make it work for many EdgeNumber ?
Sim
Sim 2021년 8월 21일
Fantastic @Wan Ji, you totally nailed it! Very grateful!
Issue solved! :-)
Jothimani V
Jothimani V 2023년 6월 25일
hello sir
how to split subgraphs(by selected edges) from given complete graph in matlab sir?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Wan Ji
Wan Ji 2021년 8월 20일

1 개 추천

hi, friend, you can make EdgeNumber an array that contains all the edge numbers that you are interested in. and make the line H = subgraph(G, EndNodes); changed as H = subgraph(G, EndNodes(:));

댓글 수: 3

Sim
Sim 2021년 8월 20일
편집: Sim 2021년 8월 21일
Hi, thanks for the nice reply... I used
H = subgraph(G, unique(EndNodes));
instead of your suggestion:
H = subgraph(G, EndNodes(:));
since, with your hint, it gave me the following error:
Error using graph/subgraph (line 40)
Nodes must be a vector of unique node indices, a vector of unique node names, or a logical vector of length equal to the number of
nodes.
Now it works, but there is still an important issue to fix.
In this example:
clc; close all; clear all;
s = [1 1 1 1 2 2 2 2 2 2 2 2 2 2 15 15 15 15 15];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
x = [1 2 0 0 0 1 7 8 9 10 1 2 4 3 5 6 7 8 9 10];
y = [0 1 0 1 2 2 0 1 2 3 4 5 3 5 6 3 4 6 7 8];
G = graph(s,t);
G.Nodes.X = x(:); G.Nodes.Y = y(:);
EdgeNumber = [1 2 15] ; % the no. of egde
EndNodes = G.Edges.EndNodes(EdgeNumber,:)
H = subgraph(G, unique(EndNodes));
figure
hold on
plot(G,'XData',x,'YData',y,'EdgeColor','blue','LineWidth',1)
h = plot(H,'XData',H.Nodes.X,'YData',H.Nodes.Y,'EdgeColor','red','LineWidth',2);
h.NodeLabel = {};
the result is:
EndNodes =
1 2
1 3
15 16
However, in the subgraph H I have an additional edge, i.e 4 edges, instead of 3 edges:
>> H.Edges
ans =
1 2
1 3
2 4
4 5
which can be seen in the following picture too (here, the nodes labels refer to G and not to H):
This is due to the fact that I had to take the unique nodes in G, as required by the subgraph function:
H = subgraph(G,nodeIDs)
i.e.
>> unique(EndNodes)
ans =
1
2
3
15
16
In summary, this method includes one edge more, i.e. the edge (2,15) in G:
EndNodes =
1 2
1 3
2 15
15 16
If we can solve this issue it is done!
Wan Ji
Wan Ji 2021년 8월 20일
yes, you have done a good job, 👍👍👍
Sim
Sim 2021년 8월 21일
many thanks @Wan Ji! :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

질문:

Sim
2021년 8월 20일

댓글:

2023년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by