forked from ArsalanKhairani/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizedContraction.py
49 lines (40 loc) · 1.51 KB
/
RandomizedContraction.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
################################################################################
## Copyright(c) Arsalan Khairani, 2014 ##
## Karger's Min Cut ##
## ##
## Randomized contraction algorithm to find minimum cut of a graph. ##
## File Input: The file contains the adjacency list representation of a ##
## simple undirected graph. There are 200 vertices. ##
################################################################################
import random
import copy
cuts = []
# Function: Returns the minimum cut given the graph as input
def karger(graph):
while len(graph) > 2:
random.seed()
u = random.choice(list(graph.keys()))
v = random.choice(graph[u])
# remove self loops
for edge in graph[v]:
if edge != u:
graph[u].append(edge)
for edge in graph[v]:
graph[edge].remove(v)
if edge != u:
graph[edge].append(u)
del graph[v]
mincut = len(graph[list(graph.keys())[0]])
cuts.append(mincut)
file = open("kargerMinCut.txt")
graph = {}
for line in file:
Array = line.split()
node = int(Array[0])
edges = []
edges = [int(i) for i in Array[1:]]
graph[node] = edges
for i in range(1,801):
graph1 = copy.deepcopy(graph)
karger(graph1)
print ("Mincut is", min(cuts))