Imagine a weekend trip with friends, a college event, or a shared apartment where everyone pays for different things: hotel, food, tickets, fuel, and activities. By the end, the payment network becomes messy:
- Aarav owes Meera.
- Meera owes Kabir.
- Kabir owes Riya.
- Riya owes Aarav.
- Some people are both debtors and creditors at the same time.
Instead of settling every original debt one by one, this project computes each participant's net balance and generates the minimum number of final settlement transactions.
The goal is to transform a dense directed debt graph into a compact settlement graph.
Before optimization: many pairwise debts
After optimization : only essential payments remain
- ๐งพ Add, view, undo, reset, and export transactions
- ๐ Real-time net balance calculation
- โก Greedy optimization using two
priority_queueheaps - ๐งญ DFS/BFS graph traversal helpers
- ๐ Circular debt detection such as
A โ B โ C โ A - ๐งฉ Independent group counting for disconnected components
- ๐ฅ๏ธ Menu-driven console UI with structured output
- ๐งช Simulation mode: compare before vs after minimization
- ๐ Performance metrics: original transactions vs optimized settlements
- ๐งผ Input validation for names, amounts, and menu choices
Every transaction is represented as a directed graph edge:
A owes B โน100
So the balance update becomes:
A = A - 100
B = B + 100
After processing all transactions:
| Balance Type | Meaning |
|---|---|
| Negative | Person is a debtor |
| Positive | Person is a creditor |
| Zero | Person is already settled |
The optimizer ignores zero balances and directly matches the largest debtor with the largest creditor.
- Read all original transactions.
- Build net balances for every participant.
- Push creditors into a max-heap.
- Push debtors into a max-heap based on absolute debt value.
- Repeatedly match:
- largest debtor
- largest creditor
- Settle the minimum possible amount between them.
- Push remaining balance back into the respective heap.
- Stop when both heaps are empty.
At each iteration, the algorithm chooses the participant who owes the most and the participant who should receive the most.
settledAmount = min(abs(maxDebtor.balance), maxCreditor.balance);This works efficiently because each settlement fully clears at least one participant:
- The debtor becomes zero, or
- The creditor becomes zero, or
- Both become zero
That means every transaction reduces the active problem size.
Cash-Flow-Minimizer/
โ
โโโ main.cpp # Application entry point
โโโ Models.h # Transaction, Settlement, Result models
โ
โโโ UI.h
โโโ UI.cpp # Menu, input prompts, tables, CLI rendering
โ
โโโ CashFlow.h
โโโ CashFlow.cpp # Net balance + heap-based minimization logic
โ
โโโ Graph.h
โโโ Graph.cpp # DFS, BFS, cycle detection, components
โ
โโโ Utils.h
โโโ Utils.cpp # Validation, formatting, ANSI helpers
โ
โโโ .vscode/
โ โโโ tasks.json # VS Code build task
โ
โโโ assets/
โโโ problem_graph.svg
โโโ optimized_settlement.svg
โโโ algorithm_pipeline.svg
โโโ cli_preview.svg
Starts the application and delegates control to the console UI.
Handles the user-facing layer:
- Menu rendering
- User input
- Tables and formatted output
- Step-by-step settlement display
- Simulation and export options
Handles business logic:
- Transaction storage
- Undo stack
- Net balance calculation
- Greedy heap settlement
- Exporting final results
Handles graph-related analysis:
- Adjacency list creation
- BFS traversal
- DFS traversal
- Circular debt detection
- Independent group counting
Contains reusable utilities:
- Input validation
- Amount formatting
- Console separators
- ANSI color helpers
Aarav -> Meera : โน1200
Meera -> Kabir : โน700
Kabir -> Riya : โน950
Riya -> Aarav : โน500
Aarav -> Kabir : โน650
Riya -> Kabir : โน400
Meera -> Riya : โน300
Kabir -> Meera : โน250
# Payer Receiver Amount
-------------------------------------------
1 Aarav Meera โน1200
2 Aarav Kabir โน250
3 Riya Kabir โน550
Transactions before optimization : 8
Transactions after optimization : 3
Transactions reduced : 5
Reduction percentage : 62.50%
========================================================================
Cash Flow Minimization
========================================================================
1. Add Transaction
2. View All Transactions
3. View Net Balances
4. Minimize Cash Flow
5. Show Settlement Steps
6. Simulation Mode
7. Graph Analysis
8. Export Final Settlements
9. Undo Last Transaction
10. Reset System
11. Exit
------------------------------------------------------------------------
Choose an option: 1
========================================================================
Add Transaction
========================================================================
Debtor name : A
Creditor name: B
Amount owed : 100
Transaction added successfully.
ID Name Status Balance
------------------------------------------------------------------------
0 A Owes 100.00
1 B Receives 100.00
Sample Run
A owes B โน100
B owes C โน50
C owes A โน30
Net balances:
A = -70
B = +50
C = +20
Optimized settlement:
# Payer Receiver Amount
------------------------------------------------------------------------
1 A B 50.00
2 A C 20.00
Transactions before optimization : 3
Transactions after optimization : 2
Transactions reduced : 1
Step-by-step mode:
Step 1: choose largest debtor A (owes 70.00) and largest creditor B (receives 50.00).
Settle 50.00. Remaining debtor liability: 20.00, creditor claim: 0.00.
Step 2: choose largest debtor A (owes 20.00) and largest creditor C (receives 20.00).
Settle 20.00. Remaining debtor liability: 0.00, creditor claim: 0.00.
A pays B โน50
A pays C โน20
The circular chain is removed because the algorithm settles based on net balance instead of blindly preserving every original edge.
| Edge Case | Handling |
|---|---|
| Zero-balance participant | Ignored during heap settlement |
| Already settled system | Returns no settlement transactions |
| Circular debt | Reduced using net balance calculation |
| Duplicate transactions | Treated as valid separate records |
| Self-debt | Rejected by validation |
| Invalid amount | Re-prompted until valid input is given |
| Disconnected groups | Graph helper can count independent components |
| Large input | Heap-based approach keeps optimization efficient |
Let:
N= number of participantsE= number of original transactionsK= number of non-zero-balance participantsT= number of optimized settlement transactions
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Net balance calculation | O(E) |
O(N) |
| Heap construction | O(K log K) |
O(K) |
| Greedy settlement | O(T log K) |
O(T) |
| DFS / BFS traversal | O(N + E) |
O(N + E) |
| Cycle detection | O(N + E) |
O(N) |
Overall settlement complexity:
O(E + K log K + T log K)
| Approach | Behavior | Drawback / Benefit |
|---|---|---|
| Naive settlement | Preserve original debt edges | May require many unnecessary payments |
| Net balance approach | Collapse all incoming/outgoing money | Removes redundant cycles |
| Heap-based greedy | Match largest debtor with largest creditor | Efficient and easy to explain in interviews |
- Install a C++ compiler such as
g++through MinGW-w64. - Make sure
g++is available in your system PATH. - Open the project folder in VS Code.
- Press:
Ctrl + Shift + B
- Run the generated executable.
.\cashflow.exeg++ -std=c++17 -Wall -Wextra -O2 main.cpp UI.cpp CashFlow.cpp Graph.cpp Utils.cpp -o cashflow
./cashflowFor Windows PowerShell:
g++ -std=c++17 -Wall -Wextra -O2 main.cpp UI.cpp CashFlow.cpp Graph.cpp Utils.cpp -o cashflow.exe
.\cashflow.exe1. Add Transaction
2. View All Transactions
3. View Net Balances
4. Minimize Cash Flow
5. Show Settlement Steps
6. Simulation Mode
7. Graph Analysis
8. Export Final Settlements
9. Undo Last Transaction
10. Reset System
11. Exit
The application can export final settlements to a text file:
settlements.txt
Example:
Final Optimized Settlements
-------------------------------------------
Aarav pays Meera โน1200
Aarav pays Kabir โน250
Riya pays Kabir โน550
This project demonstrates:
- Graph modeling
- Greedy algorithms
- Priority queues / heaps
- DFS and BFS traversal
- Cycle detection
- Modular C++ design
- Console UI/UX
- Real-world financial problem solving
- Clean separation of concerns
- JSON or CSV import/export
- Unit tests using GoogleTest
- Persistent transaction database
- GUI version using Qt
- Web dashboard version
- Multi-currency support
- Payment-mode constraints such as UPI, card, wallet, bank transfer
- Weighted settlement preferences based on transaction limits or trust score
After building this project, you will understand:
- How to convert real-world debt relationships into a graph
- Why net balance removes redundant circular payments
- How heaps improve greedy selection efficiency
- How to design modular C++ applications
- How to present an algorithmic project professionally on GitHub