cmatlabmultidimensional-array# generating distanced matrix for an n-dimensional hypercube

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

Solution

I know that this is a bit late, but the solution is distinct from the others and based on recursion, so hopefully someone will find it useful.

First lets build some intuition about how to build an N-cube from an (N-1) cube! When you think of a 1-cube, it's just a line segment. To get a 2-cube(square), we make a copy of the line segment, translate it, and add edges along the path of translation. Similarly, to get a 3-cube, we take that square, make a copy of it, translate it, and add the translation edges. If you have a hard time visualizing that, contemplate the image below:

If only there was mathematical machinery that does this! We're in luck because there is: the Kronecker product. In essence it gives us a way to copy adjacency matrices, and then it's just a matter of glueing the results together. Glueing is accomplished by adding the dashed lines in the above figures as additional edges in the adjacency matrix of the two copies of the lower dimensional cubes. In picture form (because this heathen site doesn't support LaTeX): And that's that! The adjacency matrix for a hypercube of arbitrary dimension is the sum of these two pieces. One last caveat: these matrices grow exponentially and are mostly filled with zeros (each node only connects to N of the 2^N other nodes), so sparse matrices are a must. Here's a quick MATLAB implementation. Enjoy!

```
function A = hypercube(N)
if N==1
A = sparse([0 1;1 0]);
else
A = kron(sparse(eye(2)),hypercube(N-1)) + ...
kron(hypercube(1),diag(sparse(ones(1,2^(N-1)))));
end
end
```

- Static variable in a loop vs variable and a loop in a block
- Should I use global variables in recursion
- What are canonical types in Clang?
- Difference between passing array and array pointer into function in C
- undefined reference error in VScode
- Serial communication C/C++ Linux thread safe?
- Invalid c declaration that passes k&r grammar
- Delete a file named "NUL" on Windows
- C++ parallel sort
- sprintf_s was not declared in this scope
- How can I read an input string of unknown length?
- confuse on building and using dll with c++
- Static assert in C
- Program doesn't stop after reading from pipe
- Can FFmpeg be used as a library, instead of a standalone program?
- #include <Python.h> no such file or directory for windows 10 fix
- CS50 problem with check on Bulbs exercise, dysplaing a extra bulb on check but not on terminal
- Export a structure from C to wasm?
- Why can't linux write more than 2147479552 bytes?
- Raw LWIP Send TCP Transmission to Static IP
- Flex+ Bison integration errors: stray '\' and expected ';' before '{' token
- Bitwise operation without using any predefined C functions
- Can a C macro be made to take an arbitrary list of primitive types, and work out the sum of their sizeof() values?
- mmap SIGBUS error and initializing the file
- How do I make a linked list filled with random numbers and display them?
- STM32F4 ADC with DMA 7 Channel regular group isn't working
- What exactly is the base pointer and stack pointer? To what do they point?
- How to define INT128_MAX and INT128_MIN for __int128?
- Lifetime of a string literal returned by a function
- wrong usage of pthread_create?