def METHOD_NAME(self, graph):
    graph.remove_edges_from(nx.selfloop_edges(graph))
    while True:
        dead_end_nodes = [node for node, degree in graph.degree() if degree <= 1]
        if dead_end_nodes:
            graph.remove_nodes_from(dead_end_nodes)
        else:
            return