def test_case(filename): data = get_input(filename) output = get_output(filename.replace("input", "output")) shortest = clustering(data) assert shortest == output
if not detect_cycle(bfs_graph_addition, u) and tuple(sorted( [u, v])) not in t: t = t.union([tuple(sorted([u, v]))]) cost += w bfs_graph[u].add(v) bfs_graph[v].add(u) return cost def kruskal_uf(graph): uf = UF(len(graph)) t = set() cost = 0 edges = [] for u in graph.keys(): for v, w in graph[u]: edges.append((u, v, w)) edges = sorted(edges, key=lambda x: x[2]) for u, v, w in edges: if uf.find(u - 1) != uf.find(v - 1): t = t.union([tuple(sorted([u, v]))]) uf.union(u - 1, v - 1) cost += w return cost if __name__ == "__main__": data = get_input("test.txt") # print(data) # print(len(data)) print(kruskal_uf(data))
from algorithms.mst_prim import get_input from algorithms.single_link_clustering import clustering import pytest import glob filename = "input_completeRandom_*.txt" all_files = glob.glob(filename) def get_output(filename): with open(filename, 'r') as f: line = f.readline() return int(line) @pytest.mark.parametrize("filename", all_files) def test_case(filename): data = get_input(filename) output = get_output(filename.replace("input", "output")) shortest = clustering(data) assert shortest == output if __name__ == "__main__": data = get_input("challenge.txt") shortest = clustering(data) print(shortest)
def test_case(filename): data = get_input(filename) output = get_output(filename.replace("input", "output")) shortest = kruskal_uf(data) assert shortest == output