select subgraph from graph

2 vues (au cours des 30 derniers jours)
Hassan
Hassan le 13 Sep 2012
Réponse apportée : ag le 15 Sep 2024
Dear All,
i have a graph such as
W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W)
i need to select subgraph for only some nodes such as [6,2,4]
thanks in advance hassan

Réponses (1)

ag
ag le 15 Sep 2024
Hi Hassan,
To construct a subgraph containing the specified nodes, you can use the MATLAB function "subgraph". The "subgraph" function requires a "graph" object, which can be created by using the "digraph" function applied to the specified sparse adjacency matrix.
The following code snippet illustrates the above workflow:
W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W)
DG = 6x6 sparse double matrix (11 nonzeros)
(4,1) 0.4500 (6,2) 0.4100 (2,3) 0.5100 (5,3) 0.3200 (6,3) 0.2900 (3,4) 0.1500 (5,4) 0.3600 (1,5) 0.2100 (2,5) 0.3200 (1,6) 0.9900 (4,6) 0.3800
%Create a graph object for the given DG matrix
G = digraph(DG);
% Specify the nodes for the subgraph
selectedNodes = [6, 2, 4];
% Create the subgraph containing only the specified nodes
subG = subgraph(G, selectedNodes)
subG =
digraph with properties: Edges: [2x2 table] Nodes: [3x0 table]
plot(subG,'EdgeLabel', subG.Edges.Weight);
Please note, that the node numbering in the subgraph is reset.
Hope this helps!

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by