1

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.

Chris K
  • 20,207
  • 3
  • 39
  • 74
Alessia T
  • 29
  • 2

1 Answers1

1

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[{},
  SeedRandom[1000];
  RandomGraph[{n, 2 n}, VertexSize -> {u -> size}]]

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

Manipulate[
 HighlightGraph[
  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
  • 4,979
  • 1
  • 15
  • 28