design and analysis of algorithms introduction to divide-and-conquer haidong xue summer 2012, at gsu
TRANSCRIPT
![Page 1: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/1.jpg)
Design and Analysis of AlgorithmsIntroduction to Divide-and-conquer
Haidong XueSummer 2012, at GSU
![Page 2: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/2.jpg)
Knowledge treeAlgorithms
Analysis DesignAlgorithms for
classic problemsClassic data
structure
Asymptotic notations
Probabilistic analysis
Sorting Shortest path
Matrix multiplication …
Divide & Conquer
GreedyDynamic
Programming
O(), o(), (), (), ()
Heap,Hashing,
Binary Search Tree,RBT,….
Quicksort,Heapsort,
Mergesort,…
… … …… … …
… … … … … … … … … … … ……
![Page 3: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/3.jpg)
What is divide-and-conquer?
• An example problem: “eat some cake”• Description: given some amount of cake, eat
all of them• When the cake is like:
![Page 4: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/4.jpg)
What is divide-and-conquer?
• When the cake is like….
• Hint: you need not to eat all of them at the first time
![Page 5: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/5.jpg)
What is divide-and-conquer?
• Any ideas?• Case 1: a small piece of cake– Eat all of them
• Case 2: a lot of cake– Cut off a small piece– and EatAllTheCake( the small piece)– Put the rest into a refrigerator – 24 housrs later, EatAllTheCake( the rest of the
cake )
Base case
Recursive case
Solve a smaller problem
Solve a smaller problem
Do some other things
Do some other things
Smaller input size
![Page 6: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/6.jpg)
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
![Page 7: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/7.jpg)
Pros and cons
• Pros: – Many problems can be solved by a straightforward
divide-and-conquer algorithm• Cons:– Time complexity is not always good
![Page 8: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/8.jpg)
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;
![Page 9: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/9.jpg)
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); Recursive case
Base case
Smaller problem Combination
Let’s try it in Java
![Page 10: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/10.jpg)
Tail recursion
• 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;
fact2(1) return 1;
For each call, there is some storage used to save the current state
Can we save those space?
fact2(n)if(n<=1) return 1;return n*fact2(n-1);
![Page 11: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/11.jpg)
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?
![Page 12: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/12.jpg)
Tail recursion
• What happen when fact3(5)? fact3(5, currentFactotial) m <- currentFactotial*5; fact3(4,m );
With a tail recursion, local variable need not to be saved
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;
fact3(n, currentFactotial)if(n<=1) return currentFactotial;return fact3(n-1, currentFactotial*n); Let’s improve the code
![Page 13: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/13.jpg)
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”);
![Page 14: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/14.jpg)
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.
![Page 15: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/15.jpg)
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
![Page 16: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/16.jpg)
More exercises
• Have you seen other algorithms?– Quicksort– Mergesort– Heapify– Heapsort….
![Page 17: Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU](https://reader036.vdocuments.us/reader036/viewer/2022062619/5517a7ee5503463e368b5d06/html5/thumbnails/17.jpg)
Analysis of divide-and-conquer algorithms
• Recurrence • Solve recurrence