算法分析與設(shè)計(jì)教學(xué)課件:Chapter 22 Elementary Graph_第1頁(yè)
算法分析與設(shè)計(jì)教學(xué)課件:Chapter 22 Elementary Graph_第2頁(yè)
算法分析與設(shè)計(jì)教學(xué)課件:Chapter 22 Elementary Graph_第3頁(yè)
算法分析與設(shè)計(jì)教學(xué)課件:Chapter 22 Elementary Graph_第4頁(yè)
算法分析與設(shè)計(jì)教學(xué)課件:Chapter 22 Elementary Graph_第5頁(yè)
已閱讀5頁(yè),還剩56頁(yè)未讀 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

T&RTeamofAlgorithmDesignCollegeofComputerScienceandEngineering,CQUAlgorithmAnalysis&Design

IntroductiontoAlgorithmChapter22:

ElementaryGraphAlgorithms

outlinesBasicnotionsofgraphsStandardgraph-searchingalgorithmsDefinitionsGraphG=(V,E)V=setofverticesE=setofedges(VV)|E|=O(|V|2)TypesofGraphsUndirected:edge(u,v)=(v,u);

forallv,(v,v)E(Noselfloops.)Directed:(u,v)isedgefromutov,denotedasuv.Selfloopsareallowed.Weighted:eachedgehasanassociatedweight,givenbyaweightfunctionw:ER.Definitions–continue-If(u,v)E,thenvertexvisadjacenttovertexu.Adjacencyrelationshipis:SymmetricifGisundirected.NotnecessarilysoifGisdirected.IfGisconnected:Thereisapathbetweeneverypairofvertices.|E|

|V|–1.Furthermore,if|E|

=|V|–1,thenGisatree.RepresentationofGraphsTwostandardways.AdjacencyLists.AdjacencyMatrix.adcb

abcdb

a

d

d

c

c

a

b

a

c

adcb1234123410111210103110141010AdjacencyListsConsistsofanarrayAdjof|V|lists.Onelistpervertex.Foru

V,Adj[u]={allverticesadjacenttou}.adcb

abcdb

c

d

d

c

adcb

abcdb

a

d

d

c

c

a

b

a

c

Ifweighted,storeweightsalsoinadjacencylists.StorageRequirementFordirectedgraphs:Sumoflengthsofalladj.listsis

out-degree(v)=|E|

vV

Totalstorage:

(|V|+|E|)Forundirectedgraphs:Sumoflengthsofalladj.listsis

degree(v)=2|E|

vV

Totalstorage:

(|V|+|E|)NumberofedgesleavingvNumberofedgesincidentonv.AdjacencyMatrix|V||V|matrixA.Numberverticesfrom1to|V|Aisthengivenby:adcb1234123410111200103000140000adcb1234123410111210103110141010A=ATforundirectedgraphs.SpaceandTimeSpace:

(|V|2).Notmemoryefficientforlargegraphs.Time:

tolistallverticesadjacenttou:(|V|).Time:

todetermineif(u,v)

E:(1).Canstoreweightsforweightedgraph.

Graph-SearchingAlgorithms

Standard

AlgorithmsSearchingagraph:Systematicallyfollowtheedgesofagraph

tovisittheverticesofthegraphdiscoveringthestructureofagraph.Standardgraph-searchingalgorithms.Breadth-firstSearch(BFS).Depth-firstSearch(DFS).Breadth-FirstSearchInput:

-GraphG=(V,E),eitherdirectedorundirected,

-sourcevertexs

V.Output:

forallv

Vd[v]=lengthofshortestpathfromstov

(d[v]=ifvisnotreachablefroms).[v]=uif(u,v)

islastedgeonshortestpathsv.uisv’spredecessor.breadth-firsttree

=atreewithrootsthatcontainsallreachablevertices.DefinitionsonBSFPathbetweenverticesuandv:

vertices(v1,v2,…,vk)suchthat

u=v1

andv=vk,(vi,vi+1)E,forall1ik-1.Lengthofthepath:Numberofedgesinthepath.Pathissimpleifnovertexisrepeated.PrincipleofBreadth-FirstSearchExpandsthefrontierbetweendiscoveredandundiscoveredverticesuniformly

acrossthebreadthofthefrontier.Avertexis“discovered”thefirsttimeitisencounteredduringthesearch.Avertexis“finished”ifallverticesadjacenttoithavebeendiscovered.BFSforShortestPathsFinishedDiscoveredS111S222222S33333Colorstheverticestokeeptrackofprogress.UndiscoveredSBFS(G,s)1. foreachvertexuinV[G]–{s}2 do

color[u]white3 d[u]

4 [u]nil5 color[s]gray6 d[s]07 [s]nil8 Q9 enqueue(Q,s)10 whileQ11 doudequeue(Q)12 foreachvinAdj[u]13 do

ifcolor[v]=white14 thencolor[v]gray15 d[v]d[u]+116 [v]u17 enqueue(Q,v)18 color[u]blackwhite:undiscoveredgray:discoveredblack:finishedQ:aqueueofdiscoveredverticescolor[v]:colorofvd[v]:distancefromstov[u]:predecessorofvExample(BFS)0rstuvwxyQ:s0frontierExample(BFS)0rstuvwxyQ:s0frontierExample(BFS)101rstuvwxyQ:wr11Example(BFS)10122rstuvwxyQ:rtx122Example(BFS)101222rstuvwxyQ:txv222Example(BFS)1012232rstuvwxyQ:xvu223Example(BFS)10123232rstuvwxyQ:vuy233Example(BFS)10123232rstuvwxyQ:uy33Example(BFS)10123232rstuvwxyQ:y3Example(BFS)10123232rstuvwxyQ:

Example(BFS)10123232rstuvwxyBFTreeBreadth-FirstTreePredecessorsub-graph

ofG=(V,E)withsourcesisG=(V,E)whereV={vV:[v]NIL}+{s}E={([v],v)E:vV-{s}}Gisabreadth-firsttreeif:

VconsistsoftheverticesreachablefromsforallvV,thereisauniquesimplepathfromstovinG

thepathisalsoashortestpathfromstovinG.TheedgesinEarecalledtreeedges.

|E|=|V|-1.AnalysisofBFSInitializationtakesO(|V|).TraversalLoopEachvertexisenqueuedanddequeuedatmostonce,sothetotaltimeforqueuingisO(|V|).Theadjacencylistofeachvertexisscannedatmostonce.Thesumoflengthsofalladjacencylistsis(|E|).TotalrunningtimeofBFSisO(|V|+|E|)CorrectnessofBFS(seeDijkstralater)Exercises22.2-222.2-5ShortTestinClasssComputetheshortestdistancesofeachvertexfromthesourcevertexs,andgivetheBSFtreeofthegraphbelow.Depth-FirstSearch(DFS)Exploreedgesoutofthemostrecentlydiscoveredvertexv.Whenalledgesofvhavebeenexplored,backtracktoitspredecessortoexploreotheredges“Searchasdeepaspossiblefirst.”Continueuntilallverticesarediscovered.Depth-FirstSearchInput:

G=(V,E),directedorundirected.Nosourcevertexgiven!Output:

2timestamps

oneachvertex.d[v]=discoverytime(vturnsfromwhitetogray)f[v]=finishingtime(vturnsfromgraytoblack)[v]:predecessorofv=u,suchthatvwasdiscoveredduringthescanofu’sadjacencylist.ProgramDFS(G)1.foreachvertexuV[G]2.docolor[u]white3.[u]NIL4.time

05.foreachvertexuV[G]6.doifcolor[u]=white7.thenDFS-Visit(u)Usesaglobaltimestamptime.DFS-Visit(u)color[u]GRAYuhasbeendiscoveredtimetime+1

d[u]

time

foreachvAdj[u]

do

if

color[v]=WHITE

then

[v]

uDFS-Visit(v)

color[u]BLACKBlackenu;itisfinished.

f[u]

timetime+1DFS:KindsofedgesDFSintroducesanimportantdistinctionamongedgesintheoriginalgraph:Treeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/uvwxyzTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/2/uvwxyzTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/3/2/uvwxyzTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/4/3/2/uvwxyzTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/4/3/2/uvwxyzBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/4/53/2/uvwxyzBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/4/53/62/uvwxyzBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/4/53/62/7uvwxyzBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/4/53/62/7uvwxyzBFTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/62/7uvwxyzBFTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/62/79/uvwxyzBFTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/62/79/uvwxyzBFCTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/610/2/79/uvwxyzBFCTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/610/2/79/uvwxyzBFCBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/610/112/79/uvwxyzBFCBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesExample(DFS)1/84/53/610/112/79/12uvwxyzBFCBTreeedge:encounternew(white)vertexBackedge:fromdescendenttoancestorForwardedge:fromancestortodescendentCrossedge:betweenatreeorsubtreesClassificationofEdgesTreeedge:

inthedepth-firstforest,byexploring(u,v).Backedge:

(u,v),whereuisadescendantofv(inthedepth-firsttree).(includeself-loop)Forwardedge:

(u,v),wherevisadescendantofu,butnotatreeedge.Crossedge:

anyotheredge.Cangobetweenverticesinsamedepth-firsttreeorindifferentdepth-firsttrees.IdentificationofEdgesEdgetypeforedge(u,v)

canbeidentifiedwhenitisfirstexploredbyDFS.Identificationisbasedonthecolorofv.White–treeedge.Gray–backedge.Black–forwardorcrossedge.IdentificationofEdgesTheorem:InDFSofanundirectedgraph,wegetonlytreeandbackedges.Noforwardorcrossedges.Proofbycontradiction:Assumethere’saforwardedgeButF?edgemustactuallybeabackedge(why?)sourceF?IdentificationofEdgesTheorem:InDFSofanundirectedgraph,wegetonlytreeandbackedges.Noforwardorcrossedges.sourceC?Proofbycontradiction:Assumethere’sacrossedgeButC?edgecannotbecross!Soinfactthepictureiswrong…bothlowertreeedgescannotinfactbetreeedgesDepth-FirstTreesPredecessorsubgraphisslightlydifferentfromthatofBFS.ThepredecessorsubgraphofDFSG=(V,E)

E={([v],v)

:vV

and

[v]NIL}.

Gformsadepth-firstforestcomposedofseveraldepth-firsttrees.Econsistsoftreeedges.Definition:Forest:AnacyclicgraphGthatmaybedisconnected.AnalysisofDFSDFS(G)1.foreachvertexuV[G]2.docolor[u]white3.[u]NIL4.time

05.foreachvertexuV[G]6.doifcolor[u]=white7.thenDFS-Visit(u)Usesaglobaltimestamptime.DFS-Visit(u)color[u]GRAY

uhasbeendiscoveredtimetime+1

d[u]

time

foreachvAdj[u]

do

if

color[v]=WHITE

then

[v]

uDFS-Visit(v)

color[u]BLACK

Blackenu;itisfinished.

f[u]

timetime+1AnalysisofDFSLoopsonlines1-2&5-7take(|V|)time,excludingtimetoexecuteDFS-Visit.DFS-VisitiscalledonceforeachwhitevertexvVwhenit’spaintedgraythefirsttime.Lines3-6ofDFS-Visitisexecuted|Adj[v]|times.ThetotalcostofexecutingDFS-VisitisvV|Adj[v]|=(|E|)

TotalrunningtimeofDFSis

(|V|+|E|).ParenthesisTheoremTheorem22.7Forallu,v,exactlyoneofthefollowingholds:1.d[u]<f[u]<d[v]<f[v]ord[v]<f[v]<d[u]<f[u]andneitherunorvisadescendantoftheother.2.d[u]<d[v]<f[v]<f[u]andvisadescendantofu.3.d[v]<d[u]<f[u]<f[v]anduisadescendantofv.Sod[u]<d[v]<f[u]<f[v]cannothappen.Likeparentheses:OK:()[]([])[()]NotOK:([)][(])Corollaryvisaproperdescendantofuifandonlyifd[u]<d[v]<f[v]<f[u].Example(ParenthesisTheorem)3/64/57/812/132/91/10yzsxwvBF14/1511/16utCCCCB(s(z(y(xx)y)(ww)z)s)(t(vv)(uu)t)White-pathTheorem

Theorem22.9visadescendantofuifandonlyifattimed[u],thereisapathu

vconsistingofonlywhitevertices.(Exceptforu,whichwasjustcoloredgray.)1/uvxyExercises22.3-222.3-322.3-8ShortTestinClass1/sComputethe(d[v]/f[v])foreachvertexvandidentifythetypeofeachedge.

TopologicalSorting

DirectedAcyclicGraphDAG–Directedgraphwithnocycles.Goodformodelingprocessesandstructuresthathaveapartialorder:a>bandb>c

a>c.(transitiveclosure)Butmayhaveaandbsuchthatneithera>bnorb>a.Canalwaysmakeatotalorder

(eithera>borb>aforalla

b)fromapartialorder.ExampleDAGofdependenciesforputtingongoalieequipment.socksshortshosepantsskateslegpadsT-shirtchestpadsweatermaskcatchgloveblockerbattinggloveCharacterizingaDAGProof::Showthatbackedgecycle.Supposethereisabackedge(u,v).Thenvisancestorofuindepth-firstforest.Therefore,thereisapathv

u,sov

u

visacycle.Lemma22.11AdirectedgraphGisacycliciffaDFSofGyieldsnobackedges.vuTTTBCharacterizingaDAGProof(Contd.)::Showthatacycleimpliesabackedge.c

:cycleinG,v:firstvertexdiscoveredinc,(u,v):v’sprecedingedgeinc.Attimed[v],verticesofcformawhitepathvu.Why?Bywhite-paththeorem,uisadescendentofvindepth-firstforest.Therefore,(u,v)

isabackedge.vuTTTBTopologicalSortWantto“

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論