11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

514 Chap. 16 P<strong>at</strong>terns of <strong>Algorithm</strong>s011 745 ∞3∞ ∞2 212113∞Figure 16.1 An example of k-p<strong>at</strong>hs in Floyd’s algorithm. P<strong>at</strong>h 1, 3 is a 0-p<strong>at</strong>hby definition. P<strong>at</strong>h 3, 0, 2 is not a 0-p<strong>at</strong>h, but it is a 1-p<strong>at</strong>h (as well as a 2-p<strong>at</strong>h, a3-p<strong>at</strong>h, <strong>and</strong> a 4-p<strong>at</strong>h) because the largest intermedi<strong>at</strong>e vertex is 0. P<strong>at</strong>h 1, 3, 2 isa 4-p<strong>at</strong>h, but not a 3-p<strong>at</strong>h because the intermedi<strong>at</strong>e vertex is 3. All p<strong>at</strong>hs in thisgraph are 4-p<strong>at</strong>hs.One solution is to run Dijkstra’s algorithm for finding the single-source shortestp<strong>at</strong>h (see Section 11.4.1) |V| times, each time computing the shortest p<strong>at</strong>h from adifferent start vertex. If G is sparse (th<strong>at</strong> is, |E| = Θ(|V|)) then this is a goodsolution, because the total cost will be Θ(|V| 2 + |V||E| log |V|) = Θ(|V| 2 log |V|)for the version of Dijkstra’s algorithm based on priority queues. For a dense graph,the priority queue version of Dijkstra’s algorithm yields a cost of Θ(|V| 3 log |V|),but the version using MinVertex yields a cost of Θ(|V| 3 ).Another solution th<strong>at</strong> limits processing time to Θ(|V| 3 ) regardless of the numberof edges is known as Floyd’s algorithm. It is an example of dynamic programming.The chief problem with solving this problem is organizing the search processso th<strong>at</strong> we do not repe<strong>at</strong>edly solve the same subproblems. We will do this organiz<strong>at</strong>ionthrough the use of the k-p<strong>at</strong>h. Define a k-p<strong>at</strong>h from vertex v to vertex u tobe any p<strong>at</strong>h whose intermedi<strong>at</strong>e vertices (aside from v <strong>and</strong> u) all have indices lessthan k. A 0-p<strong>at</strong>h is defined to be a direct edge from v to u. Figure 16.1 illustr<strong>at</strong>esthe concept of k-p<strong>at</strong>hs.Define D k (v, u) to be the length of the shortest k-p<strong>at</strong>h from vertex v to vertex u.Assume th<strong>at</strong> we already know the shortest k-p<strong>at</strong>h from v to u. The shortest (k + 1)-p<strong>at</strong>h either goes through vertex k or it does not. If it does go through k, thenthe best p<strong>at</strong>h is the best k-p<strong>at</strong>h from v to k followed by the best k-p<strong>at</strong>h from kto u. Otherwise, we should keep the best k-p<strong>at</strong>h seen before. Floyd’s algorithmsimply checks all of the possibilities in a triple loop. Here is the implement<strong>at</strong>ionfor Floyd’s algorithm. At the end of the algorithm, array D stores the all-pairsshortest distances.

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

Saved successfully!

Ooh no, something went wrong!