Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a."— Presentation transcript:

1 Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a general algorithm design technique Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems “Programming” here means “planning” “Programming” here means “planning” Main idea: Main idea: solve several smaller (overlapping) subproblems solve several smaller (overlapping) subproblems record solutions in a table so that each subproblem is only solved once record solutions in a table so that each subproblem is only solved once final state of the table will be (or contain) solution final state of the table will be (or contain) solution

2 Design and Analysis of Algorithms - Chapter 82 Example: Fibonacci numbers Recall definition of Fibonacci numbers: f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2) Computing the n th Fibonacci number recursively (top-down): f(n) f(n-1) + f(n-2) f(n-2) + f(n-3) f(n-3) + f(n-4)...

3 Design and Analysis of Algorithms - Chapter 83 Example: Fibonacci numbers Computing the n th fibonacci number using bottom-up iteration: f(0) = 0 f(1) = 1 f(2) = 0+1 = 1 f(3) = 1+1 = 2 f(4) = 1+2 = 3 f(5) = 2+3 = 5 f(n-2) = f(n-1) = f(n) = f(n-1) + f(n-2)

4 Design and Analysis of Algorithms - Chapter 84 Examples of Dynamic Programming Algorithms Computing binomial coefficients Computing binomial coefficients Optimal chain matrix multiplication Optimal chain matrix multiplication Constructing an optimal binary search tree Constructing an optimal binary search tree Warshall’s algorithm for transitive closure Warshall’s algorithm for transitive closure Floyd’s algorithms for all-pairs shortest pathsFloyd’s algorithms for all-pairs shortest paths Some instances of difficult discrete optimization problems:Some instances of difficult discrete optimization problems: travelling salesman travelling salesman knapsack knapsack

5 Design and Analysis of Algorithms - Chapter 85 Warshall’s Algorithm: Transitive Closure Computes the transitive closure of a relation Computes the transitive closure of a relation (Alternatively: all paths in a directed graph) (Alternatively: all paths in a directed graph) Example of transitive closure: Example of transitive closure: 3 4 2 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 11 1 1 1 1 1 3 4 2 1

6 Design and Analysis of Algorithms - Chapter 86 Warshall’s Algorithm Main idea: a path exists between two vertices i, j, iff Main idea: a path exists between two vertices i, j, iff there is an edge from i to j; orthere is an edge from i to j; or there is a path from i to j going through vertex 1; orthere is a path from i to j going through vertex 1; or there is a path from i to j going through vertex 1 and/or 2; orthere is a path from i to j going through vertex 1 and/or 2; or there is a path from i to j going through vertex 1, 2, and/or 3; orthere is a path from i to j going through vertex 1, 2, and/or 3; or...... there is a path from i to j going through any of the other verticesthere is a path from i to j going through any of the other vertices 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 R 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 R 1 0 0 1 0 1 1 0 1 1 0 0 0 1 0 0 R 2 0 0 1 0 1 0 1 1 0 0 1 1 1 1 1 1 1 R 3 0 0 1 0 1 0 1 1 0 0 1 1 R 4 0 0 1 0 1 1 1 1 1 0 0 1 1 3 4 2 1

7 Design and Analysis of Algorithms - Chapter 87 Warshall’s Algorithm In the k th stage determine if a path exists between two vertices i, j using just vertices among 1,…,kIn the k th stage determine if a path exists between two vertices i, j using just vertices among 1,…,k R (k-1) [i,j] (path using just 1,…,k-1) R (k) [i,j] = or R (k) [i,j] = or (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k and from k to i and from k to i using just 1,…,k-1) using just 1,…,k-1) i j k k th stage {

8 Design and Analysis of Algorithms - Chapter 88 Floyd’s Algorithm: All pairs shortest paths In a weighted graph, find shortest paths between every pair of vertices In a weighted graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D(0), D(1), … using an initial subset of the vertices as intermediaries.Same idea: construct solution through series of matrices D(0), D(1), … using an initial subset of the vertices as intermediaries. Example: Example: 3 4 2 1 4 1 6 1 5 3


Download ppt "Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a."

Similar presentations


Ads by Google