1
$\begingroup$

I would like to generate a random graph on $[0,1]^2$. Each node is connected to one other if the distance between two nodes is say 1/4 or less. Here is an example with 50 nodes and 200 edgesenter image description here

I would like to have a graph almost like this but with only one red node probably in the middle of the network.

Here is a similar question Random Geometric Graphs What I dont want is "manipulate". the colors of the nodes should be black and the edges should be blue in color.

Could someone help me?

$\endgroup$
  • $\begingroup$ If all you need is to remove the Manipulate around the solution of the linked question and change the color, then I would consider this question a duplicate - perhaps you can distinguish your question somewhat more? $\endgroup$ – Yves Klett Feb 23 '15 at 15:16
  • $\begingroup$ I would also consider it as a duplicate but I also need to have black nodes and blue edges, plus I need a red bigger point marked with letter A in the middle of the network. These are the only extras and I dont know how to do. $\endgroup$ – Seyhmus Güngören Feb 23 '15 at 15:18
  • $\begingroup$ In the interest of teaching a man how to fish: Check out the documentation for Graph, under Examples > Scope > Styling. The first example there demonstrates VertexStyle and EdgeStyle. $\endgroup$ – Rahul Feb 23 '15 at 16:07
  • $\begingroup$ @Rahul thank you very much for teaching. I would better prefer to go real fishing with you. I am almost sure that we will catch at least some, of course with your help:) $\endgroup$ – Seyhmus Güngören Feb 23 '15 at 19:53
  • $\begingroup$ @YvesKlett I tried, got some improvement but not yet enough. $\endgroup$ – Seyhmus Güngören Feb 23 '15 at 20:50
2
$\begingroup$

Re-using some pieces from the answer in the linked Q/A:

n = 30;
d = 1/4;
v = RandomReal[{0, 1}, {n, 2}];
vertices = Range@n;
edges = Select[Flatten[Table[{a, b}, {a, v}, {b, v}], 1], 
              (#[[1, 1]] > #[[2, 1]] && EuclideanDistance[#[[1]], #[[2]]] < d) &];
edgelst = Map[Rule[Position[v, #[[1]]][[1, 1]], Position[v, #[[2]]][[1, 1]]] &, edges];

nF = Nearest[v -> vertices];
cntr = nF[Mean@v][[1]];

styles = {VertexCoordinates -> v, DirectedEdges -> False, VertexStyle -> Black, 
    EdgeStyle -> Blue, VertexLabels -> "Name", ImagePadding -> 20, PlotRange -> {{0, 1}, {0, 1}}};
cntrstyle = {VertexStyle -> Red, VertexSize -> Scaled[.05], 
             VertexLabels -> Placed[{"Name", Style["A", 14]}, {Above, Center}]};

Use Property to set the properties of the vertex cntr:

Graph[vertices /. cntr -> Property[cntr, cntrstyle], edgelst, styles]

enter image description here

or, use SetProperty to change the properties of cntr:

g1 = Graph[vertices, edgelst, styles];
SetProperty[{g1, cntr}, cntrstyle]
(* same picture *)
$\endgroup$
  • $\begingroup$ very beautiful thank you very much. $\endgroup$ – Seyhmus Güngören Feb 23 '15 at 21:32
  • $\begingroup$ @Seyhmus, my pleasure. $\endgroup$ – kglr Feb 23 '15 at 21:33
  • $\begingroup$ I obtained a very nice graph and I put it in to the text but the numbers seemed to be a bit small and the edges are a bit thin. Given a graph already drawn, is it possible ot modify it? $\endgroup$ – Seyhmus Güngören Feb 26 '15 at 1:26
  • $\begingroup$ @Seyhmus, you can change the VertexStyle settings in styles to VertexLabels -> (# -> Placed[#, Above, Style[#, 16] &] & /@ vertices) and that in cntrstyle to VertexLabels -> Placed[{cntr, "A"}, {Above, Center}, Style[#, 16] &] to change the fontsizes. To get thicker edges you can use EdgeStyle -> Directive[Thick, Blue] or, for more control on thickness, EdgeStyle -> Directive[CapForm["Round"], Thickness[.03], Blue]. $\endgroup$ – kglr Feb 27 '15 at 12:07
1
$\begingroup$
Module[{nOld, kOld, v, vertices, edges}, nOld = -1; kOld = -1; Manipulate[If[n != nOld, v= RandomReal[{0, 1}, {n, 2}]; nOld = n]; vertices = Range@n; edges = Select[Flatten[Table[{a, b}, {a, v}, {b, v}], 1], (#[[1, 1]] > #[[2, 1]] && EuclideanDistance[#[[1]], #[[2]]] < d) &];edgelst = Map[Rule[First@First@Position[v, #[[1]]], First@First@Position[v, #[[2]]]] &, edges]; Graph[vertices, edgelst, VertexSize -> 1, VertexStyle -> Black, EdgeStyle -> Blue, VertexCoordinates -> v, DirectedEdges -> False], {n, 10, 100, 10}, {d, 0, 1, 1/4}]]

This is the current situation. I still dont have the red label point. I tried but didnt work out. I also deleted manipulate and then it didnt function. Can someone help me? I need to have it worked without manipulate and I need a red point on one middle node with a letter say "A"

I used this

SetProperty[g, {EdgeStyle -> Directive[CapForm["Round"], Thickness[.003424658], Blue], Frame -> True, FrameStyle -> LightGray, VertexLabelStyle -> Directive[Black, 16], VertexStyle -> Black}]

enter image description here

your suggestion eventually gave me enter image description here

$\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.