09.07.2015 Views

Dijkstra's shortest path algorithm • This algorithm is a “greedy” one ...

Dijkstra's shortest path algorithm • This algorithm is a “greedy” one ...

Dijkstra's shortest path algorithm • This algorithm is a “greedy” one ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The inductive step of Dijkstra• The IH: Assume for n = k:1. for each t ∈ S, L k (t) <strong>is</strong> the length of a <strong>shortest</strong> <strong>path</strong> from s to t; and2. for v ∈ Q, L k (v) <strong>is</strong> the length of a <strong>shortest</strong> <strong>path</strong> from s to v that liestotally in S except for v itself.• Prove (1) for n = k + 1. Suppose u <strong>is</strong> added to S at the (k + 1)-st iteration.Before th<strong>is</strong> iteration, L k (u) <strong>is</strong> the length of a <strong>shortest</strong> <strong>path</strong> to u lying totally<strong>is</strong> S except for u itself. We have L k+1 (u) = min{L k (v) : v /∈ S}. We claimthat L k+1 (u) <strong>is</strong> the length of a <strong>shortest</strong> <strong>path</strong> from s to u. For if not, theremust be a shorter <strong>path</strong> to u exiting S at some vertex p ∈ S, on <strong>one</strong> step outof S for the first time to some v ≠ u. From part (2) of the IH we have L k (v)<strong>is</strong> the length of a <strong>shortest</strong> <strong>path</strong> from a to v lying in S except for v. We alsohave L k (v) < L k (u), because if L k (v) ≥ L k (u), then the “<strong>shortest</strong>” <strong>path</strong>length from a (through v) to u would be greater than the length of the <strong>path</strong>from a to u remaining in S except for u, that <strong>is</strong> L k (u). But th<strong>is</strong> contradictsthe choice of u having the minimum L k value. <strong>Th<strong>is</strong></strong> proves the claim.• Prove (2) for n = k + 1. Let p ∈ Q after iteration k + 1. A <strong>shortest</strong> <strong>path</strong>through (the new) S to p either goes through u as the last vertex in s or not.In the first case its length <strong>is</strong> L k (p). In the second case it <strong>is</strong> L k (u) + w(u, p).So its length <strong>is</strong> min{L k (p), L k (u) + w(u, p)}.5


Improving the complexity of DIJKSTRA• We can do better than running through all of Q to find and remove theelement of Q with minimum L value, in case the graph G = (V, E) <strong>is</strong>sparse – each vertex <strong>is</strong> of low degree.• We realize Q not with a table, but with a heap. <strong>Th<strong>is</strong></strong> <strong>is</strong> a (complete) binarytree whose nodes are labeled with L-values in such a way that each nodevalue <strong>is</strong> less than that of its immediate descendants.7


Ultimate effect on complexity of DIJKSTRA• Let G = (V, E).• EXTRACT-MIN takes O(log |V |) time, and there are |V | such operations.• Building the heap in the first place takes O(|V |).• Can do the step of setting vertices v /∈ S with an edge to the current u tomin{L(v), L(u) + w(u, v)}, in O(|E| log |V |) (not shown).• Total time <strong>is</strong> then O(|E| log |V |). Thus if the graph <strong>is</strong> sparse (say a constantnumber of edges per node, so |E| = O(|V |), we can get the time down toO(|V | log |V |).11


Priority Queues• A priority queue <strong>is</strong> a l<strong>is</strong>t of items in which each item has associated with ita priority. In general, different items may have different priorities and wespeak of <strong>one</strong> item having a higher priority than another. Given such a l<strong>is</strong>twe can determine which <strong>is</strong> the highest (or the lowest) priority item in thel<strong>is</strong>t. Items are inserted into a priority queue in any, arbitrary order. However,items are withdrawn from a priority queue in order of their prioritiesstarting with the highest (lowest) priority item first.• For example, consider the software which manages a printer. In general, it<strong>is</strong> possible for users to submit documents for printing much more quicklythan it <strong>is</strong> possible to print them. A simple solution <strong>is</strong> to place the documentsin a FIFO queue. In a sense th<strong>is</strong> <strong>is</strong> fair, because the documents are printedon a first-come, first-served bas<strong>is</strong>.• However, a user who has submitted a short document for printing willexperience a long delay when much longer documents are already in thequeue. An alternative solution <strong>is</strong> to use a priority queue in which the shortera document, the higher its priority. By printing the <strong>shortest</strong> documents first,we reduce the level of frustration experienced by the users. In fact, it canbe shown that printing documents in order of their length minimizes theaverage time a user waits for her document.• Removing a job with the smallest number of pages can be d<strong>one</strong> with EXTRACT-MIN.• You also need to be able to insert a new item into the queue (e.g., whenfirst building the set Q in Dijkstra).12


Insertion Operation• Insert 14; make a “hole” at the end of the array• Move the hole up to the “proper” position, then insert the 1413


Floyd-Warshall for the all-pairs <strong>shortest</strong> <strong>path</strong>• For Kevin Bacon numbers we ran FLOYD-WARSHALL. <strong>Th<strong>is</strong></strong> used a matrixW.• Let the initial entries of W be integers representing <strong>path</strong> lengths: W [i, j] =w(i, j). <strong>Th<strong>is</strong></strong> assumes you number the vertices.• The code for finding the length of the <strong>shortest</strong> <strong>path</strong> between any two vertices<strong>is</strong>initialize Wfor j = 1 to ndo for i, k = 1 to ndo W(i, k) ← min(W(i, k), W(i, j) + W(j, k))• The complexity <strong>is</strong> O(n 3 ). Clearly to find a <strong>shortest</strong> <strong>path</strong> from a to z weshould use DIJKSTRA.• What about correctness?• Recall that just before iteration j, the number W j (i, k) <strong>is</strong> the <strong>shortest</strong> <strong>path</strong>length from i to k for a <strong>path</strong> whose interior vertices are in {1, . . . , j − 1}.<strong>Th<strong>is</strong></strong> we use for our inductive statement.14


Proving correctness of FLOYD-WARSHALL• The inductive statement: just before iteration j, the number W j (i, k) <strong>is</strong> the<strong>shortest</strong> <strong>path</strong> length from i to k for a <strong>path</strong> whose interior vertices are in{1, . . . , n − 1}.• We start with n = 1. By convention, {1, −1} = ∅. The statement then saysthat the length of the <strong>shortest</strong> <strong>path</strong> between any two adjacent points u andv <strong>is</strong> w(u, v), which <strong>is</strong> true.• Assume the statement for n = j and prove it for n = j + 1. The new valueW j+1 (i, k) <strong>is</strong> min(W j (i, k), W j (i, j) + W j (j, k)). We need to show th<strong>is</strong><strong>is</strong> the correct value. A <strong>shortest</strong> <strong>path</strong> from i to k whose interior vertices arein {i, . . . , j} either goes through j or not. If not, its length <strong>is</strong> W j (i, k), byinductive hypothes<strong>is</strong>. Otherw<strong>is</strong>e it goes through j, and clearly not morethan once through j. So it can be broken into two parts, <strong>one</strong> part a <strong>shortest</strong><strong>path</strong> from i to j with interior vertices in {1, . . . , j −1} and another <strong>shortest</strong><strong>path</strong> from j to k, again with interior vertices in {1, . . . , j − 1}. The lengthof th<strong>is</strong> <strong>is</strong> W j (i, j)+W j (j, k), again by inductive hypothes<strong>is</strong>. So the correctlength <strong>is</strong> min(W j (i, k), W j (i, j) + W j (j, k)), as the <strong>algorithm</strong> stipulates,and th<strong>is</strong> completes the induction.• FLOYD-WARSHALL <strong>is</strong> a dynamic programming <strong>algorithm</strong>.15


What’s coming up after 203?• EECS 281 <strong>is</strong> about <strong>algorithm</strong>s and data structures. You learn about concretestorage structures like linked l<strong>is</strong>ts, arrays, and the like.• You also learn about more abstract structures such as priority queues.• Ideally with these abstract structures, you can define them using structuralinduction, and prove properties about them the same way.• An example would be “binary trees”.• EECS 376 <strong>is</strong> about models of computation. You learn about finite-statemachines, which are really useful in modelling all sorts of computationalsystems.• You also learn about undecidability, uncomputability, and computationalcomplexity, including more about P and NP.16


How do you prove that there <strong>is</strong>n’t any <strong>algorithm</strong> to solve a problem?• The basic method <strong>is</strong> to prove it by contradiction.• The method also assumes that <strong>algorithm</strong>s can all be coded as programs ina single programming language.• It then uses the fact that all programs are just strings over an alphabet.• It <strong>is</strong> assumed that (arbitrary length) strings are a datatype in the programminglanguage.• We then consider a specific property P of programs. The set of strings representingprograms with that property, as it turns out, cannot be recognizedby any <strong>one</strong> program in the language. Since each <strong>algorithm</strong> <strong>is</strong> representableby <strong>one</strong> program in the language, th<strong>is</strong> means that no <strong>algorithm</strong> can solve thequestion: does a given program have property P ?• What’s the magic property P ? A program x has the property if x goes intoan infinite loop when fed the string x (its own encoding) as an input.• If there was a program p that recognized th<strong>is</strong> property of other programs,then we could alter it so that it went into an infinite loop whenever it wouldhave answered “no.” (Just change the print “no” statement into a loop.)• Then we give th<strong>is</strong> altered program p a its own program as input. If the run <strong>is</strong>infinite, that means that the unaltered program p predicted that the answerwas “no” – p a did not go into an infinite loop on its own input. But p a justdid, so it can’t be the case that p a went into an infinite loop on itself asinput.• So p a says “yes” to its own input, which means that the original programp predicted that p a went into an infinite loop on its own input. But p a juststopped and said “yes”, so th<strong>is</strong> case can’t happen either.• Other properties can be shown not to have an <strong>algorithm</strong> by proving that ifthey did, then there would be an <strong>algorithm</strong> to solve property P of programs.17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!