Pavanaj DAA Report
Pavanaj DAA Report
Project Details:
Longest Common Subsequences Of String Using Dynamic
Project Title:
Programming
Place of Project: REVA UNIVERSITY, BENGALURU
Student Details:
Email-ID: [email protected]
SRN: R22EF146
1
Contents
1. Abstract 3
2. Introduction 4
4. Project overview. 8
5. Implementation. 11
7. Conclusions 19
8. References 20
2
1. ABSTRACT
Matrix Chain Multiplication addresses the challenge of determining the most efficient way to
multiply a series of matrices. By leveraging dynamic programming, we minimize the number
of scalar multiplications required, thus optimizing computational efficiency. We present the
recursive formulation of the problem, followed by the dynamic programming approach that
constructs a solution through a bottom-up method, utilizing a table to record the minimum
cost of multiplying sub chains of matrices.
Both problems illustrate the essence of dynamic programming: optimal substructure and
overlapping subproblems. Through detailed examples and algorithmic analysis, this paper
underscores the significance of dynamic programming in developing efficient and scalable
solutions for a variety of computational problems.
3
2. INTRODUCTION
In this paper, we focus on two quintessential problems that exemplify the power and versatility
of dynamic programming: the Knapsack Problem and Matrix Chain Multiplication. These
problems not only demonstrate the practical utility of dynamic programming but also provide
insight into its theoretical underpinnings.
The Knapsack Problem is a classic problem in combinatorial optimization. Given a set of items,
each with a weight and a value, the task is to determine the number of each item to include in
a collection such that the total weight does not exceed a given limit and the total value is
maximized. This problem can be categorized into different types, such as the 0/1 Knapsack
Problem, where each item can either be taken or left, and the Fractional Knapsack Problem,
where items can be broken into smaller pieces. In this paper, we focus on the 0/1 Knapsack
Problem, demonstrating how dynamic programming can be used to develop an efficient
solution.
Matrix Chain Multiplication is another classic problem that illustrates the application of
dynamic programming. The problem involves determining the most efficient way to multiply
a given sequence of matrices. Since matrix multiplication is associative, the order in which the
matrices are multiplied can significantly affect the computational cost. The goal is to find the
parenthesizing that minimizes the number of scalar multiplications required. This problem is
particularly important in fields such as computer graphics, scientific computing, and
optimization.
Both the Knapsack Problem and Matrix Chain Multiplication are fundamentally about making
optimal choices and efficiently managing computational resources. By exploring these
problems, this paper aims to provide a comprehensive understanding of dynamic programming
techniques and their applications. Through detailed explanations, algorithmic formulations,
and illustrative examples, we hope to demonstrate how dynamic programming can transform
complex problems into manageable tasks, leading to optimal and efficient solutions.
4
3. PROBLEM STATEMENT
1. Knapsack
The Knapsack Problem involves selecting items with given weights and values to maximize
the total value without exceeding a weight limit. In the 0/1 Knapsack Problem, each item can
either be included or excluded from the knapsack.
Problem Statement
Given:
Objective:
We use a table K where K[i][w] is the maximum value achievable with the first i items and
weight limit w.
Example
5
The maximum value achievable with a weight limit of 50 is 220, achieved by including items
2 and 3.
The Matrix Chain Multiplication involves finding the most efficient way to multiply a
sequence of matrices. The goal is to minimize the number of scalar multiplications.
Problem Statement
Given:
Objective:
We use a table mmm where m[i][j] represents the minimum number of scalar multiplications
needed to compute the matrix product Ai to Aj
6
Example
• A1:10×30
• A2:30×5
• A3:5×60
• For l=2:
o m[1][2]=10×30×5=1500
o m[2][3]=30×5×60=9000
• For l=3l = 3l=3:
o m[1][3]=min(10×30×60+1500,1500+10×5×60)=4500
The optimal way to multiply the matrices with minimal scalar multiplications is 4500,
achieved by computing (A1×A2)×A3.
7
4. PROJECT OVERVIEW
This project explores two classic applications of dynamic programming: the Knapsack Problem and
Matrix Chain Multiplication. Dynamic programming is a powerful algorithmic technique used to solve
complex problems by breaking them down into simpler subproblems and solving each subproblem just
once, storing the solutions for future reference. This project aims to provide a comprehensive
understanding of these two problems, demonstrate how dynamic programming can be applied to solve
them efficiently, and highlight their practical significance.
4.1 Objectives:
2. Knapsack Problem:
To develop and implement a dynamic programming solution for the 0/1 Knapsack Problem.
To formulate and solve the Matrix Chain Multiplication problem using dynamic programming.
4. Algorithm Analysis:
To analyze the time and space complexities of the dynamic programming solutions for both
problems.
5. Practical Applications:
To discuss the practical significance and applications of these problems in real-world scenarios.
8
6. Performance Analysis:
Analyze the performance of the developed algorithms in terms of
space and time efficiency.
Compare the efficiency of the basic dynamic programming solutions
with the optimized solutions to highlight improvements.
7. Practical Implementation:
Implement the algorithms in a suitable programming language, such as
Python or C++.
Validate the correctness and efficiency of the implementations using a
variety of test cases.
4.2 Goals:
2. Knapsack Problem:
9
• Analyze the time and space complexity of the dynamic programming solution.
5. Complexity Analysis:
• Conduct a thorough analysis of the time and space complexities of the dynamic
programming algorithms developed for both problems.
• Discuss the practical significance and applications of the Knapsack Problem and
Matrix Chain Multiplication in real-world scenarios.
• Gain insights into how dynamic programming can be utilized to solve a wide
range of optimization problems.
10
5 PROBLEM ANALYSIS AND DESCRIPTION:
The Knapsack Problem and Matrix Chain Multiplication are two classic examples that
highlight the utility of dynamic programming in solving complex optimization problems. The
0/1 Knapsack Problem requires selecting a subset of items with given weights and values to
maximize the total value without exceeding a specified weight limit. This problem can be
approached using a dynamic programming table where each entry represents the maximum
value achievable with a certain weight capacity and subset of items. The recurrence relation
helps in filling the table by deciding whether to include or exclude an item based on its weight
and value. For instance, given items with specific weights and values, and a knapsack with a
weight capacity of 50, dynamic programming can determine the optimal subset of items to
maximize the total value without exceeding the weight limit.
Matrix Chain Multiplication focuses on finding the optimal order to multiply a sequence of
matrices to minimize the total number of scalar multiplications. Since matrix multiplication is
associative, the order of operations can significantly impact the computational cost. Using
dynamic programming, a table is constructed where each entry represents the minimum
multiplication cost for multiplying a subset of matrices. The recurrence relation for this
problem involves breaking down the multiplication sequence into smaller parts and
determining the cost for each possible partition. For example, given matrices with specific
dimensions, dynamic programming can identify the optimal parenthesization to achieve the
minimum multiplication cost. Both problems illustrate the power of dynamic programming in
breaking down complex problems into manageable subproblems and solving them efficiently.
11
Matrix Chain Multiplication Solution Module:
Utility Modules:
1. Input Parsing Module: Implement functions to parse input data for both problems,
such as item weights and values for the Knapsack Problem, and matrix dimensions for
Matrix Chain Multiplication.
2. Output Formatting Module: Develop functions to format and display the results of
the dynamic programming solutions for both problems.
3. Algorithm Analysis Module: Create functions to analyze the time and space
complexities of the dynamic programming algorithms for performance evaluation.
4. Visualization Module (Optional): Optionally, develop functions to visualize the
dynamic programming tables and optimal solutions for better understanding and
debugging purposes.
Main Program:
1. Main Function: Write the main function to orchestrate the execution of the solution
modules, including input parsing, dynamic programming algorithm execution, and
output formatting.
2. User Interaction: Implement user interaction for providing input data and displaying
the results of the dynamic programming solutions.
3. Error Handling: Include error handling mechanisms to handle invalid input data and
unexpected errors gracefully.
12
5.2 CODE IMPLEMENTATION
#include <stdio.h>
#include <limits.h>
int knapsack_memoization(int idx, int capacity, int n, int weights[], int values[], int
dp[][MAX_N + 1]) {
int take = 0;
13
if (i == j) return 0;
min_cost = cost;
int main() {
printf("Press 'X' for Knapsack or '0' for Matrix Chain Multiplication: ");
14
scanf(" %c", &choice); // Use %c to read a character, add space before %c to consume any
leading whitespace
int n, W;
scanf("%d", &n);
scanf("%d", &weights[i]);
scanf("%d", &values[i]);
scanf("%d", &W);
int m;
scanf("%d", &m);
scanf("%d", &dims[i]);
dp_matrix_chain[i][j] = -1;
16
// Compute minimum cost of Matrix Chain Multiplication (memoization)
} else {
printf("Invalid choice!\n");
return 0;
17
6 OUTPUT AND RESULT:
18
18
RESULT:
The project successfully implemented solutions for both the Knapsack Problem and Matrix
Chain Multiplication using dynamic programming techniques.
Additionally, utility modules were developed for input parsing, output formatting, algorithm
analysis, and optional visualization. The main program orchestrated the execution of solution
modules, handled user interaction, and implemented error handling mechanisms for robustness
and reliability. Overall, the project successfully addressed the targeted problems and provided
comprehensive results.
6. CONCLUSION
In conclusion, this project effectively addressed both the Knapsack Problem and Matrix
Chain Multiplication using dynamic programming, yielding optimal solutions validated
through rigorous testing. The developed utility modules facilitated input parsing, output
formatting, and algorithm analysis, while the main program provided a seamless user
experience. By demonstrating the practical applicability of dynamic programming in
computational optimization, this project contributes to a deeper understanding of algorithmic
problem-solving and underscores the importance of efficient solution methodologies in
tackling complex computational challenges
19
7. References:
20