1 introduction to algorithms l ecture 17 (chap. 24) shortest paths i 24.2 single-source shortest...

Download 1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra 1930-2002

Post on 18-Jan-2016

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Introduction to Algorithms

    LECTURE 17 (Chap. 24)

    Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstras algorithm Edsger Wybe Dijkstra1930-2002

  • Edsger Wybe Dijkstra (May 11, 1930 August 6, 2002)Born in Rotterdam, Dijkstra studied theoretical physics at Leiden University, but he quickly realized he was more interested in computer science.Originally employed by the Mathematisch Centrum in Amsterdam, he held a professorship at the Eindhoven University of Technology in the Netherlands, worked as a research fellow for Burroughs Corporation in the early 1970s, and later held the Schlumberger Centennial Chair in Computer Sciences at The University of Texas at Austin, in the United States. He retired in 2000

  • Paths in graphsConsider a digraph G = (V, E) with edge-weightfunction w : E . The weight of path p = v1 v2 vk is defined to beExample :

  • Shortest pathsA shortest path from u to v is a path ofminimum weight from u to v. The shortest-path weight from u to v is defined as

    (u, v) = min{w(p) : p is a path from u to v}.

    Note: (u, v) = if no path from u to v exists.

  • Optimal substructureTheorem. A subpath of a shortest path is ashortest path.Proof. Cut and paste:

  • Triangle inequality Theorem. For all u, v, x V, we have(u, v) (u, x) + (x, v).Proof.

  • Well-definedness of shortest pathsIf a graph G contains a negative-weight cycle,then some shortest paths may not exist.Example:

  • Single-source shortest pathsProblem. From a given source vertex s V, findthe shortest-path weights (s, v) for all v V.

    If all edge weights w(u, v) are nonnegative, allshortest-path weights must exist.

    IDEA: Greedy. Maintain a set S of vertices whose shortest-path distances from s are known.At each step add to S the vertex v V S whose distance estimate from s is minimal.Update the distance estimates of vertices adjacent to v.

  • Dijkstras algorithmd[s] 0for each v V {s} do d[v] S Q V Q is a priority queue maintaining V Swhile Q do u Extract-Min(Q) S S {u} for each v Adj[u] doImplicit DECREASE-KEYrelaxationstepif d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v)

  • Example of DijkstrasalgorithmGraph withnonnegativeedge weights:

  • Example of DijkstrasalgorithmS : {}

  • Example of DijkstrasalgorithmS : {A}

  • Example of DijkstrasalgorithmS : {A}

  • Example of DijkstrasalgorithmS : {A,C}

  • Example of Dijkstrasalgorithm

  • Example of Dijkstrasalgorithm

  • Example of Dijkstrasalgorithm

  • Example of Dijkstrasalgorithm

  • Example of Dijkstrasalgorithm

  • Example of Dijkstrasalgorithm

  • Correctness Part ILemma. Initializing d[s] 0 and d[v] for allv V {s} establishes d[v] (s, v) for all v V,and this invariant is maintained over any sequenceof relaxation steps.Proof. Suppose not. Let v be the first vertex forwhich d[v] < (s, v), and let u be the vertex thatcaused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] < (s, v) supposition (s, u) + (u, v) triangle inequality (s,u) + w(u, v) sh. path specific path d[u] + w(u, v) v is first violationContradiction.

  • Correctness Part IITheorem. Dijkstras algorithm terminates withd[v] = (s, v) for all v V.Proof. It suffices to show that d[v] = (s, v) for everyv V when v is added to S. Suppose u is the firstvertex added to S for which d[u] (s, u). Let y be the first vertex in V S along a shortest path from s to u, and let x be its predecessor:

  • Correctness Part II(continued)Since u is the first vertex violating the claimed invariant,we have d[x] = (s, x). Since subpaths of shortest pathsare shortest paths, it follows that d[y] was set to (s, x) +w(x, y) = (s, y) when (x, y) was relaxed just after x wasadded to S. Consequently, we have d[y] = (s, y) (s, u) d[u]. But, d[u] d[y] by our choice of u, and hence d[y]= (s, y) = (s, u) = d[u]. Contradiction.

  • Analysis of Dijkstra|V|timesdegree(u)timeswhile Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) thenHandshaking Lemma (E) implicit DECREASE-KEYs.

    Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY

    Note: Same formula as in the analysis of Primsminimum spanning tree algorithm.

    d[v] d[u] + w(u, v)

  • Analysis of Dijkstra(continued)

    Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q TEXTRACT-MIN TDECREASE-KEY Total

    arrayO(V)O(1)O(V2)binaryheapO(lg V)O(lg V)O(E lg V)

    FibonacciheapO(lg V)amortizedO(1)amortizedO(E + V lg V)worst case

  • Unweighted graphsSuppose w(u, v) = 1 for all (u, v) E. Can thecode for Dijkstra be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first searchwhile Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v)Analysis: Time = O(V + E).

Recommended

View more >