subgraph
Extract subgraph
Description
Examples
Extract Subgraph
Create and plot a graph.
s = [1 1 1 1 2 2 2 2 2 2 2 2 2 2 15 15 15 15 15]; t = [3 5 4 2 14 6 11 12 13 10 7 9 8 15 16 17 19 18 20]; G = graph(s,t); plot(G,'Layout','force')
Extract a subgraph from G
by specifying which nodes to include. The node numbering in the subgraph is reset.
idx = [2 15 16 17 18 19 20 1 3 4 5]; H = subgraph(G,idx); plot(H,'Layout','force')
Subgraph with Node and Edge Properties
Create and plot a weighted graph with named nodes.
s = [1 1 1 2 2 2 8 8 8 8]; t = [2 3 4 5 6 7 9 10 11 12]; weights = [10 30 40 80 60 60 20 30 90 80]; names = {'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L'}; G = graph(s,t,weights,names); plot(G,'EdgeLabel',G.Edges.Weight)
Extract a subgraph that contains node 'B'
and all of its neighbors. subgraph
preserves the node names and edge weights. However, the numeric node IDs in H
are renumbered compared to G
.
N = neighbors(G,'B'); H = subgraph(G, ['B'; N]); plot(H,'EdgeLabel',H.Edges.Weight)
Input Arguments
nodeIDs
— Node identifiers
node indices | node names
Node identifiers, specified as one or more node indices or node names.
nodeIDs
selects a subset of the nodes in
G
to generate the subgraph,
H
.
This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.
Form | Single Node | Multiple Nodes |
---|---|---|
Node index | Scalar Example: | Vector Example: |
Node name | Character vector Example: | Cell array of character vectors Example: |
String scalar Example: | String array Example: |
Example: H = subgraph(G,[1 2 5])
Example: H = subgraph(G,{'A' 'B' 'E'})
idx
— Node selection vector
vector
Node selection vector, specified as a logical vector. The subgraph
contains only the nodes J
for which
idx(J)
is logical 1
(true
). The index of node J
in
H
is I(J)
, where I =
find(idx)
.
Example: subgraph(G,degree(G)>2)
Data Types: logical
Output Arguments
H
— Subgraph
graph
object | digraph
object
Subgraph, returned as a graph
or
digraph
object. H
contains only
the nodes that were selected with nodeIDs
or
idx
. Other nodes in G
(and the
edges connecting to those nodes) are discarded. The node properties and edge
properties of the selected nodes and edges are carried over from
G
into H
.
See graph
or digraph
for more information
about graph objects.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The node identifiers
nodeIDs
must be node indices of the input graphG
. Node names are not supported.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Version History
Introduced in R2015b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)