Adjacency matrix

We can represent an n-node graph by an n-by-n matrix. Assume the nodes are labeled $$v_1, \ldots, v_n$$. Then the adjacency matrix $$A$$ is an n by n matrix whose entry $$A_{ij}$$ is defined to be
 * 1, if there is an edge from $$v_i$$ to $$v_j$$
 * 0, otherwise.

Note that an undirected graph corresponds to a symmetric matrix.

We could instead put weights on the edges, and define $$A_{ij}$$ to be
 * 0, if there is no edge from $$v_i$$ to $$v_j$$
 * $$w$$, if there is an edge, and $$w$$ is its weight.

See Graph Data Structures for another definition of the adjacency matrix.

Multiplication of the Adjacency Matrix
Note that the entry $$A_{ij}$$ of an unweighted adjacency matrix simply counts the number of paths from $$v_i$$ to $$v_j$$ 'of length one'. A useful fact is that similarly, the entry $$(A^k)_{ij}$$ of the $$k$$th power of the adjacency matrix counts the number of paths of length exactly $$k$$ from $$v_i$$ to $$v_j$$. By using repeated squaring we can sometimes count paths quickly.