Algorithms Part 1-Question 3- the min cut problem-最小割問題

來源:互聯網
上載者:User
Algorithms: Design and Analysis, Part 1 

Download the text file here. (Right click and save link as)

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

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.

  第三單元課件中的演算法python實現代碼如下:

import copyimport randomdef contraCut(mapD,edgeList):    while len(mapD)>2:        [u,v]=edgeList.pop(random.randrange(0,len(edgeList)-1))        while([v,u] in edgeList):            edgeList.remove([v,u])        while([u,v] in edgeList):            edgeList.remove([u,v])        for ind in range(0,len(edgeList)):            if edgeList[ind][0]==v:edgeList[ind][0]=u            if edgeList[ind][1]==v:edgeList[ind][1]=u        mapD[u]=mapD[u]-{v}        mapD[v]=mapD[v]-{u}        for [x,y] in mapD.items():            if v in y:                mapD[x]=(mapD[x]|{u})-{v}        mapD[u]=mapD[u]|mapD[v]        del mapD[v]    return len(edgeList)/2if __name__ == '__main__':    f=open('kargerMinCut.txt','r')    mapDict={}    for line in f.readlines():        tmp=[int(x) for x in line.split()]        mapDict[tmp[0]]=set(tmp[1:])    f.close()        edgeList=[]    for [x,y] in mapDict.items():        edgeList.extend([[x,v] for v in y])        numList=[]    for i in range(20):        cpmapDict=copy.deepcopy(mapDict)        cpedgeList=copy.deepcopy(edgeList)                #print cpmapDict        num=contraCut(cpmapDict,cpedgeList)        numList.append(num)        numList.sort()        print num,        i+=1    print numList

  讀取的txt檔案sample如下:

1 3 52 4 53 14 25 1 2

  每行第一列代表pointer,後面跟的是鄰接點。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.