Ticket #13730: trac_17711_needs_info.patch

File trac_17711_needs_info.patch, 6.0 KB (added by slani, 7 years ago)
  • sage/graphs/base/c_graph.pyx

    # HG changeset patch
    # User Uros Slana <urossla@gmail.com>
    # Date 1361803876 -3600
    # Node ID 401e7150a68d38edba8fe55c45d40273638c9c6b
    # Parent  149d0fbb14ef5c748a937997d4fb8686a1adf214
    added the function to handle vertex removals
    
    diff --git a/sage/graphs/base/c_graph.pyx b/sage/graphs/base/c_graph.pyx
    a b  
    516516        """
    517517        if self.has_vertex(v):
    518518            self.del_vertex_unsafe(v)
    519 
     519           
    520520    cpdef int current_allocation(self):
    521521        """
    522522        Report the number of vertices allocated.
     
    15091509            ...
    15101510            TypeError: unhashable type: 'list'
    15111511        """
     1512       
    15121513        retval = None
    15131514        if name is None:
    15141515            name = 0
     
    15251526                     self._cg_rev,
    15261527                     (self._directed and
    15271528                      self._cg_rev is not None)) # this will add the vertex
    1528 
    15291529        return retval
    15301530
    15311531    def add_vertices(self, object vertices):
     
    16331633            self.vertex_ints.pop(v)
    16341634            self.vertex_labels.pop(v_int)
    16351635
     1636        self.del_vertex_neighbors(v)
     1637   
    16361638    def del_vertices(self, vertices):
    16371639        """
    16381640        Delete vertices from an iterable container.
     
    17021704            sage: list(P._backend.iterator_nbrs(0))
    17031705            [1, 4, 5]
    17041706        """
    1705 
    17061707        if self.neighbor.has_key(v):
    1707             return list(self.neighbor[v])
    1708         else:
    1709             return []
    1710 
     1708            return iter(self.neighbor[v])
     1709        if self.has_vertex(v):
     1710            return iter({})
    17111711
    17121712    def iterator_in_nbrs(self, v):
    17131713        """
     
    30373037                                 self.graph.vertex_ints,
    30383038                                 self.graph.vertex_labels,
    30393039                                 self.graph._cg)
     3040                               
    30403041
    30413042        if v_id == -1:
    30423043            raise LookupError("Vertex ({0}) is not a vertex of the graph.".format(repr(v)))
  • sage/graphs/base/dense_graph.pyx

    diff --git a/sage/graphs/base/dense_graph.pyx b/sage/graphs/base/dense_graph.pyx
    a b  
    752752        self._directed = directed
    753753        self.vertex_labels = {}
    754754        self.vertex_ints = {}
     755        self.neighbor = {}
     756
     757    def add_neighbors(self, object u, object v):
     758
     759        if self.neighbor.has_key(u):
     760            self.neighbor[u].add(v)
     761        else:
     762            self.neighbor[u] = {v}
     763
     764        if self.neighbor.has_key(v):
     765            self.neighbor[v].add(u)
     766        else:
     767            self.neighbor[v] = {u}
     768   
     769    def add_vertex_neighbors(self, object u):
     770
     771        if not self.neighbor.has_key(u):
     772            self.neighbor[u] = {}
     773
     774    def del_neighbors(self, object u, object v):
     775
     776        if self.neighbor.has_key(u) and v in self.neighbor[u]:
     777            self.neighbor[u].remove(v)
     778
     779        if self.neighbor.has_key(v) and u in self.neighbor[v]:
     780            self.neighbor[v].remove(u)
     781   
     782    def del_vertex_neighbors(self, object u):
     783
     784        if self.neighbor.has_key(u):
     785            for x in self.neighbor[u]:
     786                if u in self.neighbor[x]:
     787                    self.neighbor[x].remove(u)
     788            del(self.neighbor[u])
     789
    755790
    756791    def add_edge(self, object u, object v, object l, bint directed):
    757792        """
     
    787822        else:
    788823            self._cg.add_arc(u_int, v_int)
    789824            self._cg.add_arc(v_int, u_int)
     825        self.add_neighbors(u, v)
    790826
    791827    def add_edges(self, object edges, bint directed):
    792828        """
     
    859895        else:
    860896            self._cg.del_all_arcs(u_int, v_int)
    861897            self._cg.del_all_arcs(v_int, u_int)
     898        self.del_neighbors(u, v)
    862899
    863900    def get_edge_label(self, object u, object v):
    864901        """
  • sage/graphs/base/graph_backends.py

    diff --git a/sage/graphs/base/graph_backends.py b/sage/graphs/base/graph_backends.py
    a b  
    733733                i=i+1
    734734            name = i
    735735            retval = name
    736 
    737736        self._nxg.add_node(name)
    738 
    739737        return retval
    740738
    741739    def add_vertices(self, vertices):
  • sage/graphs/base/sparse_graph.pyx

    diff --git a/sage/graphs/base/sparse_graph.pyx b/sage/graphs/base/sparse_graph.pyx
    a b  
    15881588        else:
    15891589            self.neighbor[v] = {u}
    15901590
     1591    def add_vertex_neighbors(self, object u):
     1592
     1593        if not self.neighbor.has_key(u):
     1594            self.neighbor[u] = {}
     1595
     1596    def del_neighbors(self, object u, object v):
     1597
     1598        if self.neighbor.has_key(u) and v in self.neighbor[u]:
     1599            self.neighbor[u].remove(v)
     1600
     1601        if self.neighbor.has_key(v) and u in self.neighbor[v]:
     1602            self.neighbor[v].remove(u)
     1603   
     1604    def del_vertex_neighbors(self, object u):
     1605
     1606        if self.neighbor.has_key(u):
     1607            for x in self.neighbor[u]:
     1608                if u in self.neighbor[x]:
     1609                    self.neighbor[x].remove(u)
     1610            del(self.neighbor[u])
     1611
     1612
    15911613    def add_edge(self, object u, object v, object l, bint directed):
    15921614        """
    15931615        Adds the edge ``(u,v)`` to self.
     
    17601782            if l_int:
    17611783                self.edge_labels.pop(l_int)
    17621784
     1785        self.del_neighbors(u, v)
     1786
    17631787    def get_edge_label(self, object u, object v):
    17641788        """
    17651789        Returns the edge label for ``(u,v)``.
  • sage/graphs/generic_graph.py

    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b  
    79927992            sage: list(D.neighbor_iterator(0))
    79937993            [1, 2, 3]
    79947994        """
    7995         return self._backend.iterator_nbrs(vertex)
     7995        return iter(set(self._backend.iterator_nbrs(vertex)))
    79967996
    79977997    def vertices(self, key=None, boundary_first=False):
    79987998        r"""
     
    81098109            sage: sorted(P[4])
    81108110            [0, 3, 9]
    81118111        """
    8112         return self.neighbor_iterator(vertex)
     8112        return list(self.neighbor_iterator(vertex))
    81138113
    81148114    __getitem__ = neighbors
    81158115