How can I find if edges in a subgraph SG appear inside/belong to a graph G?
조회 수: 1 (최근 30일)
이전 댓글 표시
How can I find if edges in a subgraph SG appear inside/belong to a graph G?
I have this example with a graph G:
s = [1 1 2 2 2 3 3 3];
t = [2 3 3 4 5 6 7 5];
x = [0 0 1 0 4 3 1];
y = [0 1 0 4 5 0 -1];
G = graph(s,t);
G.Nodes.X = x'; G.Nodes.Y = y';
plot(G,'XData',G.Nodes.X,'YData',G.Nodes.Y)
From the graph G I "extract" a subgraph SG, i.e.:
SG = subgraph(G,[2 3 5 6])
figure
plot(SG,'XData',SG.Nodes.X,'YData',SG.Nodes.Y)
Now I would like to find the edges of SG inside/which belong to G. The answer should be:
edges_in_G =
2 3
2 5
3 5
3 6
% which correspond to
edges_in_SG =
1 2
1 3
2 3
2 4
How to do it since the nodes in the subgraph SG are renumbered (compared to the graph G)?
댓글 수: 4
Jon
2022년 1월 26일
Hi I'm glad that approach worked for you. Good idea for the automatic generation of unique edge names. Thanks for putting all of the pieces together so the answer is clearer. Good luck with your project.
채택된 답변
Jon
2022년 1월 25일
You can name the edges in G and then the corresponding edges in the subgraph will have the same names as those in the main graph. So something like this:
s = [1 1 2 2 2 3 3 3];
t = [2 3 3 4 5 6 7 5];
x = [0 0 1 0 4 3 1];
y = [0 1 0 4 5 0 -1];
G = graph(s,t);
G.Nodes.X = x'; G.Nodes.Y = y';
% Name the edges, column name in edge table must be "Names"
G.Edges.Names = {'a','b','c','d','e','f','g','h'}'
plot(G,'XData',G.Nodes.X,'YData',G.Nodes.Y)
% From the graph G I "extract" a subgraph SG, i.e.:
SG = subgraph(G,[2 3 5 6])
figure
plot(SG,'XData',SG.Nodes.X,'YData',SG.Nodes.Y)
% find out which edges in SG are in G
idl = ismember(G.Edges.Names,SG.Edges.Names)
inG = G.Edges.Names(idl)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!