# Random geometrical graph generation

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 edges 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?

• 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? – Yves Klett Feb 23 '15 at 15:16
• 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. – Seyhmus Güngören Feb 23 '15 at 15:18
• 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. – Rahul Feb 23 '15 at 16:07
• @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:) – Seyhmus Güngören Feb 23 '15 at 19:53
• @YvesKlett I tried, got some improvement but not yet enough. – Seyhmus Güngören Feb 23 '15 at 20:50

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[#[], #[]] < d) &];
edgelst = Map[Rule[Position[v, #[]][[1, 1]], Position[v, #[]][[1, 1]]] &, edges];

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

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] or, use SetProperty to change the properties of cntr:

g1 = Graph[vertices, edgelst, styles];
SetProperty[{g1, cntr}, cntrstyle]
(* same picture *)

• very beautiful thank you very much. – Seyhmus Güngören Feb 23 '15 at 21:32
• @Seyhmus, my pleasure. – kglr Feb 23 '15 at 21:33
• 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? – Seyhmus Güngören Feb 26 '15 at 1:26
• @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]. – kglr Feb 27 '15 at 12:07
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[#[], #[]] < d) &];edgelst = Map[Rule[First@First@Position[v, #[]], First@First@Position[v, #[]]] &, 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}] your suggestion eventually gave me 