Exemplo n.º 1
0
 def delete_node(self, n):
     try:
         if len(self.succ[n])+len(self.pred[n])==1: # allowed for leaf node
             DiGraph.delete_node(self,n) # deletes adjacent edge too
         else:
             raise NetworkXError( \
           "deleting interior node %s not allowed in tree"%(n))
     except KeyError: # NetworkXError if n not in self
         raise NetworkXError, "node %s not in graph"%n
Exemplo n.º 2
0
 def delete_node(self, n):
     try:
         if len(self.succ[n]) + len(
                 self.pred[n]) == 1:  # allowed for leaf node
             DiGraph.delete_node(self, n)  # deletes adjacent edge too
         else:
             raise NetworkXError( \
           "deleting interior node %s not allowed in tree"%(n))
     except KeyError:  # NetworkXError if n not in self
         raise NetworkXError, "node %s not in graph" % n
Exemplo n.º 3
0
 def add_edge(self, u, v=None):  
     if v is None: (u,v)=u  # no v given, assume u is an edge tuple
     if self.has_edge(u,v): return # no parallel edges
     elif u in self and v in self:
         raise NetworkXError, "adding edge %s-%s not allowed in tree"%(u,v)
     elif u in self or v in self:
         DiGraph.add_edge(self,u,v) # u->v
         return
     elif len(self.adj)==0: # first leaf
         DiGraph.add_edge(self,u,v) # u->v           
         return
     else:
         raise NetworkXError("adding edge %s-%s not allowed in tree"%(u,v))
Exemplo n.º 4
0
 def add_edge(self, u, v=None):
     if v is None: (u, v) = u  # no v given, assume u is an edge tuple
     if self.has_edge(u, v): return  # no parallel edges
     elif u in self and v in self:
         raise NetworkXError, "adding edge %s-%s not allowed in tree" % (u,
                                                                         v)
     elif u in self or v in self:
         DiGraph.add_edge(self, u, v)  # u->v
         return
     elif len(self.adj) == 0:  # first leaf
         DiGraph.add_edge(self, u, v)  # u->v
         return
     else:
         raise NetworkXError("adding edge %s-%s not allowed in tree" %
                             (u, v))
Exemplo n.º 5
0
    def __init__(self, data=None, **kwds):
        DiGraph.__init__(self, **kwds)
        if data is not None:

            try:  # build a rooted tree
                D = DiGraph()
                for (child, parent) in data.par.iteritems():
                    D.add_edge(parent, child)
            except AttributeError:
                D = DiGraph(data)
            except:  # else nothing we can do
                raise NetworkXError, "Data %s is not a rooted tree:" % data

            if D.order() == D.size() + 1:
                self.pred = D.pred.copy()
                self.succ = D.succ.copy()
                self.adj = self.succ
                del D
            else:  # not a tree
                raise NetworkXError, "Data %s is not a rooted tree:" % data
Exemplo n.º 6
0
 def delete_edge(self, u, v=None):
     if v is None: (u, v) = u
     if self.degree(u) == 1 or self.degree(v) == 1:
         DiGraph.delete_edge(self, u, v)
     else:
         raise NetworkXError(
             "deleting interior edge %s-%s not allowed in tree" % (u, v))
     if self.degree(u) == 0: DiGraph.delete_node(self, u)
     if self.degree(v) == 0: DiGraph.delete_node(self, v)
Exemplo n.º 7
0
 def delete_edge(self, u, v=None): 
     if v is None: (u,v)=u
     if self.degree(u)==1 or self.degree(v)==1:
         DiGraph.delete_edge(self,u,v)
     else:
         raise NetworkXError(
               "deleting interior edge %s-%s not allowed in tree"%(u,v))
     if self.degree(u)==0: DiGraph.delete_node(self,u)
     if self.degree(v)==0: DiGraph.delete_node(self,v)
Exemplo n.º 8
0
    def __init__(self,data=None,**kwds):
        DiGraph.__init__(self,**kwds)
        if data is not None:

            try: # build a rooted tree
                D=DiGraph()
                for (child,parent) in data.par.iteritems():
                    D.add_edge(parent,child)
            except AttributeError: 
                D=DiGraph(data)
            except: # else nothing we can do
                raise NetworkXError, "Data %s is not a rooted tree:"%data
                    
            if D.order()==D.size()+1:
                self.pred=D.pred.copy()
                self.succ=D.succ.copy()
                self.adj=self.succ
                del D
            else: # not a tree
                raise NetworkXError, "Data %s is not a rooted tree:"%data