• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple python package that helps to visualise any recursive function by adding a single line of code.

Recursion Visualiser

PyPI downloads Stars Forks

Recursion visualiser is a python tool that visualizes recursion tree with animation and draws recursion tree for recursive function. It works with almost any type of recursive function. Just add the recursion-visualiser decorator to your function and let it do the rest of the work.

Installation

1. Installing graphviz

Windows

The only dependency for recursion visualiser is Graphviz

  • Download graphviz binary
  • Add graphviz bin to path manually or by adding the following line on your script. Change the installation directory according to your installation path
# Set it to bin folder of graphviz  
os.environ["PATH"] += os.pathsep +  'C:/Program Files (x86)/Graphviz2.38/bin/'  

Ubuntu

  • Install graphviz
 sudo apt install graphviz

The instructions to install graphviz for other operating system is available here

2. Installing recursion-visualiser

The easiest way to install recursion-visualiser package is from pypi

pip install recursion-visualiser

An alternative way is to clone the repository and install all the requirements.

pip install -r requirements.txt

Alternative Installation using Docker

If you have docker and docker-compose installed then you can install recursion-tree-visualiser using Docker and docker-compose.yml file

  1. Download Docker file from repo
curl https://raw.githubusercontent.com/Bishalsarang/Recursion-Tree-Visualizer/master/Dockerfile --output Dockerfile
  1. Download docker-compose.yml
curl https://raw.githubusercontent.com/Bishalsarang/Recursion-Tree-Visualizer/master/docker-compose.yml --output docker-compose.yml
  1. Start docker container
CURRENT_UID=$(id -u):$(id -g) docker-compose up
  1. Run any python scripts and run using
docker-compose exec vs python fibonacci.py

Usage

The preferred way to import the decorator class from the package is as:

from visualiser.visualiser import Visualiser as vs

1. Fibonacci

Let's draw the recursion tree for fibonacci number.
Here is how the simple code looks like

def fib(n):  
    if n <= 1: 
        return n 
    return fib(n - 1) + fib(n - 2)  

print(fib(6))  

Now we want to draw the recursion tree for this function. It is as simple as adding a decorator

# Author: Bishal Sarang

# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add decorator
# Decorator accepts optional arguments: ignore_args , show_argument_name, show_return_value and node_properties_kwargs
@vs(node_properties_kwargs={"shape":"record", "color":"#f57542", "style":"filled", "fillcolor":"grey"})
def fib(n):
    if n <= 1:
        return n
    return fib(n=n - 1) + fib(n=n - 2)


def main():
    # Call function
    print(fib(n=6))
    # Save recursion tree to a file
    vs.make_animation("fibonacci.gif", delay=2)


if __name__ == "__main__":
    main()

Here are the changes required:

  • Add decorator Visualiser which accepts optional arguments ignore_args, show_argument_name and 'show_return_value'
  • Change every function calls to pass as keyword arguments.
  • Make_animation

The output image are saved as "fibonacci.gif" and "fibonacci.png"

Here is how the recursion tree looks like:
Animation: enter image description here

enter image description here

Support:

Find other examples here and read more about recursion-visualiser here

TODO:

  • Minimal working version
  • Upload package to pypi
  • Support animation
  • Add node styles
  • Support aliasing for function name
  • Show repeated states
  • Support node_color, backgroundcolor etc
  • Refactor
  • Handle base cases
  • Make more beautiful trees

More Repositories

1

Leetcode-Questions-Scraper

Scrape Algorithm Questions from leetcode and generate html and epub file
Python
143
star
2

Leetcode-Questions

Checks leetcode.com daily for new problems and saves problems as epub and HTML. Contains cronjob as well as manual workflow. Uses https://github.com/Bishalsarang/Leetcode-Questions-Scraper and Github Actions
Python
21
star
3

Pearson-PDF-Downloader

An utility to download ebooks pdf from pearson concurrently.
Python
13
star
4

The-Sign-Language-Platform

A Computer Vision based project that uses CNN to translate American Sign Language(ASL) to text and speech
Jupyter Notebook
8
star
5

Draw-Io-Clone

This repository contains the code for Draw Io clone which was done as an internship project at Leapfrog Inc. The code is written in pure HTML, CSS and JS with no third party libraries and dependencies.
JavaScript
7
star
6

Missionaries-and-Cannibals-Problem

This repository contains the solution to Missionaries and Cannibal Problem using BFS and DFS search.
Python
6
star
7

Pokemon-Info-Flask

This repository contains implementation of flask application allowing CRUD operations following MVT pattern.
Python
1
star
8

N-Queens-Visualisation

Simple Visualisation of NQueens using C++ and OpenGL
C++
1
star
9

Staff-and-student-Management

2nd Semester Project Made using : QT 5.8.0
C++
1
star
10

Todo-Web-Components-

Todo App built with HTML custom web components, lit-html and lit-element
JavaScript
1
star
11

Codeforces-Scraper

A simple scraper made in python for the purpose of downloading codeforce contests and problems as pdf It allows downloading problem based on contest id, rating, difficulty and tags
Python
1
star