Commits

Peter Arrenbrecht committed ad1da54

move children to dagutil

Comments (0)

Files changed (2)

     '''generic interface for DAGs'''
 
     def __init__(self):
+        self._children = None
         pass
     def heads(self):
         '''return array of head ids'''
     def parents(self, id):
         '''return array of parents ids of id'''
         return []
+    def children(self, id):
+        '''return array of child ids of id'''
+        if self._children is None:
+            cs = {}
+            for n in self.walk():
+                ps = self.parents(n)
+                for p in ps:
+                    cs.setdefault(p, []).append(n)
+            self._children = cs
+        return self._children.get(id, [])
 
     def subset(self, heads):
         '''return sub-dag with only the given heads and their ancestors'''

src/discovery_tonfa.py

         if dist[n] == factor:
             sample.add(n)
 
-    children = {}
-    for n in dag.walk():
-        ps = dag.parents(n)
-        for p in ps:
-            children.setdefault(p, []).append(n)
-
     visit = list(roots)
     order = []
     dist = {}
         d = dist.setdefault(curr, 1)
         order.append(curr)
         seen.add(curr)
-        for c in children.get(curr, []):
+        for c in dag.children(curr):
             if c not in nodes:
                 continue
             dist.setdefault(c, d+1)