算法习题解答(二)

算法习题解答(二)

ID:5337271

大小:99.20 KB

页数:4页

时间:2017-12-08

算法习题解答(二)_第1页
算法习题解答(二)_第2页
算法习题解答(二)_第3页
算法习题解答(二)_第4页
资源描述:

《算法习题解答(二)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、Homework6Solutions1.22.2-6(pg.539)Therearetwotypesofprofessionalwrestlers:goodguys,andbadguys.Betweenanypairofprofessionalwrestlers,theremayormaynotbearivalry.Supposethatwehavenprofessionalwrestlersandwehavealistofrpairsofwrestlersforwhichtherearerivalrie

2、s.GiveanO(n+r)-timealgorithmthatdetermineswhetheritispossibletodesignatesomeofthewrestlersasgoodguysandtheremainderasbadguyssuchthateachrivalryisbetweenagoodguyandabadguy.Ifitispossibletoperformsuchadesignationyouralgorithmshouldproduceit.Assumptions:1.Ar

3、ivalry(A,B)representsthefactthatAhasarivalrywithB.ItdoesnotspecifywhetherornotAorBisthegoodorbadguy.2.Somewrestlersmaynotbeinvolvedinrivalries.Thesewrestlersareneithergoodguysnorbadguys.Thealgorithmisnotconcernedwiththem.3.Anypossiblesolutionofgoodguysand

4、badguysisacceptable.Forexample,considerthefollowingsetup:Wrestlers={A,B,C,D,E},Rivalries={(A,B),(B,C),(D,E)}Therearefourpossiblesolutionsforthissetup:1.Goodguys={A,C,D},Badguys={B,E}2.Goodguys={A,C,E},Badguys=(B,D}3.Goodguys={B,E},Badguys={A,C,D}4.Goodguy

5、s={B,D},Badguys={A,C,E}Ancorrectalgorithmcouldreturnanyoneofthesesolutions.Setup:Werepresenttherivalriesasagraph,G=(V,E).Theverticesarethewrestlers,andtheedgesaretherivalries.Therefore,

6、V

7、=n,and

8、E

9、=r.Algorithm:1.Discardallverticesofdegree0.Thesearewrestle

10、rswhohavenorivalries.Wearenotconcernedwiththem.2.Separateallconnectedcomponentsoftheremaininggraph.Processeachcomponentindividuallyinthefollowingsteps.3.LetC=(Vc,Ec)betheconnectedcomponent.Chooseonevertexratrandom(ordeterministically-itdoesn’tmatter)fromV

11、c.Withoutlossofgenerality(remember,anypossiblesolution,iscorrect),letrbeclassifiedasaGoodguy.4.Runamodifiedbreadth-firstsearchfromtheroot.Insteadofmaintainingd[]andf[]arrays,savethepath-lengthfromeachvertextor.AllverticeswithoddpathlengthstorareBadguys;al

12、lverticeswithevenpathlengthstorareGoodguys.5.ExamineeveryedgeinEc.Iftheedgeisbetweentwoverticeswhosepathlengthstorarebothevenorbothodd,thenwehaveestablishedarivalrybetweentwoGoodorBadguys.Ifthisisthecase,wereturnfal

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。