Graph Representations

Lecture 39: Dijkstra’s • Algorithm • Adjacency List

CSCI 62 Fall, 2016

Kim Bruce & Peter Mawhorter

Adjacency Matrix Adjacency Lists

A B C D A B C D A 0 1 1 1 B A D B 1 0 0 1 C A D A B C 1 0 0 0 D 1 1 0 0 • Good for sparse graphs, saves space • Good for dense graphs Can put in edge • lists on right can be vectors, array on left could be • Constant time lookup for edges. weights as entries map Linear time lookup for edges. • Symmetric if undirected. • Complexity of BFS Single-Source Shortest Paths

• Enqueue the start node • while the queue is not empty Like Breadth-first search • Dequeue a node • • if the the node has not been visited previously, • If all paths have length 1 visit it • • Otherwise use and mark with • enqueue all of the node’s children predecessor so can find shortest path. • Adjacency List: • Assume all edges have non-negative weights. • Each edge contributes both end points to queue • Due to Dijkstra • O(max(v,e)) if each visit takes constant time • More expensive with adjacency matrix

Denver Chicago Denver Chicago Williams ∞ ∞ Williams 13 13 ∞ 20 20 7 7 SF SF 0 12 11 0 12 11

∞ 15 15 Boston Boston 42 ∞ 42

LA LA Denver Chicago Denver Chicago 20 ∞ Williams 20 ∞ Williams 13 ∞ 13 ∞ 20 20 7 7 SF SF 0 12 11 0 12 11

∞ 57 15 15 Boston Boston 15 42 15 42

LA LA

PQ = [SF], adding LA and Denver PQ = [LA,D], adding B, D

Denver Chicago Denver Chicago 20 33 Williams 20 33 Williams 13 ∞ 13 ∞ 20 20 7 7 SF SF 0 12 11 0 12 11

57 40 15 15 Boston Boston 15 42 15 42

LA LA

PQ = [D,B], adding C, LA, SF PQ = [C, B], adding B Denver Chicago Denver Chicago 20 33 Williams 20 33 Williams 13 51 13 51 20 20 7 7 SF SF 0 12 11 0 12 11

40 40 15 15 Boston Boston 15 42 15 42

LA LA

PQ = [B], adding W

Single Source Shortest Path Single Source Shortest Path Problem Problem

• From a starting node s, find the shortest path • If all edges have weights ≥ 0, then we use (and its length) to all other (reachable) nodes Dijkstra’s algorithm • The collection of all shortest paths form a , • Essentially just BFS with priority queue called... the shortest path tree! • Priorities are best known distance to a node from the source • If all edges have the same weight, we can use BFS. • Keep track of parents as in BFS so can get path • Otherwise … • Example of a “greedy” algorithm! Dijkstra Dijkstra Algorithm

• Set dist[v] to ∞ for all v, except dist[s] = 0 • Variables • Add s to Q with priority 0.0 • graph G • loop while Q is not empty: • vertex_t s // start node get node cur with min priority d (distance from s) if (d ≤ dist[cur]) • double length[n][n] // Edge lengths (adj. list) for each out going edge cur→v:

• double dist[n] // Current best distance if dist[cur] + length[cur][v] < dist[v]: • vertex_t parent[n] // Current parent dist[v] = dist[cur] + length[cur][v] • pqueue Q // priority queue parent[v] = cur Add v to Q with new priority dist[v]

Run Dijkstra on Sample Run-Time of Dijstra Graph • Let v = #vertices, e = #edges • Adding and removing from priority queue, O(log v) • Each goes on and off once, so O(v log v) • reduce_priority O(log v) • Worst case, once for each edge, so O(e log v) • Total time O((e+v) log v)