250 likes | 412 Views
The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang. Algorithms. April-May 2013. Dr. Youn-Hee Han. Dynamic Programming. Review: Divide and Conquer split problem into smaller problems solve each smaller problem recursively
E N D
The Project for the Establishing the Korea ㅡ Vietnam College of Technology in BacGiang Algorithms April-May 2013 Dr. Youn-Hee Han
Dynamic Programming • Review: Divide and Conquer • split problem into smaller problems • solve each smaller problem recursively • split the smaller problem into even smaller problems • solve each even smaller problem recursively • split smaller problem into easier problems • … • … • recombine the results • recombine the results
f(7) f(6) f(5) f(5) f(4) f(4) f(3) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) … … … … … … … 1 Dynamic Programming • Review: Divide and Conquer • Fibonacci series – recursive method • It takes exponential time (2n) complexity long fib(int n) { if (n <= 1) return n; else return (fib(n-1) + fib(n-2)); }
Dynamic Programming f(7) • Reuse earlier results! Dynamic Programming • “memorization” or “tabling” • Iterative methodby using array f(6) long fib2(int n) { if (n <= 1) return n; else return fmemo(n-1) + fmemo(n-2) } f(5) f(4) … long fib2 (int n) { index i; long[] f = new long[n+1]; f[0] = 0; if (n > 0) { f[1] = 1; for (i = 2; i <= n; i++) f[i] = f[i-1] + f[i-2]; } return f[n]; }
Dynamic Programming • DP (Dynamic Programming) • Conceptually, exactly same as divide-and-conquer • But, DP (dynamic programming) stores the solutions of subproblemsinto a memory (array) and reuse it to solve the original problem. • So, you should devise “recursive property” for dynamic programming • Recursive property for Fibonacci • f[i] = f[i-1] + f[i-2] long fib2 (int n) { index i; long[] f = new long[n+1]; f[0] = 0; if (n > 0) { f[1] = 1; for (i = 2; i <= n; i++) f[i] = f[i-1] + f[i-2]; } return f[n]; }
Binomial Coefficient • Binomial formula and Binomial Coefficient • For natural numbers n and k, the binomial coefficientis the coefficient of xn-kykin the expansion of (x+y)n. • For n=2, 3, or 4
Binomial Coefficient • Pascal’s triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 … c(0,0) c(1,0) c(1,1) c(2,0) c(2,1) c(2,2) c(3,0) c(3,1) c(3,2) c(3,3) c(4,0) c(4,1) c(4,2) c(4,3) c(4,4)
Binomial Coefficient • The recursive definition of binomial coefficient • Proof
Binomial Coefficient • “Divide and Conquer” strategy • The number of nodes in the recursive tree • The asymptotic complexity: O(n!) int bin(int n, int k){ if (k == 0 || n == k) return 1; else return (bin(n-1,k-1) + bin(n-1,k)); }
Binomial Coefficient • “Dynamic Programming” strategy • The recursive property = B[4][2] j int bin2(int n, int k){ index i, j; int[][] B = new int[0..n][0..k]; for(i=0; i <= n; i++) for(j=0; j <= minimum(i,k); j++) if (j==0 || j==i) B[i][j] = 1; else B[i][j] = B[i-1][j-1] + B[i-1][j]; return B[n][k]; } i
Binomial Coefficient • “Dynamic Programming” strategy • i=0, j-loop 1 • i=1, j-loop 2 • i=2, j-loop 3 • ……………… • i=k-1, j-loop k • i=k, j-loop k + 1 • i=k+1, j-loop k + 1 • ……………… • i=n, j-loop k + 1 • The asymptotic complexity: O(nk)
Shortest Path in a Graph • Problem • Finding the shortest pathsfrom each vertex to othervertices • This is the optimization problem • Candidate solution • Optimal solution • E.g.) v1 v3 • There are three candidate solutions • (1) v1 v2 v3 : the sum of weights – 4 • (2) v1 v4 v3: the sum of weights – 3 • Optimal solution • (3) v1 v2 v4 v3 : the sum of weights - 5 1 v2 v1 9 3 5 1 2 v5 3 A given weighted directed graph 3 2 v3 v4 4
1 v2 v1 9 3 5 1 2 v5 3 3 2 v3 v4 4 Shortest Path in a Graph • How to store the graph in the computer? • Data structure of a given weighted directed graph • Adjacency matrix W W
1 v2 v1 9 3 5 1 2 v5 3 3 2 v3 v4 4 Shortest Path in a Graph • Our goal • Build the matrix D representing the shortest paths in the graph D
1 v2 v1 9 3 5 2 1 v5 3 3 2 v3 v4 4 Shortest Path in a Graph • Let’s have the following definition • The length of a shortest path from vi to vj using only vertices in the set {v1, v2, …, vk} asintermediate vertices
1 v2 v1 9 3 5 1 2 v5 3 3 2 v3 v4 4 Shortest Path in a Graph • “Dynamic Programming” strategy • The recursive property • Case 1 • The shortest path from vi to vj using only vertices in {v1, v2, …, vk} asintermediate vertices dose not include vk • E.g. = • v5 does not contribute the shortest path from v1 to v3 Case 1 Case 2
1 v2 v1 9 3 5 1 2 v5 3 3 2 v3 v4 4 Shortest Path in a Graph • “Dynamic Programming” strategy • The recursive property • Case 2 • The shortest path from vi to vj using only vertices in {v1, v2, …, vk} asintermediate vertices dose include vk • E.g. = Case 1 Case 2
Shortest Path in a Graph • “Dynamic Programming” strategy • Floyd algorithm • Asymptotic Complexity: O(n3) void floyd(int n, number[][] W, number[][] D){ index i, j, k; D = W; for(k=1; k <= n; k++) for(i=1; i <= n; i++) for(j=1; j <= n; j++) D[i][j] = minimum(D[i][j], D[i][k]+D[k][j]); }
Shortest Path in a Graph • How to print out the shortest path? • We need a more matrix P • The updated Floyd algorithm void floyd2(int n, number W[][], number D[][], index P[][]){ index i, j, k; for(i=1; i <= n; i++) for(j=1; j <= n; j++) P[i][j] = 0; D = W; for(k=1; k<= n; k++) for(i=1; i <= n; i++) for(j=1; j<=n; j++) if ((D[i][k] + D[k][j]) < D[i][j]) { P[i][j] = k; D[i][j] = D[i][k] + D[k][j]; } }
1 v2 v1 9 3 5 2 1 v5 3 2 3 v3 v4 4 Shortest Path in a Graph • Example 1) v2 v1 P[2][1]=5, P[2][5]=4, P[2][4]=0 hence, v2 v4 v5 v1 2) v5 v3 P[5][3]=4, P[5][4]=1, P[5][1]=0 hence, v5 v1 v4 v3
Shortest Path in a Graph • Printing the shortest path • path(5,3) path(5, 3) path(5, P[5][3]=4) path(5, P[5][4]=1) P[5][1]=0 return; print “v1” print “v4” That is, v5 v1 v4 v3 void path(q, r){ if (P[q][r] != 0) { path(q, P[q][r]); System.out.print(“ v“ + P[q][r]); } }
[Programming Practice 4] • Floyd Algorithm • Visit • http://link.koreatech.ac.kr/courses/2013_1/AP-KOICA/AP-KOICA20131.html • Download “Floyd.java” and run it • Analyze the source codes • Complete the source codes while insert right codes within the two functions • public static void floyd(int n) • public static void path(int q, int r)
[Programming Practice 4] • Floyd Algorithm • The output you should get 1 v2 v1 9 3 5 1 2 v5 3 3 2 v3 v4 A given weighted directed graph 4