Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to get the neighboring vertices and incident edges from a vertex in a graph. I assume NeighborhoodVertices can be used for the former. Anything better?

Or given something like {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1}, how to directly get these information? Can I do list matching to some pattern?

share|improve this question
up vote 8 down vote accepted

A random graph

 g = RandomGraph[{15, 35}, VertexLabels -> "Name", PlotRangePadding -> .2]

enter image description here

Edges for the vertex 4:

 EdgeList[g, 4 \[UndirectedEdge] _]


enter image description here

Now vertexes around vertex4:

 VertexList[NeighborhoodGraph[g, 4]]


 {4, 14, 11, 6, 7, 9, 13, 15}


  HighlightGraph[g, NeighborhoodGraph[g, 4]]

enter image description here

There are many other ways. You could also use AdjacencyMatrix to get all that info.

share|improve this answer
yes, NeighborhoodVertices[g, 4, 1] can also get the same list with shorter typing. :) – Qiang Li Feb 17 '12 at 22:41
@QiangLi Only if you use GraphUtilities package - which I do not. ;-) I use built in Graph functionality. – Vitaliy Kaurov Feb 17 '12 at 22:47
True. :) +1 Thank you. – Qiang Li Feb 17 '12 at 22:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.