karger min cut algorithm in python 2.7

前端 未结 6 1995
借酒劲吻你
借酒劲吻你 2021-02-10 04:53

Here is my code for the karger min cut algorithm.. To the best of my knowledge the algorithm i have implemented is right. But I don get the answer right. If someone can check wh

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-10 05:49

    As stated by Phil, I had to run my program 100 times. And one more correction in the code was :

    for edge in edgelist:
            if edge[0] == edge[1]:
                edgelist.remove(edge)
    

    This part of the code did not correctly eliminate the self loops. So I had to change the code like :

    for i in range((len(edgelist)-1),-1,-1):
            if edgelist[i][0] == edgelist[i][1]:
                edgelist.remove(edgelist[i])
    

    And this line was not needed. since the target node would be automatically changed to self loop and it would be removed.

    edgelist.remove(target_edge)
    

    Then as said earlier, the program was looped for 100 times, and I got the minimum cut by randomization. :)

提交回复
热议问题