Beispiel #1
0
 def add_edges_from(self, ebunch, attr_dict=None, **attr):
     # set up attribute dict
     if attr_dict is None:
         attr_dict = attr
     else:
         try:
             attr_dict.update(attr)
         except AttributeError:
             raise MiniNXError(\
                 "The attr_dict argument must be a dict.")
     # process ebunch
     for e in ebunch:
         ne = len(e)
         if ne == 3:
             u, v, dd = e
             assert hasattr(dd, "update")
         elif ne == 2:
             u, v = e
             dd = {}
         else:
             raise MiniNXError(\
                 "Edge tuple %s must be a 2-tuple or 3-tuple."%(e,))
         if u not in self.succ:
             self.succ[u] = self.adjlist_dict_factory()
             self.pred[u] = self.adjlist_dict_factory()
             self.node[u] = {}
         if v not in self.succ:
             self.succ[v] = self.adjlist_dict_factory()
             self.pred[v] = self.adjlist_dict_factory()
             self.node[v] = {}
         datadict = self.adj[u].get(v, self.edge_attr_dict_factory())
         datadict.update(attr_dict)
         datadict.update(dd)
         self.succ[u][v] = datadict
         self.pred[v][u] = datadict
Beispiel #2
0
 def remove_edge(self, u, v, key=None):
     try:
         d = self.adj[u][v]
     except (KeyError):
         raise MiniNXError(
             "The edge %s-%s is not in the graph." % (u, v))
     # remove the edge with specified data
     if key is None:
         d.popitem()
     else:
         try:
             del d[key]
         except (KeyError):
             raise MiniNXError(
             "The edge %s-%s with key %s is not in the graph." % (u, v, key))
     if len(d) == 0:
         # remove the key entries if last edge
         del self.succ[u][v]
         del self.pred[v][u]
Beispiel #3
0
 def remove_node(self, n):
     try:
         nbrs = self.succ[n]
         del self.node[n]
     except KeyError:  # MiniNXError if n not in self
         raise MiniNXError("The node %s is not in the digraph." % (n, ))
     for u in nbrs:
         del self.pred[u][n]  # remove all edges n-u in digraph
     del self.succ[n]  # remove node from succ
     for u in self.pred[n]:
         del self.succ[u][n]  # remove all edges n-u in digraph
     del self.pred[n]  # remove node from pred
Beispiel #4
0
 def add_node(self, n, attr_dict=None, **attr):
     # set up attribute dict
     if attr_dict is None:
         attr_dict = attr
     else:
         try:
             attr_dict.update(attr)
         except AttributeError:
             raise MiniNXError(\
                 "The attr_dict argument must be a dictionary.")
     if n not in self.succ:
         self.succ[n] = self.adjlist_dict_factory()
         self.pred[n] = self.adjlist_dict_factory()
         self.node[n] = attr_dict
     else:  # update attr even if node already exists
         self.node[n].update(attr_dict)
Beispiel #5
0
 def add_edge(self, u, v, key=None, attr_dict=None, **attr):
     # set up attribute dict
     if attr_dict is None:
         attr_dict = attr
     else:
         try:
             attr_dict.update(attr)
         except AttributeError:
             raise MiniNXError(
                 "The attr_dict argument must be a dictionary.")
     # add nodes
     if u not in self.succ:
         self.succ[u] = self.adjlist_dict_factory()
         self.pred[u] = self.adjlist_dict_factory()
         self.node[u] = {}
     if v not in self.succ:
         self.succ[v] = self.adjlist_dict_factory()
         self.pred[v] = self.adjlist_dict_factory()
         self.node[v] = {}
     if v in self.succ[u]:
         keydict = self.adj[u][v]
         if key is None:
             # find a unique integer key
             # other methods might be better here?
             key = len(keydict)
             while key in keydict:
                 key += 1
         datadict = keydict.get(key, self.edge_key_dict_factory())
         datadict.update(attr_dict)
         keydict[key] = datadict
     else:
         # selfloops work this way without special treatment
         if key is None:
             key = 0
         datadict = self.edge_attr_dict_factory()
         datadict.update(attr_dict)
         keydict = self.edge_key_dict_factory()
         keydict[key] = datadict
         self.succ[u][v] = keydict
         self.pred[v][u] = keydict
Beispiel #6
0
 def add_edge(self, u, v, attr_dict=None, **attr):
     # set up attribute dict
     if attr_dict is None:
         attr_dict = attr
     else:
         try:
             attr_dict.update(attr)
         except AttributeError:
             raise MiniNXError(\
                 "The attr_dict argument must be a dictionary.")
     # add nodes
     if u not in self.succ:
         self.succ[u] = self.adjlist_dict_factory()
         self.pred[u] = self.adjlist_dict_factory()
         self.node[u] = {}
     if v not in self.succ:
         self.succ[v] = self.adjlist_dict_factory()
         self.pred[v] = self.adjlist_dict_factory()
         self.node[v] = {}
     # add the edge
     datadict = self.adj[u].get(v, self.edge_attr_dict_factory())
     datadict.update(attr_dict)
     self.succ[u][v] = datadict
     self.pred[v][u] = datadict
Beispiel #7
0
 def predecessors(self, n):
     try:
         return iter(self.pred[n])
     except KeyError:
         raise MiniNXError("The node %s is not in the digraph." % (n, ))
Beispiel #8
0
 def remove_edge(self, u, v):
     try:
         del self.succ[u][v]
         del self.pred[v][u]
     except KeyError:
         raise MiniNXError("The edge %s-%s not in graph." % (u, v))