# Lecture 4 - Shortest paths in weighted directed graphs

Specifically, we will talk about single-source shortest path. This is the shortest path to all vertices from a single vertex.

## Dijkstra’s Algorithm

### The Problem

Given a directed graph $G = (V,E)$, a length (weight / cost) function $w: E \rightarrow \Q \ge 0$, and a source vertex $s$.

Find the shortest directed path from $s$ to every other vertex.

### How does Dijkstra work?

Works similar to BFS or Prim’s algorithm. When extending the set $S$, set the potential distance of a vertex to the shortest path found to it. You only need to look at the last vertices and the distance previously computed. Any vertices not yet visited, we assume its distance is infinity.

It iteratively grows the set $S$ of visited vertices, for which we calculate the shortest path from start $s$ to a vertex $u$ ($l(u)$). ### Dijkstra Pseudocode ### Running time of Dijkstra’s algorithm

We can see from the pseudocode:

procedure Dijkstra(D,w,s)
for v in V // O(n)
...
for v in N+(s) // O(n)
...
while (L != emtpy) // O(n)
...
choose // This choice is also O(n)
...
for v in N+(u) // Constant, so O(deg+(u))
...