Skip to content

Commit

Permalink
Use the transitive closure to calculate the graph
Browse files Browse the repository at this point in the history
Don't maintain the links manually while removing nodes
Just take the transitive closure and remove all nodes
  • Loading branch information
Jacob Beck committed Mar 18, 2019
1 parent 9ae229a commit 69c8a09
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions core/dbt/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,33 +174,18 @@ def join(self):
self.inner.join()


def _remove_node_from_graph(graph, node):
# find all our direct parents in the graph, and all our direct
# children. note: do not use the _iter forms here, we need actual lists
# as we'll be mutating the graph again
parents = graph.predecessors(node)
children = graph.successors(node)
# remove the actual node
graph.remove_node(node)
# now redraw the edges, so that if A -> B -> C and B is to be
# removed, we will now have A -> C
for parent in parents:
for child in children:
graph.add_edge(parent, child)


def _subset_graph(graph, include_nodes):
"""Create and return a new graph that is a shallow copy of graph but with
only the nodes in include_nodes. Transitive edges across removed nodes are
preserved as explicit new edges.
"""
new_graph = nx.DiGraph(graph)
new_graph = nx.algorithms.transitive_closure(graph)

include_nodes = set(include_nodes)

for node in graph.nodes():
if node not in include_nodes:
_remove_node_from_graph(new_graph, node)
new_graph.remove_node(node)

for node in include_nodes:
if node not in new_graph:
Expand Down

0 comments on commit 69c8a09

Please sign in to comment.