Skip to main content

A Python package for visualizing graph algorithms.

Project description

vinal

PyPI pyversions Documentation Status

vinal is a Python package for visualizing graph/network algorithms. Currently, the following algorithms are implemented:

NetworkX graphs can be constructed from a single .csv file of node locations. Alternatively, one can specify edges of the graph by providing an additional .csv file of edges. The package relies on bokeh to generate standalone HTML files which can be viewed in a Jupyter Notebook inline or in a web browser.

Examples

dijkstras prims us_tour

Installation

The quickest way to get started is with a pip install.

pip install vinal

Usage

First, import the vinal package (commonly renamed to vl).

import vinal as vl

# Calling output_notebook() makes show() display plot in a Jupyter Notebook.
# Without it, a new tab with the plot will be opened.
from bokeh.io import output_notebook, show
output_notebook()  # only include if you want plot inline

All of the algorithm and plotting functions take a NetworkX graph as input. Thebuild module provides a way of constructing NetworkX graphs from .csv files. The standard nodes.csv and edges.csv files have the following form:

nodes.csv x y
0 0 0
1 1 5
2 7 6
3 8 6
4 4 6
edges.csv u v
0 0 1
1 1 2
2 4 3
3 3 1
4 2 4

Use pandas to import these .csv files as dataframes.

import pandas as pd
nodes = pd.read_csv('nodes.csv', index_col=0)
edges = pd.read_csv('edges.csv', index_col=0)

We can now use vl.create_network() to create a NetworkX graph.

G = vl.create_network(nodes, edges)

If an edges dataframe is not provided, the graph is assumed to be complete (every pair of nodes is connected) and the weights of each edge are determined by the euclidean distance between the pair of nodes.

G = vl.create_network(nodes)

Here, we give a summary of the various graph algorithms one can run.

# ----- Shortest path problem -----
# Returns: List of edges in the shortest path tree

# s: index of source vertex
tree = vl.dijkstras(G, s=0)


# ----- Minimum Spanning Tree (MST) -----
# Returns: List of edges in the shortest path tree

# i: index of initial vertex
mst = vl.prims(G, i=0)
mst = vl.kruskals(G)
mst = vl.reverse_kruskals(G)

# returns the cost of the minimum spanning tree
vl.spanning_tree_cost(G, mst)


# ----- Traveling Salesman Problem (TSP) -----
# Returns: List of node indices in the order they are visited

# i: index of initial vertex
tour = vl.random_neighbor(G, i=0)
tour = vl.nearest_neighbor(G, i=0)
# intial_tour: initial 2-node tour
tour = vl.nearest_insertion(G, intial_tour=[0,1,0])
tour = vl.furthest_insertion(G, intial_tour=[0,4,0])
# tour: initial tour to improve
tour = vl.two_opt(G, tour=tour)

# returns the cost of the tour
vl.tour_cost(G, tour)

There are four types of plots that vinal can generate:

  • Static solution plots
  • Non-iteractive algorithm visualization plots
  • Interactive create plots
  • Interactive algorithm plots

After genrating a solution via one of the algorithms, a static plot of the solution can be shown. In the following example, a tour is found using nearest insertion and then plotted.

tour = vl.nearest_insertion(G, initial_tour=[0,1,0])
show(vl.tour_plot(G, tour))

nearest_insertion_plot_tour

If one wishes to see each iteration of the algorithm, a plot with a Previous and Next button can be generated. In most cases, the cost of the solution in each iteration is shown and the text "done." will appear on the final iteration. In the following example, a tour is found using random neighbor and then a plot is returned showing each iteration of the 2-OPT tour improvement heuristic.

tour = vl.random neighbor(G)
show(vl.tsp_heuristic_plot(G, algorithm='2-OPT', tour=tour))

2-opt

Tours and spanning trees can also be constructed in a point-and-click fashion. When creating a tour, click the next node you wish to visit. When creating a spanning tree, click each edge you want in the tree.

show(vl.create_spanning_tree_plot(G))
show(vl.create_tour_plot(G))

build_tour

Lastly, an interactive version of Dijkstra's algorithm and the MST algorithms can be plotted. For Dijkstra's algorithm, the user is asked to select the next node from the frontier set to explore. For the MST algorithms, the user is asked to select the next edge to be added/removed from the tree. In all cases, a helpful error message will appear when the user selects incorreclty.

show(vl.assisted_mst_algorithm_plot(G, algorithm='kruskals'))

kruskals_assisted

License

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vinal-0.0.7.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vinal-0.0.7-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file vinal-0.0.7.tar.gz.

File metadata

  • Download URL: vinal-0.0.7.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for vinal-0.0.7.tar.gz
Algorithm Hash digest
SHA256 4f3d4aedc99cd0bc830a2df02eec09dc381504b22d3bad7167ca81551d3a8373
MD5 32778389efb9967921c25ba4d68f0f82
BLAKE2b-256 13f5b27aabd4dd40c12ddbc15020eed341e763ce2606f89e54718073af0fa414

See more details on using hashes here.

File details

Details for the file vinal-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: vinal-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for vinal-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 1e75fb22276612a5fc489aa8da8542b56890b3660a7e0edb84f18691f5287a02
MD5 293d82357c8b1948c236a9f5470f261d
BLAKE2b-256 1ec21b07d0a97565d9483e97cc65652cbae161a4d44cffcc30473639a76c11c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page