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

daa exp02

The document outlines an experiment on implementing the Divide and Conquer algorithm using merge sort and quick sort, with a focus on understanding their time complexities and performance. It includes a detailed explanation of Dynamic Programming, particularly in the context of Matrix Chain Multiplication, along with algorithms and pseudocode for both sorting methods and matrix multiplication. The document also provides a program for Matrix Chain Multiplication, including its output and analysis of best, average, and worst-case scenarios.

Uploaded by

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

daa exp02

The document outlines an experiment on implementing the Divide and Conquer algorithm using merge sort and quick sort, with a focus on understanding their time complexities and performance. It includes a detailed explanation of Dynamic Programming, particularly in the context of Matrix Chain Multiplication, along with algorithms and pseudocode for both sorting methods and matrix multiplication. The document also provides a program for Matrix Chain Multiplication, including its output and analysis of best, average, and worst-case scenarios.

Uploaded by

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

BHARATIYA VIDYA BHAVAN’S

SARDAR PATEL INSTITUTE OF TECHNOLOGY


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Name Ria Parag Talsania

UID no. 2023300242

Experiment No. 2

Date of Submission 04-02-2025

AIM: To understand and implement the Divide and Conquer algorithm using merge sort and quick sort
respectively.

Objective: 1. To learn divide and conquer algorithm.


2. To develop an algorithm for mergesort and quicksort.
3. To find the best,worst and average cases of merge as well as quick sort respectively in
terms of time complexity,number of exchanges and comparison rates.

Theory: Dynamic Programming (DP) is a powerful technique used to optimize recursive algorithms,
particularly when a problem involves overlapping subproblems. In a basic recursive approach, a
problem is often divided into smaller subproblems, but many of these are solved multiple times,
leading to redundant calculations. This inefficiency results in exponential time complexity,
making the solution impractical for large inputs.

DP overcomes this redundancy by storing the results of subproblems as they are computed.
When the same subproblem arises again, its result is retrieved from storage—often referred to as
a memoization table or cache—rather than being recalculated. By ensuring that each subproblem
is solved only once, DP significantly reduces time complexity from exponential to polynomial.
A classic example of a problem that benefits from DP is Matrix Chain Multiplication.

The objective is to determine the optimal way to parenthesize a sequence of matrices to minimize
the number of scalar multiplications required. Instead of trying all possible ways to multiply the
matrices, DP breaks the problem into smaller subproblems, computing the minimum number of
multiplications for smaller matrix chains first and then using those results to solve larger
subproblems. This systematic approach efficiently finds the most cost-effective order for matrix
multiplication.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Algorithm:
MATRIX-CHAIN-ORDER (dims)
1. n = length[dims] - 1
2. for i ← 1 to n
3. do cost[i, i] ← 0
4. for length ← 2 to n // length is the chain length
5. do for i ← 1 to n - length + 1
6. do j ← i + length - 1
7. cost[i, j] ← ∞
8. for split ← i to j - 1
9. do temp ← cost[i, split] + cost[split + 1, j] + dims[i - 1] * dims[split] * dims[j]
10. If temp < cost[i, j]
11. then cost[i, j] ← temp
12. splitPoint[i, j] ← split
13. return cost and splitPoint

PRINT-OPTIMAL-PARENTHESIS (splitPoint, i, j)
1. if i = j
2. then print "M"
3. else
4. print "("
5. PRINT-OPTIMAL-PARENS (splitPoint, i, splitPoint[i, j])
6. PRINT-OPTIMAL-PARENS (splitPoint, splitPoint[i, j] + 1, j)
7. print ")"

Use Cases:

1. String Processing
2. Graph Algorithms
3. Sequence and Array Optimization
4. Game Theory & Decision Making
5. Matrix & Partitioning Problems

Description:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Quick Sort is another divide and conquer algorithm. It picks an element (called the pivot),
partitions the array into two sub-arrays (one with elements smaller than the pivot and the other
with elements larger than the pivot), and then recursively sorts the sub-arrays.

Algorithm:
QUICKSORT(arr, start, end):
1. If start < end:
2. p = PARTITION(arr, start, end)
3. QUICKSORT(arr, start, p - 1)
4. QUICKSORT(arr, p + 1, end)

PARTITION(arr, start, end):


1. pivot = arr[end]
2. i = start - 1
3. For j = start to end - 1:
4. If arr[j] < pivot:
5. i=i+1
6. Swap arr[i] with arr[j]
7. Swap arr[i + 1] with arr[end]
8. Return i + 1

Use Cases:
 When a fast sorting algorithm is required, as Quick Sort often outperforms Merge Sort in
practice due to better cache performance.
 Suitable for arrays stored in memory since it doesn’t require extra space for merging.
 Ideal for datasets that are mostly sorted, as well as when in-place sorting is a priority.

Matrix Chain Multiplication


Matrix Chain Multiplication is an optimization problem used to determine the most efficient way
to multiply a sequence of matrices. Since matrix multiplication is associative, different
parenthesizations yield the same result, but the number of scalar multiplications required can
vary significantly. The goal is to find the order of multiplication that minimizes the number of
scalar multiplications.
Problem Statement:
Given n matrices A₁, A₂, ..., Aₙ, where matrix Aᵢ has dimensions pᵢ₋₁ × pᵢ, find the optimal
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

order of multiplication that minimizes the total number of scalar multiplications.


Key Observations:
1. Order Matters for Efficiency – Although matrix multiplication is associative, choosing
the right parenthesization can significantly reduce computation time.
2. Dynamic Programming Approach – The problem has overlapping subproblems and
optimal substructure, making DP an ideal solution.
3. State Definition – Let m[i, j] represent the minimum number of scalar multiplications
needed to multiply matrices from Aᵢ to Aⱼ.
4. Recurrence Relation: m[i,j]=min⁡i≤k<j(m[i,k]+m[k+1,j]+pi−1pkpj)m[i, j] = \min_{i \leq
k < j} \left( m[i, k] + m[k+1, j] + p_{i-1} p_k p_j \right)m[i,j]=i≤k<jmin(m[i,k]+m[k+1,j]
+pi−1pkpj) Here, k represents the position where the matrix sequence is split into two
parts.

Matrix Chain Multiplication Pseudocode

MATRIX-CHAIN-MULTIPLICATION(p)
1. n = length of p - 1
2. Define m[1…n, 1…n] and s[1…n-1, 2…n]
3. for i = 1 to n:
4. m[i, i] = 0 // Zero cost for single matrix
5. for l = 2 to n: // l represents the chain length
6. for i = 1 to n - l + 1:
7. j=i+l-1
8. m[i, j] = ∞ // Initialize cost to a large value
9. for k = i to j - 1:
10. q = m[i, k] + m[k+1, j] + p[i-1] * p[k] * p[j]
11. if q < m[i, j]:
12. m[i, j] = q
13. s[i, j] = k // Store split point
14. return m and s

Explanation of Pseudocode
1. Initialize Tables:
o m[i, j] stores the minimum cost to multiply matrices from Aᵢ to Aⱼ.
o s[i, j] keeps track of the split point that gives the optimal parenthesization.
2. Base Case (Single Matrix Multiplication):
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

o When i == j, the multiplication cost is 0, since a single matrix does not require
multiplication.
3. Iterate Over Different Chain Lengths (l):
o We start by solving subproblems of length 2, then 3, up to n.
4. Compute Minimum Cost Using All Possible Splits (k):
o The for k loop tries every possible split between i and j.
o The recurrence relation calculates the cost of multiplying matrices from i to k and
k+1 to j, and then combining the results.
5. Store and Return the Optimal Solution:
o The m[i, j] table contains the minimum multiplication cost.
o The s[i, j] table is used to reconstruct the optimal multiplication order.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Program: Code for Matrix Chain Multiplication:


#include <stdio.h>
#include <limits.h>

char name = 'A';

// Function to print the optimal parenthesization of the matrix chain


void printParenthesis(int i, int j, int n, int dp[n][n]) {
if (i == j) {
printf("%c", name++);
return;
}
printf("(");
printParenthesis(i, dp[i][j], n, dp);
printParenthesis(dp[i][j] + 1, j, n, dp);
printf(")");
}

// Function for Matrix Chain Multiplication


void matrixChainOrder(int p[], int n) {
int m[n][n]; // Stores minimum multiplication cost
int dp[n][n]; // Stores the index where the optimal split occurs

// Initialize matrices to zero


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m[i][j] = 0;
dp[i][j] = 0;
}
}

// Compute m[i][j] for increasing chain length


for (int L = 2; L < n; L++) { // L is the chain length
for (int i = 1; i < n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = INT_MAX;
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

for (int k = i; k < j; k++) {


// Compute cost q for splitting at k
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
dp[i][j] = k; // Store split point
}
}
}
}

printf("\nOptimal Cost is: %d\n", m[1][n - 1]);

// Print Matrix Chain Table (m)


printf("\nMatrix Chain Table (m):\n");
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (j < i)
printf("\t"); // Print space for lower triangle
else
printf("%d\t", m[i][j]);
}
printf("\n");
}

// Print Split Point Table (dp)


printf("\nK Table:\n");
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (j < i)
printf("\t"); // Print space for lower triangle
else
printf("%d\t", dp[i][j]); // Print actual value
}
printf("\n");
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

// Print Optimal Parenthesization


printf("\nOptimal Parenthesization is: ");
printParenthesis(1, n - 1, n, dp);
printf("\n");
}

int main() {
int n;

printf("Enter the number of matrices: ");


scanf("%d", &n);

int arr[n + 1]; // +1 because dimensions are from 0 to n

printf("Enter the dimensions: ");


for (int i = 0; i <= n; i++) {
scanf("%d", &arr[i]);
}

matrixChainOrder(arr, n + 1); // +1 because we have n matrices and (n+1) dimensions

return 0;
}
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Output:

Example 1:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Example 2:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Dry Run:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Observation and Analysis:


Best Case: O(n³)
Scenario:
 The input matrices are already optimally ordered, so the minimal number of calculations is
required.
 The DP table gets filled quickly with minimal updates.
Example:
 If multiplying (A × (B × (C × D))) is already optimal, then DP just verifies this order rather than
exploring other unnecessary splits.
 This reduces the number of unnecessary calculations but still requires O(n³) table filling.
Average Case: O(n³)
Scenario:
 On average, all possible splits are considered while filling the DP table.
 The number of recursive calls in memoization reduces redundant work, but the overall table-filling
still requires O(n³) operations.
Example:
 Given random matrices, the algorithm checks various parenthesization possibilities and finds the
optimal way dynamically.
 Most cases fall into this category, where the DP table is fully utilized, leading to O(n³) complexity.

Worst Case: O(n³)


Scenario:
 Matrices are in the worst possible order, meaning the algorithm explores all possible split positions
before finding the optimal order.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

 The DP table must consider every possible way to multiply matrices, making all computations
necessary.
Example:
 If given matrices in a non-optimal order, the algorithm must evaluate every split before
determining the best one.
 Every subproblem is computed from scratch, leading to O(n³) complexity.

Conclusion:
I have learned that matrix chain multiplication involves finding the optimal order to multiply matrices while
minimizing the number of scalar multiplications. The Dynamic Programming (DP) approach helps in solving
this problem efficiently by avoiding redundant calculations using memoization.
How DP Helps in Matrix Chain Multiplication
1. Avoids Redundant Calculations:
o The naive recursive approach recalculates the same subproblems multiple times, leading to
exponential time complexity (O(2ⁿ)).
o DP stores the results of subproblems in a table (m[][]), ensuring each subproblem is solved
only once, reducing complexity to O(n³).
2. Optimizes Parenthesization:
o Since matrix multiplication is associative, different parenthesizations yield different
multiplication costs.
o The DP approach efficiently determines the optimal split points using a decision table (dp[][]).
3. Improves Computational Efficiency:
o Instead of brute force searching for the best order, DP breaks the problem into smaller
subproblems and combines their results optimally.
o This makes large-scale matrix multiplications feasible in polynomial time.

You might also like