1717class Graph (Datastructure ):
1818 """Base graph data structure for describing the topological relationships between nodes connected by edges.
1919
20+ Parameters
21+ ----------
22+ name: str, optional
23+ The name of the graph.
24+ Defaults to "Graph".
25+ default_node_attributes: dict, optional
26+ Default values for node attributes.
27+ default_edge_attributes: dict, optional
28+ Default values for edge attributes.
29+
2030 Attributes
2131 ----------
2232 node : dict
@@ -65,18 +75,22 @@ def DATASCHEMA(self):
6575 def JSONSCHEMANAME (self ):
6676 return 'graph'
6777
68- def __init__ (self ):
78+ def __init__ (self , name = None , default_node_attributes = None , default_edge_attributes = None ):
6979 super (Graph , self ).__init__ ()
7080 self ._max_node = - 1
71- self .attributes = {'name' : 'Graph' }
81+ self .attributes = {'name' : name or 'Graph' }
7282 self .node = {}
7383 self .edge = {}
7484 self .adjacency = {}
7585 self .default_node_attributes = {}
7686 self .default_edge_attributes = {}
87+ if default_node_attributes :
88+ self .default_node_attributes .update (default_node_attributes )
89+ if default_edge_attributes :
90+ self .default_edge_attributes .update (default_edge_attributes )
7791
7892 def __str__ (self ):
79- tpl = "<Network with {} nodes, {} edges>"
93+ tpl = "<Graph with {} nodes, {} edges>"
8094 return tpl .format (self .number_of_nodes (), self .number_of_edges ())
8195
8296 # --------------------------------------------------------------------------
@@ -352,7 +366,7 @@ def index_uv(self):
352366 # builders
353367 # --------------------------------------------------------------------------
354368
355- def add_node (self , key , attr_dict = None , ** kwattr ):
369+ def add_node (self , key = None , attr_dict = None , ** kwattr ):
356370 """Add a node and specify its attributes (optional).
357371
358372 Parameters
@@ -367,7 +381,7 @@ def add_node(self, key, attr_dict=None, **kwattr):
367381
368382 Returns
369383 -------
370- str
384+ hashable
371385 The key of the node.
372386
373387 Notes
@@ -382,6 +396,14 @@ def add_node(self, key, attr_dict=None, **kwattr):
382396 --------
383397 >>>
384398 """
399+ if key is None :
400+ key = self ._max_node = self ._max_node + 1
401+ try :
402+ if key > self ._max_node :
403+ self ._max_node = key
404+ except (ValueError , TypeError ):
405+ pass
406+
385407 if key not in self .node :
386408 self .node [key ] = {}
387409 self .edge [key ] = {}
0 commit comments