Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
Create Matrix Chain Multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
darshkantaria authored Oct 27, 2024
1 parent 5aefa58 commit 3cec92d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Beginner Level 📁/Matrix Chain Multiplication
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Matrix Chain Multiplication

Given an array `p` where `p[i]` represents the dimensions of the matrix \(A_i\) such that \(A_i\) has dimensions \(p[i-1] \times p[i]\) (for \(i \geq 1\)), find the minimum number of scalar multiplications needed to compute the product of a chain of matrices \(A_1 \times A_2 \times \ldots \times A_n\).

The goal is to determine the most efficient order of multiplying these matrices. Matrix multiplication is associative, meaning the order of multiplication can be changed to reduce the number of scalar multiplications. However, matrices can only be multiplied if the number of columns in one matrix equals the number of rows in the next.

For example, given the array `p = [10, 20, 30, 40, 30]`, matrices \(A_1\), \(A_2\), \(A_3\), and \(A_4\) would have dimensions \(10 \times 20\), \(20 \times 30\), \(30 \times 40\), and \(40 \times 30\), respectively. The goal is to find the minimum cost to compute \(A_1 \times A_2 \times A_3 \times A_4\).

## Input
- An integer array `p` of size \( n+1 \), where \( n \) is the number of matrices.

## Output
- An integer representing the minimum number of scalar multiplications needed to multiply the chain of matrices.

### Constraints:
- \( 1 \leq n \leq 100 \)
- \( 1 \leq p[i] \leq 500 \)

## Example

**Input:**
p = [10, 20, 30, 40, 30]

**Output:**
30000

**Explanation:**
The optimal order of multiplication is \(( (A_1 \times A_2) \times (A_3 \times A_4) )\), with a minimum cost of 30000 scalar multiplications.

0 comments on commit 3cec92d

Please sign in to comment.