Skip to content

Commit c6e1dcf

Browse files
committed
Contig stitcher: optimize transitive closure calculation
1 parent 77df8c2 commit c6e1dcf

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

micall/core/plot_contigs.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -454,25 +454,19 @@ def remove_transitive_edges(graph):
454454
ret[parent] = lst
455455
return ret
456456

457-
def get_all_ancestors(recur, lst, graph, ancestor_name):
458-
if ancestor_name not in recur:
459-
recur = recur.copy()
460-
recur.add(ancestor_name)
461-
462-
if ancestor_name not in lst:
463-
lst.append(ancestor_name)
464-
465-
existing_ancestors = graph.get(ancestor_name, [])
466-
for existing in existing_ancestors:
467-
get_all_ancestors(recur, lst, graph, existing)
457+
def get_transitive_children(recur, lst, graph, current):
458+
for child in graph.get(current, []):
459+
if child not in recur:
460+
recur.add(child)
461+
lst.append(child)
462+
get_transitive_children(recur, lst, graph, child)
468463

469464
def transitive_closure(graph):
470465
ret = {}
471-
for parent, children in graph.items():
472-
lst = []
473-
for child in children:
474-
get_all_ancestors(set(), lst, graph, child)
475-
ret[parent] = lst
466+
for parent in graph:
467+
children = []
468+
get_transitive_children(set(), children, graph, parent)
469+
ret[parent] = children
476470
return ret
477471

478472
def copy_graph(graph):

0 commit comments

Comments
 (0)