karger min cut algorithm in python 2.7

前端 未结 6 1996
借酒劲吻你
借酒劲吻你 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:35

    I totally agree with the above answers. But when I run your code with Python 3.x, it turns out to produce Code 1. This code sometimes works, but sometimes fails. In fact, as @user_3317704 mentioned above, there is a mistake in your code:

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

    You should not change the items of the list when you do a for-do. It raises mistakes. For your reference, it could be

    newEdgeList = edgeList.copy();
    for edge in edgeList:
        if edge[0] == edge[1]:
            newEdgeList.remove(edge);
    edgeList = newEdgeList;
    

提交回复
热议问题