Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from igraph import *
- from union_find import UnionFind
- # Union find from git@github.com:deehzee/unionfind.git
- def mst_kruskal(G):
- c = G.get_edgelist()
- w = [G[i] for i in c]
- c = [x for _,x in sorted(zip(w,c))]
- uf = UnionFind(G.vs.indices)
- T = Graph(len(G.vs.indices))
- for e in c:
- if not uf.connected(e[0], e[1]):
- T.add_edge(e[0], e[1])
- uf.union(e[0], e[1])
- return T
- if __name__ == '__main__':
- n, m = [int(i) for i in input().split(' ')]
- g = Graph(n)
- g.es["weight"] = 1.0
- for i in range(m):
- a, b, w = [int(i) for i in input().split(' ')]
- print(a, b)
- g.add_edge(a, b)
- g[a, b] = w
- mst = mst_kruskal(g)
- plot(mst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement