0
$\begingroup$

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}

$\endgroup$
  • 3
    $\begingroup$ You could use Position[] on the result of VertexDegree[]. $\endgroup$ – J. M. will be back soon May 23 '16 at 17:17
1
$\begingroup$

Get vertex names:

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

Get vertex indices:

Pick[Range@VertexCount[g], VertexDegree[g], n]
$\endgroup$
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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