# Lecture 6 - Shortest Path Algorithms

## Matrix Multiplication Algorithm

Let $\delta^{(m)}_{i,j}$ be the length of a shortest path from $i$ to $j$, subject to the condition that the path contains no more than $m$ arcs. It follows that:

This is the recurrence equation. We are slicing the problem of shortest path into layers, with each layer containing $m$ edges. If $m = n$ then we have the shortest path, where $n$ is the number of vertices.

We define a new matrix multiplication $p_{i,j} = A \bigotimes B$ as $p_{i,j} = min_k{a_{i,k} + b_{i,k}}$.

Let $W = w(i,j)$ be the matrix of arc weights. Let further $D^{(m)} = \delta^{(m)}_{i,j}$ be the matrix of the $m$th order approximation:

Note that $D^{(0)}$ is the identity matrix. It follows that $D^{(n-1)} = W^{(n-1)}$. Hence we can reduce the number of computational steps to solve by calculating $W^2 = W \bigotimes W, W^4 = W^2 \bigotimes W^2$

Okay I’m lost. Next slide pls

### Example for the Matrix Multiplication Algorithm

We are given the directed graph with negative edges (note that we don’t have negative cycles as that would make computation last infinitely):

This produces the following matrix

Multiplication happens like this:

$D = W^4 \bigotimes W =$

## Floyd-Warshall Algorithm

Also performs the same task, finding the shortest path in a directed graph with possible negative edges but no negative cycles.

### Definition

Let $\delta^m_{i,j}$ be the length of a shortest path from $i$ to $j$ in $G$ that uses only the vertices in ${1,…m,i,j}$

Then $\delta^{n}_{ij}$ provides the distances between any pair $(i,j)$ of vertices in $G$ and can be computed by the following recurrence relation:

Operations on the matrix using this algorithm (Eq. 5, I don’t remember seeing this I’m sure it’s in the notes)

This algorithm has a running time of $O(n^3)$, since we do the pass $n$ times, filling $n^2$ values into the matrix.

### Example on the same graph

HAIKO GIVE ME MY GRAPH BACK

omg he returned.

This is now a live commentary.

### Correctness

Since we allow negative weights, we have to ensure that the concatenation of the paths $(i,…,m)$ and $(m,…,j)$ is a simple path. We apply induction on $m$

• For $m = 0$ all our shortest paths have at most one edge
• For $k \gt 0$ assume, for the sake of contradiction that $(u,…,m,…,v)$ contains a vertex $w$ twice
• By induction hypothesis: $(u,…,m)$ and $(m,…,v)$ are simple paths, and both contain $w$.
• If the walk is shorter than the path, then the cycle has negative weight
• We can decide that we ignore this