Subgraph from selected edges
9 次查看(过去 30 天)
显示 更早的评论
Hi, is it possible to extract a subgraph by selecting the edges, instead of nodes? If Yes, how?
H = subgraph(G,nodeIDs)
H = subgraph(G,idx)
0 个评论
采纳的回答
Wan Ji
2021-8-20
编辑:Wan Ji
2021-8-21
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 个评论
Jothimani V
2023-6-25
hello sir
how to split subgraphs(by selected edges) from given complete graph in matlab sir?
更多回答(1 个)
Wan Ji
2021-8-20
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!