Presentation on theme: "What is divide and conquer? Divide and conquer is a problem solving technique. It does not imply any specific computing problems. The idea is to divide."— Presentation transcript:
What is divide and conquer? Divide and conquer is a problem solving technique. It does not imply any specific computing problems. The idea is to divide a DIFFICULT task to SIMPLE task(s). Solve the simple task(s). Combine the results of simple task(s) for the original task.
A classic example: Counterfeit coin You have 9 coins, in which one is counterfeit (being lighter) You have only one balance. How can you find the counterfeit dollar?
Counterfeit dollar MINDLESS approach: Grab the first one as control, test all others with respect to the first one (i.e. the control). Best case: The 1 st or the 2 nd is counterfeit 1 weighing. Worst case: The 9 th is counterfeit 8 weighing.
R < G G < R G=R Separate the 9 coins into 3 groups, each with 3 coins. Weigh 2 groups: Repeat the same process for the suspicious group.
Best case? Worst case? Divide and Conquer usually gives us a faster approach to the problem, that ’ s why it is useful. Many well known algorithms make use of D&C to formulate. However, please be noted that D&C does not necessarily provide you the best algorithm to solve it. Shell sort, Merge sort, Quick sort, Binary Search, Interpolation Search, Cut Vertices, Cut Edges, etc …
Difficult problem Sub Task 1Sub Task 2Sub Task n … Recombination Sub Tasks must be simpler Solution readily known or obtained by D&C again. Sol n of Task 1Sol n of Task 2Sol n of Task n …
Some terms:TermsMeaningRemarkD&C Divide and Conquer Problem solving technique Simpler 1. Algorithm already known Reduced problem 2. Answer already known Base cases 3. Same problem, smaller in size Recursive approach Faster Faster in worst case Best and average cases are not our main concern
L-Block Filling You are given an n X n square board, in which one cell is marked. Find a way to cover the board with L blocks. n is a power of 2 n = 2 k
L-block filling 4 smaller sub-problems
L-block filling What is the base case? It is very important to know that for any recursive approach, there must be a base case! An equally important note is that the way you design your recursion should gradually progress to your base case (s). Function Hello(n : integer) : Integer; Begin If n = 1 Then Hello := n Else Hello := Hello(n-2); End; What is the problem of this recursion?