from union_find import UnionFind def redundant_connection(edges): graph = UnionFind(len(edges)) for v1, v2 in edges: if not graph.union(v1, v2): return [v1, v2] def main(): edges = [ [[1, 2], [1, 3], [2, 3]], [[1, 2], [2, 3], [1, 3]], [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]], [[1, 2], [1, 3], [1, 4], [3, 4], [2, 4]], [[1, 2], [1, 3], [1, 4], [1,5], [2, 3], [2, 4], [2, 5]] ] for i in range(len(edges)): print(i+1, ".\tEdges: ", edges[i], sep = "") print("\tThe redundant connection in the graph is: ", redundant_connection(edges[i]), sep = "") print("-" * 100) if __name__ == '__main__': main()