Explain any one application that can be solved by divide and conquer.


Q.) Explain any one application that can be solved by divide and conquer.

Subject: Analysis And Design of Algorithm

Introduction

Divide and Conquer is a popular algorithmic paradigm that is based on multi-branched recursion. It works by breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem. The Divide and Conquer algorithm consists of three main steps:

  1. Divide: This involves dividing the problem into some sub problem.
  2. Conquer: Sub problem by calling recursively until sub problem solved.
  3. Combine: The Sub problem Solved so that we will get find problem solution.

Application of Divide and Conquer: Binary Search

One of the most common applications of the Divide and Conquer algorithm is the Binary Search. Binary Search is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array and based on this comparison, it decides to eliminate half of the search space.

Detailed Explanation of Binary Search

The Binary Search algorithm works as follows:

  1. Divide: The array is divided into two halves by finding the middle element. The formula used to find the middle element is: mid = low + (high - low) / 2.
  2. Conquer: If the target value is equal to the middle element, then the search is over. If the target value is less than the middle element, then the search continues on the left half of the array. If the target value is greater than the middle element, then the search continues on the right half of the array.
  3. Combine: This process is repeated, each time on a smaller half of the array, until the target value is found or all elements have been checked.

Formulas used in Binary Search

The formula used to find the middle element of the array is: mid = low + (high - low) / 2. Here, low is the starting index of the array and high is the ending index of the array. Depending on whether the target value is greater or less than the middle element, the value of low or high is adjusted accordingly.

Example of Binary Search

Let's consider an array of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and let's say we want to find the position of the number 7 in the array.

  1. The middle element of the array is 5. Since 7 is greater than 5, we discard the left half of the array.
  2. Now, our array is [6, 7, 8, 9, 10]. The middle element is 8. Since 7 is less than 8, we discard the right half of the array.
  3. Now, our array is [6, 7]. The middle element is 6. Since 7 is greater than 6, we discard the left half of the array.
  4. Now, our array is [7]. The middle element is 7, which is the target value. So, the position of 7 in the original array is 6.

Program for Binary Search

Here is a simple Python program that implements Binary Search:

def binary_search(arr, low, high, x):
    if high >= low:
        mid = (high + low) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binary_search(arr, low, mid - 1, x)
        else:
            return binary_search(arr, mid + 1, high, x)
    else:
        return -1

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = 7
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
    print("Element is present at index", str(result))
else:
    print("Element is not present in array")

Time Complexity of Binary Search

The time complexity of Binary Search is O(log n). This is because with each comparison, the algorithm eliminates half of the remaining search space. This results in a logarithmic time complexity.

Conclusion

Binary Search is a great example of how the Divide and Conquer algorithm can be used to solve problems efficiently. Other applications of Divide and Conquer include sorting algorithms like Merge Sort and Quick Sort, and computational geometry algorithms like the Convex Hull algorithm.

Diagram: Not necessary for this question.

Summary

Divide and Conquer is a popular algorithmic paradigm that breaks down a problem into sub-problems, solves them recursively, and combines the solutions to give a solution to the original problem. One application of Divide and Conquer is Binary Search, which finds the position of a target value within a sorted array. Binary Search has a time complexity of O(log n).

Analogy

Divide and Conquer is like solving a jigsaw puzzle. You break down the puzzle into smaller pieces, solve each piece individually, and then combine the solved pieces to complete the puzzle.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is the main idea behind the Divide and Conquer algorithm?
  • Breaking down a problem into sub-problems
  • Combining the solutions of sub-problems
  • Solving sub-problems recursively
  • All of the above