Ejemplo n.º 1
0
 def delete_node(self, n):
     try:
         if len(self.adj[n]) == 1:  # allowed for leaf node
             Graph.delete_node(self, n)
         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)
Ejemplo n.º 2
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 allowed
     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:
         Graph.add_edge(self, u, v)
         return
     elif len(self.adj) == 0:  # first leaf
         Graph.add_edge(self, u, v)
         return
     else:
         raise NetworkXError("adding edge %s-%s not allowed in tree" %
                             (u, v))
Ejemplo n.º 3
0
    def remove_edge(self, u, v, data=None):
        """Remove edge between (u,v).

        If d is defined only remove the first edge found with 
        edgedata == d.  

        If d is None, remove all edges between u and v.
        """
        if data is None:
            super(MultiDiGraph, self).remove_edge(u, v)
        else:
            try:
                dlist = self.succ[u][v]
                # Bug fixes by Juho Heimonen # modified line
                #                 if d in dlist: # modified line
                #                     dlist.remove(d) # modified line
                #                 if len(dlist)==1: # modified line
                if data in dlist:  # modified line
                    dlist.remove(data)  # modified line
                if len(dlist) == 0:  # modified line
                    # End of changes # modified line
                    # remove the key entries if last edge
                    del self.succ[u][v]
                    del self.pred[v][u]
            except KeyError:
                raise NetworkXError("edge %s-%s not in graph" % (u, v))
Ejemplo n.º 4
0
    def remove_node(self, n):
        """Remove node n.

        Removes the node n and adjacent edges in the graph.
        Attempting to remove a non-existent node will raise an exception.

        Examples
        --------
        >>> G=nx.complete_graph(3)  # complete graph on 3 nodes, K3
        >>> G.edges()
        [(0, 1), (0, 2), (1, 2)]
        >>> G.remove_node(1)
        >>> G.edges()
        [(0, 2)]

        """
        adj = self.adj
        try:
            nbrs = adj[n].keys(
            )  # keys handles self-loops (allow mutation later)
        except KeyError:  # NetworkXError if n not in self
            raise NetworkXError("node %s not in graph" % (n, ))
        for u in nbrs:
            del adj[u][n]  # remove all edges n-u in graph
        del adj[n]  # now remove node
Ejemplo n.º 5
0
    def remove_edge(self, u, v):
        """Remove the edge between (u,v).

        Parameters
        ----------
        u,v: nodes 
            

        See Also
        --------
        remove_edges_from : remove a collection of edges

        Examples
        --------
        >>> G=nx.path_graph(4)
        >>> G.remove_edge(0,1)
        >>> e=(1,2)
        >>> G.remove_edge(*e) # unpacks e from an edge tuple
        >>> e=(2,3,'data')
        >>> G.remove_edge(*e[:2]) # edge tuple with data

        """
        try:
            del self.adj[u][v]
            if u != v:  # self loop needs only one entry removed
                del self.adj[v][u]
        except KeyError:
            raise NetworkXError("edge %s-%s not in graph" % (u, v))
Ejemplo n.º 6
0
    def get_edge(self, u, v, default=None):
        """Return the data associated with the edge (u,v).

        Parameters
        ----------
        u,v : nodes
            If u or v are not nodes in graph an exception is raised.

        default:  any Python object            
            Value to return if the edge (u,v) is not found.
            If not specified, raise an exception.

        Examples
        --------
        >>> G=nx.path_graph(4) # path graph with edge data all set to 1
        >>> G.get_edge(0,1) 
        1
        >>> e=(0,1)
        >>> G.get_edge(*e) # tuple form
        1

        Notes
        -----
        It is faster to use G[u][v].

        >>> G[0][1]
        1

        """
        try:
            return self.adj[u][v]
        except KeyError:
            if default is not None and u in self and v in self: return default
            raise NetworkXError("edge (%s,%s) not in graph" % (u, v))
Ejemplo n.º 7
0
 def add_node(self, n):
     if n in self:
         return  # already in tree
     elif len(self.adj) == 0:
         Graph.add_node(self, n)  # first node
     else:  # not allowed
         raise NetworkXError(\
             "adding single node %s not allowed in non-empty tree"%(n))
Ejemplo n.º 8
0
 def __init__(self, data=None, **kwds):
     Tree.__init__(self, **kwds)
     self.comp = {}  # dictionary mapping node to component
     self.nc = 0  # component index, start at zero, sequential (with holes)
     if data is not None:
         try:
             self = convert.from_whatever(data, create_using=self)
         except:
             raise NetworkXError("Data %s is not a forest" % data)
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
 def bunch_iter(nlist, adj):
     try:
         for n in nlist:
             if n in adj:
                 yield n
     except TypeError, e:
         print e.message
         # capture error for non-sequence/iterator nbunch.
         if 'iterable' in e.message:
             raise NetworkXError(
                 "nbunch is not a node or a sequence of nodes.")
         # capture error for unhashable node.
         elif 'hashable' in e.message:
             raise NetworkXError(
                 "Node %s in the sequence nbunch is not a valid node."
                 % n)
         else:
             raise
Ejemplo n.º 11
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
Ejemplo n.º 12
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:  # leaf edge
         Graph.delete_edge(self, u, v)
     else:  # interior edge
         raise NetworkXError(\
               "deleting interior edge %s-%s not allowed in tree"%(u,v))
     if self.degree(u) == 0:  # OK to delete remaining isolated node
         Graph.delete_node(self, u)
     if self.degree(v) == 0:  # OK to delete remaining isolated node
         Graph.delete_node(self, v)
Ejemplo n.º 13
0
    def neighbors_iter(self, n):
        """Return an iterator over all neighbors of node n.

        Examples
        --------
        >>> G=nx.path_graph(4)
        >>> print [n for n in G.neighbors(0)]
        [1]

        Notes
        -----
        It is faster to iterate over the using the idiom
        >>> print [n for n in G[0]]
        [1]
        
        """
        try:
            return self.adj[n].iterkeys()
        except KeyError:
            raise NetworkXError("node %s not in graph" % (n, ))
Ejemplo n.º 14
0
    def remove_edge(self, u, v, data=None):
        """Remove the edge between (u,v).

        If d is defined only remove the first edge found with 
        edgedata == d.  

        If d is None, remove all edges between u and v.
        """
        if data is None: 
            super(MultiGraph, self).remove_edge(u,v)
        else:
            try:
                dlist=self.adj[u][v]
                if data in dlist: 
                    # remove the edge with specified data
                    dlist.remove(data)
                if len(dlist)==1: 
                    # remove the key entries if last edge
                    del self.adj[u][v]
                    del self.adj[v][u]
            except KeyError: 
                raise NetworkXError(
                    "edge %s-%s with data %d not in graph"%(u,v,data))
Ejemplo n.º 15
0
    def neighbors(self, n):
        """Return a list of the nodes connected to the node n.

        Examples
        --------
        >>> G=nx.path_graph(4)
        >>> G.neighbors(0)
        [1]

        Notes
        -----
        It is sometimes more convenient (and faster) to access
        the adjacency dictionary as G[n]

        >>> G=nx.Graph()
        >>> G.add_edge('a','b','data')
        >>> G['a']
        {'b': 'data'}
        """
        try:
            return self.adj[n].keys()
        except KeyError:
            raise NetworkXError("node %s not in graph" % (n, ))
Ejemplo n.º 16
0
 def get_node(self, n):
     if n not in self.adj:
         raise NetworkXError("node %s not in graph"%(n,))
     else:
         data=self.label.get(n,None)            
     return data
Ejemplo n.º 17
0
 def remove_edge(self, u, v):
     try:
         del self.succ[u][v]
         del self.pred[v][u]
     except KeyError:
         raise NetworkXError("edge %s-%s not in graph" % (u, v))