I have a graph g and would like to find all the vertices in g with degree n.

VertexDegree[g] gives a list of the degrees, and

# == n & /@ VertexDegree[g]

gives a list of True/False for each vertex, but I don't know how to get the indices. That is, I want

{1, 3}

rather than

{True, False, True, False, False, False}

  • 3
    $\begingroup$ You could use Position[] on the result of VertexDegree[]. $\endgroup$ May 23, 2016 at 17:17

2 Answers 2


Get vertex names:

Pick[VertexList[g], VertexDegree[g], n]

Get vertex indices:

Pick[Range@VertexCount[g], VertexDegree[g], n]

As mentioned by JM in his comment, here's how you would find the vertex index of vertices of degree 5 in the following graph:

g = GraphData[{"Fan", {4, 4}}];
VertexDegree[g]                              (* {5, 6, 6, 5, 4, 4, 4, 4} *)
Flatten@Position[VertexDegree[g], 5]         (* {1, 4} *)

The Flatten is there to provide the output format you indicated, since Position will produce a nested list otherwise.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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