0% found this document useful (0 votes)
4 views6 pages

Assign 2 Shiksha

The document describes two algorithms: one for finding the maximum sum of a contiguous subarray using the Divide and Conquer approach, and another for multiplying matrices using Strassen's algorithm. The maximum subarray sum for the given input is 6, achieved by dividing the array and calculating sums in the left, right, and crossing sections. Strassen's algorithm improves matrix multiplication efficiency by dividing matrices into submatrices and has a recurrence relation that can be analyzed for time complexity using the Master Theorem.

Uploaded by

aditya0314n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Assign 2 Shiksha

The document describes two algorithms: one for finding the maximum sum of a contiguous subarray using the Divide and Conquer approach, and another for multiplying matrices using Strassen's algorithm. The maximum subarray sum for the given input is 6, achieved by dividing the array and calculating sums in the left, right, and crossing sections. Strassen's algorithm improves matrix multiplication efficiency by dividing matrices into submatrices and has a recurrence relation that can be analyzed for time complexity using the Master Theorem.

Uploaded by

aditya0314n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q.

1 (CO2) Given an array A of n integers, find the maximum sum of a contiguous subarray
using the Divide and Conquer approach. Analyze the time complexity of your solution and
verify if the Master Theorem applies.

1. Input: A = [-2, 1, -3, 4, -1, 2, 1, -5, 4]

2. Output: Maximum sum = 6 (subarray: [4,−1,2,1]).

Approach: We use the Divide and Conquer approach to solve the problem. The algorithm
divides the array into two halves:

1. Finds the maximum subarray sum in the left half.

2. Finds the maximum subarray sum in the right half.

3. Finds the maximum subarray sum crossing the middle point.

The final result is the maximum of these three values.

#include <stdio.h>

#include <limits.h>

// Function to find the maximum of two numbers

int max(int a, int b) {

return (a > b) ? a : b;

// Function to find the maximum of three numbers

int maxOfThree(int a, int b, int c) {

return max(a, max(b, c));

}
// Function to find the maximum crossing subarray sum

int maxCrossingSum(int arr[], int low, int mid, int high) {

int left_sum = INT_MIN, right_sum = INT_MIN;

int sum = 0;

// Find the maximum sum in the left half

for (int i = mid; i >= low; i--) {

sum += arr[i];

if (sum > left_sum)

left_sum = sum;

sum = 0;

// Find the maximum sum in the right half

for (int i = mid + 1; i <= high; i++) {

sum += arr[i];

if (sum > right_sum)

right_sum = sum;

return left_sum + right_sum;

// Function to find the maximum sum of a subarray

int maxSubArraySum(int arr[], int low, int high) {

if (low == high)

return arr[low];
int mid = (low + high) / 2;

int left_sum = maxSubArraySum(arr, low, mid);

int right_sum = maxSubArraySum(arr, mid + 1, high);

int cross_sum = maxCrossingSum(arr, low, mid, high);

return maxOfThree(left_sum, right_sum, cross_sum);

int main() {

int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

int n = sizeof(arr) / sizeof(arr[0]);

int max_sum = maxSubArraySum(arr, 0, n - 1);

printf("Maximum sum = %d\n", max_sum);

return 0;

Q.2 (CO2) Given two n×n times matrices A and B, multiply them using Strassen’s Divide
and Conquer algorithm. Analyze the recurrence relation and determine the time
complexity using the Master Theorem.

Approach: Strassen's algorithm for matrix multiplication is based on dividing the matrices
into four submatrices and performing recursive operations to calculate the product
efficiently.

The recurrence relation for Strassen’s algorithm is:


#include <stdio.h>

#include <stdlib.h>

// Function to add two matrices

void add(int A[2][2], int B[2][2], int C[2][2]) {

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

C[i][j] = A[i][j] + B[i][j];

// Function to subtract two matrices

void subtract(int A[2][2], int B[2][2], int C[2][2]) {

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

C[i][j] = A[i][j] - B[i][j];

// Strassen's algorithm for matrix multiplication

void strassen(int A[2][2], int B[2][2], int C[2][2]) {

int M1, M2, M3, M4, M5, M6, M7;

M1 = (A[0][0] + A[1][1]) * (B[0][0] + B[1][1]);

M2 = (A[1][0] + A[1][1]) * B[0][0];


M3 = A[0][0] * (B[0][1] - B[1][1]);

M4 = A[1][1] * (B[1][0] - B[0][0]);

M5 = (A[0][0] + A[0][1]) * B[1][1];

M6 = (A[1][0] - A[0][0]) * (B[0][0] + B[0][1]);

M7 = (A[0][1] - A[1][1]) * (B[1][0] + B[1][1]);

C[0][0] = M1 + M4 - M5 + M7;

C[0][1] = M3 + M5;

C[1][0] = M2 + M4;

C[1][1] = M1 - M2 + M3 + M6;

int main() {

int A[2][2] = {{1, 2}, {3, 4}};

int B[2][2] = {{5, 6}, {7, 8}};

int C[2][2] = {0};

strassen(A, B, C);

printf("Product matrix:\n");

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

printf("%d ", C[i][j]);

printf("\n");

return 0;
}

You might also like