({{, (RefLink[Subgraph, paclet:ref/Subgraph])[g,{(Subscript[v, 1]),(Subscript[v, 2]),…}] 
gives the subgraph of the graph g generated by the vertices (Subscript[v, i]).}, {, (RefLink[Subgraph, paclet:ref/Subgraph])[g,{(Subscript[e, 1]),(Subscript[e, 2]),…}]
gives the subgraph generated by the edges (Subscript[e, j]).}, {, (RefLink[Subgraph, paclet:ref/Subgraph])[g,patt]
gives the subgraph generated by the vertices and edges that match the pattern patt.}, {, (RefLink[Subgraph, paclet:ref/Subgraph])[{v->w,…},…]
uses rules v->w to specify the graph g. }})

OpenersNotesSection(Spacer1)Details and Options

The subgraph generated by the vertices (Subscript[v, 1]),(Subscript[v, 2]),… includes the vertices (Subscript[v, i]) and all edges connecting them in the original graph g.

The subgraph generated by the edges (Subscript[e, 1]),(Subscript[e, 2]),… includes the edges (Subscript[e, j]) and all edges connecting vertices (Subscript[v, i]) of (Subscript[e, j]) in the original graph g.

(RefLink[Subgraph, paclet:ref/Subgraph]) works with undirected graphs, directed graphs, multigraphs, and mixed graphs.

OpenersPrimaryExamplesSection(Spacer1)Examples  (14)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Give a subgraph of a complete graph:

g=CompleteGraph[5]

h=Subgraph[%,{1,2,3}]

Highlight the subgraph within the main graph:

HighlightGraph[g,h,GraphHighlightStyle->"Thick"]

OpenersExampleSection1(Spacer1)Scope  (7)

(RefLink[Subgraph, paclet:ref/Subgraph]) works with undirected graphs:

Subgraph[,{1,3,5,6}]

Directed graphs:

Subgraph[,{1,3,6}]

Multigraph:

Subgraph[,{1,3,5,6}]

Mixed graph:

Subgraph[,{1,3,5,6}]

Use rules to specify the graph:

Subgraph[{1->3,2->1,3->6,4->6,1->5,5->4,6->1},{1,3,6}]

Use patterns to select subgraphs:

g=PetersenGraph[5,2]

Find the subgraph with edges that are incident to 3:

HighlightGraph[g,Subgraph[g,_\[UndirectedEdge]3]]

Find the subgraph with vertices less than 6:

HighlightGraph[g,Subgraph[g,_?((#>5)&)]]

(RefLink[Subgraph, paclet:ref/Subgraph]) works with large graphs:

g=GridGraph[{10,10,10,10}];

Subgraph[g,_?EvenQ]//VertexCount//Timing

OpenersExampleSection3(Spacer1)Applications  (2)

Select random subgraphs of size 5:

RandomSubgraph[g_?GraphQ,s_]:=Subgraph[g,RandomSample[VertexList[g],s]]

g=;

Table[HighlightGraph[g,RandomSubgraph[g,5]],{4}]

Find all the subgraphs of the graph g that are isomorphic to the graph h:

{g,h}={PetersenGraph[5,2],CycleGraph[5]}

Enumerate all subsets of vertices that have the same number of vertices as h:

s=Subsets[Range[VertexCount[g]],{VertexCount[h]}];

Select the subgraphs that are isomorphic to h:

Select[Subgraph[g,#]&/@s,IsomorphicGraphQ[#,h]&];

Highlight them:

HighlightGraph[g,#]&/@%

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

The vertices of subgraphs are subsets of the original vertices:

{g=PetersenGraph[5,2],h=Subgraph[g,{1,3,4}]}

MemberQ[VertexList[g],#]&/@VertexList[h]

The edges of subgraphs are subsets of the original edges:

{g=PetersenGraph[5,2],h=Subgraph[g,{1,3,4}]}

MemberQ[EdgeList[g],#]&/@EdgeList[h]

The subgraph of a complete graph is a complete graph:

g=CompleteGraph[8]

HighlightGraph[g,h=Subgraph[g,Range[5]]]

CompleteGraphQ[h]

OpenersExampleSection5(Spacer1)Neat Examples  (1)

Subtract random neighborhoods from a grid:

g=GridGraph[{25,50}];

Subgraph[g,Complement[VertexList[g],VertexList[NeighborhoodGraph[g,RandomSample[VertexList[g],25],1]]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Graph, paclet:ref/Graph])  ▪  (RefLink[DirectedGraph, paclet:ref/DirectedGraph])  ▪  (RefLink[TreeGraph, paclet:ref/TreeGraph])  ▪  (RefLink[PathGraph, paclet:ref/PathGraph])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Graph Construction & Representation, paclet:guide/GraphConstructionAndRepresentation])}, {▪(RefLinkPlain[Graph Operations and Modifications, paclet:guide/GraphModifications])}, {▪(RefLinkPlain[Computation on Graphs, paclet:guide/ComputationOnGraphs])}})}})