Shortest Paths - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Some concept of Data Structures and Algorithm are Permutation, Representation, Implemented, Algorithm Design, Dynamic Programming, Graph Data Structures, String Processing, General Trees. Main points of this lecture are: Shortest Paths, Problem, Day, Minimum Spanning, Efficient, Spanning Tree, Minimum Spanning, Run, Algorithm, Correct Algorithms

Typology: Slides

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Shortest Paths
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Shortest Paths - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Shortest Paths

Problem of the Day

Suppose we are given the minimum spanning tree T of a given graph G (with n vertices and m edges) and a new edge e = (u, v) of weight w that we will add to G. Give an efficient algorithm to find the minimum spanning tree of the graph G + e. Your algorithm should run in O(n) time to receive full credit, although slower but correct algorithms will receive partial credit.

Shortest Paths and Sentence Disambiguation

In our work on reconstructing text typed on an (overloaded) telephone keypad, we had to select which of many possible interpretations was most likely. We constructed a graph where the vertices were the possible words/positions in the sentence, with an edge between possible neighboring words.





 

    (^)    (^)    (^)    

 

 

    (^)    (^)    (^)    

^ ^ ^  ! "#%^ $#$# &$# ^ ^ ^ ! "#%&^ $#$#$#

'(')'+-,.,0/2123.42123.523267,048,9:';'('

<=?> @BA @BCEDF= G<IH

J JJFK L LLFM J JJFK

=?G NPO+ QSR TPU+VXW7Y;Z;[P\PU+] ^_] [PU

aTPUb ]b+Tc^_Yda[PUe#^#f_gZ;^_] [PU

hcY U^Y U+Z;Yji7] e_Tckml ]\PgTP^] U\

nO+ NcO+

Dynamic programming (the Viterbi algorithm) can be used on the sentences to obtain the same results, by finding the shortest paths in the underlying DAG.

Shortest Paths: Unweighted Graphs

In an unweighted graph, the cost of a path is just the number of edges on the shortest path, which can be found in O(n+m) time via breadth-first search. In a weighted graph, the weight of a path between two vertices is the sum of the weights of the edges on a path. BFS will not work on weighted graphs because sometimes visiting more edges can lead to shorter distance, ie. 1 + 1 + 1 + 1 + 1 + 1 + 1 < 10. Note that there can be an exponential number of shortest paths between two nodes – so we cannot report all shortest paths efficiently.

Dijkstra’s Algorithm

The principle behind Dijkstra’s algorithm is that if s,... , x,... , t is the shortest path from s to t, then s,... , x had better be the shortest path from s to x. This suggests a dynamic programming-like strategy, where we store the distance from s to all nearby nodes, and use them to find the shortest path to more distant nodes.

Initialization and Update

The shortest path from s to s, d(s, s) = 0. If all edge weights are positive, the smallest edge incident to s, say (s, x), defines d(s, x). We can use an array to store the length of the shortest path to each node. Initialize each to ∞ to start. Soon as we establish the shortest path from s to a new node x, we go through each of its incident edges to see if there is a better way from s to other nodes thru x.

Dijkstra Example

4

5 2

A

1

6

2 4 A

3

G Dijkstra(G,A)

5 3 5

9

2

4 7

7

12

7

Dijkstra’s Implementation

See how little changes from Prim’s algorithm! dijkstra(graph g, int start) ( WAS prim(g,start) ) { int i; ( counter *) edgenode p; ( temporary pointer ) bool intree[MAXV]; ( is the vertex in the tree yet? ) int distance[MAXV]; ( distance vertex is from start ) int v; ( current vertex to process ) int w; ( candidate next vertex ) int weight; ( edge weight ) int dist; ( best current distance from start *) for (i=1; i<=g− >nvertices; i++) { intree[i] = FALSE; distance[i] = MAXINT; parent[i] = -1; } distance[start] = 0; v = start;

Prim’s/Dijkstra’s Analysis

Finding the minimum weight fringe-edge takes O(n) time – just bump through fringe list. After adding a vertex to the tree, running through its adjacency list to update the cost of adding fringe vertices (there may be a cheaper way through the new vertex) can be done in O(n) time. Total time is O(n^2 ).

Improved Time Bounds

An O(m lg n) implementation of Dijkstra’s algorithm would be faster for sparse graphs, and comes from using a heap of the vertices (ordered by distance), and updating the distance to each vertex (if necessary) in O(lg n) time for each edge out from freshly known vertices. Even better, O(n lg n + m) follows from using Fibonacci heaps, since they permit one to do a decrease-key operation in O(1) amortized time.

Dynamic Programming and Shortest Paths

The four-step approach to dynamic programming is:

  1. Characterize the structure of an optimal solution.
  2. Recursively define the value of an optimal solution.
  3. Compute this recurrence in a bottom-up fashion.
  4. Extract the optimal solution from computed information.

Initialization

From the adjacency matrix, we can construct the following matrix:

D[i, j] = ∞, if i 6 = j and (vi, vj) is not in E D[i, j] = w(i, j), if (vi, vj) ∈ E D[i, j] = 0, if i = j

This tells us the shortest path going through no intermediate nodes.