GraphI - Python Graph Interface and Types¶
Documentation Topics Overview:
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.
Frequently Asked Questions¶
- Yet another graph library?
The goal of
GraphIis 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.GraphIis suitable for interactive, explorative use. At the same time, it also allows for a seamless specialisation to optimised data structures.- What parts do I actually need?
The
GraphIlibrary provides several points of interest:- The
graphi.graphtype, the most performant general purpose graph type available. Use this as the starting point, the way you would usedict,listand others. - The
graphi.typesmodule which offers various graph types for different use-cases. Use this for specialisation, the way you would usenumpy.arrayand others. - The
graphi.types.decoratorhelpers which can produce undirected and bounded graph types. Use this for custom types to quickly provide variants from directed graphs. - The
graphi.abcwhich allows to code against several different graph implementations. Use this for generic algorithms, the way you would usecollections.abctypes.
- The
- What is this thing you call ABC?
GraphIdoes not just provide graph implementations, but also an efficient graph interface. This interface is defined by thegraphi.abcabstract 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,
GraphIis designed for you to work on graph data instead of pre-sliced storybook data.GraphIimplements only algorithms that- are fundamental building blocks for advanced algorithms, and/or
- benefit from knowledge of internal data structures.
At the moment, you can find basic operators in the
graphi.operatorsmodule.- What about performance?
At its core,
GraphIuses 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
GraphIas a whole.For
CPython, an optimised graph type implemented in C is available. Note thatgraphi.graphwill always represent the most optimised graph type available.