Decrease and Conquer


Imagine yourself in a situation where you are having trouble coming up with a solution to your initial issue. What if I told you that a small part of the problem is simpler to solve and that you can use this answer to find the answer to the larger one? Interesting? The decrease and conquer strategy achieves just this.

A problem-solving strategy known as "Decrease and Conquer" involves slicing the size of the input at every stage of the solution procedure. Identical to divide-and-conquer as it breaks the problem down into smaller sub-problems, decrease-and-conquer reduces the size of the input data at each stage rather than increasing it.

Finding the maximum or smallest element in an array, finding the nearest pair of points in a group of points, and binary search are a few cases that can be resolved using the decrease-and-conquer strategy.

Since the quantity of input data is decreased at each stage, decreasing the space and time complexities of the solution, the decrease-and-conquer benefit is that it frequently produces efficient algorithms. A poor decision could result in an algorithm that is ineffective, thus it's critical to pick the best technique for minimizing the quantity of the input data.

Variations in the method of Decrease and Conquer

The decrease-and-conquer strategy can take one of three main forms:

The first one is, "decrease by a specific value or a constant".

In this variant, each repetition or recursive step of said algorithm decreases the total size of the instance by the same constant. While other constant size decreases can occur, this constant often equals one. Many algorithms, including the following, use this variant.

Topological sorting, Graph search algorithms DFS and BFS, Insertion sort and various algorithms for producing subsets or permutations are examples of these.

The second one is, "decrease by a constant factor".

For each iteration or recursive step of an algorithm, this strategy decreases a problem instance by a constant amount. This constant factor usually equals two in applications. Very uncommon is a reduction by such a factor apart from two. Especially when the factor is bigger than 2, as in the fake-coin situation, decrease by a constant factor techniques are particularly effective. Examples of applications for this strategy are

Russian peasant multiplication, challenges with fake coins, and binary search.

The third or the last one is "decrease variable size".

In this modification, the algorithm's steps or iterations' size-reduction patterns change. Although the value for the second parameter in the problem of calculating the gcd of two numbers is always less on the right than on the left, it does not decrease by a constant or even a constant factor. Here are some examples of this strategy in action, and these are

Euclid's technique, interpolation search, and computing the median and selection problems

Example 1

An example of how we create permutations using the decrease and conquer method

How can we get all n! permutations with a set of n items, such as a1, a2, a3, ... an? To find a solution, produce all (n-1)! permutations. If this is resolved, we can solve the broader problem by placing n into each location within each permutation of the (n-1) elements.

If there is a single element, there's just one permutation, which is the recursive base case.

Example 2

Let's take an example of a binary search method where we apply the decrease-and-conquer technique. Finding an element's location inside a sorted array can be done using the searching method known as binary search.

With this method, an array's middle is always searched for the element.

Consider a sorted integer array, a = {5, 10, 15, 20, 25}. We want to find the index of element 10.

Approach

For instance, to obtain the permutations of {1,2,3}

First, we find the solution to permutations of {1, 2}

Start by finding the solution to permutations of {1} that is just {1}

Now we Insert 2 into {1}, giving: {2 1} and {1 2}

Insert 3 into {2 1} and {1 2} giving: {3 2 1} {2 3 1} {2 1 3} {3 1 2} {1 3 2} {1 2 3}

The total number of permutations we produce, n!, results in a runtime of n!. This is painfully slow for all but the smallest values of n, which isn't the algorithm's fault. The issue is simply that there are too few objects to generate.

Finding all paths for the traveling salesman problem is such a problem in which this might be applied.

Algorithm

Step 1: Start

Step 2: Set p (the element whose position to be found)

Step 3: Set the pointers first (at the first position) and last (at the last position)

Step 4: Find the middle element (mid)

Step 5: If p==mid, then print the middle element

Step 6: If p>mid, check p with elements right to mid

Step 7: If p<mid, check p with elements left to mid

Step 8: Repeat steps 4 to 7 till first meets last

Step 9: Return the result

Step 10: Stop

Example

// Binary Search in C
#include <stdio.h>
int binSearch(int a[], int p, int first, int last) {
   // Repeat till the low and high collide with each other
   while (first <= last) {
   int mid = first + (last - first) / 2;
   if (a[mid] == p)
      return mid;
   if (a[mid] < p)
      first = mid + 1;
   else
      last = mid - 1;
   }
   return -1;
}
int main(void) {
   int a[] = {5, 10, 15, 20, 25};
   int s = sizeof(a) / sizeof(a[0]);
   int p = 10;
   int res = binSearch(a, p, 0, s - 1);
   if (res == -1)
   printf(" Element not present");
   else
  //printing the position of the element
   printf("Element %d is found at index position %d of the array.",p ,res);  
  return 0;
}

Output

On execution, it will produce the following output:

Element 10 is found at index position 1 of the array.

Advantages

Some of the benefits of "decrease and conquer" include;

Simplicity: When compared to other strategies including dynamic programming or divide-and-conquer, decrease-and-conquer is frequently easier to put into practise.

Effective Algorithms: The technique frequently produces efficient algorithms since each step reduces the size of the input data, which lowers both time and space complexity of the result.

Problem-Specific: The approach works effectively for particular issues where a simplified version of the issue can be resolved more quickly.

Disadvantages

Some of the setbacks of Decrease and Conquer include;

Problem-Specific: The method may not be appropriate for problems that are more complicated or to all difficulties.

Implementation Complexity: Compared to other strategies like divide-and-conquer, the strategy may be more difficult to put into practice and may necessitate more thorough planning.

Updated on: 23-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements