Hacker-style Matrix Chain Multiplication visualizer
Project description
Matrix Chain Multiplication Optimizer 🔗⚡
A hacker-style interactive visualizer for the Matrix Chain Multiplication problem using Dynamic Programming. This tool demonstrates the classic algorithm with colorful terminal output, live computation visualization, and detailed step-by-step analysis.
🎯 What is Matrix Chain Multiplication?
The Matrix Chain Multiplication problem is a classic dynamic programming problem that finds the optimal way to parenthesize a chain of matrix multiplications to minimize the total number of scalar multiplications.
Example: For matrices A₁(5×4), A₂(4×6), A₃(6×2), A₄(2×7)
- Bad parenthesization:
((A₁ × A₂) × A₃) × A₄= 240 + 60 + 84 = 384 operations - Optimal parenthesization:
A₁ × ((A₂ × A₃) × A₄)= 48 + 84 + 140 = 272 operations
✨ Features
- 🎨 Colorful hacker-style interface with ASCII art banner
- 🔍 Live computation visualization showing each DP step
- 📊 Beautiful table displays for cost and split matrices
- 🎯 Optimal parenthesization output
- 📝 Two input modes: Quick paste or step-by-step interactive
- ⚡ Real-time scanning effects for enhanced user experience
🚀 Installation
Option 1: Direct Installation
# Clone the repository
git clone <repository-url>
cd matrixchain
# Install dependencies
pip install colorama
# Run the program
python -m matrixchain
Option 2: Package Installation
# Install as a package
pip install -e .
# Run from anywhere
matrixchain
🎮 Usage
Quick Start
python -m matrixchain
Input Formats
Method 1: Quick Paste
Enter matrix dimensions P as space-separated integers.
Example for matrices A1 (5x4), A2 (4x6), A3 (6x2), A4 (2x7):
5 4 6 2 7 (this is P0 P1 P2 P3 P4)
[#] Paste dims (or press Enter to input step-by-step): 5 4 6 2 7
Method 2: Interactive Input
[#] Number of matrices (n): 4
Enter rows of A1 (or P0): 5
Enter cols of A1 (or P1): 4
Enter rows of A2 (or P1): 4
Enter cols of A2 (or P2): 6
...
Example Session
__ __ _ _ _ _ _ _
| \/ | __ _| |_ _ __| | | |_ __ (_) |_(_) ___ _ __
| |\/| |/ _` | __| '__| | | | '_ \| | __| |/ _ \| '_ \
| | | | (_| | |_| | | |_| | | | | | |_| | (_) | | | |
|_| |_|\__,_|\__|_| \___/|_| |_|_|\__|_|\___/|_| |_|
Matrix Chain Multiplication Optimizer
============================================================
Dimensions P = [5, 4, 6, 2, 7] (matrices = 4)
[INFO] Initializing...
[INFO] Checking multiplication paths...
[SCAN] Enumerating chain lengths...
[SCAN] Evaluating possible splits...
[HIT] Possible optimization detected...
[INFO] Calculating minimal cost path...
[+] Starting DP computation...
[SCAN] DP[1,2] via k=1 -> cost=120
[HIT] DP[1,2] = 120 (k=1)
[SCAN] DP[2,3] via k=2 -> cost=48
[HIT] DP[2,3] = 48 (k=2)
...
[LOOT] Minimal Multiplication Cost Table (mTable)
+---------------+---------------+---------------+---------------+
| | A1 | A2 | A3 |
+---------------+---------------+---------------+---------------+
| A1 | 0 | 120 | 168 |
| A2 | -- | 0 | 48 |
| A3 | -- | -- | 0 |
+---------------+---------------+---------------+---------------+
[LOOT] Optimal Parenthesization
A1 x ((A2 x A3) x A4)
[+] Minimum multiplication cost: 272
[+] Mission Complete
🧮 Algorithm Details
The program implements the classic Dynamic Programming solution:
- State Definition:
m[i][j]= minimum cost to multiply matrices from i to j - Recurrence:
m[i][j] = min(m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1])for all k - Base Case:
m[i][i] = 0(single matrix requires no multiplication) - Reconstruction: Uses split table
s[i][j]to build optimal parenthesization
Time Complexity: O(n³) Space Complexity: O(n²)
📁 Project Structure
matrixchain/
├── matrixchain/
│ ├── __init__.py # Package initialization
│ ├── __main__.py # Module entry point
│ └── core.py # Main algorithm and UI logic
├── setup.py # Package setup configuration
├── pyproject.toml # Modern Python packaging
├── README.md # This file
└── LICENSE # MIT License
🛠️ Requirements
- Python: 3.7+
- Dependencies:
colorama- For cross-platform colored terminal output
🎨 Features Breakdown
Visual Elements
- ASCII Art Banner: Eye-catching startup screen
- Color-coded Output: Different colors for different types of information
- Progress Indicators: Live scanning effects during computation
- Formatted Tables: Professional-looking cost and split matrices
Algorithm Features
- Complete DP Implementation: Full matrix chain multiplication solver
- Parenthesization Reconstruction: Shows optimal grouping
- Step-by-step Visualization: See each DP computation in real-time
- Input Validation: Robust error handling for user input
🤝 Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📚 Educational Use
This project is perfect for:
- Algorithm Visualization: Understanding how DP works step-by-step
- Computer Science Education: Teaching optimization problems
- Interview Preparation: Classic DP problem implementation
- Performance Analysis: Comparing different parenthesizations
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
👨💻 Author
Chauhan Pruthviraj
🙏 Acknowledgments
- Classic Dynamic Programming algorithm for Matrix Chain Multiplication
- Colorama library for cross-platform terminal colors
- ASCII art generation tools
Happy optimizing! 🚀
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matrixchain-1.0.0.tar.gz.
File metadata
- Download URL: matrixchain-1.0.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
885be52e82c86cba721f81ba4b39de731763e03851424b0034874026952c158a
|
|
| MD5 |
0809a6602915ee6fa7a23b2da6d2de72
|
|
| BLAKE2b-256 |
2d299917aaaa0a4893524a44189e303c7293ac96c1c11aad76ccdea5bdebf6bc
|
File details
Details for the file matrixchain-1.0.0-py3-none-any.whl.
File metadata
- Download URL: matrixchain-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba59103511733af95dfe542d702b06b0042dd81b31881d10c38865d475722511
|
|
| MD5 |
7f0f82e1dfeb692c99b6d376d6341fd3
|
|
| BLAKE2b-256 |
f91f2b4ce2aa1578f024a03942a184818937823c49e8b89882df638bb07e6d58
|