python-3.xminimum-spanning-treekruskals-algorithm

it's not sorted using "sorted", and getting a wrong answer. And I am not sure if the algorithm is right


I am using Kruskal's Algorithm to find the Minimum Spanning Tree. I just followed the algorithm that has been provided during the lecture and I should keep the format of having Edge class. sorted is not working so I can't figure out if the logic is wrong.

Is there any reason of naming parent in the constructor of Edge class.

import sys

class Edge:

    def __init__(self, start_ver, to_vertex, weight):
        self.start_ver = start_ver
        self.to_vertex = to_vertex
        self.weight = weight
        self.spanning_tree = False

    # def __lt__(self, other):
    #     return self.weight < other.weight


class UnionFind:

    def __init__(self, ver_num):
        self.parent = None
        self.create_set(ver_num)

    def create_set(self, ver_num):
        self.parent = list(range(ver_num))

    def find_set(self, ver_num):
        if self.parent[ver_num] != ver_num:
            self.parent[ver_num] = self.find_set(self.parent[ver_num])
        return self.parent[ver_num]

    def merge_set(self, one_ver, two_ver):
        self.parent[self.find_set(one_ver)] = self.find_set(two_ver)

def MST_Kruskal(ver_num, edge_list):

    union_find = UnionFind(ver_num)
    mst_tree = list()
    sorted(edge_list, key=lambda vertex: vertex.weight)
    for edge in edge_list:
        if not edge.spanning_tree:
            if union_find.find_set(edge.start_ver) != union_find.find_set(edge.to_vertex):
                mst_tree.append(edge)
                if len(mst_tree) == ver_num - 1:
                    edge.spanning_tree = True
                union_find.merge_set(edge.start_ver, edge.to_vertex)
            sorted(edge_list, key=lambda vertex: vertex.weight)
        else:
            return
    total = 0
    for x in mst_tree:
        total += x.weight
    print(total)




def main():
    edge_list = list()
    vertex_num, edge_num = map(int, (sys.stdin.readline().split()))
    for e in range(edge_num):
        start, end, weight = map(int, sys.stdin.readline().split())
        edge = Edge(start-1, end-1, weight)
        edge_list.append(edge)

    MST_Kruskal(vertex_num, edge_list)

if __name__== "__main__":
    main()

input

4 5
1 2 10
2 3 15
1 3 5
4 2 2
4 3 40


expected output

17

Solution

  • You are confusing the function sorted(iterable[, key][, reverse]) with the list method sort(*, key=None, reverse=None).

    Where sorted according to the documentation does: "Return a new sorted list from the items in iterable." While sort according to documentationd does: This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).

    So for your code to work you need to change: sorted(edge_list, key=lambda vertex: vertex.weight) to edge_list.sort(key=lambda vertex: vertex.weight)

    This assuming that everything else is correct in your code