Say I want to construct an $n$-dimensional hypercube graph $G$ using the command: G = HypercubeGraph[n]
. I'd like to assign edge weights to the $2^{(n-1)}n$ edges in $G$, $(e_1,e_2,...)$, with something like a function assignWeights[G,weightList]
where weightList
is of the form:
weightList = {{grayCodeA, grayCodeB, w1},{grayCode..., grayCode..., w2}, ...};
Here, each entry in weightList
specifies a weight $w_i$ for an edge between vertices in the hypercube correspond to specific Gray codes (e.g. between "0010" and "0110" codes for the $n = 4$ dimensional hypercube in this picture: http://en.wikipedia.org/wiki/File:Hamming_distance_4_bit_binary.svg). Nothing's better than an example, so let's construct one for the $n = 3$ dimensional hypercube, where our edgeweights are just random real numbers over the interval $[0,1]$:
n = 3;
tupleSet = Tuples[{0, 1}, n];
weightList = Array[{} &, 2^(n - 1)*n];
counter = 0;
For[a = 1, a <= Length[tupleSet], a++,
For[b = a + 1, b <= Length[tupleSet], b++,
If[HammingDistance[tupleSet[[a]], tupleSet[[b]]] == 1,
counter += 1;
weightList[[counter]] = {tupleSet[[a]], tupleSet[[b]], RandomReal[{0, 1}]};
];
];
];
weightList
(Please let me know if you there's a nice built-in way to generate these tuple pairs without nested loops...)
How would we write something like assignWeights[G,weightList]
? Once we assign the weights, can we color code the edges to visually represent the weight assignments?