graph_test.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:algorithm 作者: Firkraag 项目源码 文件源码
def testKruskal(self):
        a = Vertex('a')
        b = Vertex('b')
        c = Vertex('c')
        d = Vertex('d')
        e = Vertex('e')
        f = Vertex('f')
        g = Vertex('g')
        h = Vertex('h')
        i = Vertex('i')
        vertices = [a, b, c, d, e, f, g, h, i]
        edges = [(a, b), (a, h), (b, a), (b, c), (b, h), (c, b), (c, i), (c, f), (c, d), (d, c), (d, e), (d, f), (e, d), (e, f), (f, d), (f, e), (f, c), (f, g), (g, f), (g, h), (g, i), (h, a), (h, b), (h, i), (h, g), (i, c), (i, h), (i, g)]
        G = Graph(vertices, edges)
        weight = [4, 8, 4, 8, 11, 8, 2, 4, 7, 7, 9, 14, 9, 10, 14, 10, 4, 2, 2, 1, 6, 8, 11, 7, 1, 2, 7, 6]
        z = dict()
        for x,y in zip(edges, weight):
            z[x] = y    
            print "{}: {}".format(x, y)
        def w(x, y):
            return z[(x, y)]        
        ls = G.Kruskal(w)
        print ls
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号