Sub topic 14
The incidence matrix
This chapter assumes you know the edge list, the adjacency matrix, and the adjacency list from the earlier chapters. It also assumes you can read nested Python lists and use introductory Big-O notation.
What it stores
An incidence matrix relates vertices to edges instead of vertices to vertices. For a graph with n vertices and m edges, it is an n-by-m table. Each row is a vertex and each column is an edge. An entry is nonzero when that vertex is an endpoint of that edge.
An adjacency matrix answers "are these two vertices joined?". An incidence matrix answers "which vertices does this edge touch, and which edges touch this vertex?". Each edge gets its own column, so the structure can describe graphs that the plain adjacency matrix cannot.
Undirected graphs
In an undirected graph, put a 1 in each row for the two endpoints of an edge. The code below uses a graph with a parallel edge (A–B appears twice).
edges = [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"), ("A", "B")]
nodes = ["A", "B", "C", "D"]
index = {name: i for i, name in enumerate(nodes)}
m = len(edges)
inc = [[0] * m for _ in nodes]
for j, (u, v) in enumerate(edges):
inc[index[u]][j] += 1
inc[index[v]][j] += 1 # a self-loop (u == v) adds 2 to one cell
for name, row in zip(nodes, inc):
print(name, row)
print([sum(row) for row in inc]) # degrees: [3, 3, 2, 2]The output is:
A [1, 1, 0, 0, 1]
B [1, 0, 1, 0, 1]
C [0, 1, 0, 1, 0]
D [0, 0, 1, 1, 0]
[3, 3, 2, 2]Two properties follow from this layout:
- Every column has exactly two 1s, because every edge has two endpoints. A self-loop produces a single 2.
- Summing a row gives that vertex's degree, including the effect of parallel edges and self-loops.
Parallel edges A–B appear as two separate columns (0 and 4). An adjacency matrix would need a count in one cell to show this.
Directed graphs
For a directed edge, mark the tail with -1 and the head with +1. The tail is where the edge starts. The head is where it ends.
edges = [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D")]
nodes = ["A", "B", "C", "D"]
index = {name: i for i, name in enumerate(nodes)}
m = len(edges)
inc = [[0] * m for _ in nodes]
for j, (u, v) in enumerate(edges):
inc[index[u]][j] = -1 # edge leaves u
inc[index[v]][j] = 1 # edge enters v
for name, row in zip(nodes, inc):
print(name, row)
print([sum(inc[i][j] for i in range(len(nodes))) for j in range(m)]) # [0, 0, 0, 0]Every column sums to 0 because each edge contributes one -1 and one +1. Count the -1 entries in a row to get that vertex's out-degree. Count the +1 entries to get its in-degree.
A directed self-loop does not fit this scheme, because the -1 and +1 land in the same cell and cancel. If you need directed loops, store them separately.
Costs
| Operation | Incidence matrix |
|---|---|
| Space | n × m cells |
| Are u and v adjacent? | Scan the columns, up to O(m) |
| Endpoints of edge j | Scan one column, O(n) |
| Edges at vertex v | Scan one row, O(m) |
Compared with an adjacency matrix, the incidence matrix wins only when m is smaller than n. That is rare for connected graphs, since a connected graph has at least n − 1 edges. It is also mostly zeros, with just two nonzero cells per column.
Where it is used
- Linear algebra. For a directed graph, multiplying the incidence matrix by a vector of edge values gives the net flow at each vertex. Network-flow problems use this to write conservation constraints.
- Graph properties. The rank of a directed incidence matrix equals n minus the number of connected components.
- Optimization. Directed incidence matrices are totally unimodular. This means linear-programming solutions to flow and shortest-path formulations come out as whole numbers.
- Hypergraphs. A column can have more than two nonzero entries, so one "edge" can join several vertices. An adjacency matrix cannot express this.
Exercise
A directed graph has edges A→B, B→C, and A→C. Write its incidence matrix with rows A, B, C and columns in that edge order. Then say what the row sums of the negative entries tell you.
Show answer
Row A is [-1, 0, -1], row B is [1, -1, 0], and row C is [0, 1, 1]. Each column sums to 0. Counting the -1 entries in a row gives the out-degree: A has 2, B has 1, C has 0. Counting the +1 entries gives the in-degree: A has 0, B has 1, C has 2.