Karger's minimum cut algorithm

451 Views Asked by At

While implementing Karger's minimum cut algorithm in python,i am getting the output as

edgelist remaining:  [[11, 20], [11, 20], [20, 11], [20, 11], [20, 11]]
nodelist remaining:  [11, 20]

Does this mean that the minimum cut is 5? I am using the following code:

with open('foo1.txt') as req_file:
    mincut_data = []
    for line in req_file:
        line = line.split()
        if line:
            line = [int(i) for i in line]
            mincut_data.append(line)

#extracting edges from the data #            
edgelist = []
nodelist = []
for every_list in mincut_data:
    nodelist.append(every_list[0])
    temp_list = []
    for temp in range(1,len(every_list)):
        temp_list = [every_list[0], every_list[temp]]
        flag = 0
        for ad in edgelist:
            if set(ad) == set(temp_list):
                flag = 1
        if flag == 0 :
            edgelist.append([every_list[0],every_list[temp]])


#karger min cut algorithm#
while(len(nodelist) > 2):
    val = randint(0,(len(edgelist)-1))
    print (val)
    target_edge = edgelist[val]
    replace_with = target_edge[0]
    should_replace = target_edge[1]
    for edge in edgelist:
        if(edge[0] == should_replace):
            edge[0] = replace_with
        if(edge[1] == should_replace):
            edge[1] = replace_with

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

print ('edgelist remaining: ',edgelist)
print ('nodelist remaining: ',nodelist)

I am trying to learn Karger's algorithm in python but i am not sure whether this is correct.

1

There are 1 best solutions below

0
On

I found the answer.The edge list remaining seems to be minimum cut.Since Karger's algorithm is a randomized algorithm,the program needs to be run multiple times to get the best solution.