-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmin_cuts.py
32 lines (25 loc) · 853 Bytes
/
min_cuts.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
import random
def union_vertices(G, v, u):
# unions 2 vertices, self loops are removed
G[u] = [z for z in G[u] if z != v]
G[v] = [z for z in G[v] if z != u]
for x in G[v]:
G[u].append(x)
G[x] = [z if z != v else u for z in G[x]]
G[v] = []
return G
def count_probable_min_cuts(G):
# tries to count the min cuts.
# probablity to succeed is low.
# so it should be executed multiple times and the
# min value of all executions should be remembered
vertices_remaining = [i for i in range(len(G))]
while len(vertices_remaining) > 2:
r = random.randint(0, len(vertices_remaining)-1)
x = vertices_remaining.pop(r)
y = random.choice(G[x])
union_vertices(G, x, y)
for u in range(len(G)):
if len(G[u]) > 0:
return len(G[u])
return 0