Algorithms Part 1-Question 3- the min cut problem-最小割问题

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<nobr style="border:0px; padding:0px; margin:0px; max-width:none; max-height:none; vertical-align:0px; line-height:normal; text-decoration:none; white-space:nowrap!important"><span class="math" id="MathJax-Span-1" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:23px; height:0px; font-size:18px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mrow" id="MathJax-Span-2" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="msubsup" id="MathJax-Span-3" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:22.4px; height:0px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mn" id="MathJax-Span-4" style="font-family:MathJax_Main; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none">6</span></span><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-48.3px; left:9px"><span class="texatom" id="MathJax-Span-5" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mrow" id="MathJax-Span-6" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mi" id="MathJax-Span-7" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">t</span><span class="mi" id="MathJax-Span-8" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">h</span></span></span></span></span></span></span></span></span></span></nobr>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 copy
import random

def 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)/2

if __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 5
2 4 5
3 1
4 2
5 1 2

每行第一列代表pointer,后面跟的是邻接点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《算法导论》Hardcover版的引言(Introduction to Algorithms - Hardcover Edition)是一本经典的计算机科学教材。该版本不仅在内容上与平装版相同,还具有精美的硬皮封面,能够更好地保护书籍,并增添一份高质感和专业感。 首先,这本书是由Thomas H. Cormen等四位作者共同编写。他们是计算机科学领域的权威人物,在算法研究和教育方面具有丰富的经验。这本书的目的是为计算机科学专业的学生和从业人员提供系统而全面的算法知识,帮助他们深入理解和应用算法。 《算法导论》Hardcover版首先介绍了算法设计和分析的基础知识,包括分治法、贪婪算法、动态规划和回溯法等。接着,书中详细阐述了各种经典算法,如排序、图算法、字符串匹配、高级数据结构等。此外,书中还介绍了算法的优化技巧和应用领域,例如算法的并行化和近似算法。 与平装版相比,Hardcover版的封面更加美观,书页由高品质纸张制成,更加耐用。这使得读者在长时间研究和使用这本书时,能够更好地保存它的完整性和精美外观。此外,Hardcover版也更加适合作为礼品或收藏品,体现了读者对该书的重视和对算法学习的热爱。 总之,《算法导论》Hardcover版是一本内容丰富、思想深刻的算法教材,通过系统化的介绍和实例,帮助读者深入理解和应用各种算法。同时,Hardcover版的精美外观和耐用性也增强了读者在日常使用和收藏方面的满意度。无论是学习算法的新手还是资深专家,都能从这本书中获得极大的收益。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值