GraphI - Graphs for Humans¶
Documentation is available in docstrings of modules, classes and functions.
In an interactive session, use
help, or ipython’s
For example, use
help(graphi.GraphABC) to view the graph interface.
For further help, tutorials and examples visit http://graphi.readthedocs.io to view the full documentation.
You should start by using the type
graphi.graph - the most well-rounded
graph implementation on your system. Like all
graphi types, it uses an
interface similar to the Python builtin types:
from graphi import graph from datetime import timedelta # create a graph with initial nodes airports = graph("New York", "Rio", "Tokyo") # add edges between nodes airports["New York":"Rio"] = timedelta(hours=9, minutes=50) airports["New York":"Tokyo"] = timedelta(hours=13, minutes=55)
All graphs behave like a blend of a
set of nodes and a
dict of edges.
Bulk lookups, such as the adjacency of a node or edges of a graph, provide
efficient views. For example, len(graph[node])` provides the number of
outgoing edges from
node without building intermediate containers.
print('There are', len(airports), 'airports in the world!') # iterate directly on nodes... for node in airports: print(node, 'has', len(airports[node]), 'outgoing connections.') # ... or use graph.nodes(), graph.edges(), graph.values() or graph.items() for edge in airports.edges(): print('Origin:', edge.start, ' Destination:', edge.stop, ' Flight Time:', airports[edge])
Edge(start, stop, step=None)¶
An edge in a graph as a pair of nodes
- start – the start or tail of an edge
- stop – the stop or head of an edge
- step – currently unused
This is a verbose interface for creating edges between nodes for use in a graph. It allows using slice notation independent of a graph:
>>> atb = Edge[a:b] >>> a2b = Edge(a, b) >>> graph[a2b] = 1337 >>> graph[a:b] == graph[atb] == graph[a2b] == graph[Edge[a:b]] == graph[Edge(a, b)] True
Edgecan also be used for explicit containment tests:
>>> Edge[a:b] in graph True
In addition to their slice-like nature,
Edgeis iterable and indexable. This allows for easy unpacking:
>>> edge = Edge[a:b] >>> tail, head = edge
This class creates a representation of an edge as a connection between nodes. Edge values can be arbitrary objects.
- graphi.graph_io package
- graphi.operators package
- graphi.types package