You may recall that in the Computational Thinking module, we discussed decomposition. Not only does decomposition make the solution to a problem easier, but it also makes it more efficient. Let’s see how.
As discussed by Ankit, if the problem is divided into subproblems, the number of steps required to solve it reduces significantly. In the phonebook example that we discussed, you started at the middle, thus dividing the search space into two halves. The phonebook coincidently had names starting with L in the middle. If you were looking for a name that starts with T, you would have discarded everything to the left of L and moved to the right of L. This would have saved the number of steps required to search in the left half, which is insignificant to you. So in each step, you reduce the search space by half. Therefore, if the search space initially had 128 elements, you reduced the number to 64 in the first pass, 32 in the next, 16 in the next, 8 in the next, 4 after that, 2 after that, and finally, the last element. The following table puts this in a more concise form.
Initial Size | 128 |
After Step 1 | 64 |
After Step 2 | 32 |
After Step 3 | 16 |
After Step 4 | 8 |
After Step 5 | 4 |
After Step 6 | 2 |
After Step 7 | 1 |
So, you reached this last element in seven steps for an initial list with 128 elements. This, if you look carefully, is log 2128. So, let’s see if it lends you another search algorithm.
The name explains itself, doesn't it? Essentially, we are not going to talk about anything new. If you can recall the computational thinking module we discussed decomposition there. There we talked about breaking down a problem into smaller subproblems and solving those smaller ones makes the task easier. Let's understand it better with an example. Let's imagine you have a phone book and you are looking for a name. Chang let's say you open the phone book in the middle and you see what names is in that middle page. If the middle page has names that start in L, then to look up the names that start with C, you would look at the pages to the left of L. Now you may think, why should I start in the middle? What if the phone book has 90% of the names starting with A and 10% of the names starting with any other letter apart from A? In such a case, starting at the middle doesn't make much sense. But in reality, we don't know how many names start with A or for that matter, any other letter. So it's
always a good practice to start in the middle of the phone book, since in this case, the middle page has names that start with L and we are looking for a name that starts with C. So we will discard everything that's to the right of L and we will move to the left of L and we will repeat the same process for the left side. This kind of an approach where you are dividing a problem into halves or smaller subproblems certainly looks like a more programmatic way to attack a problem. This is what divide and conquer is. You divide a problem into smaller problems. You conquer by solving those smaller subproblems. Then you construct the solution to the original problem from the solutions to the smaller sub problems.
Divide and conquer algorithm involve breaking down a problem into smaller subproblems and solving them.
This approach is more programmatic and efficient.
The concept is explained using the example of finding a name in a phone book.
Starting in the middle of the phone book is a good practice, as it can help in quickly finding the required name.
This approach involves dividing the problem into halves or smaller subproblems.
The solutions to the smaller subproblems are used to construct the solution to the original problem.
Looking for an efficient way to solve complex problems? Learn about the divide and conquer algorithm and its benefits in our video. We explain this concept with the example of finding a name in a phone book, and provide insights on how to start in the middle to quickly locate the required name. With this approach, you can divide a problem into smaller subproblems and solve them efficiently to construct the solution to the original problem. Watch now to learn more.