Python graph theory Algorithm (iii)--kruskal

Source: Internet
Author: User
Edge = [[[1,2,1],[1,2,3],[1,3,2],[2,3,1],[2,4,4],[3,4,2]]  #这是用边集合表示的图s = [[]]n = 4for i in range (n):    s.append ([i +1]) #print The S#compare method is written in order to sort the edges, passing in sort as a parameter, you can sort the Def compare (A, B):    if (a[2] > b[2]):        return 1    elif (a[ 2] < b[2]):        return-1    Else:        return 0edge.sort (Compare) #这就是sort () The big point for the       e in edge:#    print s< C11/>if s[e[0]]! = s[e[1]]:        print E for        i in S[e[1]]:            if (I am not in s[e[0]):                s[e[0]].append (i)            s [e[1]] = s[e[0]]
This involves merging and comparing collections.

Python graph theory Algorithm (iii)--kruskal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.