Code Monkey home page Code Monkey logo

algorithm-visualizer's Introduction

Algorithm Visualizer

This tool visualizes the operations of the most popular pathfinding, sorting, and searching algorithms. Algorithms are written both in C++ and Python with easy comparisons by editing 'include_cpp.json'. GUI and user inputs are always in python. This was implemented using NumPy and Matplotlib for Search and Sort visualizations, and Pygame with google maps API for pathfinding visualizations. All are fully interactive with multithreading support and per pixel screen refreshes. Visualizers can be easily compilied into a single .exe for portability while maintaining configurability with 'include_cpp.json'.


Pathfinding Visualizer:

Able to draw custom walls and drag nodes after algorithm completion:

animated

(Algorithms slowed down for illustration purposes)


Visit the streets of anywhere in the world! Powered by google maps API:

animated

(Algorithms slowed down for illustration purposes)

Add mid node to layer the search! Able to drag mid node as well: Generate mazes with recursive division animations or instantly:
Pathfinding Demo #2 Pathfinding Demo #3

Sorting Visualizer:

Able to change array size, generate new arrays, and select algorithms:

animated


Searching Visualizer:

Able to change array size, generate new arrays, sort arrays, change search term and of course, select algorithms:

animated

Performance Improvements

  • Compiling with PyInstaller
    • Up to 3x faster
  • Partial Display Refresh (Only update changed pixels between frames)
    • Increased performance by ~2x (Adding nodes) to ~354x (Large maze generation)
    • A typical performance increase between ~30x to ~100x for pathfinding algorithms and medium maze generation
    • 'V' button can be used to visualize the changed squares between toggles
    • For comparison, check out archived branch archive/V2.0/feature-complete
  • C++ Algorithms
    • 50x faster than pure python with #include algorithms.h
      • Python only interacts with C++ through thread locked Observer Pattern style calls
      • At 60fps for GUI and user input updates, limiting factor is C++ code (<10ms of 16.6ms rendering budget)
      • Above 60fps, limiting factor becomes python blocking C++ through thread locks for reading data
    • 1.6x slower than pure python with #include square.h (High overhead sending data between C++ and python up to 10kHz)
    • Nodes are flattened from 2D into a 1D Vector for cache efficiency.
    • Uses pybind11 to send data between python and C++, CMake for compiling config
    • For comparison, edit 'include_cpp.json' to switch between C++ and python
    • Current #includes shown as GUI window title
  • Total speed up: 66480x (Large maze generation - 2.77min to 2.5ms ๐Ÿคฏ)

Speed up in action! (Relative to above gifs):

(Pure Python) Partial Display Refresh: C++ Algorithms:
Partial Display Refresh C++ Algorithms
The purple color shows what pixels have been changed since the 'V' button toggle. It visualizes the per-pixel display update feature. It may not look much faster but this is a 50x perf improvement! Notice the algorithm timer in the center of the legend.

Installation (C++20 | Python 3.10)

Clone this repo and cd into it:

git clone https://github.com/ShanaryS/algorithm-visualizer.git
cd algorithm-visualizer

Create and activate your virtual environment:

  • Windows:
python -m venv venv
venv\Scripts\activate
  • MacOS/Linux:
virtualenv --no-site-packages venv
source venv/bin/activate

Install the required packages (While inside the virtual environment):

pip install -r requirements.txt

For compiling the C++ code, the CMakeLists.txt is all you need. The result will be a python extension (.pyd) in the same directory as the source files (for easy importing to python).

Usage

To create portable .exe files for each visualizer, setup the virtual environment as described above along with installing requirements.txt (On windows just run 'create_venv.cmd'). Then simply run the 'create_exe.cmd' file. After about 2 minutes, all three visualizers will be in the newly created 'bin' folder in the root of the project. The script will attempt to sign the executables with microsoft's signtool if a valid certificate is available. You can create a valid self signed certificate by following this guide.

Or run each visualizer directly using:

  • Pathfinding Visualizer:
python run_pathfinding_visualizer
  • Sorting Visualizer:
python run_sorting_visualizer
  • Searching Visualizer:
python run_searching_visualizer

To use google maps functionality, you need static maps api key from google.

You can get it for free at: https://developers.google.com/maps/documentation/maps-static/get-api-key.

Once you have access, create a '.env' file in the lib directory (also for .exe) with the text:

API_KEY="YOUR_KEY"

Replace YOUR_KEY with your key.

License

MIT

algorithm-visualizer's People

Contributors

shanarys avatar stonecoleq avatar

Stargazers

Svetlana avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.