Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.

Similar presentations


Presentation on theme: "Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive."— Presentation transcript:

1 Nattee Niparnan

2 Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive

3 Today Topic  Divide and Conquer

4 Algorithm Design Technique Divide and conquer

5 Divide and Conquer

6 Induction

7 Induction Example

8 The Inductive Step  The inductive step  Assume that it is true for (n-1)  Check the case (n)  1 + 2 + 3 +… + n = (1 + 2 + 3 + … + (n-1) ) + n  = (n-1)(n) / 2 + n  = n((n-1)/2 + 1)  = n(n/2-1/2 + 1)  = n(n/2 + 1/2)  = n(n+1)/2

9 The Inductive Step  The inductive step  Assume that it is true for (n-1)  Check the case (n)  1 + 2 + 3 +… + n = (1 + 2 + 3 + … + (n-1) ) + n  = (n-1)(n) / 2 + n  = n((n-1)/2 + 1)  = n(n/2-1/2 + 1)  = n(n/2 + 1/2)  = n(n+1)/2 By using (n-1)

10 The Induction  By using the result of the smaller case  We can proof the larger case

11 Key of Divide and Conquer

12 Steps in D&C  Questions  What if we has the solution of the subproblem (smaller problem)?  What is the subproblem? (how to divide?)  What can we do with the result? (how to conquer?)  If we know the answer, the rest comes automatically from the recursion

13 Code Example ResultType DandC(Problem p) { if (p is trivial) { solve p directly return the result } else { divide p into p 1,p 2,...,p n for (i = 1 to n) r i = DandC(p i ) combine r 1,r 2,...,r n into r return r }

14 Code Example ResultType DandC(Problem p) { if (p is trivial) { solve p directly t s return the result } else { divide p into p 1,p 2,...,p n t d for (i = 1 to n) t r r i = DandC(p i ) combine r 1,r 2,...,r n into r t c return r }

15 Examples  Let’s see some examples

16 Merge Sort

17 The Sorting Problem  Given a sequence of numbers  A = [a 1,a 2,a 3,…,a n ]  Sort the sequence from min to max

18 The question  What if we know the solution of the smaller problem  What is the smaller problem?  Try the same problem  sorting  What if we know the result of the sorting of some elements?

19 The Question  How to divide?  Let’s try sorting of the smaller array  Divide exactly at the half of the array  How to conquer?  Merge the result directly

20 Idea  Simplest dividing  Divide array into two array of half size  Laborious conquer  Merge the result

21 Divide

22

23

24 Merge

25 Analysis  T(n) = 2T(n/2) + O(n)  Master method  T(n) = O(n lg n)

26 Quick Sort

27 The Sorting Problem (again)  Given a sequence of numbers  A = [a 1,a 2,a 3,…,a n ]  Sort the sequence from min to max

28 Problem of Merge Sort  Need the use of external memory for merging  Are there any other way such that conquering is not that hard?

29 The Question  How to divide?  Try doing the same thing as the merge sort  Add that every element in the first half is less than the second half  Can we do that?  How to conquer?  The sorted result should be easier to merge?

30 Idea  Laborious dividing  Divide array into two arrays  Add the requirement of value of the array  Simplest conquer  Simply connect the result

31 Is dividing scheme possible?  Can we manage to have two subproblem of equal size  That satisfy our need?  Any trade off?

32 The Median

33 Simplified Division

34 partitioning First k element Other n- k element

35 Solve by Recursion sorting

36 Do nothing!

37 Analysis  T(n) = T(k) + T(n – k) + Θ (n)  There can be several cases  Up to which K that we chosen

38 Analysis : Worst Case  K always is 1 T(n) = T(1) + T(n – 1) + Θ (n) = T(n – 1) + Θ (n) = Σ Θ (i) = Θ (n 2 )

39 Analysis : Best Case  K always is the median T(n) = 2T(n/2) + Θ (n) = Θ (n log n) The same as the merge sort (without the need of external memory)

40 Fixing the worst case  Worst case happens when we select wrong pivot  If we select the first element as a pivot  What if the data is sorted?

41 Fixing the worst case  Select wrong pivot leads to worst case.  There will exist some input such that for any strategy of “deterministic” pivot selection leads to worst case.

42 Fixing the worst case  Use “non-deterministic” pivot selection  i.e., randomized selection  Pick a random element as a pivot  It is unlikely that every selection leads to worst case  We can hope that, on average,  it is O(n log n)

43 Exponential

44 The Problem  Computing x n mod k  Given x, n and k

45 Naïve method res = x mod n; for (i = 2 to n) do { res = res * x; res = res mod n; } Θ (n)

46 The Question  What if we knows the solution to the smaller problem?  Smaller problem  smaller N  x n ?? from x (n-e) ?  Let’s try x (n/2)

47 The Question  How to divide?  x n = x n/2 * x n/2 (n is even)  x n = x n/2 * x n/2 * x (n is odd)  How to conquer?  Compute x n/2 mod k  Square and times with x, if needed,  mod k afterward

48 Analysis  T(n) = T(n/2) + Θ (1)  = O(log n)

49 Example 2 92 mod 10  2 92 = 2 46 × 2 46  2 46 = 2 23 × 2 23  2 23 = 2 11 × 2 11 × 2  2 11 = 2 5 × 2 5 × 2  2 5 = 2 2 × 2 2 × 2  2 2 = 2 1 × 2 1 = 4×4 mod 10 = 6 = 8×8 mod 10 = 4 = 8×8×2 mod 10 = 8 = 2×2×2 mod 10 = 8 = 4×4 × 2 mod 10 = 2 = 2×2 mod 10 = 4

50 Maximum Sum of Subsequence

51 The MSS Problem  Given a sequence of numbers  A = [a 1,a 2,a 3,…,a n ]  Find a subsequence s = [a i,a i+1,a i+2,…,a k ]  Such that the sum of the element in s is maximum

52 Naïve approach  Try all possible sequences  How many sequence?  How much time does it use to compute the sum?

53 Naïve approach

54 The DC Approach  What if we know the solution of the smaller problem  What if we know MSS of the first half and the second half?

55 The Question  How to divide?  By half of the member  How to conquer?  Does the result of the subproblem can be used to compute the solution?  Let’s see

56 Combining the result from sub MSS 4-35-2 26-2

57 Combining the result from sub MSS 4-35-226-2

58 Combining the result from sub MSS 4-35-226-2 Sum = 6Sum = 8 Shall this be our answer?

59 Combining the result from sub MSS 4-35-2 26-2 Do we consider all possibilities?

60 Combining the result from sub MSS 4-35-2 26-2 Considered all pairs in each half But not the pair including member from both half There are (n/2) 2 additiona l pairs

61 Compute max of cross over  Consider this  The max sequence from the green part is always the same, regardless of the pink part  The sequence always start at the left border 4-35-2 26-2

62 Compute max of cross over  Similarly  The max sequence from the pink part is always the same, regardless of the green part  The sequence always start at the right border 4-35-2 26-2

63 Compute max of cross over  Just find the marginal max from each part 4-35-226-2

64 Compute max of cross over  Just find the marginal max from each part 4-35-226-2

65 Compute max of cross over  Just find the marginal max from each part 4-35-226-2

66 Compute max of cross over  Just find the marginal max from each part 4-35-226-2 Less than previous

67 Compute max of cross over  Just find the marginal max from each part 4-35-226-2

68 Compute max of cross over  Just find the marginal max from each part 4-35-226-2

69 Compute max of cross over  Just find the marginal max from each part 4-35-226-2

70 Compute max of cross over  Just find the marginal max from each part 4-35-226-2 This is max

71 Compute max of cross over  Just find the marginal max from each part 4-35-226-2 Total = 13 takes Θ (n/2)

72 Combine  Max from the three problem  The left half  The right half  The cross over half

73 Analysis  T(n) = 2 T(n/2) + Θ (n)  = Θ (n log n) Left and right parts Cross over part


Download ppt "Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive."

Similar presentations


Ads by Google