Example: biology

Lecture 7: Minimum Spanning Trees and Prim’s Algorithm

Lecture7:MinimumSpanningTreesandPrim s AlgorithmCLRSC hapter23 OutlineofthisLecture Spanningtreesandminimumspanningtrees. Theminimumspanningtree(MST)problem. Thegeneric algorithmforMSTproblem. Prim s algorithmfortheMSTproblem. Thealgorithm Correctness Implementation+ RunningTime1 SpanningTreesSpanningTrees:Asubgraph of a undirectedgraph is aspanningtreeof if it is a treeandcontainsevery vertex of .Example: abcdeabcdeabcdeabcdeGraphspanningtree1sp anningtree2spanningtree32 SpanningTreesTheorem:Every connectedgraphhasa :Why is thistrue?Question:Givena connectedgraph , how canyoufinda spanningtreeof ?3 WeightedGraphsWeightedGraphs:A weightedgraphis a graph,inwhicheachedgehasa weight(somerealnumber).Weightofa : abcdeabcdeabcdeabcde10973223103223977932 23322310weightedgraphTree2, w=71 Tree3, w= anundirectedconnectedweightedgraphis a spanningtreeofminimumweight(amongallspan ningtrees).

Minimum Spanning Tree Problem MST Problem: Given a connected weighted undi-rected graph , design an algorithm that outputs a minimum spanning tree (MST) of . Question: What is most intuitive way to solve? Generic approach: A tree is an acyclic graph. The idea is to start with an empty graph and try to add

Tags:

  Lecture, Minimum, Tree, Spanning, Algorithm, Pirms, Lecture 7, Minimum spanning trees and prim, Minimum spanning

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Lecture 7: Minimum Spanning Trees and Prim’s Algorithm

1 Lecture7:MinimumSpanningTreesandPrim s AlgorithmCLRSC hapter23 OutlineofthisLecture Spanningtreesandminimumspanningtrees. Theminimumspanningtree(MST)problem. Thegeneric algorithmforMSTproblem. Prim s algorithmfortheMSTproblem. Thealgorithm Correctness Implementation+ RunningTime1 SpanningTreesSpanningTrees:Asubgraph of a undirectedgraph is aspanningtreeof if it is a treeandcontainsevery vertex of .Example: abcdeabcdeabcdeabcdeGraphspanningtree1sp anningtree2spanningtree32 SpanningTreesTheorem:Every connectedgraphhasa :Why is thistrue?Question:Givena connectedgraph , how canyoufinda spanningtreeof ?3 WeightedGraphsWeightedGraphs:A weightedgraphis a graph,inwhicheachedgehasa weight(somerealnumber).Weightofa : abcdeabcdeabcdeabcde10973223103223977932 23322310weightedgraphTree2, w=71 Tree3, w= anundirectedconnectedweightedgraphis a spanningtreeofminimumweight(amongallspan ningtrees).

2 Example: abcdeabcdeabcdeabcde10973223103223977932 23322310weightedgraphTree2, w=71 Tree3, w= :Theminimumspanningtreemay notbeunique. However, if theweightsofalltheedgesarepairwisedistin ct,it is indeedunique(we won tprovethisnow).Example:126724126724 MST1 MST2weighted graph12210067246 MinimumSpanningTreeProblemMSTP roblem:Givena connectedweightedundi-rectedgraph , designanalgorithmthatoutputsaminimumspan ningtree(MST)of .Question:Whatis mostintuitive way to solve?Genericapproach:A treeis to start withanemptygraphandtry to addedgesoneata time, we aresureevery timetheresultinggraphalwaysis a subsetofsomeminimumspanningtree, we bea setofedgessuchthat , where is a MST. Anedge is asafe edgefor , if is alsoa subsetof ateachstep, we canfinda safe edge , wecan grow a MST. Thisleadstothefollowinggenericapproach:G eneric-MST(G,w)Let A=EMPTY;whileA doesnot forma spanningtreefindan edge(u, v) thatis safefor Aadd (u, v) to AreturnAHow canwe finda safe edge?

3 8 How tofinda safe edgeWe firstgive somedefinitions. Let define:CutAcut of G is a partitionof crossesthecut if oneofitsendpointsis in , andtheotherisin .RespectA cutrespectsa set ofedgesif noedgein A cutif itsweightis theminimumofany tofinda safe edgeLemmaLet bea connected,undirectedgraphwitha real-valuedweightfunction definedon . Let bea subsetof thatis includedin someminimumspanningtreefor , let beanycutof thatrespects , andlet bea lightedgecrossingthecut . Then,edge is safe for .It meansthatwe canfinda safe cutthatrespects , a safe , where is a MST. Suppose . is to constructanotherMST thatcon-tainsboth and , thereby showing isa safe edgefor . , and areonoppositesidesofthecut , thereis at leastoneedgein onthepathfrom to besuchedge. Sincethecutrespects , .Since is a lightedgecrossingthecut,wehave .a cut respects Aanother MST T AvuyxAMST to , it createsa cycle.

4 Byremovinganedgefromthecycle, it becomesa , we remove ( ) tomake anew tree . is is a MST, we musthave ,hence is alsoa is alsoa subsetof (aMST), is safe for .13 Prim s AlgorithmThegeneric algorithmgivesusanideahow to grow a youreadthetheoremandtheproofcarefully, youwillnoticethatthechoiceofa cut(andhencethecorrespondinglightedge) canselectany cut(thatrespectsthese-lectededges)andfin dthelightedgecrossingthatcutto salgorithmmakesa naturechoiceof thecutineachiteration it growsa singletreeandaddsalightedgein s Algorithm : How togrow a treeGrow a tree Start by pickingany vertex tobetherootofthetree. Whilethetreedoesnotcontainallverticesin thegraphfindshortestedgeleavingthetreean daddit to .15 MoreDetailsStep0:Chooseany element ; set and . (Take astherootof ourspanningtree.)Step1:Finda lightestedgesuchthatoneendpointis in andtheotheris in.

5 Addthisedgeto andits(other)endpointto .Step2:If , thenstop& output( Minimum )spanningtree . Otherwisegoto :expandthecurrenttreeby addingthelightest(shortest)edgeleavingit 2420261614122323new edge1212hh16 Prim s AlgorithmWorkedExampleabcdefg48982197105 62bcdefg481082179562S={a}Step 0 aV \ S = {b,c,d,e,f,g}9 Connected graphlightest edge = {a,b}17 Prim s AlgorithmPrim s Example Continuedbcdefg4898219710562cdefg8910821 79562aaStep Step after4S={a}S={a,b}bbeforeV \ S = {b,c,d,e,f,g}V \ S = {c,d,e,f,g}lightest edge = {a,b}lightest edge = {b,d}, {a,c}A={}A={{a,b}}18 Prim s AlgorithmPrim s Example Continuedcdefg4898219710562cefg810821795 62aa4bbeforebStep S={a,b}Step afterS={a,b,d}dV \ S = {c,d,e,f,g}V \ S = {c,e,f,g}9lightest edge = {b,d}, {a,c}lightest edge = {d,c}A={{a,b}}A={{a,b},{b,d}}19 Prim s AlgorithmPrim s Example Continuedcefg4898219710562efg89108217956 2aa4bbeforebdStep Step afterS={a,b,d}dS={a,b,c,d}V \ S = {c,e,f,g}V \ S = {e,f,g}clightest edge = {d.}

6 C}lightest edge = {c,f}A={{a,b},{b,d}}A={{a,b},{b,d},{c,d} }20 Prim s AlgorithmPrim s Example Continuedefg4898219710562eg891082179562a a4bbeforebddcS={a,b,c,d}V \ S = {e,f,g}cS={a,b,c,d,f}V \ S = {e,g}Step Step afterflightest edge = {c,f}lightest edge = {f,g}A={{a,b},{b,d},{c,d}}A={{a,b},{b,d} ,{c,d},{c,f}}21 Prim s AlgorithmPrim s Example Continuedeg4898219710562e891082179562aa4 bbeforebddccfS={a,b,c,d,f}V \ S = {e,g}fStep Step afterS={a,b,c,d,f,g}V \ S = {e}{f,g}}glightest edge = {f,g}lightest edge = {f,e}A={{a,b},{b,d},{c,d},{c,f}}A={{a,b} ,{b,d},{c,d},{c,f}, 22 Prim s AlgorithmPrim s Example Continuede4898219710562891082179562aa4bb eforebddccffgS={a,b,c,d,f,g}V \ S = {e}{f,g}}gStep Step afterS={a,b,c,d,e,f,g}V \ S = {}{f,g},{f,e}}MST completedelightest edge = {f,e}A={{a,b},{b,d},{c,d},{c,f}, A={{a,b},{b,d},{c,d},{c,f}, 23 RecallIdeaofPrim s AlgorithmStep0:Chooseany element andset and.

7 (Take astherootof ourspanningtree.)Step1:Finda lightestedgesuchthatoneendpointis in andtheotherisin .Addthisedgeto andits(other)endpointto .Step2:If , thenstopandoutputtheminimumspan-ningtree .Otherwisegoto : Why doesthisproduceaMinimumSpanningTree? How doesthealgorithmfindthelightestedgeandup date efficiently? How doesthealgorithmupdate efficiently?24 Prim s AlgorithmQuestion:How doesthealgorithmupdate efficiently? whenthevertex is movedto . Usecolor[ ]to :Howdoesthealgorithmfindthelightestedgea ndupdate efficiently?Answer:(a)Useapriorityqueuet o findthelightestedge.(b)Usepred[ ]to update .25 ReviewingPriorityQueuesPriorityQueueis a datastructure(canbeimplementedasa heap)whichsupports thefollowingoperations:insert( ):Insert withthekey value in .u = extractMin():Extracttheitemwiththeminimu mkey valuein .decreaseKey( - ):Decrease s key valueto.

8 Remark:PriorityQueuescanbeimplementedsot hateachoperationtakestime . SeeCLRS!26 Usinga PriorityQueuetoFindtheLightestEdgeEachit emof thequeueis a triple , ,where is a vertex in , is theweightof thelightestedgefrom to any vertex in , and is theendpointof thisedgein .Thearray is usedto edgekey[f] = 8, pred[f] = e1212key[i] = infinity, pred[i] = nilkey[i] = 23, pred[i] = fAfter adding the new edgeand vertex f, update the key[v] and pred[v] for each vertex v adjacent to fkey[g] = 16, pred[g] = ckey[h] = 24, pred[h] = bf has the Minimum keyhh27 DescriptionofPrim s AlgorithmRemark: is arestoredin a priorityqueuewithkey=valueoflightestedge tovertex in .Prim( ) foreach initialize ; ; ;start at root ! ;" new PriQueue( );putverticesin"while("is nonempty)untilallverticesin MST u="$#extraxtMin();lightestedgeforeach(%& ' )(* ) if (( %! )&&( %!))

9 ,+ %! )) - . %/ %! ;new lightestedge"$#decreaseKey(% - . %! ); 0 %! ; 12 Whenthealgorithmterminates," andtheMSTis3 % 0 %! $4/%& #ThepredpointersdefinetheMSTasaninverted treerootedat .28 ExampleforRunningPrim s Algorithmabcdef1234511034ukey[u]pred[u]a bcdef29 AnalysisofPrim s AlgorithmLet and . ThedatastructurePriQueuesupports thefollowingtwo operations:(SeeCLRS) toextracteachvertex timetodecreasethekey valueof mostonceforeachedge Totalcostis then 30 AnalysisofPrim s Algorithm ContinuedPrim(G, w, r) { for each (u in V){key[u] = +infinity; color[u] = white; }key[r] = 0; pred[r] = nil; Q = new PriQueue(V); while (Q. nonempty()){u = (); for each (v in adj[u]){if ((color[v] == white) &(w(u,v) < key[v]) key[v] = w(u, v); {}}color[u] = black; }}1O(log n)111pred[v] = u;O(log n)1O(deg(u) log n)1[O(log n) + O(deg(u) log n)]u in (v, key[v]); 31 AnalysisofPrim s Algorithm ContinuedSotheoverallrunningtimeis 32


Related search queries