0

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

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • What does "generating a 5-dimensional hypercube" mean? What data needs to be generated? – Oliver Charlesworth Apr 09 '12 at 17:20
  • well i have to setup the n-dimensional hypercube to then take the adjacency matrix of it(which isn't hard once i have it setup) to then do some eigen value and linear algebra analysis of the adjacency matrix – pyCthon Apr 09 '12 at 17:23
  • Ok. Then you should definitely edit your question to say that it's the adjacency matrix that you're after. Also, C and Matlab are very different languages; which one are you interested in? – Oliver Charlesworth Apr 09 '12 at 17:29
  • you beat me to the edits xD thanks and yeah ican work with c or matlab fairly eaisly so that's why i put those – pyCthon Apr 09 '12 at 18:34

1 Answers1

3

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
Richante
  • 4,353
  • 19
  • 23
  • this is very nice and thank you for even going a step further – pyCthon Apr 09 '12 at 17:46
  • i'm getting all 0's as a result? i'm going to try to figure out why – pyCthon Apr 09 '12 at 18:00
  • L1_dists = sum(vertices - repmat(vertices(v, :), 2^5, 1), 2); this line is given me an error – pyCthon Apr 09 '12 at 18:16
  • is the above line supposed to be `L1_dists = sum(adj_mat- repmat(vertices(v, :), 2^5, 1), 2);`? – pyCthon Apr 09 '12 at 18:37
  • Yikes! Really sorry, I messed up `adj_mat(v, L1_dists == 1) = 1`. That should have had a `find` in it. I've fixed it now. – Richante Apr 09 '12 at 19:37
  • 1
    I'm pretty sure the rest is correct - it works for me. Did you copy the `ttable` function and run `vertices = ttable(ones(1, 5)*2)-1`? – Richante Apr 09 '12 at 19:38
  • what about this line ? I get this error Error using - Matrix dimensions must agree `L1_dists = sum(vertices - repmat(vertices(v, :), 2^5, 1), 2);` – pyCthon Apr 09 '12 at 19:39
  • yeah i copied it exact let me try and copy it again – pyCthon Apr 09 '12 at 19:39
  • check what `size(vertices)` and `size(repmat(vertices(v, :), 2^5, 1))` are. They should both be 32-by-5. – Richante Apr 09 '12 at 19:40
  • size(verticies) is 32-by-5 but size(repmat(vertices(v, :), 2^5, 1)) is giving me 1024-by-5 – pyCthon Apr 09 '12 at 19:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9875/discussion-between-richante-and-pycthon) – Richante Apr 09 '12 at 19:45