ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3138Divide Conquer Algorit…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3138

    Design and Analysis of Algorithms

    Progress0 / 53 done
    Topics53
    1Introduction to Algorithm Design2Data Structures3Efficiency in Algorithms4Analysis of Algorithms5Mathematical Review6Mathematical Analysis of Algorithms7Types of Functions8Order of Growth9Asymptotic Notations10Sorting Algorithms11Selection Sort Algorithm12Example and Analysis of Selection Sort13Insertion Sort Algorithm14Divide and Conquer Algorithms15Merge Sort Algorithm16Quick Sort Algorithm17Bucket Sort Algorithm18Radix Sort Algorithm19Counting Sort Algorithm20Heap Sort Basics21Heap Algorithms22Heap Properties and Examples23Heap Operations24Heap Sort Algorithm Analysis25Heap Insertion and Deletion26Tree Based Algorithms and Hashing27Red Black Tree Basics28Binary Search Tree Basics29Tree Searching Algorithms30Analysis of Tree Searching Algorithms31Analysis of Insertion and Deletion in BST32Hashing Basics33Examples of Hash Functions34Analysis of Collision Resolution Techniques35Dynamic Programming360-1 Knapsack Problem37Fractional Knapsack Problem38Longest Common Subsequence39Shortest Path Finding40Matrix Chain Multiplication41Assembly Line Chain Problem42Greedy Algorithms43Prim's Algorithm44Kruskal's Algorithm45Dijkstra's Algorithm46Huffman Coding47NP-Completeness48Polynomial Time Verification49Reducibility50NP-Completeness Proofs51Randomized Algorithms52Particle Swarm Optimization53Genetic Algorithms
    7 min read
    Topic 14 of 53
    Topic Study Note Illustration
    14

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms