-
Notifications
You must be signed in to change notification settings - Fork 7
/
kargerMinCut.py
49 lines (43 loc) · 1.22 KB
/
kargerMinCut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 30 16:10:40 2018
@author: Abdallah-Elshamy
"""
from random import choice
from copy import deepcopy
def contract(graph):
u = choice(list(graph.keys()))
v = choice(graph[u])
new_key = u+"-"+v
graph[new_key] = graph[u] + graph[v]
del graph[u]
del graph[v]
for key in graph.keys():
copy = graph[key][:]
if new_key == key:
for item in copy:
if item == u or item == v:
graph[key].remove(item)
else:
for item in copy:
if item == u or item == v:
graph[key].remove(item)
graph[key].append(new_key)
def min_cut(graph):
n = len(graph)
minimum = n*(n-1)//2
for i in range(n):
copy = deepcopy(graph)
while len(copy) > 2:
contract(copy)
minimum = min(minimum , len(list(copy.values())[0]))
return minimum
graph = {}
with open('kargerMinCut.txt') as f:
data = f.readlines()
for line in data:
elements = list(map(str,line.split('\t')[:-1]))
graph[str(elements[0])] = elements[1:]
f.close()
print(min_cut(graph))