0% found this document useful (0 votes)
27 views26 pages

BD 03518

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
27 views26 pages

BD 03518

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 26
"El acceso a la version digitalizada se brinda con fines académicos, unicamente para las secciones que lo requieren Vv habilitado exclusivamente para el ciclo académico vigente. Tener en cuenta las consecuencias legales que se desprenden de hacer uso indebido de estos documentos, de acuerdo a D.L. 822." Prete is far too brief tobe used in such a course, You might find it uefa to use an adltonal work on NP-completenesso augment this ext Exercises Eserlses, provided tthe end of each shapter, match the order which materiale pre= ‘sented The ast esecises may addres the chapter as 2 whole rater than a specifi seton, Difficult exereves ate marked with an asterisk, and more challenging exercises have vo aseriks References References are placed at the end of each chapter Generally the rlerences ether a his torical, representing the orginal source ofthe materal,o they Yepresent extensions and lunprosements to the results given in the text. Some references represent solutions (0 ‘Supplements ‘The following supplements are availble oll reader t won:pearsoninterationaledtons + Source code for example programs + Ema In addition, the following material is svalble only to quafed instructors at Pearson Instructor Resource Center (woapersoniuemationaledtons comes). Vist the IRC of contaet your Pearson Education sales representative for access, + Solutions to selected exerises + Figures rom the Boole + Bras ‘Acknowledgments Many, many people have helped me in dhe prepa Tse in eer versions ofthe book, thanks ll ssa the wring process was made easier bythe profesional t Pearson, 1 ike to thank my editor, Tay Johnson, and production editor, Maniyn Loyd. My wonderful el deserves eta special thanks for everything she does. Final Fd ke 10 thank the numerous readers who have sent e-mail messages and pointed out eros inconsisiencies i eater version. My webate wines fi edocs val also contain updaced source cove Gin C++ an Java), an erat ist, an alin to submit bug reports, Maw Mami, Foi jon of books in this seis. Some ae The publishers wish to thank Arup Bhatachajee and Soumen Mukherjee for reviewing ‘he content ofthe Inventions Eton. cuaprer I Programming: A General Overview In this chaps, we discuss the sims and goals ofthis est and briefly review programing concepts an discrete mathematics, We wl + See that howe 3 program performs for reasonably lrg input sat a important as ts performance on moderate moun of put + Summarize the basic mathematical background needed forthe rest ofthe book + Billy review reewesion + summarize some important features of C+ that sed throughout the text 1.1 What's This Book About? Suppose you havea group of N numbers and would Mke to detennine the ith gest. This \s Kou a the selection problem, Most students who have had programming couse for to would have no dificult writing a program to solve tis problem, Thee are quite a Few “abvious solutions ‘One way to sli this problem would be to read the N auraers ino an array sor the array in decreasing order by seme simple algorhm such as bubble som, and then return the element in postion k ‘A somtesthat beter algorithm might be to ead the ist element into an aay and son them in decreasing order). Next, each remaningelement ead one by one. AS new Clement series tt ignored fits smaller than the hh element n he stay Otherwie, is placed in ts correct spot in the ray bumping one element out ofthe array When the algorithm ends, the element in the ih postions returned a5 the answer Both algorithm ae simpler code, and you are encouraged todo so, The natural ques tions, then are: Which algoethm is better? And, more imporant, s either algorithm good enough? A sinuladon sing a tandom fle of 30 milion elements and k= 15,000,000 voll show that nether algorhm finishes ina reasonable amount of time; each requires "ver days of computer processing to teninat abet eventually with a correct answer) An akeraive method, discussed in Chapter 7, gives a solution in abouts second. Thus, although our proposed algorthns work, they cannot be considered good slgonthme, 20 r234 yews afwoanes s/o a hg afte ae Figure 1.1 Sample word pazsle because they are emily impractical for ipot sizes that acid algorithm can handle sna reasonable amount of time ‘A second problem isto solve a popular word puzzle. The input consis of & w60- Aimensional array of eters and ast of words. The objet sto ind the words he puck ‘These words may be horizontal, vera, or dagonal in any ircton. As an example, the puzzle shown in Figure 11 contains the words ths, ew, fat and that. The word this begins at row 1, column 1, or (1,1), and estends 10 (1.4); mo goes from (1,1) 10 (3.0 fat goes from 4,1) to (2.3) and hat goes from (44) t0 QD. Agin, there ar at east to ssightforward algorthms that solve the problem, For each word inthe wordlist, we check each ordered triple (ow, column, erinttion forthe pres ‘ence ofthe word. This amounts to lts of nested for loops bu is basicly straightforward, Alerativey foreach ordered quadruple ow clumn,orentaon, number of characters) that doesnt runoff an end ofthe puzzle, we can test whether the word ct tin the ‘word ls. Again, this amounts to ls of nested for loop is posible to sve some time ifthe maximum numberof characters in any word is known, ris elately easy to code up ether method of solton an salve many ofthe el ie puzales commonly published in magazines. These ypcally have 16 os, 16 clus, an ADors0 words Suppose, however we consider the aration where nly the puzzle board is given and the word hist i essenllyan English dictionary Bath of the solutions proposed ‘equi considerable time to solve this problem and therefore might not be acceptable However, tis possible, even with a lage wor lst, to solve the problem very quickly in important concept is tat, in many problems, wetng a working program isnot good enough. Ilse progam sto be run ona lange dataset then the running ime becomes an issue, Throughout this book we will se how to estimate the running time of a program for large inputs and, more important, how to compare the runing times of two programs without actually coding them. We wil sce techniques for drastically improving te speed ofa program and for determining program botlentcks. These technics will enable us find the section ofthe code on which to concentrate our optimization elon 1.2. Mathematics Review ‘This section iss some ofthe basic formulas you need to memorize, of beable to derive, and reviews basi proof techniques. | | | 12 Nata een a 1.2.1 Exponents whyt axe ax oo a xt IN aN Wah a2! 1.2.2 Logarithms ‘in compute scence, ll logarithms are othe base 2 uns specified aberwise. Definition 1.1 AF = Bifand only logy B= A ‘Several convenient equalities follow fom this definition. ‘Theorem 1. lB 7 logy B= ES: ABC> OAH Proof Lex = loca log, 8. Then, by the definition of logs: ruhins, CX = B, c Combining these three equalities yields B= Ch = (CF Therefore, X = YZ, which implies Z = X/Y, proving the theorem, Theorem 12 logAB = logA-+ log; A.B > 0 Proof Let X = log, ¥ = loge, and Z = logAB, Then, assuming the deal base of 2, DY = 4.2) = Band 2° = AB Combining the last three equalities yields 22" = AB = 2 Therefore, X-+Y = Z, which proves the theorem, Some other useful formas, which cn al be derived in a similar manne, allow. logs log loge logla®) = Blog logX 0 log2=1, logi,024= 10, og ],048,376 = 20 ChaperPropamming: A Gener! Overew 1.2.3 Series The easiest formulas to remember are and the companion, ye In the later forms, FO = A = 1. chen Sra and as N tends to 00, the sum approaches 1/ formulas "We can derive the lst formula for SiEp A! (© < A <1) inthe following manner Let Sethe sm. Then A). These are the “geometric sees DARA EAE AN A Then ASHALR EA HATED Ie we subract these two equations (Which i permissible only fora convergent series) ‘rally ll the tems on the night side cance, lean sas ‘which implies that ‘We can use this sae technique to compute SOS; 1/22 sum that occurs frequent Went and mukiply by 2, obtaining a 2 3 asai4243 wetiege Subtracting these two equations yields Thus. $22 12 Mathematics Raven 2s Another typeof common series in analyse isthe arithmetic eres. Any such seri can ‘be evaluated fromthe base formula NED roeaes For instance, to ind the sum 2 4548-4 .-- + Gk— 1, rewnte tas 30142434 “+8)—(L+1414---4+ 1), whichis clearly 3+ 1)/2— ke Another way to remember tists toad the frst and las terms (total 3k-+ 1), the second and nextlast tems (otal 3k + 1, and so on. Since there ate h/2of these pairs, the ttl sum sR + 1/2, which ‘isthe same answer as belo, “The next ro formuls pop up now and then bt are fry uncommon, When t= —1, the ater formula isnot valid. We then need the fllwing formu, ‘which ts used far more in computer scence than in her mathematical disciplines The ‘numbers Hy are knoen s the harmonic numbers, and the sim is known as a harmonic sum, The err inthe following approximation tends to 0:37721366, hich s know ‘Euler’ constant y= Zyo- Sy 1.2.4 Modular Arithmetic We say that A is congruent to 8 modulo N, writen A= B (mod N), ICN’ divides AB Inzutively. this means thatthe remainder the same when either A or B is Aided by N. Ths, 81 = 61 = 1 (sid 10) Ae with equaly. EA = B nod N}, shen A4-C 8 4C (mod Nand AD = BD (mod N) Chapter Pojaning: A Gener Overen (ten, Ais @ prime number. that as, there are thre important theorems: Firs, Ns prime, then ab = 0 (mod N) is ewe if and only ifa = 0 (rod N) ov b = 0 (mod N), In ther word, i prime number N’ divides a product of two number, detest least one ofthe two numbers Second, if Nis prime, then the equation ax = 1 (mod N) bas a unique solution (ened N) forall << N This saludo, 0 ~ x = Ns the mula inverse “Thind,if.N i prime, then the equation 32 = a (mod 1) has either eva soltione {mod N) for all a = Nor has no solutions There ate many theorems that appyto molar arithmetie and some of them require earaedinary proofs in number theory. We wil use modular arthmett sparingly. andl the preceding cheers wil sie. 1.2.5 The P Word The (wo most common ways of proving statements in daastractre analysis ae proof ‘by induction and proof by coneadiction (and occasionally preof by inumidation, used by professors only). The best way of proving tha a theorems false is by exhib counterexample, Proof by Induction [A proof by induction has two standard ports. The fs tp ie roving a base case, hats ‘establishing that theorem iste for some small (usually degenerate valet: this step lest aay el. Next an ictive hypothesis is assumed. General this means hat the theorem i assumed 10 be true forall cases up to some limit Using this assumption, the theorem is then shown to be tne forthe next value, which is typically h-+ 1. Thi proves the theorem (as long as is int. ‘Asan example, we prove thatthe Fibonacci numbers, Fy = 1, F, = 1,2 = 2.F Fy 5... Fim Ft +A, satis F< (6/3) for 1 Some definitions have F which sh the series) To do ths, we Bs verify thatthe theorem is tue forthe trivial cases tis easy to very that Fj = 1 = 3/3 and F) = 2 ~ 25/9, this proves the bass ‘We assume thatthe theorem fs tre for = 1.2... thee the inductive hypothesis, To prove the theorem, we need to show that Fig = /3)"). We have tit by dhe definition, and we can use the inductive hypothesis on the right-hand side, obtaining 3, Fun Fa <9 46/3 = G95)! 4.G/s%G/3"4 = B/D) +G/2515/3" which simplifies to Fan O/ F9/2515/9)" = aH 2515/38" | <6, | proving the theorem. ‘Asaszcond example, we establish the following theorem, “The prof sb induction, For the bass edly sen that the theorem sre when N= Forthe induce hypothesis, asm tht the theorem se for 1 = k= WN ‘We wl eaubiah that under thie asumpion, the theorem tre for N+ 1. We have Desoen! | Applying the inductive hypothe, we obain w+ aN +» i NOE DANDY | ory ye 3 NaN mo n[SNP sors] Me 4 TNS move pitves _ ove vs DANED ~ 6 Thus, Sie = We MIN v4 ules D+ 2 ¢ proving the theorem, Proof by Counterexample The seen i = IP ase, The east ay to prove tis sro compute iy = Weil Proof by Contradiction roof by contradiction proceeds by assuming thatthe theorem false ane showing that his assumption implies that seme known propery is fae, and hence the orignal sumption ‘was erroneous. A classic example isthe proof that there ean infinite numberof primes. To prove this, we assume thatthe theorem is fe, so that there is some lgest prime Fi. Let y,Pa,-.-sPh be ll he primes in order and consider haper Progamming: A General Ovenew Ni PaPys Pat Clearly, N 1s lager than Py 39, by assumption, Nis not prime. However, none of PisPhy so PadidesN exactly, because ther wl aleays bea remainder of 1. Thisisacon- teadcton, because every umber s ether prime ora product of primes. Hence, the original ‘sumption, thot Fs the lagest prime, se, which implies thar the theorem sue 1.3 A Brief Introduction to Recursion ‘Most mathematical functions that we are fala wih are described by a simple formula For instane, we can convert temperatures from Fahrenheit t Celsius by applying the formula « (F329 Given this Formla, ts eval ro werte a C+ function; with declarations and braces ‘removed, the one-fne formula transits to one Ine of C+ ‘Mathematical functions are sometimes define ina less standard form. Asan example, swe can define 9 function f, vali om nonnegative integers, that sashes J(0) = D and J) = 2fte~ 1) +32, Flom tis definition we see tha (1) = 1,f@) = 6,3) = 21, nd f() = 58, A function that defined in terms of sl called reeursive. Cr allows Factions to be recursive! I important to remember that what C+ provides is merely an atemp to follow the secursive spint. No all mathemaealyreeusive functions are ficient (orcorety} implemented by C5 simulation af recursion, The ideas thatthe "ecusive function f ought tobe expressible in only afew line, jus like a nonsecursive Fantom, Figure 1.2 shows the recursive implementation off Lines 3 and 4 handle what is known as the base case, tht i, the value for ‘which the faction i diet known wathout resorting to recursion. Just as declaring Jos) = Ye— 1) +. t meaningless, mathematically without including the fet that ‘l0) = 0. the recursive Cr fnetion doesnt make Sense without base case, Line 6 makes the recursive cal 2 ane FC tne) 2 3 ilxo) + return 05 Ses ‘ retum 2* fe 1) exes ra Figure 1.2. & recursive function ad ie. We ave dane 0 a lst the ae "Qing euson freien aly oe | 15 Ali readucian to Recs There ae several important and possibly confusing points about recursion, A common question is: Isnt this just crculae loge? The answer is that although we ae defining function in cerms of tl, we are not defining pariculr instance ofthe function in terms of sel. In other words, evaluating (5) by computing f() wuld be circular. Evaluating (3) by computing J is not crcular—unles, of couse, (3) is evaluated by eventually {computing (3). The wo most mportant issues are probably the how and why questions In Chapiee 3, the how an nh issues ae formally resolved. We wil gve an incomplete description bere Te turns out that recursive calls are handled no diferent fom any others Uf is called withthe value of 4, hen line 6 requires the computation of 2 #3) +44. Ths, a calli rade 1 compute 3). This regres the computation of 24/(2)+3+3, Therefore another ‘alls made to compute (2). This means that 2«/()-+2-#2 must be evaluated. To do 50, {f)iscomputed as 24f(0)+ 161. Now, (@) must be evaluated, Since thisis a bse case, we ‘know a prior that f(O) =O. This enables the completion ofthe calculation for (1), which Isnow seen tobe 1 Then (2). (3), and finally (4) canbe determined Al the bookkeeping needed to keep tack of pening function calls (those stared but wating fora secutsve call complete), long with ther variables, s done by the computer automaticaly. An ‘important point, however, is that recursive calls will ke on being made una hase case is reached, For instance, an atterpe to evaluate f(—1) wil eso nels 0 f(—2).J(-3), and soon. Since this wil never ge oa base case, the program wont be able o compute ‘he answer whichis undefined anyway). Occasional; a mich more subtle error i made, ‘whichis exhibited in Figure 3, The error in gute 13 is tht bad) 8 defined, byline 6, be bai(2). Obviously. this doesnt give any clue a 10 what bad() actly is, The computer will hus repeatedly make calls to bad(2) in an atempt to resolve i values Eventually, ts bookkeeping system will an out of space, an the program wl teminate sbnormally: Generally, we would say that this fiction doesnt work for one special case buts correct othersise. Ths sn ere ere, sine bas) als ba). Thus, bad) cannot be evalusted either. Furthermore, ad(3) tae), and ba4() all make calls to be2). Since bad2) isnot evaluable, none ofthese values ar either Infact, this program doesn work. for any nonnegauve value of», except 0. With recursive programs, thete fo such thing asa special ease “These considerations lead to the rst wo fundamental rules of ecursion 1. Base cass. You must alvays have some base cases, which can be solved without 2. Maling ogres. For the eases that are to be solved recursively, the ecutive call mast, lays be ta case that makes progres toward a base case, fn taal it) C return bad a / 340) + n= 2 6 ) Figure 1.3. Anonvtminating recursive faction a Chaper 1 Proamming: A Gerral Overview “Throughout this book, we wall use recursion solve problems. As an example ofa honmtthematical use, consider large dictionary. Words in dictionaries ae defined in tems of ether words. When we look up a word, we might not always understand the fefntion, so we might have to look up words inthe defition. Likewise, we might nat tundertand some of those so we might have to continue this earch ora wl, Because the Alitonary ifn, evenly eer (1) we will come 1o a point where We understand all ofthe words in some dfinion (and thus understand that definition and rerace our path through the other definitions) or (2) we wil ind that he definition ae cxeular and we ate stick, or that some word we ned tounderstana fora defnton isnt inthe conan: (Our recursive strategy to understand words is as follows: If we know the meaning of a ‘word, then we ate done; others, we look the word up inthe dictionary. we understand all she words in the definition, we ae done; otherwise, ve igure out what che definiion rmeans by recurstely looking up the words we don know: Tis procekre wl terminate ifthe dictionary is well defined but can loop indefinitely fa word iether not defined or carelaly define. Printing Out Numbers Suppose we havea pastve integer, hat we wish to pint out, Our routine wil have the Deading printut(n). Assume thatthe only UO routines avaiable wll ake a single-digit ‘number and output it We wil ell:his oucine pinta or example, printoiatt() will, ouput as Recursion provides avery clean solution to his problem. To print ovt 76234, we nese to first prin out 7623 and then prin cut +. The second scp is easy accomplished with the siatementgrintoigit(asi0), but the fist doesnt seem any simpler than the orginal problem. Inded iti virally the same problem, so we can solve i eeusively with the statement printout (0/0. ‘This tll us how to solve che general problem, but we sil need to make sure shat, {he program doesnt loop indefinitely. Since we haven defined base case yet is lear {Gat we il have something todo, Our base case wil be printbigt(o)

You might also like