Algorithm Search⌘K

Proofs: Incidence matrix

These are the human proofs of Incidence matrix, at full length.

The incidence-matrix operations keep the column invariant and answer the graph queries full

This proof shows: correctness.

We use the pseudocode of the page. The graph has the vertices 0,…,n−10, \ldots, n - 1 and the edges e0,…,em−1e_0, \ldots, e_{m-1}, where edge ee has the ends t(e)t(e) and h(e)h(e) (the tail and the head if the graph is directed). The invariant is:

(C) BB has exactly mm columns. In the directed case, column ee has B[t(e),e]=−1B[t(e), e] = -1, B[h(e),e]=+1B[h(e), e] = +1 and 0 elsewhere, with t(e)≠h(e)t(e) \ne h(e). In the undirected case, column ee has B[v,e]=∣{i:xi=v}∣B[v, e] = |\{i : x_i = v\}| where (x1,x2)=(t(e),h(e))(x_1, x_2) = (t(e), h(e)), that is, 1 at each of two distinct ends, or 2 at the vertex of a self-loop, and 0 elsewhere.

With m=0m = 0, the matrix has nn empty rows, and (C) holds.

ADD-EDGE. Line L1 adds the column e=me = m with nn zeros, and no other column changes. In the directed case, u≠vu \ne v, and line L2 writes −1-1 in row uu and +1+1 in row vv, two different cells. In the undirected case, line L3 adds 1 to row uu and then 1 to row vv of the new column. If u≠vu \ne v the column has 1 in both rows; if u=vu = v the cell gets 0+1+1=20 + 1 + 1 = 2. In both cases column ee meets (C) for the new edge with t(e)=ut(e) = u and h(e)=vh(e) = v, and the matrix has m+1m + 1 columns.

ENDS. By (C), the nonzero entries of column ee are in the rows of the ends of ee and nowhere else. Lines L5 and L6 visit each row once and record vv with multiplicity ∣B[v,e]∣|B[v, e]|, so in the undirected case they record t(e)t(e) and h(e)h(e), which is vv twice for a self-loop at vv. In the directed case the entry −1-1 is in row t(e)t(e) and the entry +1+1 in row h(e)h(e), so line L7 returns the tail and the head.

INCIDENT. By (C), B[v,e]≠0B[v, e] \ne 0 if and only if v∈{t(e),h(e)}v \in \{t(e), h(e)\}, so line L8 returns exactly the edges at vv, each once.

ADJACENT. In the directed case, there is an edge from uu to vv if and only if some column ee has t(e)=ut(e) = u and h(e)=vh(e) = v, which by (C) is B[u,e]=−1B[u, e] = -1 and B[v,e]=+1B[v, e] = +1. Lines L9 to L11 test this for each column and return true at the first one, or false after all mm. In the undirected case with u≠vu \ne v, an edge joins uu and vv if and only if some column has nonzero entries in both rows; with u=vu = v, a self-loop at uu exists if and only if some column has B[u,e]=2B[u, e] = 2, because by (C) the entry 2 occurs only for a self-loop.

Degrees. In the undirected case, by (C), ∑eB[v,e]\sum_e B[v, e] counts each edge at vv once for each of its ends equal to vv, which is d(v)d(v) with a self-loop counted twice. In the directed case each edge leaving vv adds −1-1 and each edge entering vv adds +1+1, so the row sum is d−(v)−d+(v)d^-(v) - d^+(v).

Cost. ADD-EDGE appends one cell to each of nn rows, Θ(n)\Theta(n) amortized. ENDS reads nn cells, INCIDENT reads mm cells, and ADJACENT reads at most 2m2m cells. The matrix has nmnm cells. □\square

Back to Incidence matrix