GraphI - Python Graph Interface and Types

Code Health Documentation Status

GraphI is a lightweight graph library - it is suitable to model networks, connections and other relationships. Compared to other graph libraries, GraphI aims for being as pythonic as possible. If you are comfortable using list, dict or other types, GraphI is intuitive and straight-forward to use.

# create a graph with initial nodes
from graphi import graph
airports = graph("New York", "Rio", "Tokyo")
# add connections between nodes
airports["New York":"Rio"] = timedelta(hours=9, minutes=50)
airports["New York":"Tokyo"] = timedelta(hours=13, minutes=55)

At its heart, GraphI is built to integrate with Python’s data model. It natively works with primitives, iterables, mappings and whatever you need. For example, creating a multigraph is as simple as using multiple edge values:

# add multiple connections between nodes -> Multigraph
airports["Rio":"Tokyo"] = timedelta(days=1, hours=2), timedelta(days=1, hours=3)

By design, GraphI is primarily optimized for general convenience over specific brute force performance. It heavily exploits lazy iteration, data views and other modern python paradigms under the hood. This allows the use of common operations without loss of performance:

# get number of outgoing edges of nodes -> outdegree
outgoing_flights = {city: len(airports[city]) for city in airports}

With its general-purpose design, GraphI makes no assumptions about your data. You are free to use whatever is needed to solve your problem, not please data structure.

Frequently Asked Questions

Yet another graph library?
The goal of GraphI is not to be another graph library, but to provide an intuitive way to work with graphs. Working with complex graphs should be as easy for you as working with any other primitive type.
What is this thing you call ABC?

GraphI does not just provide graph implementations, but also an efficient graph interface. This interface is defined by the graphi.abc abstract base classes.

Any custom graph implementation can be made a virtual subclass of these ABCs. This allows you to adopt graph implementations optimized for your use-case without changing your code.

Where are all the algorithms?

First and foremost, GraphI is designed for you to work on graph data instead of pre-sliced storybook data. GraphI implements only algorithms that

  1. are fundamental building blocks for advanced algorithms, and/or
  2. benefit from knowledge of internal data structures.

At the moment, you can find basic operators in the graphi.operators module.

What about performance?

At its core, GraphI uses Python’s native, highly optimized data structures. For any non-trivial graph algorithm, the provided performance is more than sufficient.

From our experience, performance critical code is best run with PyPy. This will not just optimize isolated pieces, but the actual combination of your algorithm and GraphI as a whole.

Indices and tables


Documentation built from graphi 0.2.0 at Nov 17, 2017.