×

# Adjacency matrix for a square lattice

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

Note by Snehal Shekatkar
4 years, 2 months ago

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold
- bulleted- list
• bulleted
• list
1. numbered2. list
1. numbered
2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in $$...$$ or $...$ to ensure proper formatting.
2 \times 3 $$2 \times 3$$
2^{34} $$2^{34}$$
a_{i-1} $$a_{i-1}$$
\frac{2}{3} $$\frac{2}{3}$$
\sqrt{2} $$\sqrt{2}$$
\sum_{i=1}^3 $$\sum_{i=1}^3$$
\sin \theta $$\sin \theta$$
\boxed{123} $$\boxed{123}$$

Sort by:

You 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:

1. Vertex $$k+1$$, unless $$n$$ divides $$k$$,

2. Vertex $$k-1$$, unless $$n$$ divides $$k-1$$,

3. Vertex $$k+n$$, unless $$k+n > n^2$$,

4. 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)$

- 4 years, 2 months ago

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

- 4 years, 2 months 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.

- 4 years, 2 months ago