# 1 graph algorithms single source shortest paths problem dana shapira

Post on 15-Jan-2016

215 views

Embed Size (px)

TRANSCRIPT

Graph AlgorithmsSingle source shortest paths problem

Dana Shapira

Given vertex s, for every vertex vV find a shortest path from s to v.Dijkstras algorithm solves this problem efficiently for the case in which all weights are nonnegative.Single Source Shortest Path Problem1234651015433126118

Single Source Shortest Path ProblemWe are given a weighted,directed graph G = (V, E), with weight function : E R mapping edges to real valued weights.The weight of a path

is123465101543312618

Shortest Path PropertiesWe have optimal substructure: Let p = be the shortest path between v1 and vk. The sub-path between vi and vj, where 1 i,j k, pij = is a shortest path.

Proof: suppose some subpath is not a shortest pathThere must then exist a shorter subpath Could substitute the shorter subpath for a shorter pathBut then overall path is not shortest path. Contradiction

Shortest Path PropertiesDefine (u,v) to be the weight of the shortest path from u to v

Triangle inequality: (u,v) (u,x) + (x,v)For each edge (x,v) (u,v) (u,x) + (x,v) (why?)

xuv

Shortest Path PropertiesIn graphs with negative weight cycles, some shortest paths will not exist (Why?):< 0

Negative weight cycles-1abced-24-232-51

RelaxationFor all v, maintain upper bound d[v] on (s,v)Relax(u,v,w) { if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v);}

Dijkstras AlgorithmDijkstra(G) for each v V d[v] = ; d[s] = 0; S = ; Q = V; (s)=NULL; while (Q ) u = ExtractMin(Q); S = S{u}; for each v Adj[u] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);(v)=uWhat will be the total running time?

Correctness Of Dijkstra's Algorithmd[v] (s,v) v Let u be first vertex in S such that d[u] (s,u) when it is removed from Q. sxyup2p1We must show that when u is removed from Q, it has already converged

Correctness Of Dijkstra's AlgorithmLet y be first vertex V-S on actual shortest path from su d[y] = (s,y)Because d[x] is set correctly for y's predecessor x S on the shortest path (u was the first vertex in S such that d[u] (s,u)) , When we put x into S, we relaxed (x,y), giving d[y] the correct valuesxyup2p1

Correctness Of Dijkstra's Algorithmd[u]> (s,u) = (s,y) + (y,u) (Why?) = d[y] + (y,u) d[y]But if d[u] > d[y], wouldn't have chosen u. Contradiction.sxyup2p1

QuestionCan you give an example of a graph that includes negative weights, and does not work with Dijkstras algorithm?

Bellman-Ford AlgorithmBellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); (v)=u; for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution;

Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v);

Bellman-Ford AlgorithmBellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); (v)=u; for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution;

Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v);

What will be the running time?

O(|V||E|)

Bellman-Ford AlgorithmBellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); (v)=u; for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution;

Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w

BEDCA-1221-3534Examples

Bellman-FordNote that order in which edges are processed affects how quickly it converges.Correctness: show d[v] = (s,v) after |V|-1 passesLemma: d[v] (s,v) alwaysInitially trueLet v be first vertex for which d[v] < (s,v)Let u be the vertex that caused d[v] to change: d[v] = d[u] + (u,v)Then d[v]< (s,v) (s,u) + (u,v) d[u] + (u,v) (Why?)So d[v] < d[u] + (u,v). Contradiction.

Bellman-FordProve: after |V|-1 passes, all d values correctConsider shortest path from s to v: s v1 v2 v3 vInitially, d[s] = 0 is correct, and doesnt change (Why?)After 1 pass through edges, d[v1] is correct (Why?) and doesnt changeAfter 2 passes, d[v2] is correct and doesnt changeTerminates in |V| - 1 passes: (Why?) What if it doesnt?

Bellman-FordLet be a negative cycle. Suppose that the algorithm does not find any problem in the last iteration.

Since contradiction

Bellman-Ford Example

DAG Shortest PathsProblem: finding shortest paths in DAGBellman-Ford takes O(|V||E|) time. How can we do better?Idea: use topological sortIf were lucky and processes vertices on each shortest path from left to right, would be done in one passEvery path in a DAG is subsequence of topologically sorted vertex order, so processing vertices in that order, we will do each path in forward order (will never relax edges out of vertices before doing all edges into vertices). Thus: just one pass. What will be the running time?

DAG Shortest Paths Examples3abcd514296-1

Recommended