subgraph

Extracts the graph induced by a set of nodes.

Category: Mathematics > Graph and Network Algorithms

Syntax

H = subgraph(G, nodeIDs)

Keeps the listed nodes and every edge of G between them.

Description

It is the complement of rmnode: instead of naming the nodes to drop, you name the nodes to keep. A common pattern is to find a component with conncomp and extract it: subgraph(G, find(bins == k)).

Input arguments

G
A graph or digraph.
nodeIDs
Node indices or names to keep.

Output arguments

H
The induced subgraph. Its nodes are numbered consecutively in the order given by nodeIDs; weights and node properties are kept.

Example

Largest component

G = graph([1 2 4 5 6], [2 3 5 6 4]);
[bins, sizes] = conncomp(G);
[~, k] = max(sizes);
H = subgraph(G, find(bins == k));
numnodes(H)
disp(H.Edges.EndNodes)
Output
ans = 3
     1     2
     2     3

Notes

Node lists must be numeric indices or names; a logical mask is not accepted.

See also

rmnode conncomp graph neighbors