0% found this document useful (0 votes)
3 views

Practical No. 9 Divide and Conquer_ Implement Algorithms Like Merge Sort and Strassen's Matrix Multiplication.

The document discusses the Divide and Conquer strategy, detailing its implementation through Merge Sort and Strassen's Matrix Multiplication algorithms. Merge Sort efficiently sorts an array by recursively dividing it and merging sorted halves, while Strassen's algorithm improves matrix multiplication efficiency by reducing its complexity. Both algorithms have specific time and space complexities, making them suitable for various practical applications in computing and machine learning.

Uploaded by

Soham Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical No. 9 Divide and Conquer_ Implement Algorithms Like Merge Sort and Strassen's Matrix Multiplication.

The document discusses the Divide and Conquer strategy, detailing its implementation through Merge Sort and Strassen's Matrix Multiplication algorithms. Merge Sort efficiently sorts an array by recursively dividing it and merging sorted halves, while Strassen's algorithm improves matrix multiplication efficiency by reducing its complexity. Both algorithms have specific time and space complexities, making them suitable for various practical applications in computing and machine learning.

Uploaded by

Soham Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

9
Divide and Conquer: Implement algorithms like merge sort and Strassen's Matrix
Multiplication.

Divide and Conquer is a problem-solving strategy that involves breaking a


problem into smaller subproblems, solving them recursively, and then combining
the solutions. This approach is effective for many algorithmic problems, as it
reduces the problem size at each step, leading to more efficient solutions.

Steps for Implementing Divide and Conquer:

1. Divide: Break the problem into smaller subproblems.


2. Conquer: Solve the subproblems recursively.
3. Combine: Merge the solutions of the subproblems to get the final solution.

Practical Implementation

1. Merge Sort

Merge Sort is a sorting algorithm based on the Divide and Conquer technique. It
divides the array into halves, sorts each half recursively, and merges the sorted
halves.

def merge_sort(arr):

Merge Sort Algorithm (Divide and Conquer).


Divides the list into halves, sorts each half, and merges them.

if len(arr) <= 1: # Base case: a list with 0 or 1 element is already sorted


return arr

# Divide the list into two halves


mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

# Recursively sort both halves


left_half = merge_sort(left_half)
right_half = merge_sort(right_half)
# Merge the sorted halves
return merge(left_half, right_half)

def merge(left, right):

Merges two sorted lists into one sorted list.

sorted_list = []
while left and right:
if left[0] < right[0]:
sorted_list.append(left.pop(0))
else:
sorted_list.append(right.pop(0))

# If there are any remaining elements in either list, add them


sorted_list.extend(left)
sorted_list.extend(right)

return sorted_list

# Test the Merge Sort function


arr = [38, 27, 43, 3, 9, 82, 10]
sorted_arr = merge_sort(arr)
print(f"Sorted Array: {sorted_arr}")

2. Strassen's Matrix Multiplication

Strassen’s algorithm is a more efficient algorithm for matrix multiplication,


reducing the complexity from O(n3)O(n3)O(n3) to approximately
O(n2.81)O(n^{2.81})O(n2.81) by recursively breaking down matrix multiplications.

import numpy as np
def strassen(A, B):

Strassen's Matrix Multiplication (Divide and Conquer).


A and B are matrices, the function returns their product C.

if len(A) == 1: # Base case: 1x1 matrices


return A * B

# Divide matrices A and B into 4 submatrices


mid = len(A) // 2
A11, A12, A21, A22 = A[:mid, :mid], A[:mid, mid:], A[mid:, :mid], A[mid:, mid:]
B11, B12, B21, B22 = B[:mid, :mid], B[:mid, mid:], B[mid:, :mid], B[mid:, mid:]

# Recursively compute the 7 products


M1 = strassen(A11 + A22, B11 + B22)
M2 = strassen(A21 + A22, B11)
M3 = strassen(A11, B12 - B22)
M4 = strassen(A22, B21 - B11)
M5 = strassen(A11 + A12, B22)
M6 = strassen(A21 - A11, B11 + B12)
M7 = strassen(A12 - A22, B21 + B22)

# Combine the results to get the final product


C11 = M1 + M4 - M5 + M7
C12 = M3 + M5
C21 = M2 + M4
C22 = M1 - M2 + M3 + M6

# Combine submatrices to form the final result


C = np.zeros((len(A), len(B[0])))
C[:mid, :mid] = C11
C[:mid, mid:] = C12
C[mid:, :mid] = C21
C[mid:, mid:] = C22

return C
# Test the Strassen's algorithm for matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = strassen(A, B)
print("Matrix Product C:")
print(C)

Complexity:

1. Merge Sort:
○ Time Complexity: O(nlog⁡n)O(n \log n)O(nlogn)
○ Space Complexity: O(n)O(n)O(n)
2. Strassen’s Matrix Multiplication:
○ Time Complexity: O(nlog⁡27)≈O(n2.81)O(n^{\log_2 7}) \
approx O(n^{2.81})O(nlog27)≈O(n2.81)
○ Space Complexity: O(n2)O(n^2)O(n2)

Practical Applications:

1. Merge Sort:
○ Efficient sorting algorithm used in scenarios where stability
(preserving equal element order) is required.
2. Strassen’s Matrix Multiplication:
○ Used in high-performance computing tasks, large matrix
calculations, and algorithms in machine learning.

You might also like