ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Design and Analysis of Algorithms
    COMP3138
    Progress0 / 53 topics
    Topics
    1. Introduction to Algorithm Design2. Data Structures3. Efficiency in Algorithms4. Analysis of Algorithms5. Mathematical Review6. Mathematical Analysis of Algorithms7. Types of Functions8. Order of Growth9. Asymptotic Notations10. Sorting Algorithms11. Selection Sort Algorithm12. Example and Analysis of Selection Sort13. Insertion Sort Algorithm14. Divide and Conquer Algorithms15. Merge Sort Algorithm16. Quick Sort Algorithm17. Bucket Sort Algorithm18. Radix Sort Algorithm19. Counting Sort Algorithm20. Heap Sort Basics21. Heap Algorithms22. Heap Properties and Examples23. Heap Operations24. Heap Sort Algorithm Analysis25. Heap Insertion and Deletion26. Tree Based Algorithms and Hashing27. Red Black Tree Basics28. Binary Search Tree Basics29. Tree Searching Algorithms30. Analysis of Tree Searching Algorithms31. Analysis of Insertion and Deletion in BST32. Hashing Basics33. Examples of Hash Functions34. Analysis of Collision Resolution Techniques35. Dynamic Programming36. 0-1 Knapsack Problem37. Fractional Knapsack Problem38. Longest Common Subsequence39. Shortest Path Finding40. Matrix Chain Multiplication41. Assembly Line Chain Problem42. Greedy Algorithms43. Prim's Algorithm44. Kruskal's Algorithm45. Dijkstra's Algorithm46. Huffman Coding47. NP-Completeness48. Polynomial Time Verification49. Reducibility50. NP-Completeness Proofs51. Randomized Algorithms52. Particle Swarm Optimization53. Genetic Algorithms
    COMP3138›Divide and Conquer Algorithms
    Design and Analysis of AlgorithmsTopic 14 of 53

    Divide and Conquer Algorithms

    7 minread
    1,162words
    Intermediatelevel

    Divide and Conquer Algorithms

    Divide and Conquer is a fundamental algorithmic technique used to solve complex problems by breaking them down into smaller, more manageable subproblems. The idea is to divide the problem into smaller subproblems, solve each subproblem recursively, and then combine the solutions of the subproblems to solve the original problem.

    Basic Structure of Divide and Conquer Algorithms

    A Divide and Conquer algorithm follows three main steps:

    1. Divide: Split the problem into smaller subproblems. These subproblems should be of the same type as the original problem but simpler to solve.

    2. Conquer: Solve each of the subproblems. If the subproblems are small enough, solve them directly; otherwise, apply the divide and conquer technique recursively to break them down further.

    3. Combine: After solving the subproblems, combine the solutions of the subproblems to form the solution to the original problem.

    Example of Divide and Conquer

    To better understand how Divide and Conquer works, let’s take the example of Merge Sort, a popular sorting algorithm that follows the divide and conquer approach.


    Merge Sort Algorithm (Divide and Conquer)

    Steps in Merge Sort:

    1. Divide: Split the unsorted array into two halves until each subarray contains a single element (which is trivially sorted).

    2. Conquer: Recursively sort the two halves.

    3. Combine: Merge the two sorted halves back together to form a sorted array.

    Merge Sort Example:

    Suppose we have an unsorted array:

    [38, 27, 43, 3, 9, 82, 10]
    
    Step 1: Divide:

    We repeatedly divide the array into halves:

    • Divide [38, 27, 43, 3, 9, 82, 10] into [38, 27, 43] and [3, 9, 82, 10]
    • Divide [38, 27, 43] into [38] and [27, 43]
    • Divide [27, 43] into [27] and [43]
    • Divide [3, 9, 82, 10] into [3, 9] and [82, 10]
    • Divide [3, 9] into [3] and [9]
    • Divide [82, 10] into [82] and [10]
    Step 2: Conquer (Sort):

    Since each subarray contains only one element, each is considered trivially sorted. We now start merging them back together, sorting as we go.

    • Merge [27] and [43] into [27, 43]
    • Merge [3] and [9] into [3, 9]
    • Merge [82] and [10] into [10, 82]
    Step 3: Combine:

    Now, we begin merging the sorted subarrays:

    • Merge [38] and [27, 43] into [27, 38, 43]

    • Merge [3, 9] and [10, 82] into [3, 9, 10, 82]

    • Finally, merge [27, 38, 43] and [3, 9, 10, 82] into the fully sorted array:

      [3, 9, 10, 27, 38, 43, 82]
      

    Thus, the original array is now sorted.


    Time Complexity of Merge Sort

    The time complexity of Merge Sort can be derived by analyzing how many times we divide the array and how much work is done during each merge:

    1. Divide: At each level of recursion, the array is divided in half, so the depth of the recursion tree is O(log n) (since the array is halved at each level).

    2. Conquer: Each merge operation takes linear time, O(n), where n is the size of the array at that level of recursion.

    So, the total time complexity is the depth of the recursion tree multiplied by the time to process each level (merge):

    • Time Complexity: O(n log n)

    Space Complexity of Merge Sort

    Since Merge Sort requires additional space to store the temporary subarrays during the merge step, its space complexity is:

    • Space Complexity: O(n)

    Other Examples of Divide and Conquer Algorithms

    1. Quick Sort

    Quick Sort is another widely used sorting algorithm that follows the divide and conquer technique. It selects a "pivot" element, partitions the array into two subarrays (one with elements smaller than the pivot, and one with elements greater than the pivot), and then recursively sorts the subarrays.

    Steps:

    1. Choose a pivot element.
    2. Partition the array into two subarrays (one with elements less than the pivot, and one with elements greater than the pivot).
    3. Recursively apply quick sort to the two subarrays.
    4. Combine the results (since the subarrays are sorted, no further combining is needed).

    Time Complexity:

    • Best and Average Case: O(n log n)
    • Worst Case: O(n²) (if the pivot always ends up being the smallest or largest element)

    Space Complexity: O(log n) for the recursive stack.

    2. Binary Search

    Binary Search is a search algorithm that works by repeatedly dividing a sorted array in half to find the target element. It eliminates half of the remaining elements in each step, which makes it very efficient for searching in large datasets.

    Steps:

    1. Compare the target value with the middle element of the array.
    2. If the target value is equal to the middle element, return the index.
    3. If the target value is less than the middle element, search the left half.
    4. If the target value is greater than the middle element, search the right half.
    5. Repeat the process until the target is found or the array is exhausted.

    Time Complexity: O(log n)

    Space Complexity: O(1) for iterative binary search and O(log n) for recursive binary search.

    3. Strassen’s Matrix Multiplication

    Strassen’s algorithm for matrix multiplication is a divide and conquer algorithm that multiplies two matrices in fewer steps than the standard matrix multiplication method, reducing the time complexity.

    Time Complexity: O(n^log2(7)) ≈ O(n^2.81), which is faster than the standard O(n³) approach.


    Advantages of Divide and Conquer Algorithms

    1. Efficiency: Many divide and conquer algorithms, such as Merge Sort and Quick Sort, offer significantly better time complexities (e.g., O(n log n)) compared to simpler algorithms like Bubble Sort and Selection Sort (O(n²)).
    2. Parallelism: Divide and conquer algorithms are often well-suited for parallel computation, as the subproblems can often be solved independently. For example, Merge Sort can be parallelized easily.
    3. Scalability: Because divide and conquer algorithms split the problem into smaller subproblems, they tend to scale better for large inputs compared to brute force methods.

    Disadvantages of Divide and Conquer Algorithms

    1. Memory Usage: Some divide and conquer algorithms, such as Merge Sort, require additional space for storing temporary subarrays, leading to O(n) space complexity.
    2. Recursive Overhead: Many divide and conquer algorithms are recursive, which can result in overhead due to function calls and the use of the system stack.
    3. Worst Case: Some algorithms, like Quick Sort, can degrade to inefficient performance (O(n²)) in the worst case, depending on the choice of pivot.

    Conclusion

    Divide and Conquer is a powerful algorithmic paradigm that solves complex problems efficiently by breaking them into smaller, more manageable subproblems. Algorithms like Merge Sort, Quick Sort, Binary Search, and Strassen's Matrix Multiplication are classic examples of this approach. While these algorithms are highly efficient for many problems, they also come with trade-offs, such as higher memory usage or recursion overhead. Nonetheless, Divide and Conquer remains a core technique in computer science due to its ability to optimize the performance of many common problems.

    Previous topic 13
    Insertion Sort Algorithm
    Next topic 15
    Merge Sort Algorithm

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,162
      Code examples0
      DifficultyIntermediate