Package: graphnet
GraphnetA lightweight python library for graph and networks manipulation and visualizationInstallationpipinstallgraphnetUsage>>>fromgraphnetimportGraph>>>g=Graph()>>>g.add_node(1)>>>g.add_node(2)>>>g.add_node(3)>>>g.add_edge(1,2)>>>g.add_edge(2,3)>>>g.graph_matrixarray([[0.,1.,0.],[1.,0.,1.],[0.,1.,0.]])The node object can take any hashable object as input in this example we use integers for simplicity but you can use any object of your choice. The are two graph types supported for now, The arevectorandscalartype graphs. Graph objects by default are scalar or undirected graph to change it's type assign the variableVECTORto the Graph type argument like so.>>>fromgraphnetimportVECTOR>>>g=Graph(type=VECTOR)VisualizingGraphnet supports graph visualization throughMatplotlibAPI. All the graph plot heavy-lifting is done by one method so the is no need to worry. customization can be done using the display method parameters.>>>fromgraphnetimportVECTOR>>>importmatplotlib.pyplotasplt>>>g=Graph(type=VECTOR)>>>g.add_node(1)>>>g.add_node(2)>>>g.add_node(3)>>>g.add_node(4)>>>g.add_edge(1,2,weight=2)>>>g.add_edge(2,3,weight=6)>>>g.add_edge(3,4,weight=0)>>>g.add_edge(4,1,weight=4)>>>g.display(weighted=True)>>>plt.show()Nodes and Edges can be added through iterable to reduce code lines and to load data from a huge iterable.>>>g=Graph()>>>g.add_nodes_from_iterable(range(5))>>>edge_list=[(1,2,3),(3,4),(0,1,2),(2,4),(3,2,1),(4,2,0)]>>>g.add_edges_from_iterable(edge_list)>>>g.graph_matrixarray([[0.,2.,0.,0.,0.],[2.,0.,3.,0.,0.],[0.,3.,0.,1.,0.],[0.,0.,1.,0.,1.],[0.,0.,0.,1.,0.]])Data can also be added to the graph from dictionaries.>>>h=Graph(type=VECTOR)>>>m={1:[1,2],2:[3],3:[4,1],4:[2]}>>>h.from_dict(m)>>>h.graph_matrixarray([[1.,1.,0.,0.],[0.,0.,1.,0.],[1.,0.,0.,1.],[0.,1.,0.,0.]])Using Custom Node ClassIn some cases you might want to use a custom class as nodes in the graph to create networks. to use custom node class in the graph first we inherit from the Node class and then overwrite the__eq__,__hash__,__repr__methods in other for the custom class to work well with the graph. then pass the new reference attribute to the graph or you can use the old referencevalue.classPerson(Node):def__init__(self,name,age,sex):Node.__init__(self)self.name=nameself.age=ageself.sex=sexdef__eq__(self,other):returnself.name==other.namedef__hash__(self):returnhash(self.name)def__repr__(self):return"Person(name=%s, age=%s, sex=%s)"%(self.name,self.age,self.sex)g=Graph(ref="name")AlgorithmsThe algorithms module supports only two algorithms for now.Dijkstra AlgorithmMinimum Spanning Treefromgraphnet.algorithmsimportdijkstrawithGraph(type='vector')asg:foriinrange(6):g.add_node(Node(i))edges=[(0,1,2),(0,2,10),(1,3,10),(1,4,8),(2,5,2),(3,2,7),(4,5,5)]foreinedges:g.add_edge(*e)cost=dijkstra(g,0,5,path=True)fori,cinenumerate(cost):ifi<len(cost)-1:edge=g.connections[g.get_node_id(cost[i])][g.get_node_id(cost[i+1])]c.color="#82ffa3"edge.color='red'c.radius=0.2g.display(weighted=True)plt.show()DependenciesNumpyMatplotlibContributingAny pull requests to this projects will be reviewed and accepted as long as you follow the Contributing Guide.LicenseThis project is licensed under the MIT License, see theLICENSEfile for details.