Creating an Interactive Module that plots a graph and a highlighted vertex


For an assignment, I have to do the following:

Build an interactive model with two controls, n and u, that generates a random graph called K with n vertices and 2n edges and highlights vertex u of graph K. When the user selects a different vertex u of K, you display graph K with vertex u shown with a different color and with a different size.

At the moment, my code is as follows:

K = RandomGraph[{n}, {2 n}];
Manipulate[HighlightGraph[K, {u}], {n, 1, 10, 1}, {u, 1, n, 1}]

I think I need to state that u is a vertex, but unsure how to fix. Any help is appreciated.

Alessia T

Posted 2017-02-21T02:21:01.160

Reputation: 29

Hi Alessia, try to not use single capital letters as variables because there are already some build ones. FYI: related topics: Adding Dynamic functionality to a Graph, 128347, 116648, 104718

– Kuba – 2017-02-21T06:29:09.950



Create a function using SetDelayed to evaluate RandomGraph every time you call the function. Here, SeedRandom is used, so that every time RandomGraph is called for a given n, it gives the same result.

graph[n_, u_, size_] := With[{},
  RandomGraph[{n, 2 n}, VertexSize -> {u -> size}]]

Then, in the Manipulate to change the color, RandomColor[] function can be used in the Style attribute.

  graph[n, u, u/(5 n)], {Style[u, RandomColor[n][[u]]]}], {n, 5, 15, 
  1}, {u, 1, n, 1}, ControlType -> LabeledSlider]

enter image description here

Note: To get 2n edges, value of n should be >= 5.

Anjan Kumar

Posted 2017-02-21T02:21:01.160

Reputation: 4 551