The project has been restructured into separate, modular files for better maintainability and code organization.
cash-flow-minimizer/
├── index.html # Main HTML file (clean structure)
├── styles.css # All CSS styling
├── README.md # This file
└── js/
├── data-structures.js # Data structure implementations
├── app-state.js # Global application state
├── algorithms.js # Core optimization algorithms
└── ui-functions.js # DOM manipulation & event handlers
- Purpose: Clean HTML structure and semantic markup
- Contents:
- Document meta tags and links to external resources
- Page sections (Home, Algorithm, Code, Complexity, Implementation)
- HTML form elements and containers
- Script references to load JavaScript modules in correct order
- Size: Significantly reduced (~600 lines vs. 2300+ original)
- Purpose: All visual styling and animations
- Contains:
- CSS resets and global styles
- Layout and component styles (cards, buttons, forms)
- Animations and transitions
- Responsive media queries
- Color schemes and themes
- Size: ~1000 lines
- Purpose: Custom data structure implementations
- Classes:
- MinHeap - Min priority queue with O(log n) operations
- MaxHeap - Max priority queue (extends MinHeap)
- Graph - Adjacency list graph for transaction relationships
- Stack - LIFO stack for undo/redo operations
- StateManager - Manages undo/redo state history
- Key Features:
- O(log n) heap operations (push, pop, peek)
- Connected component detection
- Complete state serialization for undo/redo
- Purpose: Global application state initialization
- Variables:
participants- Array of participant namesparticipantSet- Hash Set for O(1) lookupstransactions- Array of transaction objectsnetBalance- Map of participant balancesminimizedTransactionsData- Optimized settlement transactionstransactionGraph- Graph of relationshipsstateManager- Undo/redo manager instance
- Size: Minimal (~15 lines)
- Purpose: Core optimization algorithms
- Functions:
- minimizeTransactions() - Main O(n log n) optimization algorithm
- displayGraphAnalysis() - Shows connected components
- renderMinimizedTransactions() - Displays results
- renderStatistics() - Shows performance metrics
- Algorithm: Greedy algorithm using actual heaps for O(n log n) complexity
- Purpose: All DOM manipulation and user interaction
- Functions:
- showSection() - Navigation between pages
- addParticipant() / removeParticipant() - Participant management
- addTransaction() - Transaction entry
- calculateBalances() / renderBalances() - Balance display
- undoAction() / redoAction() - Undo/redo functionality
- showVisualization() - Chart visualization
- reset() - Clear all data
- Total: ~400 lines of pure UI logic
Each file has a single, clear responsibility:
- Data structures are separate from UI
- Algorithms are independent of rendering
- State management is centralized
- Finding and fixing bugs is faster
- Changes to styling don't affect logic
- Reduced cognitive load when reading code
- Each file is focused and concise
- Function purposes are clear from context
- No massive 2300+ line files
- Data structures can be used in other projects
- Algorithms can be tested independently
- UI functions can be refactored without affecting core logic
- Easy to add new features
- Simple to create new visualization modules
- Can add more algorithms without bloating existing files
Scripts must load in this specific order for proper functionality:
- data-structures.js - Define MinHeap, MaxHeap, Graph, Stack, StateManager
- app-state.js - Initialize global variables (uses classes from step 1)
- algorithms.js - Define algorithm functions (uses classes and state from steps 1-2)
- ui-functions.js - Define UI functions (uses everything from steps 1-3)
This order is already configured in index.html.
| File | Lines | Purpose |
|---|---|---|
| index.html | ~600 | HTML structure |
| styles.css | ~1000 | CSS styling |
| data-structures.js | ~200 | Data structure implementations |
| app-state.js | ~15 | Global state |
| algorithms.js | ~80 | Core algorithms |
| ui-functions.js | ~400 | UI & event handling |
| Total | ~2300 | Complete application |
- Open
index.htmlin a web browser - All external resources (CSS, JS) will load automatically
- No build process or compilation required
- Determine which file it belongs in
- Add the function/class to the appropriate module
- If it needs UI, add handlers to
ui-functions.js - If it needs styling, add CSS to
styles.css
- Create the class in
data-structures.js - Initialize instances in
app-state.jsif globally needed - Use in algorithms or UI as needed
- Open browser DevTools (F12)
- Check Console tab for errors
- Each module's functions are globally accessible
- State can be inspected via
participants,transactions, etc.
- ✅ Clear variable naming conventions
- ✅ Proper code organization
- ✅ Efficient algorithms (O(n log n) vs O(n² log n))
- ✅ Comprehensive comments in data-structures.js
- ✅ Responsive design maintained
- ✅ All features working as before
- Participant lookup: O(n) → O(1) using Hash Set
- Heap operations: O(n log n) sorting → O(log n) per operation
- Memory: Cleaner, more focused modules
When user clicks "Optimize Transactions":
ui-functions.jscallsminimizeTransactions()algorithms.jsuses MinHeap/MaxHeap fromdata-structures.js- Accesses global state from
app-state.js - Saves state to
stateManagerfor undo/redo - Calls rendering functions back in
ui-functions.js
This clear separation makes the flow easy to understand and modify.