Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.

Similar presentations


Presentation on theme: "Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang."— Presentation transcript:

1 Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang

2 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 2/23

3 Dynamic Programming Review: Divide and Conquer – Fibonacci series – recursive method It takes exponential time (2 n ) complexity 3/23 long fib(int n) { if (n <= 1) return n; else return (fib(n-1) + fib(n-2)); } f(7) f(6)f(5) f(4) f(3) f(4)f(3) f(2)f(3)f(2) f(1) … … ……… …… 1

4 Dynamic Programming Reuse earlier results!  Dynamic Programming – “memorization” or “tabling” – Iterative method by using array 4/23 long fib2(int n) { if (n <= 1) return n; else return f memo (n-1) + f memo (n-2) } f(7) f(6) 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]; }

5 Dynamic Programming DP (Dynamic Programming) – Conceptually, exactly same as divide-and-conquer – But, DP (dynamic programming) stores the solutions of subproblems into 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] 5/23 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]; }

6 Binomial Coefficient Binomial formula and Binomial Coefficient – For natural numbers n and k, the binomial coefficient is the coefficient of x n-k y k in the expansion of (x+y) n. – For n=2, 3, or 4 6/23

7 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) 7/23

8 Binomial Coefficient The recursive definition of binomial coefficient – Proof 8/23

9 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)); } 9/23

10 Binomial Coefficient “Dynamic Programming” strategy – The recursive property 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 j = B[4][2] 10/23

11 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) 11/23

12 Shortest Path in a Graph Problem – Finding the shortest paths from each vertex to other vertices – This is the optimization problem Candidate solution Optimal solution – E.g.) v 1  v 3 There are three candidate solutions – (1) v 1  v 2  v 3 : the sum of weights – 4 – (2) v 1  v 4  v 3 : the sum of weights – 3 » Optimal solution – (3) v 1  v 2  v 4  v 3 : the sum of weights - 5 v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 A given weighted directed graph 12/23

13 Shortest Path in a Graph How to store the graph in the computer? – Data structure of a given weighted directed graph Adjacency matrix W v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 W 13/23

14 Shortest Path in a Graph Our goal – Build the matrix D representing the shortest paths in the graph v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 D 14/23

15 Shortest Path in a Graph v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 15/23

16 Shortest Path in a Graph Case 1Case 2 v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 16/23

17 Shortest Path in a Graph Case 1Case 2 v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 17/23

18 Shortest Path in a Graph “Dynamic Programming” strategy – Floyd algorithm – Asymptotic Complexity: O(n 3 ) 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]); } 18/23

19 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]; } 19/23

20 Shortest Path in a Graph Example v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 1) v 2  v 1 P[2][1]=5, P[2][5]=4, P[2][4]=0 hence, v 2  v 4  v 5  v 1 2) v 5  v 3 P[5][3]=4, P[5][4]=1, P[5][1]=0 hence, v 5  v 1  v 4  v 3 20/23

21 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, v 5  v 1  v 4  v 3 void path(q, r){ if (P[q][r] != 0) { path(q, P[q][r]); System.out.print(“ v“ + P[q][r]); } 21/23

22 [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) 22/23

23 [Programming Practice 4] Floyd Algorithm – The output you should get v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 A given weighted directed graph


Download ppt "Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang."

Similar presentations


Ads by Google