NeighborhoodGraph[g,v]
gives the graph neighborhood of a vertex v in the graph g.
NeighborhoodGraph[g,{a1,a2,…}]
gives the graph neighborhood of the ai that can be vertices, edges, or subgraphs of g.
NeighborhoodGraph[g,patt]
gives the graph neighborhood of the vertices and edges that match the pattern patt.
NeighborhoodGraph[g,…,d]
gives the neighborhood up to distance d.
NeighborhoodGraph[{vw,…},…]
uses rules vw to specify the graph g.
NeighborhoodGraph
NeighborhoodGraph[g,v]
gives the graph neighborhood of a vertex v in the graph g.
NeighborhoodGraph[g,{a1,a2,…}]
gives the graph neighborhood of the ai that can be vertices, edges, or subgraphs of g.
NeighborhoodGraph[g,patt]
gives the graph neighborhood of the vertices and edges that match the pattern patt.
NeighborhoodGraph[g,…,d]
gives the neighborhood up to distance d.
NeighborhoodGraph[{vw,…},…]
uses rules vw to specify the graph g.
Details and Options
- The neighborhood graph for a vertex v is given by vertices adjacent to v and the edges connecting them.
- The neighborhood graph for an edge e is the neighborhood graph for the vertices of e.
- The neighborhood graph for a subgraph h is the neighborhood graph for the vertices in h.
- The neighborhood graph at distance d is the neighborhood graph for the vertices of the neighborhood graph at distance d-1.
- The default value for d is 1.
- NeighborhoodGraph works with undirected graphs, directed graphs, multigraphs, and mixed graphs.
Examples
open all close allBasic Examples (2)
Give the neighborhood from vertex 1 in a graph:
g = PetersenGraph[5, 2]HighlightGraph[g, {NeighborhoodGraph[g, 1], Labeled[1, 1]}]HighlightGraph[g, {NeighborhoodGraph[g, {1, 2}], Labeled[#, #]& /@ {1, 2}}]Give the neighborhood up to distance k from the vertices:
GridGraph[{4, 4}]Table[HighlightGraph[%, NeighborhoodGraph[%, 1, k]], {k, 4}]Scope (8)
NeighborhoodGraph works with undirected graphs:
GridGraph[{3, 4}, VertexSize -> {1 -> 0.2, 11 -> 0.2}]HighlightGraph[%, NeighborhoodGraph[%, {1, 11}]]g = [image];HighlightGraph[g, NeighborhoodGraph[g, {1, 11}]]g = [image];HighlightGraph[g, NeighborhoodGraph[g, {1, 11}]]g = [image];HighlightGraph[g, NeighborhoodGraph[g, {1, 11}]]NeighborhoodGraph works with vertices:
g = GridGraph[{5, 5}]HighlightGraph[g, NeighborhoodGraph[g, 1]]HighlightGraph[g, NeighborhoodGraph[g, 16]]Use rules to specify the graph:
g = {1 -> 2, 1 -> 3, 2 -> 4, 3 -> 4, 3 -> 5, 4 -> 6, 5 -> 6};HighlightGraph[g, NeighborhoodGraph[g, 1]]Use patterns to specify a set of vertices:
g = PetersenGraph[5, 2]NeighborhoodGraph[g, _ ? ((# < 3)&)]NeighborhoodGraph works with large graphs:
g = GridGraph[{10, 10, 10, 10}];NeighborhoodGraph[g, 1];//TimingApplications (2)
Highlight the neighborhood from the vertices in CompleteGraph:
g = CompleteGraph[8, VertexSize -> {1 -> 0.2}];HighlightGraph[g, NeighborhoodGraph[g, 1]]g = CycleGraph[8, VertexSize -> {1 -> 0.2}];HighlightGraph[g, NeighborhoodGraph[g, 1]]g = StarGraph[8, VertexSize -> {1 -> 0.2}];HighlightGraph[g, NeighborhoodGraph[g, 1]]v = {1, 15, 18};g = GridGraph[{6, 6}, VertexSize -> 0.2];HighlightGraph[g, MapThread[Style[NeighborhoodGraph[g, #1], #2]&, {{1, 15, 18}, {Blue, Red, Green}}]]g = CompleteKaryTree[3, 3, VertexSize -> {1 -> 0.2}];HighlightGraph[g, NeighborhoodGraph[g, 1]]g = PathGraph[Range[20], VertexSize -> 0.2];HighlightGraph[g, MapThread[Style[NeighborhoodGraph[g, #1], #2]&, {{1, 6, 20}, {Blue, Red, Green}}]]g = RandomGraph[{10, 20}, VertexSize -> {1 -> 0.3}];HighlightGraph[g, NeighborhoodGraph[g, 1]]Manipulate the neighborhood of vertices:
Manipulate[HighlightGraph[GridGraph[{5, 5}], NeighborhoodGraph[GridGraph[{5, 5}], i]], {i, 1, 25, 1}]Properties & Relations (2)
Use Subgraph to find the neighborhood graph of a set of vertices:
g = GridGraph[{8, 12}]v = {28, 36, 37, 43};nbg = Subgraph[g, Join[v, AdjacencyList[g, v]]];HighlightGraph[g, nbg, GraphHighlightStyle -> "DehighlightGray"]NeighborhoodGraph[g, v]The neighborhood of a vertex in a complete graph is the graph itself:
CompleteGraph[8]NeighborhoodGraph[%, 1]Neat Examples (2)
Pick out random neighborhoods from a grid:
g = GridGraph[{50, 75}];NeighborhoodGraph[g, RandomSample[VertexList[g], 35], 1]Subtract random neighborhoods from a grid:
g = GridGraph[{25, 50}];Subgraph[g, Complement[VertexList[g], VertexList[NeighborhoodGraph[g, RandomSample[VertexList[g], 25], 1]]]]Related Guides
Text
Wolfram Research (2010), NeighborhoodGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NeighborhoodGraph.html (updated 2015).
CMS
Wolfram Language. 2010. "NeighborhoodGraph." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/NeighborhoodGraph.html.
APA
Wolfram Language. (2010). NeighborhoodGraph. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NeighborhoodGraph.html
BibTeX
@misc{reference.wolfram_2026_neighborhoodgraph, author="Wolfram Research", title="{NeighborhoodGraph}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/NeighborhoodGraph.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_neighborhoodgraph, organization={Wolfram Research}, title={NeighborhoodGraph}, year={2015}, url={https://reference.wolfram.com/language/ref/NeighborhoodGraph.html}, note=[Accessed: 12-June-2026]}