How can I find if edges in a subgraph SG appear inside/belong to a graph G?
2 views (last 30 days)
Show older comments
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)?
Accepted Answer
Jon
on 25 Jan 2022
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)
3 Comments
More Answers (0)
See Also
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!