I'm new to Mathematica. I'm trying to generate a random geometric graph in which to have a secure link between any two arbitrary nodes they should share a common key in their key rings (have assigned some keys to each node) in order to get a random key graph but when I write ShowGraph[g]
I'm unable to plot the graph. Here is my code.
v = {};
If[Length[v] == 0,
n = 100,(*Total number of nodes*)
n = Length[v];(*For pre-defined topologies*)
]
nprime = 20;(*Average number of neighbours per node*)rc =
N[Sqrt[(nprime + 1)/(n*\[Pi])]] (*Transmission range*)
rs = rc;(*Sensory range*)K = 4;(*Key ring size*)
Pstart = 10;(*Initial key pool size*)
Pend = 10;(*Last key pool size*)
If[Length[v] == 0,
nsim = 50,(*Number of iterations per P*)
nsim = 1;(*Only makes sense to simulate once for a fixed topology*)
];
edist[xi_, yi_, xj_, yj_] := {Sqrt[(xi - xj)^2 + (yi - yj)^2], xi, yi};
If[euclidean == True,
dist[xi_, yi_, xj_, yj_] := edist[xi, yi, xj, yj],
dist[xi_, yi_, xj_, yj_] := tdist[xi, yi, xj, yj]];
(* * * * * * * * * * * Main calculation * * * * * * * * * **)
(* * * * * * * * * * * * Main calculation * * * * * * * * * * * *)
For[P = Pstart, P <= Pend, P++,
For[sim = 1, sim <= nsim, sim++,
SeedRandom[sim];
(* Generate vertices if not using a fixed topology {{{ *)
If[Length[v] == 0 || sim > 1,
v = {};
For[i = 1, i <= n, i++, v = Append[v, {{Random[], Random[]}}]];
];
g = Graph[{}, v];
Print["Vertices: ", v]; (* Debug *)
(* Distribute keys randomly {{{ *)
keyrings = {};
For[i = 1, i <= n, i++,
keyring = {};
If[P == K,
For[k = 1, Length[keyring] < K, k++,
keyring = Append[keyring, k]
],
(* P > K *)
For[k = 1, Length[keyring] < K, k++,
key = Random[Integer, {1, P}];
If[MemberQ[keyring, key] == False,
keyring = Append[keyring, key]
]
]
];
keyrings = Append[keyrings, keyring];
];
Print["Key rings: ", keyrings]; (* Debug *)
(* Determine edges {{{ *)
For[i = 1, i <= n, i++,
For[j = i + 1, j <= n, j++,
xi = Extract[Extract[Extract[v, i], 1], 1];
xj = Extract[Extract[Extract[v, j], 1], 1];
yi = Extract[Extract[Extract[v, i], 1], 2];
yj = Extract[Extract[Extract[v, j], 1], 2];
If[dist[xi, yi, xj, yj][[1]] < rc,
If[Extract[keyrings, {i}] \[Intersection]
Extract[keyrings, {j}] != {},
g = AddEdge[g, {{i, j}}],
(* Else *)
Print["No secure link between ", i, " ", j]
]
]
]
];
(*g=Graph[vertices,edgelst,VertexCoordinates\[Rule]v,
DirectedEdges\[Rule]False];*)
ShowGraph[g];
]
];
What’s the problem? Can anyone help?
g
you will find out that you exceed the recursion limit1024
. So the Graph is not formed correctly... Also to useShowGraph
you must loadNeeds["Combinatorica`"]
$\endgroup$Combinatorica
it gives error like"Get::noopen: Cannot open DiscreteMathCombinatorica
." >> and without this it runs correctly it's may be because i'm using mathematica9... what should i do to plot the graph??plzz help... $\endgroup$