3 3 4 8 6 3 3 4 8 6 3 3 4 8 6 () (d) 3 Sarching Linkd Lists Sarching Linkd Lists Sarching Linkd Lists ssum th list is sortd, but is stord in a linkd list. an w us binary sarch? omparisons? Work? What if w add additional pointrs? Sam. Is this a good modl? No. ssum th list is sortd, but is stord in a linkd list. an w us binary sarch? omparisons? Work? Much highr sinc w must mov around a lot (without comparisons) to gt to th sam position. Might gt to dsird position fastr. What if w add additional pointrs? : Thory of lgorithms Spring 4 79 / 4 Prfct Skip List 3 3 4 8 6 Prfct Skip List What is th accss tim? W can insrt/dlt in log n tim as wll. Prfct Skip List 3 3 4 8 6 3 3 4 8 6 : Thory of lgorithms Spring 4 8 / 4 uilding a Skip List uilding a Skip List uilding a Skip List Pick th nod siz at random (from a suitabl probability distribution). Pick th nod siz at random (from a suitabl probability distribution). (d) 3 () : Thory of lgorithms Spring 4 8 / 4 Skip List nalysis () What distribution do w want for th nod dpths? int randomlvl(void) { // xponntial distrib for (int lvl=; Random() == ; lvl++); rturn lvl; What is th worst cost to sarch in th prfct Skip List? What is th avrag cost to sarch in th prfct Skip List? What is th cost to insrt? Skip List nalysis () Skip List nalysis () What distribution do w want for th nod dpths? int randomlvl(void) { // xponntial distrib for (int lvl=; Random() == ; lvl++); rturn lvl; What is th worst cost to sarch in th prfct Skip List? What is th avrag cost to sarch in th prfct Skip List? What is th cost to insrt? What is th avrag cost in th typical Skip List? xponntial dcay. link half of th tim, links on quartr, 3 links on ighth, and so on. los to What is th avrag cost in th typical Skip List? : Thory of lgorithms Spring 4 8 / 4
Skip List nalysis () Skip List nalysis () Skip List nalysis () How dos this diffr from a ST? Simplr or mor complx? Mor or lss fficint? Which rlis on data distribution, which on basic laws of probability? bout th sam. How dos this diffr from a ST? Simplr or mor complx? Mor or lss fficint? Which rlis on data distribution, which on basic laws of probability? On avrag, about th sam if data ar wll distributd. ST rlis on data distribution, whil skiplist mrly rlis on chanc. : Thory of lgorithms Spring 4 83 / 4 Quicksort runs into troubl on highly structurd input. Solution: Randomiz input ordr. hanc of worst cas is thn /n!. Quicksort runs into troubl on highly structurd input. Solution: Randomiz input ordr. hanc of worst cas is thn /n!. This principl is why, for xampl, th Skip List data structur has much mor rliabl prformanc than a ST. Th ST s prformanc dpnds on th input data. Th Skip List s prformanc dpnds ntirly on chanc. or random data, th two ar ssntially idntical. ut you can t trust data to b random. : Thory of lgorithms Spring 4 84 / 4 Random Numbr Gnrators Random Numbr Gnrators Random Numbr Gnrators Most computrs systms us a dtrministic algorithm to slct psudorandom numbrs. Linar congruntial mthod: Pick a sd r(). Thn, r(i) = (r(i ) b) mod t. Must pick good valus for b and t. Rsulting numbrs must b in th rang: What happns if r(i) = r(j)? Most computrs systms us a dtrministic algorithm to slct psudorandom numbrs. Linar congruntial mthod: Pick a sd r(). Thn, r(i) = (r(i ) b) mod t. Lots of commrcial random numbr gnrators hav poor prformanc bcaus thy don t gt th numbrs right. Must b in rang to t. Thy gnrat th sam numbr, which lads to ycl of lngth j i. Must pick good valus for b and t. Rsulting numbrs must b in th rang: What happns if r(i) = r(j)? : Thory of lgorithms Spring 4 8 / 4 Random Numbr Gnrators (cont) Random Numbr Gnrators (cont) Random Numbr Gnrators (cont) Som xampls: r(i) = 6r(i ) mod 3 =, 6,, 8, 9,,, 7, 3,, 4,, r(i) = 7r(i ) mod 3 =, 7,,, 9,,, 6, 3, 8, 4,, r(i) = r(i ) mod 3 =,,, 8,,,, 3, 4, 7, 9, 6, 4, Th last on dpnds on th start valu of th sd. Suggstd gnrator: r(i) = 687r(i ) mod 3 Som xampls: r(i) = 6r(i ) mod 3 =, 6,, 8, 9,,, 7, 3,, 4,, r(i) = 7r(i ) mod 3 =, 7,,, 9,,, 6, 3, 8, 4,, r(i) = r(i ) mod 3 =,,, 8,,,, 3, 4, 7, 9, 6, 4, Th last on dpnds on th start valu of th sd. Suggstd gnrator: r(i) = 687r(i ) mod 3 : Thory of lgorithms Spring 4 86 / 4
Graph lgorithms Graph lgorithms Graph lgorithms Graphs ar usful for rprsnting a varity of concpts: ata Structurs Rlationships amilis ommunication Ntworks Road Maps Graphs ar usful for rprsnting a varity of concpts: ata Structurs Rlationships amilis ommunication Ntworks Road Maps : Thory of lgorithms Spring 4 87 / 4 Tr Proof finition: fr tr is onnctd, undirctd graph that has no cycls. Thorm: If T is a fr tr having n vrtics, thn T has xactly n dgs. Proof: y induction on n. as as: n =. T consists of vrtx and dgs. Inductiv Hypothsis: Th thorm is tru for a tr having n vrtics. If T has n vrtics, thn T contains a vrtx of dgr. Rmov that vrtx and its incidnt dg to obtain T, a fr tr with n vrtics. y IH, T has n dgs. Thus, T has n dgs. graph G = (V, ) consists of a st of vrtics V, and a st of dgs, such that ach dg in is onnction btwn a pair of vrtics in V. irctd vs. Undirctd Labld graph, wightd graph Labls for dgs vs. wights for dgs Multipl dgs, loops ycl, ircuit, path, simpl path, tours ipartit, acyclic, connctd Rootd tr, unrootd tr, fr tr Tr Proof Tr Proof finition: fr tr is onnctd, undirctd graph that has no cycls. Thorm: If T is a fr tr having n vrtics, thn T has xactly n dgs. Proof: y induction on n. as as: n =. T consists of vrtx and dgs. Inductiv Hypothsis: Th thorm is tru for a tr having n vrtics. If T has n vrtics, thn T contains a vrtx of dgr. Rmov that vrtx and its incidnt dg to obtain T, a fr tr with n vrtics. y IH, T has n dgs. Thus, T has n dgs. This is clos to a satisfactory dfinition for fr tr. Thr ar svral quivalnt dfinitions for fr trs, with similar proofs to rlat thm. Why do w know that som vrtx has dgr? caus th dfinition says that th r Tr has no cycls. : Thory of lgorithms Spring 4 88 / 4 Graph Travrsals Graph Travrsals Graph Travrsals Various problms rquir a way to travrs a graph that is, visit ach vrtx and dg in a systmatic way. Thr common travrsals: ulrian tours Travrs ach dg xactly onc pth-first sarch Kps vrtics on a stack 3 radth-first sarch Kps vrtics on a quu Various problms rquir a way to travrs a graph that is, visit ach vrtx and dg in a systmatic way. a vrtx may b visitd multipl tims Thr common travrsals: ulrian tours Travrs ach dg xactly onc pth-first sarch Kps vrtics on a stack 3 radth-first sarch Kps vrtics on a quu : Thory of lgorithms Spring 4 89 / 4 ulrian Tours ulrian Tours ulrian Tours circuit that contains vry dg xactly onc. xampl: f Tour: b a f c d. f xampl: g No ulrian tour. How can you tll for sur? circuit that contains vry dg xactly onc. xampl: f Tour: b a f c d. f xampl: g No ulrian tour. How can you tll for sur? Why no tour? caus som vrtics hav odd dgr. ll vn nods is a ncssary condition. Is it sufficint? : Thory of lgorithms Spring 4 9 / 4
ulrian Tour Proof Thorm: connctd, undirctd graph with m dgs that has no vrtics of odd dgr has an ulrian tour. Proof: y induction on m. as as: Inductiv Hypothsis: Start with an arbitrary vrtx and follow a path until you rturn to th vrtx. Rmov this circuit. What rmains ar connctd componnts G, G,..., G k ach with nods of vn dgr and < m dgs. y IH, ach connctd componnt has an ulrian tour. ombin th tours to gt a tour of th ntir graph. ulrian Tour Proof ulrian Tour Proof Thorm: connctd, undirctd graph with m dgs that has no vrtics of odd dgr has an ulrian tour. Proof: y induction on m. as as: Inductiv Hypothsis: Start with an arbitrary vrtx and follow a path until you rturn to th vrtx. Rmov this circuit. What rmains ar connctd componnts G, G,..., Gk ach with nods of vn dgr and < m dgs. y IH, ach connctd componnt has an ulrian tour. ombin th tours to gt a tour of th ntir graph. as cas: dgs and vrtx fits th thorm. IH: Th thorm is tru for < m dgs. lways possibl to find ircuit starting at any arbitrary vrtx, sinc ach vrtx has vn dgr. : Thory of lgorithms Spring 4 9 / 4 pth irst Sarch pth irst Sarch pth irst Sarch void S(Graph G, int v) { // pth first sarch PrVisit(G, v); // Tak appropriat action G.stMark(v, VISIT); for (dg w = ach nighbor of v) if (G.gtMark(G.v(w)) == UNVISIT) S(G, G.v(w)); PostVisit(G, v); // Tak appropriat action Initial call: S(G, r) whr r is th root of th S. ost: Θ( V + ). void S(Graph G, int v) { // pth first sarch PrVisit(G, v); // Tak appropriat action G.stMark(v, VISIT); for (dg w = ach nighbor of v) if (G.gtMark(G.v(w)) == UNVISIT) S(G, G.v(w)); PostVisit(G, v); // Tak appropriat action Initial call: S(G, r) whr r is th root of th S. ost: Θ( V + ). : Thory of lgorithms Spring 4 9 / 4 pth irst Sarch xampl pth irst Sarch xampl pth irst Sarch xampl Th dirctions ar imposd by th travrsal. This is th pth irst Sarch Tr. : Thory of lgorithms Spring 4 93 / 4 S Tr If w numbr th vrtics in th ordr that thy ar markd, w gt S numbrs. Lmma 7.: vry dg is ithr in th S tr T, or conncts two vrtics of G, on of which is an ancstor of th othr in T. S Tr Rsults: No cross dgs. That is, no dgs conncting vrtics sidways in th tr. S Tr If w numbr th vrtics in th ordr that thy ar markd, w gt S numbrs. Lmma 7.: vry dg is ithr in th S tr T, or conncts two vrtics of G, on of which is an ancstor of th othr in T. Proof: onsidr th first tim an dg (v, w) is xamind, with v th currnt vrtx. If w is unmarkd, thn (v, w) is in T. If w is markd, thn w has a smallr S numbr than v N (v, w) is an unxamind dg of w. Thus, w is still on th stack. That is, w is on a path from v. Proof: onsidr th first tim an dg (v, w) is xamind, with v th currnt vrtx. If w is unmarkd, thn (v, w) is in T. If w is markd, thn w has a smallr S numbr than v N (v, w) is an unxamind dg of w. Thus, w is still on th stack. That is, w is on a path from v. : Thory of lgorithms Spring 4 94 / 4
S for irctd Graphs S for irctd Graphs S for irctd Graphs Main problm: connctd graph may not giv a singl S tr. 6 7 4 orward dgs: (, 3) ack dgs: (, ) 3 9 8 ross dgs: (6, ), (8, 7), (9, ), (9, 8), (4, ) Solution: Maintain a list of unmarkd vrtics. Whnvr on S tr is complt, choos an arbitrary unmarkd vrtx as th root for a nw tr. Main problm: connctd graph may not giv a singl S tr. 6 7 4 orward dgs: (, 3) ack dgs: (, ) 3 9 8 ross dgs: (6, ), (8, 7), (9, ), (9, 8), (4, ) Solution: Maintain a list of unmarkd vrtics. Whnvr on S tr is complt, choos an arbitrary unmarkd vrtx as th root for a nw tr. : Thory of lgorithms Spring 4 9 / 4 irctd ycls Lmma 7.4: Lt G b a dirctd graph. G has a dirctd cycl iff vry S of G producs a back dg. Proof: Suppos a S producs a back dg (v, w). v and w ar in th sam S tr, w an ancstor of v. (v, w) and th path in th tr from w to v form a dirctd cycl. Suppos G has a dirctd cycl. o a S on G. Lt w b th vrtx of with smallst S numbr. Lt (v, w) b th dg of coming into w. v is a dscndant of w in a S tr. Thrfor, (v, w) is a back dg. : Thory of lgorithms Spring 4 96 / 4 irctd ycls S arlir lmma. irctd ycls Lmma 7.4: Lt G b a dirctd graph. G has a dirctd cycl iff vry S of G producs a back dg. Proof: Suppos a S producs a back dg (v, w). v and w ar in th sam S tr, w an ancstor of v. (v, w) and th path in th tr from w to v form a dirctd cycl. Suppos G has a dirctd cycl. o a S on G. Lt w b th vrtx of with smallst S numbr. Lt (v, w) b th dg of coming into w. v is a dscndant of w in a S tr. Thrfor, (v, w) is a back dg.