def trim(self, graph):
    visited_nodes = set()
    node_id = graph.root_id
    while True:
        visited_nodes.add(node_id)
        node = graph.nodes[node_id]
        if (len(node.successors) > 0):
            succ_id = node.successors[0]
            succ = graph.nodes[succ_id]
            node.successors = [succ_id]
            succ.predecessors = [node_id]
            if (succ_id in visited_nodes):
                break
            node_id = succ_id
        else:
            break
    graph.nodes = {node_id: graph.nodes[node_id] for node_id in visited_nodes}
def trim(self, graph):
    visited_nodes = set()
    node_id = graph.root_id
    while True:
        visited_nodes.add(node_id)
        node = graph.nodes[node_id]
        if (len(node.successors) > 0):
            succ_id = node.successors[0]
            succ = graph.nodes[succ_id]
            node.successors = [succ_id]
            succ.predecessors = [node_id]
            if (succ_id in visited_nodes):
                break
            node_id = succ_id
        else:
            break
    graph.nodes = {node_id: graph.nodes[node_id] for node_id in visited_nodes}