CRACKING CODING INTERVIEW

Size: px
Start display at page:

Download "CRACKING CODING INTERVIEW"

Transcription

1 CRACKING tlie CODING INTERVIEW 6th Editio 189 Programmig Questios ad Solutios GAYLE LAAKMANN MCDOWELL Fouder ad CEO, CareerCup.com CareerCup, LLC Palo Alto, CA

2 CRACKING THE CODING INTERVIEW,SIXTH EDITION Copyright 2016 by CareerCup. All rights reserved. No part of this book may be reproduced i ay form by ay electroic or mechaical meas, icludig iformatio storage ad retrieval systems, without permissio i writig from the author or publisher,except by a reviewer who may quote brief passages i a review. Published by CareerCup, LLC, Palo Alto, CA. Compiled Ju 14, For more iformatio, cotact support@careercup.com (ISBN 13)

3 XI Advaced Topics Whe writig the 6th editio, I had a umber of debates about what should ad should't be icluded. Red-black trees? Dijkstra's algorith m?topological sort? O oe had, I'd had a umber of requests to iclude these topics. Some people isisted that these topics are asked "all the time"(i which case, they have a very differet idea of what this phrase meas!). There was clearly a desire-at least from some people-to iclude them. Ad learig more ca't hurt, right? O the other had, I kow these topics to be rarely asked. It happes, of course. Iterviewers are idividuals ad might have their ow ideas of what is"fa ir game"or"relevat"for a iterview. But it's rare. Whe it does come up, if you do't kow the topic, it's ulikely to be a big red flag. I Admittedly, as a iterviewer, I have asked cadidates questios where the solutio was essetially a applicatio of oe of these algorithms. O the rare occasios that a cadidate already kew the algorithm, they did ot beefit from this kowledge (or were they hurt by it). I wat to evaluate your ability to solve a problem you have't see before. So, I'll take ito accout whether you kow the uderlyig algorithm i advace. I believe i givig people a fair expectatio of the iterview, ot scarig people ito excess studyig. I also have o iterest i makig the book more "advaced" so as to help book sales, at the expese of your time ad eergy. That's ot fair or right to do to you. (Additioally, I did't wat to give iterviewers-who I kow to be readig this-the impressio that they ca or should be coverig these more advaced topics. Iterviewers: If you ask about these topics, you're testig kowledge of algorithms. You're just goig to wid up elimiatig a lot of perfectly smart people.) But there are may borderlie "importat" topics. They're ot ofte asked, but sometimes they are. Ultimately, I decided to leave the decisio i your hads. After all, you kow better tha I do how thorough you wat to be i your preparatio. If you wat to do a extra thorough job, read this. If you just love learig data structures ad algorithms, read this. If you wat to see ew ways of approachig problems, read this. But if you're pressed for time, this studyig is't a super high priority. Useful Math Here's some math that ca be useful i some questios. There are more formal proofs that you ca look up olie, but we'll focus here o givig you the ituitio behid them. You ca thik of these as iformal proofs. CrackigTheCodiglterv i ew.com I 6th Editio 629

4 XI. Advaced Topics Sum of Itegers 1 through N What is ? Let's figure it out by pairig up low values with high values. If is eve, we pair 1 with, 2 with - 1, ad so o. We will have T pairs each with sum + 1. If is odd, we pair O with, 1 with - 1, ad so o. We will have +!. pairs with sum. is eve is odd pair# a b a+b pair# a b a+b T total: T f+l f *(+l) (+l) I either case, the sum is l total: l !:!.:p- *... This reasoig comes up a lot i ested loops. For example, cosider the followig code: 1 for (iti= 0; i < ; i++) { 2 for (it j = i + 1; j < ; j++) { 3 System.out.pr itl(i+ j); 4 } 5 } O the first iteratio of the outer for loop, the ier for loop iterates - 1 times. O the secod iteratio of the outer for loop, the ier for loop iterates -2 ti mes. Next, - 3, the - 4, ad so o. There are (-ll 2 total iteratios of the ier for loop. Therefore, this code takes 0( 2 ) time. Sum of Powers of 2 Cosider this sequece: ". What is its result? A ice way to see this is by lookig at these values i biary. Power Biary sum: Decimal = 31 Therefore, the sum of " would, i base 2, be a sequece of ( + 1) 1 s. This is 2" Takeaway:The sum of a sequece of powers of two is roughly equal to the ext value i the sequece. Bases of Logs Suppose we have somethig i log 2 (log base 2). How do we covert that to log 10? That is, what's the relatioship betwee log b k ad log,k? 630 Crackig the Codig Iterview, 6th Editio

5 XI. Advaced Topics Let's do some math. Assume c = logbk ad y = log_k. log.k = c --> b' = k // This is the defiitio of log. log,(b') = log,k // Take log of both sides of b' = k. c log,b = log,k // Rules of logs. You ca move out the expoets. C = logbk = l og, xo g, b // Dividig above expressio ad substitutig C. Therefore, if we wat to covert log 2 ptolog 10, wejustd othis: log1p logiop = log210 Takeaway: Logs of differet bases are oly off by a costat factor. For this reaso, we largely igore what the base of a log withi a big O expressio. It does't matter sice we drop costats ayway. Permutatios How may ways are there of rearragig a strig of uique characters?well, you have optios for what to put i the first characters, the - 1 optios for what to put i the secod slot (oe optio is take), the - 2 optios for what to put i the third slot, ad so o. Therefore, the total umber of strigs is!.! = *!l...:.. *!l...:. *!!...: *... * 1 What if you were formig a k-legth strig (with a uique characters) from total uique characters?you ca follow similar logic, but you'd just stop your selectio/multiplicatio earlier. Combiatios! (-k)! Q *!1...:.. *!l...:. *!!...: *... * - k + l Suppose you have a set of distict characters. How may ways are there of selectig k characters ito a ew set (where order does't matter)? That is, how may k-sized subsets a here out of distict elemets? This is what the expressio -choose-k meas, which is ofte writte l k ). Imagie we made a list of all the sets by first writig all k-legth substrigs ad the takig out the dupli cates. From the above Permutatios sectio, we'd have "Yc. kl I k-legth substrigs. Sice each k-sized subset ca be rearraged k! uique ways ito a strig, each subset will be duplicated k! times i this list of substrigs. Therefore, we eed to divide by k! to take out these duplicates. Proof by Iductio ( ) 1 *!! k =rr (-k)! = k!(-k)! Iductio is a way of provig somethig to be true. It is closely related to recursio. It takes the followig form. Task:Prove statemetp{k) istrueforallk >= b. Base Case: Prove the statemet is true for P ( b). This is usually just a matter of pluggig i umbers. Assumptio: Assume the statemet is true for P{ ). Iductive Step: Prove that if the statemet is true for P { ), the it's true for P{ +l). This is like domioes. If the first domio falls, ad oe domio always kocks over the ext oe, the a the domioes must fa. Let's use this to prove that there are 2" subsets of a -elemet set. Defiitios: let S = { a" a 2, a 3,, a.} be the -elemet set. CrackigTheCodiglterv i ew.com I 6th Editio 631

6 XI. Advaced Topics Base case: Prove there are 2 subsets of{}. This is true, sice the oly subset of{} is { }. Assume that there are 2" subsets of { a., a 1, a l, Prove that there are 2" 1 subsets of { a l ' a 2, a ), Cosider the subsets of { a 1, a 1, a l,..., a 1 }. Exactly half will cotai a ad half will ot. The subsets that do ot cotai a 1 are just the subsets of {a., a 1, a l '..., a.}. We assumed there are 2" of those. Sice we have the same umber of subsets with x as without x, there are 2 subsets with a 1. Therefore, we have 2" + 2" subsets, which is 2 1 May recursive algorithms ca be proved valid with iductio....,....,., Topological Sort A topological sort of a directed graph is a way of orderig the list of odes such that if {a, b) is a edge i the graph the a will appear before bi the list. If a graph has cycles or is ot directed, the there is o topological sort. There are a umber of applicatios for th is. For example, suppose the graph represets parts o a assembly lie. The edge (Hadle, Door) idicates that you eed to assemble the hadle before the door. The topological sort would offer a valid orderig for the assembly lie. We ca costruct a topological sort with the followig approach. 1. Idetify all odes with o icomig edges ad add those odes to our topological sort.» We kow those odes are safe to add first sice they have othig that eeds to come before them. Might as we get them over with!» We kow that such a ode must exist if there's o cycle. After all, if we picked a arbitrary ode we could just walk edges backwards arbitrarily. We'll either stop at some poit (i which case we've foud a ode with o icomig edges) or we'll retur to a prior ode (i which case there is a cycle). 2. Whe we do the above, remove each ode's outboud edges from the graph.» Those odes have already bee added to the topological sort, so they're basically irrelevat. We ca't violate those edges aymore. 3. Repeat the above, addig odes with o icomig edges ad removig their outboud edges. Whe all the odes have bee added to the topological sort, the we are doe. More formally, the algorithm is this: 1. Create a queue order, which will evetually store the valid topological sort. It is curretly empty. 2. Create a queue processnext. This queue will store the ext odes to process. 3. Cout the umber of icomig edges of each ode ad set a class variable ode. iboud. Nodes typically oly store their outgoig edges. However, you ca cout the iboud edges by walkig through each ode ad, for each of its outgoig edges {, x), icremetig x. iboud. 4. Walk through theodesagai ad add to processnext ayodewhere x. iboud WhileprocessNext isot empty,dothe followig:» Remove first ode from proce s snext. 632 Crackig the Codig Iterview, 6th Editio

7 XI. Advaced Topics» For each edge (, x), decremet x. i boud.lfx. iboud == 0,apped xto processnext.» Apped to order. 6. If order cotais all the odes, the it has succeeded. Otherwise, the topological sort has failed due to a cycle. This algorithm does sometimes come up i iterview questios. Your iterviewer probably would't expect you to kow it offhad. However, it would be reasoable to have you derive it eve if you've ever see it before. Dijkstra's Algorithm I some graphs, we might wat to have edges with weights. If the graph represeted cities, each edge might represet a road ad its weight might represet the travel time. I this case, we might wat to ask, just as your GPS mappig system does, what's the shortest path from your curret locatio to aother poit p?this is where Dijksta's algorithm comes i. Dijkstra's algorithm is a way to fid the shortest path betwee two poits i a weighted directed graph (which might have cycles). All edges must have positive values. Rather tha just statig what Dijkstra's algorithm is, let's try to derive it. Cosider the earlier described graph. We could fid the shortest path from s to t by literally takig all possible routes usig actual time. (Oh, ad we'll eed a machie to cloe ourselves.) 1. Start off at s. 2. For each of s's outboud edges, cloe ourselves ad start walkig. If the edge ( s, x) has weight 5, we should actually take 5 miutes to get there. 3. Each time we get to a ode, check if ayoe's bee there before. If so, the just stop. We're automatically ot as fast as aother path sice someoe beat us here from s. If o oe has bee here before, the cloe ourselves ad head out i all possible directios. 4. The first oe to get to t wis. This works just fie. But, of course, i the real algorithm we do't wat to literally use a timer to fid the shortest path. Imagie that each cloe could jump immediately from oe ode to its adjacet odes (regardless of the edge weight), but it kept a time_so_ far log of how log its path would have take if it did walk at the "true" speed. Additioally, oly oe perso moves at a time, ad it's always the oe with the lowest time_ so_ far. This is sort of how Dijkstra's algorithm works. Dijkstra's algorithm fids the miimum weight path from a start ode s to every ode o the graph. Cosider the followig graph. CrackigTheCodiglterview.com I 6th Editio 633

8 XI. Advaced Topics Assume we are tryig to fid the shortest path from a to i. We'll use Dijkstra's algorithm to fid the shortest path from a to all other odes, from which we will clearly have the shortest path from a to i. We first iitialize several variables: path_weight [ode]: maps from each ode to the total weight of the shortest path. All values are iitialized to ifiity, except for pat h_weight [a] which is iitialized to 0. pre vious [ode]: maps from each ode to the previous ode i the (curret) shortest path. remaiig: a priority queue of all odes i the graph, where each ode's priority is defied by its path_weight. Oce we've iitialized these values, we ca start adjustig the values of pat h_weight. I A (mi) priority queue is a abstract data type that-at least i this case-supports isertio of a object ad key, removig the object with the miimum key, ad decreasig a key. (Thik of it like a typical queue, except that, istead of removig the oldest item, it removes the item with the lowest or highest priority.) It is a abstract data type because it is defied by its behavior (its operatios). Its uderlyig implemetatio ca vary. You could implemet a priority queue with a array or a mi (or max) heap (or may other data structures). We iterate through the odes i remaiig (util remaiig is empty), doig the followig: 1. Select the ode i remaiig with the lowest value i path_weight. Call this ode. 2. For each adjacet ode, compare pat h_weight[ x] (which is the weight of the curret shortest path from a to x) to path_weight [] + edge_weight [ (, x) ].That is, could we get a path from a to x with lower weight by goig through istead of our curret path? If so, update pat h_weight ad previous. 3. Remove from remaiig. Whe remaiig is empty, the pat h_weight stores the weight of the curret shortest path from a to each ode. We ca recostruct this path by tracig through previous. Let's walk through this o the above graph. 1. The first value of is a. We look at its adjacet odes (b, c, ad e). update the values of pat h_weight (to 5, 3, ad 2) ad previous (to a) ad the remove a from remaiig. 2. The, we go to the ext smallest ode, which is e. We previously updated pat h_weight [ e] to be 2. Its adjacet odes are h ad i, so we update pat h_weight (to 6 ad 9) ad pre vious for both of those. 634 I Crackig the Codig Iterview, 6th Editio

9 XI. Advaced Topics Observe that 6 is path_weight [ e] (which is 2) + the weight of the edge ( e, h) (which is 4). 3. The ext smallest ode is c, which has path_weight 3. Its adjacet odes are b ad d. The value of path_weight [ d] is ifiity, so we update it to 4 (which is path_weight [ c] + weight ( edge c, d). The value of path_weight[b] has bee previously set to 5. However, sice path_weight[c] + weight ( edge c, b) (which is = 4) is less tha 5, we update path_weight [ b] to 4 ad previous to c. This idicates that we would improve the path from a to b by goig through c. We cotiue doig this util remaiig is empty. The followig diagram shows the chages to the path_ weight (left) ad previous (right) at each step. The topmost row shows the curret value for (the ode we are removig from remaiig). We black out a row after it has bee removed from remaiig. INITIAL FINAL wt pr wt pr a 0 0 b 00 4 C C 00 3 a d 00 4 C e 00 2 a f 00 7 h g 00 6 d h 00 5 d 00 8 g Oce we're doe, we ca follow this chart backwards, startig at i to fid the actual path. I this case, the smallest weight path has weight 8 ad is a - > c - > d - > g - > i. Priority Queue ad Rutime As metioed earlier, our algorithm used a priority queue, but this data structure ca be implemeted i differet ways. The rutime of this algorithm depeds heavily o the implemetatio of the priority queue. Assume you have v vertices ad e odes. If you implemeted the priority queue with a array, the you would call remove_mi up to v times. Each operatio would take 0( v) time, so you'd sped 0( v 2 ) time i the remove_mi calls. Additioally, you would update the values of path_weight ad previous at most oce per edge, so that's 0( e) time doig those updates. Observe that e must be less tha of equal to v 2 sice you ca't have more edges tha there are pairs of vertices. Therefore, the total rutime is 0( v 2 ). If you implemeted the priority queue with a mi heap, the the remove_mi calls will each take 0( log v) time (as will isertig ad updatig a key). We will do oe remove_mi call for each vertex, so that's 0( v log v) (v vertices at 0( log v) time each). Additioally, o each edge, we might call oe update key or isert operatio,sothat'so(e log v).the total rutime iso((v + e) log v). Which oe is better? Well, that depeds. If the graph has a lot of edges, the v 2 will be close to e. I this case, you might be better off with the array implemetatio, as O ( v 2 ) is better tha O ( ( v + v 2 ) log v). However, if the graph is sparse, the e is much less tha v 2 I this case, the mi heap implemetatio may be better. CrackigTheCodiglterview.com I 6th Editio 635

10 XI. Advaced Topics Hash Table Collisio Resolutio Essetially ay hash table ca have collisios. There are a umber of ways of hadlig this. Chaiig with Liked Lists With this approach (which is the most commo), the hash table's array maps to a liked list of items. We just add items to this liked list. As log as the umber of collisios is fairly small, this will be quite efficiet. I the worst case, lookup is O ( ), where is the umber of elemets i the hash table. This would oly happe with either some very strage data or a very poor hash fuctio (or both). Chaiig with Biary Search Trees Rather tha storig collisios i a liked list, we could store collisios i a biary search tree. This will brig the worst-case rutime too(log ). I practice, we would rarely take this approach uless we expected a extremely ouiform distributio. Ope Addressig with Liear Probig I this approach, whe a collisio occurs (there is already a item stored at the desigated idex), we just move o to the ext idex i the array util we fid a ope spot. (Or, sometimes, some other fixed distace, like the idex + 5.) If the umber of collisios is low, this is a very fast ad space-efficiet solutio. Oe obvious drawback of this is that the total umber of etries i the hash table is limited by the size of the array. This is ot the case with chaiig. There's aother issue here. Cosider a hash table with a uderlyig array of size 100 where idexes 20 through 29 are filled (ad othig else). What are the odds of the ext isertio goig to idex 30?The odds are 10% because a item mapped to ay idex betwee 20 ad 30 will wid up at idex 30. This causes a issue called clusterig. Quadratic Probig ad Double Hashig The distace betwee probes does ot eed to be liear. You could, for example, icrease the probe distace quadratically. Or, you could use a secod hash fuctio to determie the probe distace. Rabi-Karp Substrig Search The brute force way to search for a substrig S i a larger strig B takes O ( s ( b- s)) time, where s is the legth of S ad bis the legth of B. We do this by searchig through the first b - s + 1 characters i B ad, for each, checkig if the ext s characters match S. The Rabi-Karp algorithm optimizes this with a little trick: if two strigs are the same, they must have the same hash value. (The coverse, however, is ot true. Two differet strigs ca have the same hash value.) Therefore, if we efficietly precompute a hash value for each sequece of s characters withi B, we ca fid the locatios of S i 0( b) time. We the just eed to validate that those locatios really do match S. For example, imagie our hash fuctio was simply the sum of each character (where space= 0, a= 1, b = 2, ad so o). If S is ear ad B = doe a re hearig me, we'd the just be lookig for sequeces where the sum is 24 (e +a+ r). This happes three times. For each of those locatios, we'd check if the strig really is ear. 636 Crackig the Codig Iterview, 6th Editio

11 XI. Advaced Topics char: d 0 e a r e h e a r i g m e code: sum of ext 3: If we computed these sums by doig hash ( 'doe' ), the hash ( 'oe ' ), the hash ( 'e a' ), ad so o, we would still be at 0( s ( b- s)) time. Istead, we compute the hash values by recogizig that hash ( 'oe ' ) hash ( 'doe' ) - code ( 'd ' ) + code(' '). This takes 0( b) time to compute all the hashes. You might argue that, still, i the worst case this will take 0( s ( b-s)) time sice may of the hash values could match. That's absolutely true-for this hash fuctio. I practice, we would use a better rollig hash fuctio, such as the Rabi figerprit. This essetially treats a strig like doe as a base 128 (or however may characters are i our alphabet) umber. hash('doe') = code('d') * code('o') * code('e') * 128 This hash fuctio will allow us to remove the d, shift the o ad e, ad the add i the space. hash('oe ') = (hash('doe') - code('d') * ) * code(' ') This will cosiderably cut dow o the umber of false matches. Usig a good hash fuctio like this will give us expected time complexity ofo( s + b ), although the worst case is 0( sb ). Usage o fthis algorithm comes up fairly frequetly i iterviews, so it's useful to kow that you ca idetify substrigs i liear time. AVL Trees A AVL tree is oe of two commo ways to implemet tree balacig. We will olyd iscuss isertios here, but you ca look up deletios separately if you're iterested. Properties A AVL tree stores i each ode the height of the subtrees rooted at this ode. The, for ay ode, we ca check if it is height balaced: that the height of the left subtree ad the height of the right subtree differ by o more tha oe. This prevets situatios where the tree gets too lopsided. balace() =.left.height -.right.height -1 <= balace() <= 1 Iserts Whe you isert a ode, the balace of some odes might chage to -2 or 2. Therefore, whe we "uwid" the recursive stack, we check ad fix the balace at each ode. We do this through a series of rotatios. Rotatios ca be either left or right rotatios. The right rotatio is a iverse of the left rotatio. CrackigTheCodiglterview.com I 6th Editio 637

12 XI. Advaced Topics RIGHT LEFT Depedig o the balace ad where the imbalace occurs, we fix it i a differet way. Case 1: Balace is 2. I this case, the left's height is two bigger tha the right's height. If the left side is larger, the left subtree's extra odes must be hagig to the left (as i LEFT LEFT SHAPE) or hagig to the right (as i LEFT RIGHT SHAPE). If it looks like the LEFT RIGHT SHAPE, trasform it with the rotatios below ito the LEFT LEFT SHAPE the ito BALANCED. If it looks likethe LEFT LEFT SHAPE already.just trasform it ito BALANCED. LEFT RIGHT SHAPE LEFT LEFT SHAPE BALANCED LEFT ROTATION ---. RIGHT ROTATION ---. Case 2: Balaceis-2. This case is the mirror image of the prior case. The tree will look like either the RIGHT LEFT SHAPE or the RIGHT RIGHT SHAPE. Perform the rotatios below to trasform it ito BALANCED. RIGHT LEFT SHAPE RIGHT RIGHT SHAPE BALANCED RIGHT ROTATION ---. I both cases, "balaced" just meas that the balace of the tree is betwee -1 ad 1. It does ot mea that the balace is 0. We recurse up the tree, fixig ay imbalaces. If we ever achieve a balace of Oo a subtree, the we kow that we have completed all the balaces. This portio of the tree will ot cause aother, higher subtree to have a balace of -2 or 2. If we were doig this o-recursively, the we could break from the loop Crackig the Codig Iterview, 6th Editio

13 XI. Advaced Topics Red-Black Trees Red-black trees (a type of self-balacig biary search tree) do ot esure quite as strict balacig, but the balacig is still good eough to esure 0( log N) isertios, deletios, ad retrievals.they require a bit less memory ad ca rebalace faster (which meas faster isertios ad removals), so they are ofte used i situatios where the tree wil I be modified frequetly. Red-black trees operate by eforcig a quasi-alteratig red ad black colorig (uder certai rules, described below) ad the requirig every path from a ode to its leaves to have the same umber of black odes. Doig so leads to a reasoably balaced tree. The tree below is a red-black tree (where the red odes are idicated with gray): Properties 1. Every ode is either red or black. 2. The root is black. 3. The leaves, which are NULL odes, are cosidered black. 4. Every red ode must have two black childre. That is, a red ode caot have red childre (although a black ode ca have black childre). 5. Every path from a ode to its leaves must have the same umber of black childre. Why It Balaces Property #4 meas that two red odes caot be adjacet i a path (e.g., paret ad child). Therefore, o more tha half the odes i a path ca be red. Cosider two paths from a ode (say, the root) to its leaves. The paths must have the same umber of black odes (property #5), so let's assume that their red ode couts are as differet as possible: oe path cotais the miimum umber of red odes ad the other oe cotais the maximum umber. Path 1 (Mi Red): The miimum umber of red odes is zero. Therefore, path 1 has b odes total. Path 2 (Max Red):The maximum umber of red odes is b, sice red odes must have black childre ad there are b black odes. Therefore, path 2 has 2b odes total. Therefore, eve i the most extreme case, the legths of paths caot differ by more tha a factor of two. That's good eough to esure a 0( log N) fid ad isert rutime. If we ca maitai these properties, we'll have a (sufficietly) balaced tree-good eough to esure 0( log N) isert ad fid, ayway. The questio the is how to maitai these properties efficietly. We'll oly discuss isertio here, but you ca look up deletio o your ow. CrackigTheCodiglterview.com I 6th Editio 639

14 XI. Advaced Topics Isertio Isertig a ew ode ito a red-black tree starts off with a typical biary search tree isertio. New odes are iserted at a leaf, which meas that they replace a black ode. New odes are always colored red ad are give two black leaf ( NULL) odes. Oce we've doe that, we fix ay resultig red-black property violatios. We have two possible violatios: Red violatios: A red ode has a red child (or the root is red). Black violatios: Oe path has more blacks tha aother path. The ode iserted is red. We did't chage the umber of black odes o ay path to a leaf, so we kow that we wo't have a black violatio. However, we might have a red violatio. I the special case that where the root is red, we ca always just tur it black to satisfy property 2, without violatig the other costraits. Otherwise, if there's a red violatio, the this meas that we have a red ode uder aother red ode. Oops! Let's call N the curret ode. Pis N's paret. G is N's gradparet. U is N's ucle ad P's siblig. We kow that: N is red ad P is red, sice we have a red violatio. G is defiitely black, sice we did't previously have a red violatio. The ukow parts are: U could be either red or black. U could be either a left or right child. N could be either a left or right child. By simple combiatorics, that's eight cases to cosider. Fortuately some of these cases will be equivalet. Case 1 : U is red. It does't matter whether U is a left or right child, or whether Pis a left or right child. We ca merge four of our eight cases ito oe. If U is red, we ca just toggle the colors of P, U, ad G. Flip G from black to red. Flip P ad U from red to black. We have't chaged the umber of black odes i ay path. However, by makig G red, we might have created a red violatio with G's paret. If so, we recursively apply the full logic to hadle a red violatio, where this G becomes the ew N. Note that i the geeral recursive case, N, P, ad U may also have subtrees i place of each black NULL (the leaves show). I Case 1, these subtrees stay attached to the same parets, as the tree structure remais uchaged. 640 Crackig the Codig Iterview, 6th Editio

15 XI. Advaced Topics Case 2: U is black. We'll eed to cosider the cofiguratios (left vs. right child) of N ad U. I each case, our goal is to fix up the red violatio (red o top of red) without:» Messig up the orderig of the biary search tree.» Itroducig a black violatio (more black odes o oe path tha aother). If we ca do this, we're good. I each of the cases below, the red violatio is fixed with rotatios that maitai the ode orderig. Further, the below rotatios maitai the exact umber of black odes i each path through the affected portio of the tree that were i place beforehad. The childre of the rotatig sectio are either NULL leaves or subtrees that remai iterally uchaged. Case A: N ad Pare both left childre. We resolve the red violatio with the rotatio of N, P. ad G ad the associated recolorig show below. If you picture the i-order traversal, you ca see the rotatio maitais the ode orderig (a <= N < = b <= P <= c <= G <= U). The tree maitais the same, equal umber of black odes i the path dow to each subtree a, b, c, ad U (which may all be NULL). Case 8: Pis a left child, ad N is a right child. The rotatios i Case B resolve the red violatio ad maitai the i-order property: a < = P < = b < = N < = c < = G < = U. Agai, the cout of the black odes remais costat i each path dow to the leaves (or subtrees). CrackigTheCodiglterview.com I 6th Editio 641

16 XI. Advaced Topics Case C: N ad Pare both right childre. This is a mirror image of case A. Case D: N is a left child, ad Pis a right child. This is a mirror image of case B. I each of Case 2's subcases, the middle elemet by value of N, P, ad G is rotated to become the root of what was G's subtree, ad that elemet ad G swap colors. That said, do ot try to just memorize these cases. Rather, study why they work. How does each oe esure o red violatios, o black violatios, ad o violatios of the biary search tree property? MapReduce MapReduce is used widely i system desig to process large amouts of data. As its ame suggests, a MapReduce program requires you to write a Map step ad a Reduce step. The rest is hadled by the system. 1. The system splits up the data across differet machies. 2. Each machie starts ruig the user-provided Map program. 3. The Map program takes some data ad emits a <key, value> pair. 4. The system-provided Shuffle process reorgaizes the data so that all <key, value> pairs associated with a give key go to the same machie, to be processed by Reduce. 5. The user-provided Reduce program takes a key ad a set of associated values ad "reduces" them i some way, emittig a ew key ad value. The results of this might be fed back ito the Reduce program for more reducig. The typical example of usig MapReduce-basically the "Hello World" of MapReduce-is coutig the frequecy of words withi a set of documets. 642 Crackig the Codig Iterview, 6th Editio

17 XI. Advaced Topics Of course, you could write this as a sigle fuctio that reads i all the data, couts the umber of times each word appears via a hash table, ad the outputs the result. MapReduce allows you to process the documet i parallel. The Map fuctio reads i a documet ad emits just each idividual word ad the cout (which is always 1). The Reduce fuctio reads i keys (words) ad associated values (couts). It emits the sum of the couts. This sum could possibly wid up as iput for aother call to Reduce o the same key (as show i the diagram). 1 void map(strig ame, Strig documet) : 2 for each word w i documet : 3 emit(w, 1) 4 5 void reduce(strig word, Iterator partialcouts ): 6 it sum = 0 7 for each cout i partialcouts : 8 sum += cout 9 emit(word, sum) The diagram below shows how this might work o this example. at do go do go at Spl it Map )at, do, EL do g lgq. go, go o -cro g-. do, go o g.o. a1 g go Here's aother example: You have a list of data i the form {City, Temperature, Date}. Calculate the average temperature i each city every year. For example ((2012, Philadelphia, 58.2). (2011, Philadelphia, 56.6), (2012, Seattle, 45.1 )}. Map: The Map step outputs a key value pair where the key is City _Ye ar ad the value is ( Tempe rature, 1). The'l' reflects that this is the average temperature out of oe data poit.this will be importat for the Reduce step. Reduce: The Reduce step will be give a list of temperatures that correspod with a particular city ad year. It must use these to compute the average temperature for this iput. You caot simply add up the temperatures ad divide by the umber of values. To see this, imagie we have five data poits for a particularcity ad year: 25, 100, 75, 85, 50. The Reduce step might oly get some of this data at oce. If you averaged (75, 85} you would get 80. This might ed up beig iput for aother Reduce step with 50, ad it would be a mistake to just aively average80 ad 50. The 80 has more weight. Therefore, our Reduce step istead takes i ((80, 2), (50, 1 }}, the sums the weighted temperatures. So it does 80 * * 1 ad the divides by (2 + 1) to get a average temperature of 70. It the emits (70, 3). Aother Reduce step might reduce ((25, 1 ), ( 100, 1)} to get (62.5, 2). If we reduce this with (70, 3) we get the fial aswer: (67, 5). I other words, the average temperature i this city for this year was 67 degrees. We could do this i other ways, too. We could have just the city as the key, ad the value be (Year, Temperature, Cout). The Reduce step would do essetially the same thig, but would have to group by Year itself. CrackigTheCodiglterview.com I 6th Editio 643

18 XI. Advaced Topics I may cases, it's useful to thik about what the Reduce step should do first, ad the desig the Map step aroud that. What data does Reduce eed to have to do its job? Additioal Studyig So, you've mastered this material ad you wat to lear eve more? Okay. Here are some topics to get you started: Bellma-Ford Algorithm: Fids the shortest paths from a sigle ode i a weighted directed graph with positive ad egative edges. Floyd-Warshall Algorithm: Fids the shortest paths i a weighted graph with positive or egative weight edges (but o egative weight cycles). Miimum Spaig Trees: I a weighted, coected, udirected graph, a spaig tree is a tree that coects all the vertices. The miimum spaig tree is the spaig tree with miimum weight. There are various algorithms to do this. B-Trees: A self-balacig search tree (ot a biary search tree) that is commoly used o disks or other storage devices. It is similar to a red-black tree, but uses fewer 1/0 operatios. A*: Fid the least-cost path betwee a source ode ad a goal ode (or oe of several goal odes). It exteds Dijkstra's algorithm ad achieves better performace by usig heuristics. Iterval Trees: A extesio of a balaced biary search tree, but storig itervals (low -> high rages) istead of simple values. A hotel could use this to store a list of all reservatios ad the efficietly detect who is stayig at the hotel at a particular time. Graph colorig: A way of colorig the odes i a graph such that o two adjacet vertices have the same color. There are various algorithms to do thigs like determie if a graph ca be colored with oly K colors. P, NP, ad NP-Complete: P, NP, ad NP-Complete refer to classes of problems. P problems are problems that ca be quickly solved (where "quickly" meas polyomial time). NP problems are those where, give a solutio, the solutio ca be quickly verified. NP-Complete problems are a subset of NP problems that ca all be reduced to each other (that is, if you foud a solutio to oe problem, you could tweak the solutio to solve other problems i the set i polyomial time). It is a ope (ad very famous) questio whether P = NP, but the aswer is geerally believed to be o. Combiatorics ad Probability: There are various thigs you ca lear about here, such as radom variables, expected value, ad -choose-k. Bipartite Graph: A bipartite graph is a graph where you ca divide its odes ito two sets such that every edge stretches across the two sets (that is, there is ever a edge betwee two odes i the same set). There is a algorithm to check if a graph is a bipartite graph. Note that a bipartite graph is equivalet to a graph that ca be colored with two colors. Regular Expressios: You should kow that regular expressios exist ad what they ca be used for (roughly). You ca also lear about how a algorithm to match regular expressios would work. Some of the basic sytax behid regular expressios could be useful as well. There is of course a great deal more to data structures ad algorithms. If you're iterested i explorig these topics more deeply, I recommed pickig up the hefty Itroductio to Algorithms ("CLRS" by Carme, Leiserso, Rivest ad Stei) or The Algorithm Desig Maual (by Steve Skiea). 644 j Crackig the Codig Iterview, 6th Editio

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19 CIS Data Structures ad Algorithms with Java Sprig 09 Stacks, Queues, ad Heaps Moday, February 8 / Tuesday, February 9 Stacks ad Queues Recall the stack ad queue ADTs (abstract data types from lecture.

More information

Lecture 5. Counting Sort / Radix Sort

Lecture 5. Counting Sort / Radix Sort Lecture 5. Coutig Sort / Radix Sort T. H. Corme, C. E. Leiserso ad R. L. Rivest Itroductio to Algorithms, 3rd Editio, MIT Press, 2009 Sugkyukwa Uiversity Hyuseug Choo choo@skku.edu Copyright 2000-2018

More information

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1 CS200: Hash Tables Prichard Ch. 13.2 CS200 - Hash Tables 1 Table Implemetatios: average cases Search Add Remove Sorted array-based Usorted array-based Balaced Search Trees O(log ) O() O() O() O(1) O()

More information

Lecture 1: Introduction and Strassen s Algorithm

Lecture 1: Introduction and Strassen s Algorithm 5-750: Graduate Algorithms Jauary 7, 08 Lecture : Itroductio ad Strasse s Algorithm Lecturer: Gary Miller Scribe: Robert Parker Itroductio Machie models I this class, we will primarily use the Radom Access

More information

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8) CIS 11 Data Structures ad Algorithms with Java Fall 017 Big-Oh Notatio Tuesday, September 5 (Make-up Friday, September 8) Learig Goals Review Big-Oh ad lear big/small omega/theta otatios Practice solvig

More information

Big-O Analysis. Asymptotics

Big-O Analysis. Asymptotics Big-O Aalysis 1 Defiitio: Suppose that f() ad g() are oegative fuctios of. The we say that f() is O(g()) provided that there are costats C > 0 ad N > 0 such that for all > N, f() Cg(). Big-O expresses

More information

Module 8-7: Pascal s Triangle and the Binomial Theorem

Module 8-7: Pascal s Triangle and the Binomial Theorem Module 8-7: Pascal s Triagle ad the Biomial Theorem Gregory V. Bard April 5, 017 A Note about Notatio Just to recall, all of the followig mea the same thig: ( 7 7C 4 C4 7 7C4 5 4 ad they are (all proouced

More information

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov Sortig i Liear Time Data Structures ad Algorithms Adrei Bulatov Algorithms Sortig i Liear Time 7-2 Compariso Sorts The oly test that all the algorithms we have cosidered so far is compariso The oly iformatio

More information

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 201 Heaps 201 Goodrich ad Tamassia xkcd. http://xkcd.com/83/. Tree. Used with permissio uder

More information

condition w i B i S maximum u i

condition w i B i S maximum u i ecture 10 Dyamic Programmig 10.1 Kapsack Problem November 1, 2004 ecturer: Kamal Jai Notes: Tobias Holgers We are give a set of items U = {a 1, a 2,..., a }. Each item has a weight w i Z + ad a utility

More information

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015 15-859E: Advaced Algorithms CMU, Sprig 2015 Lecture #2: Radomized MST ad MST Verificatio Jauary 14, 2015 Lecturer: Aupam Gupta Scribe: Yu Zhao 1 Prelimiaries I this lecture we are talkig about two cotets:

More information

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015. Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 2015 Hash Tables xkcd. http://xkcd.com/221/. Radom Number. Used with permissio uder Creative

More information

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming Lecture Notes 6 Itroductio to algorithm aalysis CSS 501 Data Structures ad Object-Orieted Programmig Readig for this lecture: Carrao, Chapter 10 To be covered i this lecture: Itroductio to algorithm aalysis

More information

BST Sequence of Operations

BST Sequence of Operations Splay Trees Problems with BSTs Because the shape of a BST is determied by the order that data is iserted, we ru the risk of trees that are essetially lists 12 21 20 32 24 37 15 40 55 56 77 2 BST Sequece

More information

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames Uit 4, Part 3 Recursio Computer Sciece S-111 Harvard Uiversity David G. Sulliva, Ph.D. Review: Method Frames Whe you make a method call, the Java rutime sets aside a block of memory kow as the frame of

More information

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

ECE4050 Data Structures and Algorithms. Lecture 6: Searching ECE4050 Data Structures ad Algorithms Lecture 6: Searchig 1 Search Give: Distict keys k 1, k 2,, k ad collectio L of records of the form (k 1, I 1 ), (k 2, I 2 ),, (k, I ) where I j is the iformatio associated

More information

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions U.C. Berkeley CS170 : Algorithms Midterm 1 Solutios Lecturers: Sajam Garg ad Prasad Raghavedra Feb 1, 017 Midterm 1 Solutios 1. (4 poits) For the directed graph below, fid all the strogly coected compoets

More information

The isoperimetric problem on the hypercube

The isoperimetric problem on the hypercube The isoperimetric problem o the hypercube Prepared by: Steve Butler November 2, 2005 1 The isoperimetric problem We will cosider the -dimesioal hypercube Q Recall that the hypercube Q is a graph whose

More information

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13 CIS Data Structures ad Algorithms with Java Sprig 08 Stacks ad Queues Moday, February / Tuesday, February Learig Goals Durig this lab, you will: Review stacks ad queues. Lear amortized ruig time aalysis

More information

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000. 5-23 The course that gives CM its Zip Memory Maagemet II: Dyamic Storage Allocatio Mar 6, 2000 Topics Segregated lists Buddy system Garbage collectio Mark ad Sweep Copyig eferece coutig Basic allocator

More information

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU) Graphs Miimum Spaig Trees Slides by Rose Hoberma (CMU) Problem: Layig Telephoe Wire Cetral office 2 Wirig: Naïve Approach Cetral office Expesive! 3 Wirig: Better Approach Cetral office Miimize the total

More information

How do we evaluate algorithms?

How do we evaluate algorithms? F2 Readig referece: chapter 2 + slides Algorithm complexity Big O ad big Ω To calculate ruig time Aalysis of recursive Algorithms Next time: Litterature: slides mostly The first Algorithm desig methods:

More information

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Pseudocode ( 1.1) High-level descriptio of a algorithm More structured

More information

. Written in factored form it is easy to see that the roots are 2, 2, i,

. Written in factored form it is easy to see that the roots are 2, 2, i, CMPS A Itroductio to Programmig Programmig Assigmet 4 I this assigmet you will write a java program that determies the real roots of a polyomial that lie withi a specified rage. Recall that the roots (or

More information

6.854J / J Advanced Algorithms Fall 2008

6.854J / J Advanced Algorithms Fall 2008 MIT OpeCourseWare http://ocw.mit.edu 6.854J / 18.415J Advaced Algorithms Fall 2008 For iformatio about citig these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 18.415/6.854 Advaced Algorithms

More information

Priority Queues. Binary Heaps

Priority Queues. Binary Heaps Priority Queues Biary Heaps Priority Queues Priority: some property of a object that allows it to be prioritized with respect to other objects of the same type Mi Priority Queue: homogeeous collectio of

More information

Big-O Analysis. Asymptotics

Big-O Analysis. Asymptotics Big-O Aalysis 1 Defiitio: Suppose that f() ad g() are oegative fuctios of. The we say that f() is O(g()) provided that there are costats C > 0 ad N > 0 such that for all > N, f() Cg(). Big-O expresses

More information

The Magma Database file formats

The Magma Database file formats The Magma Database file formats Adrew Gaylard, Bret Pikey, ad Mart-Mari Breedt Johaesburg, South Africa 15th May 2006 1 Summary Magma is a ope-source object database created by Chris Muller, of Kasas City,

More information

1.2 Binomial Coefficients and Subsets

1.2 Binomial Coefficients and Subsets 1.2. BINOMIAL COEFFICIENTS AND SUBSETS 13 1.2 Biomial Coefficiets ad Subsets 1.2-1 The loop below is part of a program to determie the umber of triagles formed by poits i the plae. for i =1 to for j =

More information

CIS 121. Introduction to Trees

CIS 121. Introduction to Trees CIS 121 Itroductio to Trees 1 Tree ADT Tree defiitio q A tree is a set of odes which may be empty q If ot empty, the there is a distiguished ode r, called root ad zero or more o-empty subtrees T 1, T 2,

More information

Homework 1 Solutions MA 522 Fall 2017

Homework 1 Solutions MA 522 Fall 2017 Homework 1 Solutios MA 5 Fall 017 1. Cosider the searchig problem: Iput A sequece of umbers A = [a 1,..., a ] ad a value v. Output A idex i such that v = A[i] or the special value NIL if v does ot appear

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions: CS 604 Data Structures Midterm Sprig, 00 VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Istructios: Prit your ame i the space provided below. This examiatio is closed book ad closed

More information

6.851: Advanced Data Structures Spring Lecture 17 April 24

6.851: Advanced Data Structures Spring Lecture 17 April 24 6.851: Advaced Data Structures Sprig 2012 Prof. Erik Demaie Lecture 17 April 24 Scribes: David Bejami(2012), Li Fei(2012), Yuzhi Zheg(2012),Morteza Zadimoghaddam(2010), Aaro Berstei(2007) 1 Overview Up

More information

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis Itro to Algorithm Aalysis Aalysis Metrics Slides. Table of Cotets. Aalysis Metrics 3. Exact Aalysis Rules 4. Simple Summatio 5. Summatio Formulas 6. Order of Magitude 7. Big-O otatio 8. Big-O Theorems

More information

Lower Bounds for Sorting

Lower Bounds for Sorting Liear Sortig Topics Covered: Lower Bouds for Sortig Coutig Sort Radix Sort Bucket Sort Lower Bouds for Sortig Compariso vs. o-compariso sortig Decisio tree model Worst case lower boud Compariso Sortig

More information

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5 Morga Kaufma Publishers 26 February, 28 COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Iterface 5 th Editio Chapter 5 Set-Associative Cache Architecture Performace Summary Whe CPU performace icreases:

More information

CSE 417: Algorithms and Computational Complexity

CSE 417: Algorithms and Computational Complexity Time CSE 47: Algorithms ad Computatioal Readig assigmet Read Chapter of The ALGORITHM Desig Maual Aalysis & Sortig Autum 00 Paul Beame aalysis Problem size Worst-case complexity: max # steps algorithm

More information

Ones Assignment Method for Solving Traveling Salesman Problem

Ones Assignment Method for Solving Traveling Salesman Problem Joural of mathematics ad computer sciece 0 (0), 58-65 Oes Assigmet Method for Solvig Travelig Salesma Problem Hadi Basirzadeh Departmet of Mathematics, Shahid Chamra Uiversity, Ahvaz, Ira Article history:

More information

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein 068.670 Subliear Time Algorithms November, 0 Lecture 6 Lecturer: Roitt Rubifeld Scribes: Che Ziv, Eliav Buchik, Ophir Arie, Joatha Gradstei Lesso overview. Usig the oracle reductio framework for approximatig

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Pytho Programmig: A Itroductio to Computer Sciece Chapter 6 Defiig Fuctios Pytho Programmig, 2/e 1 Objectives To uderstad why programmers divide programs up ito sets of cooperatig fuctios. To be able to

More information

Alpha Individual Solutions MAΘ National Convention 2013

Alpha Individual Solutions MAΘ National Convention 2013 Alpha Idividual Solutios MAΘ Natioal Covetio 0 Aswers:. D. A. C 4. D 5. C 6. B 7. A 8. C 9. D 0. B. B. A. D 4. C 5. A 6. C 7. B 8. A 9. A 0. C. E. B. D 4. C 5. A 6. D 7. B 8. C 9. D 0. B TB. 570 TB. 5

More information

Minimum Spanning Trees

Minimum Spanning Trees Miimum Spaig Trees Miimum Spaig Trees Spaig subgraph Subgraph of a graph G cotaiig all the vertices of G Spaig tree Spaig subgraph that is itself a (free) tree Miimum spaig tree (MST) Spaig tree of a weighted

More information

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling Greedy Algorithms Greedy Algorithms Witer Paul Beame Hard to defie exactly but ca give geeral properties Solutio is built i small steps Decisios o how to build the solutio are made to maximize some criterio

More information

Numerical Methods Lecture 6 - Curve Fitting Techniques

Numerical Methods Lecture 6 - Curve Fitting Techniques Numerical Methods Lecture 6 - Curve Fittig Techiques Topics motivatio iterpolatio liear regressio higher order polyomial form expoetial form Curve fittig - motivatio For root fidig, we used a give fuctio

More information

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n)) ca see that time required to search/sort grows with size of We How do space/time eeds of program grow with iput size? iput. time: cout umber of operatios as fuctio of iput Executio size operatio Assigmet:

More information

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence _9.qxd // : AM Page Chapter 9 Sequeces, Series, ad Probability 9. Sequeces ad Series What you should lear Use sequece otatio to write the terms of sequeces. Use factorial otatio. Use summatio otatio to

More information

1 Graph Sparsfication

1 Graph Sparsfication CME 305: Discrete Mathematics ad Algorithms 1 Graph Sparsficatio I this sectio we discuss the approximatio of a graph G(V, E) by a sparse graph H(V, F ) o the same vertex set. I particular, we cosider

More information

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms. Chapter 5 Sortig IST311 - CIS65/506 Clevelad State Uiversity Prof. Victor Matos Adapted from: Itroductio to Java Programmig: Comprehesive Versio, Eighth Editio by Y. Daiel Liag why study sortig? Sortig

More information

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs What are we goig to lear? CSC316-003 Data Structures Aalysis of Algorithms Computer Sciece North Carolia State Uiversity Need to say that some algorithms are better tha others Criteria for evaluatio Structure

More information

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a 4. [10] Usig a combiatorial argumet, prove that for 1: = 0 = Let A ad B be disjoit sets of cardiality each ad C = A B. How may subsets of C are there of cardiality. We are selectig elemets for such a subset

More information

Data Structures Week #5. Trees (Ağaçlar)

Data Structures Week #5. Trees (Ağaçlar) Data Structures Week #5 Trees Ağaçlar) Trees Ağaçlar) Toros Gökarı Avrupa Gökarı October 28, 2014 Boraha Tümer, Ph.D. 2 Trees Ağaçlar) October 28, 2014 Boraha Tümer, Ph.D. 3 Outlie Trees Deiitios Implemetatio

More information

Computational Geometry

Computational Geometry Computatioal Geometry Chapter 4 Liear programmig Duality Smallest eclosig disk O the Ageda Liear Programmig Slides courtesy of Craig Gotsma 4. 4. Liear Programmig - Example Defie: (amout amout cosumed

More information

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70 NOTE:. Attempt all seve questios. Major CSL 02 2. Write your ame ad etry o o every sheet of the aswer script. Time 2 Hrs Max Marks 70 Q No Q Q 2 Q 3 Q 4 Q 5 Q 6 Q 7 Total MM 6 2 4 0 8 4 6 70 Q. Write a

More information

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs CHAPTER IV: GRAPH THEORY Sectio : Itroductio to Graphs Sice this class is called Number-Theoretic ad Discrete Structures, it would be a crime to oly focus o umber theory regardless how woderful those topics

More information

Data Structures and Algorithms. Analysis of Algorithms

Data Structures and Algorithms. Analysis of Algorithms Data Structures ad Algorithms Aalysis of Algorithms Outlie Ruig time Pseudo-code Big-oh otatio Big-theta otatio Big-omega otatio Asymptotic algorithm aalysis Aalysis of Algorithms Iput Algorithm Output

More information

Recursive Procedures. How can you model the relationship between consecutive terms of a sequence?

Recursive Procedures. How can you model the relationship between consecutive terms of a sequence? 6. Recursive Procedures I Sectio 6.1, you used fuctio otatio to write a explicit formula to determie the value of ay term i a Sometimes it is easier to calculate oe term i a sequece usig the previous terms.

More information

Parabolic Path to a Best Best-Fit Line:

Parabolic Path to a Best Best-Fit Line: Studet Activity : Fidig the Least Squares Regressio Lie By Explorig the Relatioship betwee Slope ad Residuals Objective: How does oe determie a best best-fit lie for a set of data? Eyeballig it may be

More information

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 11 Frieds, Overloaded Operators, ad Arrays i Classes Copyright 2014 Pearso Addiso-Wesley. All rights reserved. Overview 11.1 Fried Fuctios 11.2 Overloadig Operators 11.3 Arrays ad Classes 11.4

More information

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS) CSC165H1, Witer 018 Learig Objectives By the ed of this worksheet, you will: Aalyse the ruig time of fuctios cotaiig ested loops. 1. Nested loop variatios. Each of the followig fuctios takes as iput a

More information

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness 9/5/009 Algorithms Sortig 3- Sortig Sortig Problem The Sortig Problem Istace: A sequece of umbers Objective: A permutatio (reorderig) such that a ' K a' a, K,a a ', K, a' of the iput sequece The umbers

More information

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 10 Defiig Classes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types 10.4 Itroductio to Iheritace Copyright 2015 Pearso Educatio,

More information

Chapter 4 The Datapath

Chapter 4 The Datapath The Ageda Chapter 4 The Datapath Based o slides McGraw-Hill Additioal material 24/25/26 Lewis/Marti Additioal material 28 Roth Additioal material 2 Taylor Additioal material 2 Farmer Tae the elemets that

More information

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments Ruig Time Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Most algorithms trasform iput objects ito output objects. The

More information

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 9 Poiters ad Dyamic Arrays Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 9.1 Poiters 9.2 Dyamic Arrays Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 9-3

More information

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments Ruig Time ( 3.1) Aalysis of Algorithms Iput Algorithm Output A algorithm is a step- by- step procedure for solvig a problem i a fiite amout of time. Most algorithms trasform iput objects ito output objects.

More information

Analysis of Algorithms

Analysis of Algorithms Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Ruig Time Most algorithms trasform iput objects ito output objects. The

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Pytho Programmig: A Itroductio to Computer Sciece Chapter 1 Computers ad Programs 1 Objectives To uderstad the respective roles of hardware ad software i a computig system. To lear what computer scietists

More information

CMPT 125 Assignment 2 Solutions

CMPT 125 Assignment 2 Solutions CMPT 25 Assigmet 2 Solutios Questio (20 marks total) a) Let s cosider a iteger array of size 0. (0 marks, each part is 2 marks) it a[0]; I. How would you assig a poiter, called pa, to store the address

More information

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis Overview Chapter 6 Writig a Program Bjare Stroustrup Some thoughts o software developmet The idea of a calculator Usig a grammar Expressio evaluatio Program orgaizatio www.stroustrup.com/programmig 3 Buildig

More information

Examples and Applications of Binary Search

Examples and Applications of Binary Search Toy Gog ITEE Uiersity of Queeslad I the secod lecture last week we studied the biary search algorithm that soles the problem of determiig if a particular alue appears i a sorted list of iteger or ot. We

More information

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design College of Computer ad Iformatio Scieces Departmet of Computer Sciece CSC 220: Computer Orgaizatio Uit 11 Basic Computer Orgaizatio ad Desig 1 For the rest of the semester, we ll focus o computer architecture:

More information

Polynomial Functions and Models. Learning Objectives. Polynomials. P (x) = a n x n + a n 1 x n a 1 x + a 0, a n 0

Polynomial Functions and Models. Learning Objectives. Polynomials. P (x) = a n x n + a n 1 x n a 1 x + a 0, a n 0 Polyomial Fuctios ad Models 1 Learig Objectives 1. Idetify polyomial fuctios ad their degree 2. Graph polyomial fuctios usig trasformatios 3. Idetify the real zeros of a polyomial fuctio ad their multiplicity

More information

Combination Labelings Of Graphs

Combination Labelings Of Graphs Applied Mathematics E-Notes, (0), - c ISSN 0-0 Available free at mirror sites of http://wwwmaththuedutw/ame/ Combiatio Labeligs Of Graphs Pak Chig Li y Received February 0 Abstract Suppose G = (V; E) is

More information

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle: Recursio Recursio Jordi Cortadella Departmet of Computer Sciece Priciple: Reduce a complex problem ito a simpler istace of the same problem Recursio Itroductio to Programmig Dept. CS, UPC 2 Mathematical

More information

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer Data structures DATA STRUCTURES Static problems. Give a iput, produce a output. Ex. Sortig, FFT, edit distace, shortest paths, MST, max-flow,... amortized aalysis biomial heaps Fiboacci heaps uio-fid Dyamic

More information

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that. CSE Notes 8: Sortig (Last updated //8 7:6 PM) CLRS 7.-7., 9., 8.-8. 8.A. QUICKSORT Cocepts Idea: Take a usorted (sub)array ad partitio ito two subarrays such that p q r x y z x y y z Pivot Customarily,

More information

Massachusetts Institute of Technology Lecture : Theory of Parallel Systems Feb. 25, Lecture 6: List contraction, tree contraction, and

Massachusetts Institute of Technology Lecture : Theory of Parallel Systems Feb. 25, Lecture 6: List contraction, tree contraction, and Massachusetts Istitute of Techology Lecture.89: Theory of Parallel Systems Feb. 5, 997 Professor Charles E. Leiserso Scribe: Guag-Ie Cheg Lecture : List cotractio, tree cotractio, ad symmetry breakig Work-eciet

More information

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide CS11 Fall 003 Prelim Solutios ad Gradig Guide Problem 1: (a) obj = obj1; ILLEGAL because type of referece must always be a supertype of type of object (b) obj3 = obj1; ILLEGAL because type of referece

More information

One advantage that SONAR has over any other music-sequencing product I ve worked

One advantage that SONAR has over any other music-sequencing product I ve worked *gajedra* D:/Thomso_Learig_Projects/Garrigus_163132/z_productio/z_3B2_3D_files/Garrigus_163132_ch17.3d, 14/11/08/16:26:39, 16:26, page: 647 17 CAL 101 Oe advatage that SONAR has over ay other music-sequecig

More information

CS 111 Green: Program Design I Lecture 27: Speed (cont.); parting thoughts

CS 111 Green: Program Design I Lecture 27: Speed (cont.); parting thoughts CS 111 Gree: Program Desig I Lecture 27: Speed (cot.); partig thoughts By Nascarkig - Ow work, CC BY-SA 4.0, https://commos.wikimedia.org/w/idex.php?curid=38671041 Robert H. Sloa (CS) & Rachel Poretsky

More information

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures Uiversity of Waterloo Departmet of Electrical ad Computer Egieerig ECE 250 Algorithms ad Data Structures Midterm Examiatio ( pages) Istructor: Douglas Harder February 7, 2004 7:30-9:00 Name (last, first)

More information

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway. Bjare Stroustrup www.stroustrup.com/programmig Chapter 5 Errors Abstract Whe we program, we have to deal with errors. Our most basic aim is correctess, but we must deal with icomplete problem specificatios,

More information

Σ P(i) ( depth T (K i ) + 1),

Σ P(i) ( depth T (K i ) + 1), EECS 3101 York Uiversity Istructor: Ady Mirzaia DYNAMIC PROGRAMMING: OPIMAL SAIC BINARY SEARCH REES his lecture ote describes a applicatio of the dyamic programmig paradigm o computig the optimal static

More information

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 1 Itroductio to Computers ad C++ Programmig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 1.1 Computer Systems 1.2 Programmig ad Problem Solvig 1.3 Itroductio to C++ 1.4 Testig

More information

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output File class i Java File Iput ad Output TOPICS File Iput Exceptio Hadlig File Output Programmers refer to iput/output as "I/O". The File class represets files as objects. The class is defied i the java.io

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstractio ad Fuctios That Retur a Value Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 4.1 Top-Dow Desig 4.2 Predefied Fuctios 4.3 Programmer-Defied Fuctios 4.4

More information

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup   Note: Chapter 4 Computatio Bjare Stroustrup www.stroustrup.com/programmig Abstract Today, I ll preset the basics of computatio. I particular, we ll discuss expressios, how to iterate over a series of values

More information

CS 11 C track: lecture 1

CS 11 C track: lecture 1 CS 11 C track: lecture 1 Prelimiaries Need a CMS cluster accout http://acctreq.cms.caltech.edu/cgi-bi/request.cgi Need to kow UNIX IMSS tutorial liked from track home page Track home page: http://courses.cms.caltech.edu/courses/cs11/material

More information

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions CS 111: Program Desig I Lecture # 7: First Loop, Web Crawler, Fuctios Robert H. Sloa & Richard Warer Uiversity of Illiois at Chicago September 18, 2018 What will this prit? x = 5 if x == 3: prit("hi!")

More information

Analysis of Algorithms

Analysis of Algorithms Presetatio for use with the textbook, Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 2015 Aalysis of Algorithms Iput 2015 Goodrich ad Tamassia Algorithm Aalysis of Algorithms

More information

Data Structures Week #9. Sorting

Data Structures Week #9. Sorting Data Structures Week #9 Sortig Outlie Motivatio Types of Sortig Elemetary (O( 2 )) Sortig Techiques Other (O(*log())) Sortig Techiques 21.Aralık.2010 Boraha Tümer, Ph.D. 2 Sortig 21.Aralık.2010 Boraha

More information

On (K t e)-saturated Graphs

On (K t e)-saturated Graphs Noame mauscript No. (will be iserted by the editor O (K t e-saturated Graphs Jessica Fuller Roald J. Gould the date of receipt ad acceptace should be iserted later Abstract Give a graph H, we say a graph

More information

Design and Analysis of Algorithms Notes

Design and Analysis of Algorithms Notes Desig ad Aalysis of Algorithms Notes Notes by Wist Course taught by Dr. K Amer Course started: Jauary 4, 013 Course eded: December 13, 01 Curret geeratio: December 18, 013 Listigs 1 Array sum pseudocode.................................

More information

Algorithm. Counting Sort Analysis of Algorithms

Algorithm. Counting Sort Analysis of Algorithms Algorithm Coutig Sort Aalysis of Algorithms Assumptios: records Coutig sort Each record cotais keys ad data All keys are i the rage of 1 to k Space The usorted list is stored i A, the sorted list will

More information

Chapter 3 Classification of FFT Processor Algorithms

Chapter 3 Classification of FFT Processor Algorithms Chapter Classificatio of FFT Processor Algorithms The computatioal complexity of the Discrete Fourier trasform (DFT) is very high. It requires () 2 complex multiplicatios ad () complex additios [5]. As

More information

Administrative UNSUPERVISED LEARNING. Unsupervised learning. Supervised learning 11/25/13. Final project. No office hours today

Administrative UNSUPERVISED LEARNING. Unsupervised learning. Supervised learning 11/25/13. Final project. No office hours today Admiistrative Fial project No office hours today UNSUPERVISED LEARNING David Kauchak CS 451 Fall 2013 Supervised learig Usupervised learig label label 1 label 3 model/ predictor label 4 label 5 Supervised

More information

Counting Regions in the Plane and More 1

Counting Regions in the Plane and More 1 Coutig Regios i the Plae ad More 1 by Zvezdelia Stakova Berkeley Math Circle Itermediate I Group September 016 1. Overarchig Problem Problem 1 Regios i a Circle. The vertices of a polygos are arraged o

More information

Location Steps and Paths

Location Steps and Paths Locatio Steps ad Paths 3 INTHIS CHAPTER Uderstadig Locatio Steps ad Paths How do locatio paths work? We took a look at locatio paths i the overview i Chapter 1, where we saw that locatio paths look much

More information

Pattern Recognition Systems Lab 1 Least Mean Squares

Pattern Recognition Systems Lab 1 Least Mean Squares Patter Recogitio Systems Lab 1 Least Mea Squares 1. Objectives This laboratory work itroduces the OpeCV-based framework used throughout the course. I this assigmet a lie is fitted to a set of poits usig

More information

Lecture 28: Data Link Layer

Lecture 28: Data Link Layer Automatic Repeat Request (ARQ) 2. Go ack N ARQ Although the Stop ad Wait ARQ is very simple, you ca easily show that it has very the low efficiecy. The low efficiecy comes from the fact that the trasmittig

More information