Filter löschen
Filter löschen

Remove duplicate edges of undirected graph

13 Ansichten (letzte 30 Tage)
IrisL
IrisL am 21 Jul. 2022
Kommentiert: IrisL am 21 Jul. 2022
I am trying to remove duplicated edges of a garph.
I was able to get the unique edges by using
unique(G2.Edges.EndNodes)
However, this code only returns the unique edges without updating the G2.Edges table nor removing edges from the graph.
Any suggestion will be appreciated. Thanks.
My edges table look like this:

Akzeptierte Antwort

Steven Lord
Steven Lord am 21 Jul. 2022
Call simplify on your multigraph (you can tell if your graph or digraph is a multigraph using ismultigraph) to convert it to a simple graph.
s = [1 1 1 1 1 2 2 3 3 3 3 5 5];
t = [2 3 4 4 6 1 5 4 4 5 6 4 6];
names = string(1:6);
multiGraph = graph(s, t);
check = ismultigraph(multiGraph)
check = logical
1
simpleGraph = simplify(multiGraph);
Let's compare how they look.
figure
h = plot(multiGraph);
title('multigraph');
figure
% Plot the simple graph with the vertices in the same location
% as the vertices in the multigraph
plot(simpleGraph, 'XData', h.XData, 'YData', h.YData)
title('simplegraph')

Weitere Antworten (1)

Chunru
Chunru am 21 Jul. 2022
% A graph with duplicated edges
s = [1 1 1 1 1 2 2 3 3 3 3 5 5];
t = [2 3 4 4 6 1 5 4 4 5 6 4 6];
names = string(1:6);
G = graph(s, t);
% A new graph with unique edges
G.Edges;
G1 = graph(unique(G.Edges)); % Create a new graph based on the unique edge rather than updating
subplot(121);
h1 = plot(G);
subplot(122)
h2 = plot(G1);
  3 Kommentare
Chunru
Chunru am 21 Jul. 2022
If you have node names specified, such as:
% A graph with duplicated edges
s = [1 1 1 1 1 2 2 3 3 3 3 5 5];
t = [2 3 4 4 6 1 5 4 4 5 6 4 6];
names = string(1:6);
G = graph(s, t, [], names);
% A new graph with unique edges
G.Edges % EndNodes
ans = 13×1 table
EndNodes ______________ {'1'} {'2'} {'1'} {'2'} {'1'} {'3'} {'1'} {'4'} {'1'} {'4'} {'1'} {'6'} {'2'} {'5'} {'3'} {'4'} {'3'} {'4'} {'3'} {'5'} {'3'} {'6'} {'4'} {'5'} {'5'} {'6'}
% Using string array for unique (rather cell array)
newEdges = cellstr(unique(string(G.Edges.EndNodes), 'row'));
EdgeTable = table(newEdges, 'VariableNames', {'EndNodes'});
G1 = graph(EdgeTable); % Create a new graph based on the unique edge rather than updating
subplot(121);
h1 = plot(G);
subplot(122)
h2 = plot(G1);
IrisL
IrisL am 21 Jul. 2022
Thanks. This is helpful as well!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by