• date post

02-Jan-2016
• Category

## Documents

• view

43

1

Embed Size (px)

description

Introduction to Algorithms. All-Pairs Shortest Paths My T. Thai @ UF. Single-Source Shortest Paths Problem. Input : A weighted, directed graph G = ( V , E ) Output : An n × n matrix of shortest-path distances δ . δ ( i , j ) is the weight of a shortest path from i to j. - PowerPoint PPT Presentation

### Transcript of Introduction to Algorithms

• Introduction to AlgorithmsAll-Pairs Shortest Paths

My T. Thai @ UF

My T. Thaimythai@cise.ufl.edu

• Single-Source Shortest Paths ProblemInput: A weighted, directed graph G = (V, E) Output: An n n matrix of shortest-path distances . (i, j) is the weight of a shortest path from i to j.

My T. Thaimythai@cise.ufl.edu*

12345101-32-4230-41-137405342-1-50-2585160

My T. Thaimythai@cise.ufl.edu

• Can use algorithms for Single-Source Shortest PathsRun BELLMAN-FORD once from each vertexTime:

If there are no negative-weight edges, could run Dijkstras algorithm once from each vertexTime: My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• OutlineShortest paths and matrix multiplication

Floyd-Warshall algorithm

Johnsons algorithmMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Recursive solutionOptimal substructure: subpaths of shortest paths are shortest pathsRecursive solution: Let = weight of shortest path from i to j that contains m edges.

Where wij:My T. Thaimythai@cise.ufl.edu

My T. Thaimythai@cise.ufl.edu

• Computing the shortest-path weights bottom upAll simple shortest paths contain n 1 edges

Compute from bottom up: L(1), L(2), . . . , L(n-1).Compute L(i+1) from L(i) by extending one more edgeTime: My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Time: My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Shortest paths and matrix multiplicationExtending shortest paths by one more edge likes matrix product: L(i+1)= L(i).W Compute L(1), L(2), L(4) . . . , L(r) with

My T. Thaimythai@cise.ufl.edu*Time:

My T. Thaimythai@cise.ufl.edu

• ExampleMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Floyd-Warshall algorithmFor path p = , v2 vl-1 are intermediate vertices from v1 to vl Define = shortest-path weight of any path from i to j with all intermediate vertices in {1, 2, . . . , k}Consider a shortest path with all intermediate vertices in {1, 2, . . . , k}: If k is not an intermediate vertex, all intermediate vertices in {1, 2, . . . , k -1}If k is an intermediate vertex:

mythai@cise.ufl.edu*

mythai@cise.ufl.edu

• Floyd-Warshall algorithmRecursive formula:

Time:My T. Thaimythai@cise.ufl.edu*Note: since we have at most n vertices, return

My T. Thaimythai@cise.ufl.edu

• Constructing a shortest path is the predecessor of vertex j on a shortest path from vertex i with all intermediate vertices in the set {1, 2, . . . , k}My T. Thaimythai@cise.ufl.edu*(Use vertex k)

My T. Thaimythai@cise.ufl.edu

• ExampleMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Johnsons algorithmReweighting edges to get non-negative weight edges:For all u, v V, p is a shortest path using w if and only if p is a shortest path using For all (u, v) E, Run Dijkstras algorithm once from each vertexMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• ReweightingMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Proof of lemma 25.1First, we prove

With cycle ,

My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Producing nonnegative weightsConstruct Since no edges enter s, has the same set of cycles as G has a negative-weight cycle if and only if G doesDefine: Claim: Proof: Triangle inequality of shortest pathsMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Johnsons algorithmTime:

My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• ExampleMy T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• SummaryDynamic-programming algorithm based on matrix multiplicationDefine sub-optimal solutions based on the length of pathsUse the technique of repeated squaringTime:Floyd-Warshall algorithmDefine sub-optimal solutions based on the set of allowed intermediate verticesTime: My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

• Johnsons algorithmReweight edges to non-negative weight edgesRun Dijkstras algorithm once from each vertexTime:Faster than Floyd-Warshall algorithm when the graph is dense E = o(V2)

My T. Thaimythai@cise.ufl.edu*

My T. Thaimythai@cise.ufl.edu

*