# 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


1702  1702  sage: list(P._backend.iterator_nbrs(0)) 
1703  1703  [1, 4, 5] 
1704  1704  """ 
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  
1707  1711  
1708  1712  def iterator_in_nbrs(self, v): 
1709  1713  """ 
diff git a/sage/graphs/base/sparse_graph.pyx b/sage/graphs/base/sparse_graph.pyx
a

b


1574  1574  self.vertex_labels = {} 
1575  1575  self.vertex_ints = {} 
1576  1576  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} 
1577  1590  
1578  1591  def add_edge(self, object u, object v, object l, bint directed): 
1579  1592  """ 
… 
… 

1633  1646  self._cg.add_arc_label(u_int, v_int, l_int) 
1634  1647  self._cg.add_arc_label(v_int, u_int, l_int) 
1635  1648  
 1649  self.add_neighbors(u, v) 
 1650  
1636  1651  def add_edges(self, object edges, bint directed): 
1637  1652  """ 
1638  1653  Add edges from a list. 
diff git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
a

b


7992  7992  sage: list(D.neighbor_iterator(0)) 
7993  7993  [1, 2, 3] 
7994  7994  """ 
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) 
8000  7996  
8001  7997  def vertices(self, key=None, boundary_first=False): 
8002  7998  r""" 
… 
… 

8113  8109  sage: sorted(P[4]) 
8114  8110  [0, 3, 9] 
8115  8111  """ 
8116   return list(self.neighbor_iterator(vertex)) 
 8112  return self.neighbor_iterator(vertex) 
8117  8113  
8118  8114  __getitem__ = neighbors 
8119  8115  