Ticket #13730: trac_17710_needs_info.patch

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

    # HG changeset patch
    # User Uros Slana <urossla@gmail.com>
    # Date 1361465214 -3600
    # Node ID 149d0fbb14ef5c748a937997d4fb8686a1adf214
    # Parent  ec1fb07db6e23f9fbd4c34f0d48198d08ec76473
    tried to improve searching speed of neighbors for sparse graph
    
    diff --git a/sage/graphs/base/c_graph.pyx b/sage/graphs/base/c_graph.pyx
    a b  
    17021702            sage: list(P._backend.iterator_nbrs(0))
    17031703            [1, 4, 5]
    17041704        """
    1705         return iter(set(self.iterator_in_nbrs(v)) |
    1706                     set(self.iterator_out_nbrs(v)))
     1705
     1706        if self.neighbor.has_key(v):
     1707            return list(self.neighbor[v])
     1708        else:
     1709            return []
     1710
    17071711
    17081712    def iterator_in_nbrs(self, v):
    17091713        """
  • sage/graphs/base/sparse_graph.pyx

    diff --git a/sage/graphs/base/sparse_graph.pyx b/sage/graphs/base/sparse_graph.pyx
    a b  
    15741574        self.vertex_labels = {}
    15751575        self.vertex_ints = {}
    15761576        self.edge_labels = {}
     1577        self.neighbor = {}
     1578
     1579    def add_neighbors(self, object u, object v):
     1580
     1581        if self.neighbor.has_key(u):
     1582            self.neighbor[u].add(v)
     1583        else:
     1584            self.neighbor[u] = {v}
     1585
     1586        if self.neighbor.has_key(v):
     1587            self.neighbor[v].add(u)
     1588        else:
     1589            self.neighbor[v] = {u}
    15771590
    15781591    def add_edge(self, object u, object v, object l, bint directed):
    15791592        """
     
    16331646            self._cg.add_arc_label(u_int, v_int, l_int)
    16341647            self._cg.add_arc_label(v_int, u_int, l_int)
    16351648
     1649        self.add_neighbors(u, v)
     1650
    16361651    def add_edges(self, object edges, bint directed):
    16371652        """
    16381653        Add edges from a list.
  • 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         if self._directed:
    7996             return iter(set(self.neighbor_out_iterator(vertex)) \
    7997                     | set(self.neighbor_in_iterator(vertex)))
    7998         else:
    7999             return iter(set(self._backend.iterator_nbrs(vertex)))
     7995        return self._backend.iterator_nbrs(vertex)
    80007996
    80017997    def vertices(self, key=None, boundary_first=False):
    80027998        r"""
     
    81138109            sage: sorted(P[4])
    81148110            [0, 3, 9]
    81158111        """
    8116         return list(self.neighbor_iterator(vertex))
     8112        return self.neighbor_iterator(vertex)
    81178113
    81188114    __getitem__ = neighbors
    81198115