Subgraph[g,{v1,v2,…}]
gives the subgraph of the graph g generated by the vertices vi.
Subgraph[g,{e1,e2,…}]
gives the subgraph generated by the edges ej.
Subgraph[g,patt]
gives the subgraph generated by the vertices and edges that match the pattern patt.
Subgraph[{vw,…},…]
uses rules vw to specify the graph g.
Subgraph
Subgraph[g,{v1,v2,…}]
gives the subgraph of the graph g generated by the vertices vi.
Subgraph[g,{e1,e2,…}]
gives the subgraph generated by the edges ej.
Subgraph[g,patt]
gives the subgraph generated by the vertices and edges that match the pattern patt.
Subgraph[{vw,…},…]
uses rules vw to specify the graph g.
Details and Options
- The subgraph generated by the vertices v1,v2,… includes the vertices vi and all edges connecting them in the original graph g.
- The subgraph generated by the edges e1,e2,… includes the edges ej and all edges connecting vertices vi of ej in the original graph g.
- Subgraph works with undirected graphs, directed graphs, multigraphs, and mixed graphs.
Examples
open all close allBasic Examples (1)
Scope (7)
Subgraph works with undirected graphs:
Subgraph[[image], {1, 3, 5, 6}]Subgraph[[image], {1, 3, 6}]Subgraph[[image], {1, 3, 5, 6}]Subgraph[[image], {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, _3]]Find the subgraph with vertices less than 6:
HighlightGraph[g, Subgraph[g, _ ? ((# > 5)&)]]Subgraph works with large graphs:
g = GridGraph[{10, 10, 10, 10}];Subgraph[g, _ ? EvenQ]//VertexCount//TimingApplications (2)
Select random subgraphs of size 5:
RandomSubgraph[g_ ? GraphQ, s_] := Subgraph[g, RandomSample[VertexList[g], s]]g = [image];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]&];HighlightGraph[g, #]& /@ %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]See Also
Related Guides
Text
Wolfram Research (2010), Subgraph, Wolfram Language function, https://reference.wolfram.com/language/ref/Subgraph.html (updated 2015).
CMS
Wolfram Language. 2010. "Subgraph." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/Subgraph.html.
APA
Wolfram Language. (2010). Subgraph. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Subgraph.html
BibTeX
@misc{reference.wolfram_2026_subgraph, author="Wolfram Research", title="{Subgraph}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/Subgraph.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_subgraph, organization={Wolfram Research}, title={Subgraph}, year={2015}, url={https://reference.wolfram.com/language/ref/Subgraph.html}, note=[Accessed: 12-June-2026]}