izigraph
IMPORTANT: This python module is a W.I.P.
This python module is a basic data structure to work with graph theory
Installation
This module can be installed using pip
pip install izigraph
To update the izigraph module
pip install izigraph --upgrade
Usage
Import the Graph class
from izigraph import Graph
g = Graph()
Adding nodes
Nodes can be added one by one
g = Graph()
n1 = g.add_node()
n2 = g.add_node()
Or in batch
g.add_nodes(10) # 10 nodes are added to the graph
Nodes label format
The new nodes added to the graph have a default label format 'node_{0}'
g = Graph()
n0 = g.add_node() # This node label is node_0
n1 = g.add_node() # This node label is node_1
The default label format can be changed when the Graph class is instantiated
g = Graph(node_label_format='n_{0}')
n0 = g.add_node() # This node label is n_0
n1 = g.add_node() # This node label is n_1
Linking nodes
g = Graph()
n0 = g.add_node()
n1 = g.add_node()
n2 = g.add_node()
# Single link. The default weight is 1
link_0_1 = g.link_nodes(n1, n2, weight=10)
# Bidirectional link. The default weights are 1
link_1_2, link_2_1 = g.link_nodes(n1, n2, bidirectional=True, weights=[10, 20])
Nodes can be linked by their labels
link_0_1 = g.link_nodes('node_0', 'node_1', weight=20)
Working with nodes
To check if two nodes are linked
g.add_nodes(3)
g.link_nodes('node_0', 'node_1')
g.nodes_are_linked('node_0', 'node_1') # This will return True
g.nodes_are_linked('node_0', 'node_2') # This will return False
You can search Nodes in the Graph object by the label. This will return the Node object
g = Graph()
g.add_node()
n0 = g.find_node_by_label('node_0')
Get the link betweeen two nodes
g = Graph()
g.add_nodes(3)
g.link_nodes('node_0', 'node_1', weight=10)
link = g.get_link('node_0', 'node_1')
link.weight() # Will return 10
link.source() # Will return the node 'node_0'
link.destination() # Will return the node 'node_1'
link = g.get_link('node_0', 'node_2') # Will return None
Exporters
To export a Graph to certain format you can use exporters
from izigraph import Graph, GraphExporter
g = Graph(node_label_format="{0}")
g.add_nodes(2)
ge = GraphExporter(driver="driver_name")
print(ge.export(g))
To add routes or paths fetched from an algorithm
print(ge.export(g, routes=some_routes))
NetGraph Viewer Exporter
To export the graph to NetGraph Viewer file format
ge = GraphExporter(driver="ngv")
print(ge.export(g))
License
MIT License
Copyright (c) 2016 Gonzalo Felipe Sánchez Vargas
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.