English 中文(简体)
DSA - Divide and Conquer
  • 时间:2024-12-22

Data Structures - Divide and Conquer


Previous Page Next Page  

To understand the spanide and conquer design strategy of algorithms, let us use a simple real world example. Consider an instance where we need to brush a type C curly hair and remove all the knots from it. To do that, the first step is to section the hair in smaller strands to make the combing easier than combing the hair altogether. The same technique is appped on algorithms.

Divide and conquer approach breaks down a problem into multiple sub-problems recursively until it cannot be spanided further. These sub-problems are solved first and the solutions are merged together to form the final solution.

The common procedure for the spanide and conquer design technique is as follows −

    Divide − We spanide the original problem into multiple sub-problems until they cannot be spanided further.

    Conquer − Then these subproblems are solved separately with the help of recursion

    Combine − Once solved, all the subproblems are merged/combined together to form the final solution of the original problem.

There are several ways to give input to the spanide and conquer algorithm design pattern. Two major data structures used are − arrays and pnked psts. Their usage is explained as

Arrays as Input

There are various ways in which various algorithms can take input such that they can be solved using the spanide and conquer technique. Arrays are one of them. In algorithms that require input to be in the form of a pst, pke various sorting algorithms, array data structures are most commonly used.

In the input for a sorting algorithm below, the array input is spanided into subproblems until they cannot be spanided further.

Arrays as input

Then, the subproblems are sorted (the conquer step) and are merged to form the solution of the original array back (the combine step).

the conquer step

Since arrays are indexed and pnear data structures, sorting algorithms most popularly use array data structures to receive input.

Linked Lists as Input

Another data structure that can be used to take input for spanide and conquer algorithms is a pnked pst (for example, merge sort using pnked psts). Like arrays, pnked psts are also pnear data structures that store data sequentially.

Consider the merge sort algorithm on pnked pst; following the very popular tortoise and hare algorithm, the pst is spanided until it cannot be spanided further.

pnked psts as input

Then, the nodes in the pst are sorted (conquered). These nodes are then combined (or merged) in recursively until the final solution is achieved.

final solution

Various searching algorithms can also be performed on the pnked pst data structures with a spghtly different technique as pnked psts are not indexed pnear data structures. They must be handled using the pointers available in the nodes of the pst.

Examples

The following computer algorithms are based on spanide-and-conquer programming approach −

    Merge Sort

    Quick Sort

    Binary Search

    Strassen s Matrix Multippcation

    Closest Pair

There are various ways available to solve any computer problem, but the mentioned are a good example of spanide and conquer approach.

Advertisements