Remove edges in a subgraph SG using nodes IDs (numbers) from the graph G

3 次查看(过去 30 天)
Hi, I have a graph defined by node pairs (node IDs): s = [1 1 2 4 7 ...] t = [2 5 3 5 6 ...] Since the graph is huge and I would need to work only on a portion of it, I extracted a subgraph.
If I am not wrong, it looks like that subgraph is reordering the node IDs (right?), therefore, when I try to remove edges (and nodes) from the subgraph, by using the same node IDs of my graph contained in s = [...] and in t = [...], I get an error.. Any idea to fix this small issue? (I also tried to assign and employ "nodenames"..but without success)

回答(1 个)

Walter Roberson
Walter Roberson 2019-11-7
编辑:Walter Roberson 2019-11-7
Use node names instead of node numbers when you create the graph. Then in the sub-graph you can use findnode(). Or just give the node names directly in the rmedge() call.
  4 个评论
Sim
Sim 2019-11-7
编辑:Sim 2019-11-8
Thanks Walter! ....I was trying to follow your indications by converting the arrays s and t using string()
s = string(s);
t = string(t);
.. but I think I got stuck.... Probably it is better if I show my initial attempt with an example, and then, where I got stuck....
Initial attempt
First I create a graph G:
% Load s, t, x, y, nodenames and create a graph G
load('graph_example.mat') % attached in this post
G = graph(s,t,[],nodenames2);
% Plot the graph G
h = plot(G,'XData',x,'YData',y,'linewidth',2,'MarkerSize',2);
% just for a clearer plot..
h.NodeLabel = [];
text(x,y, nodenames2, 'FontSize',12, 'FontWeight','bold', ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
....here below shown:
Then, I select the nodes composing the subgraph SG and I create the subgraph:
% Select nodes to keep for the subgraph and create the subgraph SG
nk = [45 24 32 35 10 48 39 4 26 42 5 25 14 2 29 34 41]; % nodes to keep
SG = subgraph(G,nk);
% Plot subgraph SG, together with the graph G
figure
hold on
h1 = plot(G,'XData',x,'YData',y);
h2 = plot(SG,'XData',x(nk),'YData',y(nk),'linewidth',2,'MarkerSize',2);
hold off
% just for a clearer plot..
h1.NodeLabel = [];
h2.NodeLabel = [];
text(x,y, nodenames2, 'FontSize',12, 'FontWeight','bold', ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
...which looks like this:
As third and last step, I select the edges that I want to remove and I try to plot the subgraph SG without the selected edges...
% Using the node IDs of G, select edges to be removed in SG: [2 29], [2 42], [5 42], etc..
s_remove = [2 2 5 10 10 10 42];
t_remove = [29 42 42 24 42 48 48];
% Remove edges in SG
SGrmedge = rmedge(SG,s_remove,t_remove);
% Plot the subgraph SG without selected edges
figure
h = plot(SGrmedge,'XData',x,'YData',y,'linewidth',2,'MarkerSize',2);
% for a clearer plot
h.NodeLabel = [];
text(x,y, nodenames2, 'FontSize',15, 'FontWeight','bold', ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
but I get this error:
Error using graph/validateNodeID (line 529)
Numeric node IDs must be positive integers not greater than the number of nodes in the graph (17).
Error in graph/findedge (line 70)
s = validateNodeID(G, s, true); % true: allows categorical
Error in graph/rmedge (line 49)
ind = findedge(G, s, t);
Error in graph_example (line 62)
Grmedge = rmedge(SG,s_remove,t_remove);
Second attempt using string() for arrays s and t
I use string() for arrays s and t:
% Load s, t, x, y, nodenames and create the graph G
load('graph_example.mat')
s = string(s);
t = string(t);
G = graph(s,t,[],nodenames2);
and I get this error...
Error using matlab.internal.graph.constructFromEdgeList (line 136)
Edge list contains a name not present in the node names argument.
Error in graph (line 300)
matlab.internal.graph.constructFromEdgeList(...
Error in graph_example (line 32)
G = graph(s,t,[],nodenames2);
Would you be so nice to tell me where/what I am doing wrong please? Any further hint?
Sim
Sim 2019-11-8
编辑:Sim 2019-11-8
I think I found a workaround to my problem....
% Using the node IDs of G, select edges to be removed in SG: [2 29], [2 42], [5 42], etc..
s_rm = [2 2 5 10 10 10 42];
t_rm = [29 42 42 24 42 48 48];
% Convert the node IDs of G (contained in s_rm and in t_rm) into node IDs of SG
sgn = str2double(table2array(SG.Nodes));
conversion = [sgn (1:length(sgn))'];
[~,s_rmcv] = ismember(s_rm, conversion(:,1));
[~,t_rmcv] = ismember(t_rm, conversion(:,1));
% Remove edges in SG
SGrmedge = rmedge(SG,s_rmcv,t_rmcv);
% Plot the sub-graph SG without selected/removed edges, together with graph G
figure
hold on
h1 = plot(G,'XData',x,'YData',y);
h2 = plot(SGrmedge,'XData',x(sgn),'YData',y(sgn),'linewidth',2,'MarkerSize',2);
hold off
% for a clearer plot
h1.NodeLabel = [];
h2.NodeLabel = [];
text(x,y, nodenames2, 'FontSize',12, 'FontWeight','bold', ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
...which results as :
BTW, any other solution is really welcome! Thanks a lot :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by