select subgraph from graph

2 次查看(过去 30 天)
Hassan
Hassan 2012-9-13
回答: ag 2024-9-15
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

回答(1 个)

ag
ag 2024-9-15
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!

类别

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