Week 3 covered:

• PROBABILITY REVIEW
• LINEAR-TIME SELECTION
• GRAPHS AND THE CONTRACTION ALGORITHM

Programming assignment: The file contains the adjacency list representation of a simple undirected graph. Your task is to code up and run the randomized contraction algorithm for the min cut problem and use it on the above graph to compute the min cut.

The file contains the adjacency list representation of a simple undirected graph. There are 200 vertices labeled 1 to 200. The first column in the file represents the vertex label, and the particular row (other entries except the first column) tells all the vertices that the vertex is adjacent to. So for example, the 6th row looks like : "6 155 56 52 120 ......". This just means that the vertex with label 6 is adjacent to (i.e., shares an edge with) the vertices with labels 155,56,52,120,......,etc

```import sys
import random
filename = "algo3_GRAPHmincut.txt"
def ParseGraph(filename):
"""Parse a graph into adjacency list format per programming Q.3

Args:
- filename: the on-disk graph representation
Returns:
(vertices, edges) where
vertices = [vertex_1, vertex_2, ...]
edges = [(vertex_1, vertex_2), ...]
"""
vertices = []
edges = set([])

for l in open(filename):
fields = [int(f) for f in l.split()]
vertex = fields.pop(0)
incident = [tuple(sorted([vertex, f])) for f in fields]
vertices.append(vertex)
edges.update(incident)

return vertices, list(edges)

def RandomContraction(vertices, edges):
while len(vertices) > 2:
edge = random.choice(edges)
a, b = edge
vertices.remove(b)
new_edges = []
for e in edges:
if e == edge:
continue
if b in e:
if e == b:
other = e
if e == b:
other = e
e = tuple(sorted([a, other]))
new_edges.append(e)
edges = new_edges

return vertices, edges

vertices, edges = ParseGraph(filename)

minimum = sys.maxint
for i in range(0, 1000):
v, e = RandomContraction(vertices[:], edges[:])
#print v, len(e)
if len(e) < minimum:
minimum = len(e)

print "min cut: %d" % minimum```

Your task is to code up and run the randomized contraction algorithm for the min cut problem and use it on the above graph to compute the min cut. (HINT: Note that you'll have to figure out an implementation of edge contractions. Initially, you might want to do this naively, creating a new graph from the old every time there's an edge contraction. But you should also think about more efficient implementations.) (WARNING: As per the video lectures, please make sure to run the algorithm many times with different random seeds, and remember the smallest cut that you ever find.) Write your numeric answer in the space provided. So e.g., if your answer is 5, just type 5 in the space provided. 