Вы находитесь на странице: 1из 25
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)

Вам также может понравиться