3
$\begingroup$

Why doesn't this work?

SeedRandom[1]
HighlightGraph[NearestNeighborGraph[RandomReal[1, {10, 2}], 2], 1, VertexSize -> 1/2, VertexStyle -> Red]

How to make it work?

$\endgroup$
3
  • 2
    $\begingroup$ Check out the VertexList of the graph. You'll need to use IndexGraph before referring the vertices as integers. $\endgroup$
    – Greg Hurst
    Mar 23, 2020 at 18:01
  • $\begingroup$ @ChipHurst Makes sense. I mean, it doesn't, but it works. Care to make it an answer so I can accept? $\endgroup$
    – corey979
    Mar 23, 2020 at 18:06
  • $\begingroup$ I think it makes sense. Per the docs, the second argument refers to explicit parts of the graph, not an index. $\endgroup$
    – Greg Hurst
    Mar 23, 2020 at 18:28

1 Answer 1

8
$\begingroup$

The vertices of the graph are not integers:

SeedRandom[1]
g = NearestNeighborGraph[RandomReal[1, {10, 2}], 2];

VertexList[g]
{{0.614608, 0.952102}, {0.116343, 0.82104}, ...}

We can reindex the vertices to run through consecutive integers:

HighlightGraph[IndexGraph[g], 1, VertexSize -> 1/2, VertexStyle -> Red]

Alternatively we can pass in the explicit vertex:

HighlightGraph[g, VertexList[g][[1]], VertexSize -> 1/2, VertexStyle -> Red]

$\endgroup$

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.