def trim(self, graph):
    queue = self._target_nodes
    visited_nodes = set()
    while (len(queue) > 0):
        node_id = queue.pop()
        visited_nodes.add(node_id)
        node = graph.nodes[node_id]
        for pred_id in node.predecessors:
            if (pred_id not in visited_nodes):
                queue.append(pred_id)
    graph.nodes = {node_id: graph.nodes[node_id] for node_id in visited_nodes}
    for node_id in graph.nodes:
        node = graph.nodes[node_id]
        node.successors = [succ_id for succ_id in node.successors if (succ_id in visited_nodes)]
        node.predecessors = [succ_id for succ_id in node.predecessors if (succ_id in visited_nodes)]
def trim(self, graph):
    queue = self._target_nodes
    visited_nodes = set()
    while (len(queue) > 0):
        node_id = queue.pop()
        visited_nodes.add(node_id)
        node = graph.nodes[node_id]
        for pred_id in node.predecessors:
            if (pred_id not in visited_nodes):
                queue.append(pred_id)
    graph.nodes = {node_id: graph.nodes[node_id] for node_id in visited_nodes}
    for node_id in graph.nodes:
        node = graph.nodes[node_id]
        node.successors = [succ_id for succ_id in node.successors if (succ_id in visited_nodes)]
        node.predecessors = [succ_id for succ_id in node.predecessors if (succ_id in visited_nodes)]