is there any algorithm or method of generating the adjacency matrix for a hypercube for any dimension? say your input is 5 it would create a 5-dimensional hypercube
all i can find are sources from wiki
and wolfram
is there any algorithm or method of generating the adjacency matrix for a hypercube for any dimension? say your input is 5 it would create a 5-dimensional hypercube
all i can find are sources from wiki
and wolfram
If you want to generate the vertices of a N-D unit hypercube, you can basically make an N-value truthtable. Here's some code I use for that:
function output = ttable(values)
output = feval(@(y)feval(@(x)mod(ceil(repmat((1:x(1))', 1, numel(x) - 1) ./ repmat(x(2:end), x(1), 1)) - 1, repmat(fliplr(y), x(1), 1)) + 1, fliplr([1 cumprod(y)])), fliplr(values));
end
and to get the vertices of a 5-D hypercube you can call it like this:
vertices = ttable(ones(1, 5) * 2) - 1;
From here you can calculate the adjacency matrix by finding all vertices that differ by only one bit, i.e.:
adj_list = zeros(2^5, 5);
adj_mat = zeros(2^5, 2^5);
for v=1:2^5
L1_dists = sum(abs(vertices - repmat(vertices(v, :), 2^5, 1)), 2);
adj_list(v, :) = find(L1_dists == 1);
adj_mat(v, find(L1_dists == 1)) = 1;
end