I am trying to create an adjacency matrix for \(n\times n\) square lattice. If \((i,j)\) denotes a vertex in the lattice then I first index all the nodes in the lattice using a single index \(k\) such that \(k=(i-1)*n+j\). After that I want to write \(n^2\times n^2\) adjacency matrix. But I am not able to do it properly. Please help me. Thank you

## Comments

Sort by:

TopNewestYou move between adjacent vertices in the same row by adding or subtracting \(1\), and you move between adjacent vertices in the same column by adding or subtracting \(n\). Thus vertex \(k\) is adjacent to:

Vertex \(k+1\), unless \(n\) divides \(k\),

Vertex \(k-1\), unless \(n\) divides \(k-1\),

Vertex \(k+n\), unless \(k+n > n^2\),

Vertex \(k-n\), unless \(k-n\le 0\).

Adjacency matrices for \(2\times2\) and \(3\times3\) grids are \[ \left(\begin{array}{cccc}0&1&1&0\\1&0&0&1\\1&0&0&1\\0&1&1&0\end{array}\right) \qquad \qquad \left(\begin{array}{ccccccccc}0&1&0&1&0&0&0&0&0\\1&0&1&0&1&0&0&0&0\\0&1&0&0&0&1&0&0&0\\ 1&0&0&0&1&0&1&0&0\\0&1&0&1&0&1&0&1&0\\0&0&1&0&1&0&0&0&1\\ 0&0&0&1&0&0&0&1&0\\0&0&0&0&1&0&1&0&1\\0&0&0&0&0&1&0&1&0 \end{array}\right) \] – Mark Hennings · 3 years ago

Log in to reply

– Snehal Shekatkar · 3 years ago

Yess.. I modified my code and it works perfectly now!! Thank you very much sir!Log in to reply

– Snehal Shekatkar · 3 years ago

Thank you. This really looks cool.. I will modify my code now. I was mainly having problem at edges but now I got your idea of checking whether \(n\) divides those indices or not. I will get back to you soon.Log in to reply