I am trying to learn Karger's algorithm. When i was browsing, I found a code for it and I thought I would try it out. When I try to run it, however, I get an error saying the following:
Value error:
list.remove(x) :x not in list at G[x].remove[v2]
.
def choose_random_key(G):
v1 = random.choice(list(G.keys()))
v2 = random.choice(list(G[v1]))
return v1, v2
def karger(G):
length = []
while len(G) > 2:
v1, v2 = choose_random_key(G)
G[v1].extend(G[v2])
for x in G[v2]:
G[x].remove(v2)
G[x].append(v1)
while v1 in G[v1]:
G[v1].remove(v1)
del G[v2]
for key in G.keys():
length.append(len(G[key]))
return length[0]
def operation(n):
i = 0
count = 10000
while i < n:
data = copy.deepcopy(G)
min_cut = karger(data)
if min_cut < count:
count = min_cut
i = i + 1
return count
print(operation(100))
G={0: [], 1: [10, 14], 2: [5, 10], 3: [10], 4: [], 5: [4, 9, 12, 14], 6: [7, 8, 13
], 7: [1], 8: [1, 2, 4], 9: [1, 17, 18], 10: [7, 13, 19], 11: [1, 14, 16], 12: [
0, 2, 8], 13: [], 14: [], 15: [2], 16: [9, 12], 17: [2, 12, 19], 18: [0, 6, 9, 1
4], 19: [3, 16]}{0: [], 1: [10, 14], 2: [5, 10], 3: [10], 4: [], 5: [4, 9, 12, 14], 6: [7, 8, 13
], 7: [1], 8: [1, 2, 4], 9: [1, 17, 18], 10: [7, 13, 19], 11: [1, 14, 16], 12: [
0, 2, 8], 13: [], 14: [], 15: [2], 16: [9, 12], 17: [2, 12, 19], 18: [0, 6, 9, 1
4], 19: [3, 16]}
What am i doing wrong? Any help would be appreciated.