Skip to content

A comprehensive Python application built with Tkinter that provides interactive visualizations of core algorithms and data structures. The visualizer features smooth 60 FPS animations, real-time complexity tracking, and educational pseudocode display.

Notifications You must be signed in to change notification settings

IslamHesham-Dev/Python-algorithm-visualizer

Repository files navigation

Interactive Algorithm Visualizer

A comprehensive Python application built with Tkinter that provides interactive visualizations of core algorithms and data structures. The visualizer features smooth 60 FPS animations, real-time complexity tracking, and educational pseudocode display.

Features

Core Algorithms Implemented

  • Binary Search - Efficient search in sorted arrays with step-by-step pointer tracking
  • Bubble Sort - Classic sorting algorithm with swap animations and early termination detection
  • Quick Sort - Divide-and-conquer sorting with pivot highlighting and partition visualization
  • Dijkstra's Algorithm - Shortest path finding with distance updates and path reconstruction
  • BFS Maze Solver - Breadth-first search for maze solving with path tracking

Technical Features

  • Smooth 60 FPS Animations - Optimized canvas updates with double buffering
  • Real-time Complexity Counters - Live tracking of comparisons, swaps, and visits
  • Speed Control - Adjustable animation speed from 1-100 steps per second
  • Interactive Controls - Pause, resume, reset, and step-through functionality
  • Data Generation - Random, sorted, and reverse-sorted data options
  • Pseudocode Display - Real-time algorithm pseudocode with current line highlighting
  • Complexity Analysis - Big O notation display for time and space complexity
  • Keyboard Shortcuts - Quick access to common functions

Installation

Prerequisites

  • Python 3.7 or higher
  • Tkinter (usually included with Python)
  • NumPy (for mathematical operations)
  • Matplotlib (for advanced visualizations)

Setup Instructions

  1. Clone the repository:

    git clone <repository-url>
    cd Python-algorithm-visualizer
  2. Install dependencies:

    pip install -r requirements.txt
  3. Run the application:

    python main.py

Dependencies

numpy>=1.21.0
matplotlib>=3.5.0

Usage Instructions

Getting Started

  1. Launch the application using python main.py
  2. Select an algorithm from the dropdown menu
  3. Choose your data size (10-1000 elements)
  4. Select data type (Random, Sorted, Reverse Sorted)
  5. Click "Generate Data" to create the dataset
  6. Use "Start" to begin the visualization

Keyboard Shortcuts

  • Space - Toggle pause/resume
  • Enter - Start algorithm
  • Escape - Reset to initial state
  • R - Reset algorithm
  • P - Pause algorithm
  • S - Start algorithm

Algorithm-Specific Usage

Binary Search

  • Ensure data is sorted (automatically handled)
  • Set a target value to search for
  • Watch as the algorithm narrows down the search space

Sorting Algorithms (Bubble Sort, Quick Sort)

  • Adjust array size to see performance differences
  • Try different data types to observe best/worst case scenarios
  • Monitor comparison and swap counters

Graph Algorithms (Dijkstra, BFS)

  • Generate a grid with obstacles and weights
  • Adjust grid size for different complexity levels
  • Observe pathfinding in real-time

Code Architecture

MVC Pattern Implementation

The application follows a clean Model-View-Controller architecture:

Model (algorithms/)

  • sorting.py - Bubble sort and Quick sort implementations
  • searching.py - Binary search implementation
  • graph.py - Dijkstra and BFS implementations

Each algorithm is implemented as a generator function that yields step-by-step visualization data.

View (ui/, visualizers/)

  • control_panel.py - User interface controls and data generation
  • canvas_manager.py - Main visualization canvas management
  • base_visualizer.py - Abstract base class for all visualizers
  • sort_visualizer.py - Specialized visualizer for sorting algorithms
  • search_visualizer.py - Specialized visualizer for searching algorithms
  • graph_visualizer.py - Specialized visualizer for graph algorithms

Controller (main.py)

  • AlgorithmVisualizer - Main application class coordinating between model and view
  • Handles user interactions and algorithm execution
  • Manages animation timing and state transitions

Algorithm Implementation Details

Binary Search

Time Complexity: O(log n)
Space Complexity: O(1)

The binary search algorithm efficiently finds a target value in a sorted array by repeatedly dividing the search space in half. The visualization shows:

  • Left and right boundary pointers
  • Mid-point calculation
  • Eliminated sections (grayed out)
  • Target found highlighting

Bubble Sort

Time Complexity: O(n²) average, O(n) best case
Space Complexity: O(1)

Bubble sort repeatedly steps through the list, comparing adjacent elements and swapping them if they are in the wrong order. Features:

  • Adjacent element comparison highlighting
  • Swap animations with color changes
  • Early termination detection
  • Sorted portion highlighting

Quick Sort

Time Complexity: O(n log n) average, O(n²) worst case
Space Complexity: O(log n) average, O(n) worst case

Quick sort uses a divide-and-conquer approach with pivot selection and partitioning. Visualization includes:

  • Pivot selection (red highlighting)
  • Partition boundary tracking
  • Recursive subarray visualization
  • Swap animations during partitioning

Dijkstra's Algorithm

Time Complexity: O((V + E) log V)
Space Complexity: O(V)

Dijkstra's algorithm finds the shortest path in a weighted graph using a priority queue. Shows:

  • Distance updates in real-time
  • Visited nodes highlighting
  • Current node exploration
  • Shortest path reconstruction

BFS Maze Solver

Time Complexity: O(V + E)
Space Complexity: O(V)

Breadth-first search explores all neighbors at the current level before moving to the next level. Features:

  • Level-by-level exploration
  • Queue visualization
  • Path reconstruction
  • Visited node tracking
  • Works with weighted grids (values 1-9)
  • Intelligent grid generation ensures valid paths always exist

Performance Optimizations

Canvas Updates

  • Double Buffering: All drawing operations use double buffering to prevent flickering
  • Selective Redraw: Only necessary elements are redrawn between frames
  • Color Caching: Pre-computed color gradients for smooth transitions

Animation System

  • Generator-based Algorithms: Each algorithm yields step-by-step data for smooth visualization
  • Configurable Speed: Animation speed can be adjusted from 1-100 steps per second
  • State Management: Proper pause/resume/reset functionality with state preservation

Memory Management

  • Efficient Data Structures: Algorithms use space-efficient implementations
  • Garbage Collection: Proper cleanup of animation timers and generators
  • Resource Pooling: Reuse of visualization objects where possible

Educational Value

Data Structures and Algorithms Concepts

This visualizer helps students understand:

  1. Algorithm Efficiency: Real-time complexity tracking shows the difference between O(n), O(n log n), and O(n²) algorithms
  2. Search Strategies: Binary search demonstrates the power of divide-and-conquer
  3. Sorting Techniques: Comparison between simple (bubble sort) and efficient (quick sort) algorithms
  4. Graph Algorithms: Pathfinding algorithms with different strategies (BFS vs Dijkstra)
  5. Space-Time Tradeoffs: Visual representation of memory usage vs execution time

Learning Outcomes

  • Understanding of Big O notation through visual complexity tracking
  • Grasp of algorithm design patterns (divide-and-conquer, greedy, etc.)
  • Appreciation for algorithm efficiency in real-world applications
  • Hands-on experience with data structure operations

Complexity Analysis Table

Algorithm Time Complexity Space Complexity Best Case Worst Case
Binary Search O(log n) O(1) O(1) O(log n)
Bubble Sort O(n²) O(1) O(n) O(n²)
Quick Sort O(n log n) O(log n) O(n log n) O(n²)
Dijkstra O((V+E) log V) O(V) O((V+E) log V) O((V+E) log V)
BFS O(V + E) O(V) O(V + E) O(V + E)

Screenshots and Demonstrations

Binary Search Visualization

Binary Search Algorithm Binary search algorithm showing left/right pointers, mid-point calculation, and eliminated sections in gray

Bubble Sort Animation

Bubble Sort Algorithm Bubble sort with swap animations, comparison highlighting, and sorted portion indication

Quick Sort Partitioning

Quick Sort Algorithm Quick sort showing pivot selection (red), partition boundaries, and recursive subarray visualization

Dijkstra's Pathfinding

Dijkstra's Algorithm Dijkstra's shortest path algorithm with distance updates, visited nodes, and optimal path reconstruction

BFS Maze Solving

BFS Maze Solver BFS maze solver with level-by-level exploration, queue visualization, and path tracking

Technical Specifications

System Requirements

  • Operating System: Windows 10+, macOS 10.14+, or Linux
  • Python Version: 3.7 or higher
  • Memory: 512 MB RAM minimum, 1 GB recommended
  • Display: 1024x768 minimum resolution

Performance Metrics

  • Animation Frame Rate: 60 FPS target
  • Maximum Array Size: 1000 elements
  • Maximum Grid Size: 20x20 cells
  • Memory Usage: < 100 MB typical

About

A comprehensive Python application built with Tkinter that provides interactive visualizations of core algorithms and data structures. The visualizer features smooth 60 FPS animations, real-time complexity tracking, and educational pseudocode display.

Resources

Stars

Watchers

Forks

Languages