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!