# Finding vertices with a given degree; or, finding indices

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}

• You could use Position[] on the result of VertexDegree[]. – J. M. is in limbo May 23 '16 at 17:17

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.