1 / 17

Design and Analysis of Algorithms Introduction to Divide-and-conquer

Design and Analysis of Algorithms Introduction to Divide-and-conquer. Haidong Xue Summer 2012, at GSU. Knowledge tree. Algorithms. Classic data s tructure. Analysis. Algorithms for classic problems. Design. …. Sorting. Shortest path. Matrix multiplication. Asymptotic notations.

shada
Download Presentation

Design and Analysis of Algorithms Introduction to Divide-and-conquer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design and Analysis of AlgorithmsIntroduction to Divide-and-conquer HaidongXue Summer 2012, at GSU

  2. Knowledge tree Algorithms Classic data structure Analysis Algorithms for classic problems Design … Sorting Shortest path Matrix multiplication Asymptotic notations Probabilistic analysis Heap, Hashing, Binary Search Tree, RBT, …. Dynamic Programming Greedy Divide & Conquer O(), o(), (), (), () Quicksort, Heapsort, Mergesort, … … … … … … … … … … … … … … … … … … … …

  3. What is divide-and-conquer? • An example problem: “eat somecake” • Description: given some amount of cake, eat all of them • When the cake is like:

  4. What is divide-and-conquer? • When the cake is like…. • Hint: you need not to eat all of them at the first time

  5. What is divide-and-conquer? Smaller input size • Any ideas? • Case 1: a small piece of cake • Eat all of them • Case 2: a lot of cake • Cut off a small piece • andEatAllTheCake( the small piece) • Put the rest into a refrigerator • 24 housrs later,EatAllTheCake( the rest of the cake ) Base case Recursive case Do some other things Solve a smaller problem Do some other things Solve a smaller problem

  6. What is divide-and-conquer? • Textbook definition: • Divide the problem into a number of subproblems that are smaller instances of the same problem • Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straight forward manner. • Combine the solutions to the subproblems into the solution for the original problem

  7. Pros and cons • Pros: • Many problems can be solved by a straightforward divide-and-conquer algorithm • Cons: • Time complexity is not always good

  8. An example: calculate factorial of n • Problem: calculate n! • What is n!? • What is the iterative algorithm? • fact1(n) • f=1; • for i = n to 1 • f = f * i; • return f;

  9. An example: calculate the factorial of n • What is the divide-and-conquer algorithm? • fact2(n) • if(n<=1) return 1; • return n*fact2(n-1); Base case Recursive case Smaller problem Combination Let’s try it in Java

  10. Tail recursion • fact2(n) • if(n<=1) return 1; • return n*fact2(n-1); • What happen when fact2(5)? fact2(5) n <- 5; m = fact2(4); n*m; fact2(4) n <- 4; m = fact2(3); n*m; fact2(3) n <- 3; m = fact2(2); n*m; fact2(2) n <- 2; m = fact2(1); n*m; For each call, there is some storage used to save the current state fact2(1) return 1; Can we save those space?

  11. Tail recursion • Tail recursion: there is only one self call, and it is the last operation. • E.g.: • fact3(n, currentFactotial) • if(n<=1) return currentFactotial; • return fact3(n-1, currentFactotial*n); • What is the first call? • fact3(n, 1); • What happened in fact3?

  12. Tail recursion • What happen when fact3(5)? fact3(5, currentFactotial) m <- currentFactotial*5; fact3(4,m ); fact3(4, currentFactotial) m <- currentFactotial*4; fact3(3,m ); fact3(3, currentFactotial) m <- currentFactotial*3; fact3(2,m ); fact3(2, currentFactotial) m <- currentFactotial*2; fact3(1,m ); fact3(1, currentFactotial) return currentFactotial; With a tail recursion, local variable need not to be saved • fact3(n, currentFactotial) • if(n<=1) return currentFactotial; • return fact3(n-1, currentFactotial*n); Let’s improve the code

  13. More exercises-print all strings • Problem: • Each character could be: a, b, c, d or e. • Print all the possible n character strings • printAllStrings( int n, string head) • if(n==0) • print head; • else • printAllStrings(n-1, head+”a”);

  14. More exercises – tower of Hanoi • Tower of Hanoi: move all disks to the third rod • Only one disk may be moved at a time. • No disk may be placed on top of a smaller disk.

  15. More exercises – tower of Hanoi • What is the algorithm to solve tower of Hanoi problem? • Hanoi( number of disks n, current rod c, destination rod d, auxiliary rod a ) • if(n==0) //nothing • else • Move top n-1 disks to auxiliary rod: Hanoi(n-1, c, a, d) • Move the last disk to destination rod • Move the n-1 disks from auxiliary rod to destination rod: Hanoi(n-1, a, d, c) Let’s try it in Java

  16. More exercises • Have you seen other algorithms? • Quicksort • Mergesort • Heapify • Heapsort ….

  17. Analysis of divide-and-conquer algorithms • Recurrence • Solve recurrence

More Related