I have a code which takes as input RandomSample[Range[d^2],4]
for a given d
and outputs a value q
which is either 0
or 1
.
The following code outputs a Table
with 5
elements of the form {RandomSample[Range[d^2],4],q}
. In this specific case d=4
.
d = 4; times=5;
testpath[v_] :=
Module[{neighs, i, intersections},
neighs =
Delete[neighlist, {Flatten[Position[nodlist, v[[1]]]],
Flatten[Position[nodlist, v[[-1]]]]}]; intersections = List[];
For[i = 1, i <= Length[neighs], i++,
AppendTo[intersections, Length[Intersection[v, neighs[[i]]]]]];
If[Times @@ Boole[EvenQ[intersections]] == 1, 1, 0]]
nbp = 2; order = 0;
graph = GridGraph[{d, d}, VertexLabels -> "Name"];
Table[
nodlist = RandomSample[Range[d^2], 2 nbp];
For[i = 1; j = 0; lenshortpaths = List[]; allpaths = List[];
neighlist = List[]; validpaths = List[];
numvalidpaths = List[], i <= 2 nbp - 1, i = i + 2, j = j + 1;
AppendTo[lenshortpaths,
Length[FindShortestPath[graph, nodlist[[i]], nodlist[[i + 1]]]]];
AppendTo[allpaths,
FindPath[
VertexDelete[graph,
Complement[nodlist, {nodlist[[i]], nodlist[[i + 1]]}]],
nodlist[[i]], nodlist[[i + 1]], lenshortpaths[[j]] + order,
All]];
For[k = 1; l = 0, k <= Length[allpaths[[j]]], k++,
If[IsomorphicGraphQ[Subgraph[graph, allpaths[[j, k]]],
PathGraph[allpaths[[j, k]]]],
AppendTo[validpaths, allpaths[[j, k]]]; l = l + 1]];
AppendTo[numvalidpaths, l];
AppendTo[neighlist,
Complement[
VertexList[
NeighborhoodGraph[graph, nodlist[[i]]]], {nodlist[[i]]}]];
AppendTo[neighlist,
Complement[
VertexList[
NeighborhoodGraph[graph, nodlist[[i + 1]]]], {nodlist[[
i + 1]]}]]];
occ = 2; validpathsx = validpaths;
While[And[occ == 2, Length[validpathsx] >= 0],
If[Length[validpathsx] > 0,
If[testpath[validpathsx[[1]]] == 1, occ = 1;(*Print[occ]*),
validpathsx = Delete[validpathsx, 1]], occ = 0;(*Print[
occ]*)]]; {nodlist, occ}, times]
(*{{{4, 6, 5, 13}, 0}, {{12, 6, 1, 8}, 1}, {{11, 15, 5, 8}, 1},
{{3, 10, 6, 16}, 1}, {{10, 3, 11, 4}, 0}}*)
Heres what I want to do; For any number between 1
and 16
(i.e.d^2) I need to calculate the ratio between the number of times it was part of a random sample with a corresponding q
value of 1
vs the number of times with q=0
.
For example, if for times=5
I get {{{4, 6, 5, 13}, 0}, {{12, 6, 1, 8}, 1}, {{11, 15, 5, 8}, 1}, {{3, 10, 6, 16}, 1}, {{10, 3, 11, 4}, 0}}
, then for 11
the ratio would be 1
, since its part of {11, 15, 5, 8}
and {10, 3, 11, 4}
with q
values 1
and 0
respectively.
I then need to plot the d^2
ratios in a coloured grid plot.