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