Divide and conquer is a way to break complex problems into smaller problems that are easier to solve, and then combine the answers to solve the original problem. Divide and conquer is a powerful algorithm design technique used to solve many important problems such as mergesort, quicksort, calculating Fibonacci numbers, and performing matrix multiplication. There are also many problems that humans naturally use divide and conquer approaches to solve, such as sorting a stack of playing cards or looking for a phone number in a phone book.
Divide and conquer can be done in three broad steps, divide (into subproblems), conquer (by solving the subproblems), and combine (the answers to solve the original problem). Divide and conquer has a recursive step, where subproblems are solved, and a base case, which is the point where the problem can't be broken down any further.
How do you order a deck of cards?
There are many examples of problems for which humans naturally take a divide and conquer approach. Let’s say you have a stack of playing cards that you want to put in order. Sorting the entire deck is the original problem, but we can break this into subproblems by comparing only some of the cards at a time.
To do this, take the first and second card from the unsorted deck and sort those. Take another card from the unsorted deck and sort that into the sorted deck. Keep doing this until the entire deck has been sorted. Here, we divide into subproblems by sorting only some of the cards at once. At each step, we take one card from the unsorted list (the divide step), and put it into the sorted list (this makes up both the conquer and combine steps).
The divide step breaks the original problem into subproblems that are smaller instances of the original problem.
The conquer step solves the subproblems recursively.
The combine step puts the solved subproblems together to solve the original problem.
In the following algorithm, what is the "conquer" step of the divide and conquer approach?
You are trying to sort a list of basketball players by the number of points they scored in a game. To do this you divide the list into smaller lists consisting of two players each. You compare the two players in each list and sort them by who has the higher number of points. You do this comparison for every pair of lists and combine the lists to make bigger sorted lists until the entire list is sorted.
There is a variation of divide and conquer where the problem is reduced to one subproblem. Binary search is a popular example that uses decrease and conquer. Binary search looks through a sorted list to see if a desired element is in the list. It does this efficiently by halving the search space during each iteration of the program. Basically, binary search finds the middle of the list, asks “is the element I’m looking for larger or smaller than this?” and then cuts the list in half and searches only in the left list if the element is smaller, and the right list if the element is bigger. It repeats this process until it finds the element it is looking for (or reports back that the element isn’t in the list at all).
Describe how you would use decrease and conquer approach to find page 88 in a 350 page textbook.
Open the book to any page. If the page you’ve opened to is greater than 88, flip some number of pages toward the beginning of the book (and some number of pages toward the end of the book if the page number is smaller than 88). Now look at the new page number you’ve turned to. If the number is less than 88, flip some amount toward the end of the book (the amount you flip by should decrease with each iteration, since you don’t want to lose progress by overshooting), and if the number is greater than 88, flip some number of pages toward the beginning of the book. Repeat until you find page 88.
How to use divide and conquer:
- Given a problem, identify a number of significantly smaller subproblems.
- Solve each subproblem recursively (do this until the subproblem is a base-case).
- Combine these solutions into a solution for the main problem.
Divide-and-conquer algorithms often follow a generic pattern: they solve a problem of size by recursively solving subproblems of size , and then combine these answers in time. This general form can be represented by the following recurrence relation: The master theorem can be used to solve the recurrence relation for a closed form solution.
Recurrence relations are useful for determining the efficiency of algorithms.
Here is an example of merge sort, a divide and conquer algorithm in Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
- , . The Binary Search. Retrieved April 1=30, 2016, from http://interactivepython.org/runestone/static/pythonds/SortSearch/TheBinarySearch.html