The interactive, production-ready implementation of Dijkstra's deadlock avoidance algorithm.
Built with modern web technologies for educational and research purposes
Note
This is a comprehensive, interactive implementation of Dijkstra's Banker's Algorithm for deadlock avoidance in operating systems. Built with modern web technologies, it provides real-time visualization, step-by-step algorithm execution, and comprehensive testing capabilities.
Perfect for: Computer Science education, operating systems courses, research demonstrations, and understanding resource allocation in concurrent systems.
The Banker's Algorithm Simulator provides the fastest path from theory to practical understanding, offering interactive visualization, real-time safety checking, and comprehensive algorithm analysis.
The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra. This simulator makes understanding and experimenting with the algorithm simple, with real-time visualization, step-by-step execution, and comprehensive validation.
// Core algorithm implementation
const calculator = new BankersAlgorithmCalculator();
const safetyResult = calculator.checkSafety(available, allocation, need);
if (safetyResult.isSafe) {
console.log(`Safe sequence: ${safetyResult.safeSequence.join(' → ')}`);
} else {
console.log('System is in unsafe state - potential deadlock!');
}- What is the Banker's Algorithm?
- Why This Implementation?
- Features
- Quick Start
- Installation
- Usage
- Core Components
- Algorithm Implementation
- Keyboard Shortcuts
- Testing
- Architecture
- Deployment
- Contributing
- Developer Information
The Banker's Algorithm is a deadlock avoidance algorithm used in operating systems to ensure that resource allocation never leads to a deadlock state. Developed by Edsger Dijkstra in 1965, it works by:
- Resource Allocation: Managing finite resources among multiple processes
- Safety Checking: Ensuring the system can always find a safe execution sequence
- Request Processing: Evaluating resource requests before granting them
- Deadlock Prevention: Avoiding states that could lead to circular waiting
The algorithm simulates a banker who lends money (resources) to customers (processes) while ensuring they can always collect all loans back.
- Safe State: A state where there exists at least one sequence of process execution that allows all processes to complete
- Unsafe State: A state that may lead to deadlock (but doesn't guarantee it)
- Need Matrix:
Need[i][j] = Max[i][j] - Allocation[i][j]- remaining resource requirements - Safe Sequence: An ordering of processes that can all complete without deadlock
This simulator handles all the complex algorithmic details while providing an intuitive interface for learning and experimentation.
🚀 Interactive: Real-time visualization with step-by-step algorithm execution
🎓 Educational: Perfect for computer science courses and self-learning
🔍 Comprehensive: Complete implementation with validation and error handling
🧪 Tested: 31+ test cases covering edge cases and classical examples
💻 Modern: Built with Next.js 16, TypeScript 5, and React 19
📱 Responsive: Works seamlessly on desktop, tablet, and mobile devices
🎨 Beautiful UI: Dark/light mode with smooth animations using Framer Motion
⌨️ Keyboard Shortcuts: Efficient navigation and control for power users
- Safety Algorithm: Complete implementation of Dijkstra's safety checking algorithm
- Resource Request Processing: Handles resource allocation requests with full validation
- Step-by-Step Visualization: Shows each algorithm step with detailed explanations
- Multiple System States: Support for 1-10 processes and 1-10 resource types
- Process Completion: Simulate processes finishing and releasing resources
- System Validation: Comprehensive validation of all system constraints
- 🎯 Real-time Matrix Editing: Modify allocation, maximum, and available resources
- 🔄 Dynamic System Sizing: Adjust number of processes and resources on the fly
- 📊 Request Simulation: Submit resource requests and see immediate results
- 🎨 Visual Feedback: Color-coded results and animated state transitions
- 📱 Touch-Friendly: Optimized for mobile with swipe gestures
- 🌓 Dark/Light Mode: Automatic theme switching with system preference detection
- 📖 Algorithm Steps: Detailed breakdown of each algorithm iteration
- 🔢 Step Numbering: Clear step numbers (1-4) matching textbook algorithms
- ✅ Safety Sequence Display: Clear visualization of safe execution order
- ❌ Error Explanations: Comprehensive error messages for invalid states
- 📚 Classical Examples: Pre-loaded textbook examples for learning
- 🎓 Detailed Documentation: Complete algorithm explanation in REPORT.md
- ⌨️ Keyboard Shortcuts: Efficient navigation (Cmd/Ctrl+[, Cmd/Ctrl+D, Shift+Enter)
- 🔔 Toast Notifications: Beautiful animated notifications for all actions
- 💾 State Persistence: Maintains system state during navigation
- 🎭 Animated UI: Smooth transitions using Framer Motion
- 🧪 Comprehensive Testing: 31+ test cases with 100% coverage of core logic
- 📊 System Statistics: Track resource utilization and process completion
- Node.js 18+ and npm/yarn/pnpm
- Modern web browser (Chrome, Firefox, Safari, Edge)
# Clone the repository
git clone https://github.com/AlphsX/Banker-s-AlgorithmX.git
cd bankers-algorithm-simulator/frontend
# Install dependencies
npm install
# Start development server
npm run devOpen http://localhost:3000 in your browser.
- Explore Default Example: The app loads with a safe system state
- Check Safety: Click "Check Safety" or press
Shift+Enter - Modify Values: Click any cell in the matrices to edit
- Submit Request: Use the request panel to test resource allocation
- View Steps: Scroll down to see detailed algorithm execution
The system starts with a default safe state:
// Default configuration (2 processes, 3 resources)
Processes: 2 (P0, P1)
Resources: 3 (A, B, C)
Allocation Matrix:
P0: [1, 0, 0] // P0 currently has: A=1, B=0, C=0
P1: [0, 1, 0] // P1 currently has: A=0, B=1, C=0
Max Matrix:
P0: [2, 1, 1] // P0 maximum needs: A=2, B=1, C=1
P1: [1, 2, 1] // P1 maximum needs: A=1, B=2, C=1
Available: [2, 2, 3] // Available resources: A=2, B=2, C=3
Need Matrix (calculated automatically):
P0: [1, 1, 1] // [2,1,1] - [1,0,0] = [1,1,1]
P1: [1, 1, 1] // [1,2,1] - [0,1,0] = [1,1,1]Click "Check Safety" or press Shift+Enter to run the safety algorithm:
Step 1: Initialize Work = [2, 2, 3], Finish = [false, false]
Step 2: Check P0: Need[P0] = [1,1,1] ≤ Work = [2,2,3] [PASS]
Step 3: P0 finishes: Work = [2,2,3] + [1,0,0] = [3,2,3]
Step 2: Check P1: Need[P1] = [1,1,1] ≤ Work = [3,2,3] [PASS]
Step 3: P1 finishes: Work = [3,2,3] + [0,1,0] = [3,3,3]
Step 4: All processes finished [PASS] Safe Sequence: P0 → P1
- Select a process from the dropdown
- Enter requested resources for each type
- Click "Submit Request"
- View the detailed validation and safety check
Example request:
Process: P0
Request: [1, 0, 0] // P0 requests 1 unit of resource A
Validation:
[PASS] Step 1: Request ≤ Need ([1,0,0] ≤ [1,1,1])
[PASS] Step 2: Request ≤ Available ([1,0,0] ≤ [2,2,3])
[PASS] Step 3: Simulate allocation
[PASS] Step 4: System remains safe → REQUEST GRANTEDUse the controls in the sidebar to:
- Process Count: 1-10 processes
- Resource Count: 1-10 resource types
- Available Resources: Set available units for each resource type
- Reset Button: Clears all values while preserving counts
- Logo Click: Reloads the default safe example
- Keyboard: Press
Cmd/Ctrl+Nto reset
The central class implementing Dijkstra's algorithm with comprehensive validation and step tracking.
import { BankersAlgorithmCalculator } from '@/lib/bankers-algorithm-calculator';
const calculator = new BankersAlgorithmCalculator();
// Check system safety
const safetyResult = calculator.checkSafety(available, allocation, need);
// Returns: { isSafe, safeSequence, steps, finalFinishState }
// Process resource request
const requestResult = calculator.processRequest(request, currentState);
// Returns: { canGrant, newState?, errorMessage?, simulationSteps? }
// Validate system state
const errors = calculator.validateSystemState(state);
// Returns: ValidationError[]
// Complete a process (release resources)
const newState = calculator.completeProcess(state, processId);
// Get system snapshot with statistics
const snapshot = calculator.getSystemSnapshot(state);interface BankersAlgorithmState {
processCount: number;
resourceCount: number;
allocation: number[][];
max: number[][];
available: number[];
need: number[][];
finish: boolean[];
safeSequence: string[];
algorithmSteps: AlgorithmStep[];
isCalculating: boolean;
isSafe?: boolean;
lastUpdated?: Date;
}
interface AlgorithmStep {
stepNumber: number | string;
description: string;
workVector: number[];
processChecked?: string;
canFinish?: boolean;
isHighlighted?: boolean;
}
interface ResourceRequest {
processId: number;
requestVector: number[];
}Comprehensive utility functions for matrix operations:
import {
calculateNeedMatrix,
isVectorLessOrEqual,
addVectors,
cloneMatrix,
validateMatrixValues,
validateAllocationConstraints,
} from '@/utils/matrix-utils';
// Calculate Need = Max - Allocation
const need = calculateNeedMatrix(max, allocation);
// Check if vector a ≤ vector b (component-wise)
const canAllocate = isVectorLessOrEqual(request, available);
// Add two vectors
const newWork = addVectors(work, allocation[i]);
// Validate matrix constraints
const errors = validateAllocationConstraints(allocation, max);Based on Dijkstra's original algorithm with optimized implementation:
/**
* Safety Algorithm Steps:
* 1. Initialize Work = Available and Finish[i] = false for all processes
* 2. Find process Pi where Finish[i] = false and Need[i] ≤ Work
* 3. If found: Work = Work + Allocation[i], Finish[i] = true, repeat step 2
* 4. If all Finish[i] = true, system is safe; otherwise unsafe
*/
checkSafety(available: number[], allocation: number[][], need: number[][]): SafetyResultImplementation Details:
- Uses work vector to simulate available resources
- Iterates through processes to find those that can complete
- Simulates resource release when process completes
- Tracks detailed steps for educational visualization
- Returns safe sequence if system is safe
Comprehensive request processing with safety verification:
/**
* Resource Request Steps:
* 1. Check if Request[i] ≤ Need[i] (doesn't exceed declared maximum)
* 2. Check if Request[i] ≤ Available (resources are available)
* 3. Temporarily allocate resources and check if resulting state is safe
* 4. If safe, grant request; otherwise, deny and rollback
*/
processRequest(request: ResourceRequest, currentState: BankersAlgorithmState): RequestResultImplementation Details:
- Validates request against declared maximum needs
- Checks resource availability
- Simulates allocation without committing changes
- Runs safety algorithm on simulated state
- Commits changes only if safe, otherwise rolls back
Comprehensive validation of all system constraints:
// Validates entire system state
validateSystemState(state: BankersAlgorithmState): ValidationError[]
// Validates system data integrity
validateSystemData(state: BankersAlgorithmState): ValidationError[]
// Validates matrix values (non-negative integers)
validateMatrixValues(matrix: number[][]): ValidationError[]
// Validates allocation constraints (Allocation ≤ Max)
validateAllocationConstraints(allocation: number[][], max: number[][]): ValidationError[]Power user features for efficient navigation and control:
| Shortcut | Action | Description |
|---|---|---|
Cmd/Ctrl + [ |
Toggle Sidebar | Show/hide the control sidebar |
Cmd/Ctrl + D |
Toggle Theme | Switch between dark and light mode |
Shift + Enter |
Check Safety | Run the safety algorithm |
Platform-specific:
- Mac: Use
Cmdkey - Windows/Linux: Use
Ctrlkey
The project includes comprehensive test coverage with 31+ test cases:
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
# Run specific test file
npm test -- --testPathPattern=bankers-algorithmSafety Algorithm Tests (6 tests)
- Classical textbook examples
- Safe state identification
- Unsafe state detection
- Detailed step tracking
Resource Request Tests (9 tests)
- Request validation
- Availability checking
- Safety verification
- Grant/deny logic
Process Completion Tests (3 tests)
- Resource release simulation
- State updates
- Completion validation
System Validation Tests (4 tests)
- Matrix dimension validation
- Value range checking
- Constraint validation
- Error reporting
Additional Tests (9 tests)
- System snapshots and statistics
- Algorithm step numbering
- Safe sequence finding
- Matrix resizing
npm run test:coverageExpected coverage:
- Statements: >95%
- Branches: >90%
- Functions: >95%
- Lines: >95%
bankers-algorithm-simulator/
├── frontend/
│ ├── src/
│ │ ├── app/ # Next.js app router
│ │ │ ├── layout.tsx # Root layout with theme provider
│ │ │ ├── page.tsx # Main algorithm interface
│ │ │ └── globals.css # Global styles and CSS variables
│ │ ├── components/
│ │ │ ├── bankers-algorithm/ # Algorithm-specific components
│ │ │ │ ├── AlgorithmTable.tsx # Matrix display and editing
│ │ │ │ ├── SystemControls.tsx # Process/resource controls
│ │ │ │ ├── StepByStepResults.tsx # Algorithm step visualization
│ │ │ │ ├── RequestPanel.tsx # Resource request interface
│ │ │ │ ├── ProcessControl.tsx # Process count control
│ │ │ │ ├── ResourceControl.tsx # Resource count control
│ │ │ │ ├── AvailableResourcesInput.tsx
│ │ │ │ └── AnimatedFinishBadge.tsx
│ │ │ ├── ui/ # Reusable UI components
│ │ │ │ ├── toast.tsx # Toast notification system
│ │ │ │ ├── loading-screen.tsx # Loading state
│ │ │ │ └── browser-compatibility-warning.tsx
│ │ │ └── magicui/ # Enhanced UI components
│ │ │ └── AnimatedThemeToggler.tsx
│ │ ├── lib/ # Core algorithm implementation
│ │ │ ├── bankers-algorithm-calculator.ts # Main algorithm
│ │ │ └── __tests__/ # Test files
│ │ │ └── bankers-algorithm-enhanced.test.ts
│ │ ├── types/ # TypeScript type definitions
│ │ │ └── bankers-algorithm.ts # Core types and interfaces
│ │ ├── utils/ # Utility functions
│ │ │ └── matrix-utils.ts # Matrix operations
│ │ ├── hooks/ # Custom React hooks
│ │ │ ├── useDarkMode.ts # Theme management
│ │ │ ├── useKeyboardShortcuts.ts # Keyboard shortcuts
│ │ │ ├── useSwipeGesture.ts # Touch gestures
│ │ │ ├── useDynamicFavicon.ts # Favicon theming
│ │ │ ├── useAppLoading.ts # Loading state
│ │ │ ├── useMediaQuery.ts # Responsive utilities
│ │ │ └── useIdleDetection.ts # Idle detection
│ │ └── styles/ # Additional stylesheets
│ │ └── animations.css # Custom animations
│ ├── public/ # Static assets
│ ├── jest.config.js # Jest configuration
│ ├── jest.setup.js # Jest setup
│ ├── tailwind.config.ts # Tailwind configuration
│ ├── tsconfig.json # TypeScript configuration
│ └── package.json # Dependencies
└── README.md # This file
| Technology | Version | Purpose |
|---|---|---|
| Next.js | 16.0+ | React framework with app router |
| React | 19.1 | UI library |
| TypeScript | 5+ | Type-safe development |
| Tailwind CSS | 3.4+ | Utility-first styling |
| Framer Motion | 12+ | Smooth animations |
| Jest | 30+ | Testing framework |
| React Testing Library | 16+ | Component testing |
| Zustand | 5+ | State management |
| Lucide React | Latest | Icon library |
- Component Composition: Modular, reusable components
- Custom Hooks: Encapsulated logic for reusability
- Type Safety: Comprehensive TypeScript types
- Separation of Concerns: Clear separation between UI and logic
- Test-Driven Development: Tests written alongside features
cd frontend
npm install
npm run devStarts the development server at http://localhost:3000 with:
- Hot module replacement
- Fast refresh
- TypeScript checking
- ESLint validation
# Build optimized production bundle
npm run build
# Start production server
npm startThe build process:
- Optimizes and minifies code
- Generates static assets
- Creates production-ready bundle
- Enables performance optimizations
For static hosting (GitHub Pages, Netlify, Vercel):
npm run build
# Output in .next/ directoryFROM node:18-alpine AS base
# Install dependencies
FROM base AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
# Build application
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
# Production image
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]Build and run:
docker build -t bankers-algorithm .
docker run -p 3000:3000 bankers-algorithmCreate .env.local for local development:
# Optional: Analytics
NEXT_PUBLIC_GA_ID=your-ga-id
# Optional: Error tracking
NEXT_PUBLIC_SENTRY_DSN=your-sentry-dsnContributions are welcome! This project follows standard open-source practices.
-
Fork the Repository
# Fork on GitHub, then clone your fork git clone https://github.com/yourusername/bankers-algorithm-simulator.git cd bankers-algorithm-simulator/frontend
-
Create a Branch
git checkout -b feature/your-feature-name
-
Install Dependencies
npm install
-
Make Changes
- Write clean, documented code
- Follow existing code style
- Add tests for new features
- Update documentation as needed
-
Test Your Changes
# Run tests npm test # Check linting npm run lint # Build to verify npm run build
-
Commit and Push
git add . git commit -m "feat: add your feature description" git push origin feature/your-feature-name
-
Create Pull Request
- Open a PR on GitHub
- Describe your changes clearly
- Link any related issues
- Wait for review
TypeScript
- Use strict type checking
- Avoid
anytypes - Document complex types
- Use interfaces for objects
React Components
- Use functional components
- Implement proper prop types
- Use hooks appropriately
- Keep components focused
Testing
- Write tests for new features
- Maintain >90% coverage
- Test edge cases
- Use descriptive test names
Documentation
- Update README for new features
- Add JSDoc comments
- Include usage examples
- Document breaking changes
Follow Conventional Commits:
feat: add new feature
fix: bug fix
docs: documentation changes
style: formatting changes
refactor: code refactoring
test: add or update tests
chore: maintenance tasks
- 🐛 Bug Fixes: Report and fix bugs
- ✨ Features: Add new algorithm features
- 📚 Documentation: Improve docs and examples
- 🧪 Tests: Increase test coverage
- 🎨 UI/UX: Enhance user interface
- ♿ Accessibility: Improve accessibility
- 🌐 i18n: Add internationalization
- ⚡ Performance: Optimize performance
The Banker's Algorithm was developed by Edsger Dijkstra in 1965 as part of his work on the THE multiprogramming system. It's named after the way bankers manage loans to ensure they can always meet withdrawal demands.
Safe State Definition: A state is safe if there exists a sequence of processes ⟨P₁, P₂, ..., Pₙ⟩ such that for each Pᵢ, the resources that Pᵢ can still request can be satisfied by the currently available resources plus the resources held by all Pⱼ where j < i.
Key Theorem: If a system is in a safe state, no deadlock can occur. If a system is in an unsafe state, deadlock may occur (but is not guaranteed).
- Operating Systems: Process scheduling and resource management
- Database Systems: Transaction management and lock allocation
- Cloud Computing: Virtual machine resource allocation
- Network Systems: Bandwidth and connection management
- Manufacturing: Production line resource scheduling
- Time Complexity: O(m × n²) where m = resources, n = processes
- Space Complexity: O(m × n) for storing matrices
- Worst Case: Must check all processes in each iteration
- Requires advance knowledge of maximum resource needs
- Number of processes must be fixed
- Resources must be fixed and known
- Processes must eventually release resources
- Not practical for systems with dynamic resource requirements
- Deadlock Detection: Detects existing deadlocks
- Wait-Die/Wound-Wait: Timestamp-based deadlock prevention
- Resource Allocation Graph: Visual deadlock detection
- Two-Phase Locking: Database transaction management
- Dijkstra, E. W. (1965). "Cooperating sequential processes"
- Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). "Operating System Concepts"
- Tanenbaum, A. S. (2014). "Modern Operating Systems"
- GeeksforGeeks: Banker's Algorithm
Senior Full-Stack Developer specializing in Operating Systems, Algorithms, and Educational Technology
Core Competencies:
- 🎓 Operating Systems & Concurrent Programming
- 🧮 Algorithm Design & Analysis
- 💻 Modern Web Technologies (React, Next.js, TypeScript)
- 📚 Educational Software Development
- ⚡ Performance Optimization & Testing
- 🏗️ System Architecture & Design Patterns
Technology Stack:
- Frontend: React 19, Next.js 16, TypeScript 5, Tailwind CSS
- State Management: Zustand, React Hooks
- Animation: Framer Motion, CSS Animations
- Testing: Jest 30, React Testing Library
- Build Tools: Webpack, Turbopack, SWC
- Development: ESLint, Prettier, Git
Specializations:
- Deadlock avoidance algorithms and resource allocation
- Interactive algorithm visualization
- Real-time system simulation
- Educational tool development
- Responsive and accessible UI design
- Comprehensive testing strategies
This project represents the intersection of theoretical computer science and practical software engineering. The goal is to make complex operating system concepts accessible through:
- Interactive Learning: Hands-on experimentation with real algorithms
- Visual Understanding: Step-by-step visualization of algorithm execution
- Production Quality: Professional-grade code and architecture
- Open Source: Free and accessible to students worldwide
- Best Practices: Modern development standards and patterns
- Test-Driven Development: 31+ comprehensive test cases
- Type Safety: Full TypeScript coverage with strict mode
- Documentation: Extensive inline comments and external docs
- Accessibility: WCAG 2.1 AA compliance
- Performance: Optimized rendering and state management
- Maintainability: Clean code principles and SOLID design
Committed to creating high-quality educational tools that bridge the gap between academic theory and practical implementation. This project serves as:
- 📖 A learning resource for computer science students
- 🔬 A research tool for algorithm analysis
- 🎓 A teaching aid for operating systems courses
- 💡 A reference implementation of Dijkstra's algorithm
- 🌍 A contribution to the open-source education community
- GitHub: @AlphsX
Special thanks to:
- Edsger Dijkstra for the original algorithm
- Operating Systems community for educational resources
- Open source contributors for inspiration and tools
- Computer Science educators for feedback and suggestions
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2025 [AlphsX]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
⭐ Star this repository if you find it helpful!
Made with ❣️ for the computer science education community
© 2025 AlphsX, Inc.