Algorithms and Abstract Data Types
Algorithms and Abstract Data Types CMPS 101
Popular in Course
Popular in ComputerScienence
This 66 page Class Notes was uploaded by Dr. Elyssa Ratke on Monday September 7, 2015. The Class Notes belongs to CMPS 101 at University of California - Santa Cruz taught by Staff in Fall. Since its upload, it has received 65 views. For similar materials see /class/182263/cmps-101-university-of-california-santa-cruz in ComputerScienence at University of California - Santa Cruz.
Reviews for Algorithms and Abstract Data Types
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/07/15
72 Bis ZEertjp oM RapeLR WT Zamp aeML TZ 3351 A NW N902 a 39nxrwo A mm 3930 KLA To Wequ ML 1539 VnaPaL bl u ka ac T911 QR przapm ryA M471 8L vasmb Mm QM 39tmsm oq 13 m NabfVS ska 62L la 23 m m Am sum 33 war 4 estur 261 NMMA LlLdtz NL WL withle LS3 apr TSVLQIL Noofzx 9N 4u7 Two Bowman mm 940M IL Ma a 7 a mam mum LV JM LL Mat Sw u 3 m QWALNJ Wk J w 23 Jim 3mch Kr x A COMENTIDIL LMYMLAY 3914 4R0 mm wt Us mm 3quot We 317 ColamNlts quotTAX NWR IIVSIjI 39A NaAL fzb SzuCL mm ma ilkPhat L amt all mm a Lab 2 ma a Ms Tun KMQL all QthXDILa q 4X Mlcx 2 F41 g2 Mawz2L JW WMANWIij l hiwa P inxi l Z v ki w in 11Z stbiu hdJ 4w EFF maps 45 vacc 75va APme V 4me ELF A wcr ec if WWANWVIHSAamp TI WC 210 mlmnmvf NCVfWLl anvmg Ev u Zmnbnwmgx quot 42 RB InsertT z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 vlqux Pre keyz has been set y ni1 rootT while x i nil yea if keyzltkeyx x leftEx else xe rightx pZlt Y if y nil rootT z else if keyzltkeyy 1efty z else righty z 1eftz nil rightz nil colorz red RBInsertFixupT z Liqu I Is39 Arm Jugr ESFMSEOJ RBInsertFixupT Z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 39 21 22 23 24 25 26 27 28 29 30 while colorpz if pZ red 1e tppz y rightppz if colory else else red colorpz black colory b1ack colorppz red Z PEPIZJI righthIZJJ z D21 LeftRotateT colorpze black colorppz red if z 2 RightRotateT ppz Y leftEPEPEZJJJ if colory else red colorpz black coloryj black colorppz red Z PEPIZJJ If z 1eftpz Z p21 RightRotateT colorpz black colorppz red LeftRotateT ppz Z colorrootT b1ack VVVV L13 ng wg tfLLLA3 1 3 41 caner OLE 1 quotu 942 thzN ML 2 haxx nu VJ Low PLEA At lE Mk LhNLCL A VToLL mc I39 QK 7me7 Jr 7 wrz LLW TAL agwk l39LT D wwuo 31 04 M174 1 m kaxt 39OF 1K DannyJr zaTquot 42 Li xg Qk LV SUSHNA we 2 3 UML L NLg MSL W r HZ Axcg 34131444 uw39b 1 Ears CLMAAQ a PDQ 9V er Qoi39o a aF r74 7Nmu Far Sb PBL glm CerzsLAZO 391 173 UVLLLLQ ztM Q LLmz gwg 457 f quotem 169 DM3943939M a gm 41345 Sb VMSQWAL PL qulrc im Wm V ALxampezr MLLWMQ 7 UM x A m 1392 Q R 30 R Omumwggb 931 PM 4 vacuole Wm rz w PALSK Nani Km mg mg PkPLau Mg M EMAIL 17 S z mm Iw mnom of 11 I l l 2 A NAM 2 u m PNLMTA r0u1o Li R 7 GALE 3914 Iglll AIL 71544 E f 7 f N 63 2 7 quotquotvm quotMK 77 EN 04A amp ultz quotColaL PLEA HALL r MUMPKJU M R65 7 T413 172 mam PUMng FD GizT T if 9ampXBWLWT Pas012m Lg Nam MWSAM Mun 9M9 sf LlLLx Nor Jszbru WIQMJEN AampO N DTFZ quotEMT WAS 9amp0 a Q mo hJ 0 Di M ia W 652 lagma m1 Tbtzlg 3WD M ampampP mML39IuAM M ka bgatnf39 IIM CALL 3 un rampV Commquot 7 HALF 6 x3r 3 DONE 03 kTTIQUQ 7 2 Qth ub TD MEX ulwu hems RILDWL W QRcDLDT39 9 7 363 L 7 K E O V m TIM ML 4 ULPEk is away MEWSMr Clmwwen Y PLzrjl QLPAL J I nitV zr HJ2 WULM L 2 QJI39IILL up To A 7 2 332 l w 2 ltvgt 7 0 2 O k Mm m M Midterm 2 Topics Spring 2005 The midterm will be on Thursday May 19 It is a closed book exam but you may use one 3 by 5 card of notes written in your own hand Calculators will not be allowed You are responsible for chapters 1 through 9 and 15 in the book1 as well as section 282 and appendices A and B The second midterm will be comprehensive in that I will ask some questions on the material covered before the rst midterm but the material covered since the rst midterm will be emphasized Note that techniques from the rst part of the course may be required to solve questions relating to the more recent material Some of the longer questions on the the second midterm are likely to involve induction decision tree lower bounds and dynamic programming Here are the topics we have covered in lecture since the rst midterm Building a heap in linear time Strassen s matrix multiplication Average case analysis of Quicksort internal versus external randomization Using indicator random variables Radix sort and bucket sort Linear time selection median of median algorithm Lower bounds on problems Information theoretic decision tree lower bound technique and how to apply it PWNQWFWN The 9nlog 71 lower bound for comparison sorting and its relationship to Radix Sort H 0 The adversary lower bound technique H H Dynamic Programming the 6 fold path H to Dynamic Programming algorithms for particular problems A 93 the number of knight s move phone numbers A T maximizing a skateboarder s excitement largest value paths in a DAG A O parenthesizing a chain of matrix multiplications A VVVVVVVV computing the longest common subsequence D 0 1 Knapsack problem AA l h Coin changing homework Canoe rentals homework an AA 3 Number of binary search trees homework 1Chapter 5 is less important as long as you understand indicator random Variables and the average case analysis of Quicksort and much of Chapters 68 should be review of material from CMPS 101 39T r 39 x mDL 39 RET gomim L Am uTJnc mk QXmAamm a wg zgfa 1x3910gu m Rwk a EAL SEJ ll SJ 4 49635 I39 C Jg4 g 911 11 i157 V M 39 I E1AQAKLEBEQMMRWQAll 7 6 IT C 44213 MT ELMcae AAeLm emmLkszugAAg MaNprnm 7 quotNE W 1m QET K gWFLm AND 25 5 61 169 T 09 MAX 3 D Q 1 D 63 Y2 lT g f9 ED 53 a Corm m 12A a Mmam gyj 7 CALIswag P un TD Q64 31 39 qu WEQCVE JI s v mFxt EUJ Q Laid J F l 3 2X TwmvquWloH d2 TI 1 V Java quotgTL cud 7 7 r39 aquot a r r 39quot L E j TT w thwm T NWJ VWW 39 y vo 1417735 67 W WWW quot1r 17747617 Irv5339 jfLTj39j WUW I39 m l V g C VO lwflw l 619i quot39 Q V39i39 EWNquotquotquotTquotI 7vlt14r 7gS TE XAMMWEJLTIAEJNM 7 h 5M J39WLWqQMTZ Li w 7 gtlt 3 0 U1 QJJQUI A L p a 2 21 4 F mgg A 3 K I 110 OiWP NW V Mg IHKKWERWFEKPIEIEFUVVIQDPEI 5Ia111 EPIPPEFKEHAA c mwkwiblg gf llztis H igk nmvntElhptgiilla l tut 3 302E162 4 wris wEvbailg s i Em PxKEH i x 1 f 39 CMPS 111 Operating Systems Prof Scott Brandt Computer Science Department University of California Santa Cruz General Information Focus is on operating systems Complies with ACM amp IEEE courses Prerequisites CMPS 101 amp CMPW 12C Labs will have new material in them Do your work in the UniX Lab AS 105 OK to discuss assignments but Must develop your own code Cannot look at other s code Cannot use code in a book General Information cont 0 Course grade 4 programming assignments 40 2 Quizzes 10 Midterm 20 Final 30 0 Get all information from web page httpwwwcseucscedusbrandtcoursesSpringOO111 Introduction Why Study OS Understand model ofoperation Easier to see how to use the system Enables you to write ef cient code Learn to design an OS Even so OS is pure overhead of real work Application programs have the real value to person who buys the computer System Software 0 Independent of applications but common to all 0 Examples C library functions A window system A database management system Resource management functions Purpose of an OS What is Resource Management 0 Process An executing program 0 Resource Anything that is needed for a process to run Memory Space on a disk The CPU 0 An OS creates resource abstractions 0 An OS manages resource sharing Resource Abstraction loadblock length device seekdevice 236 outdevice 9 Resource Abstraction loadblock length device seekdevice 236 outdevice 9 writechar block int len int device int track int sector loadblock length device seekdevice 236 outdevice 9 Resource Abstraction loadblock length device seekdevice 236 outdevice 9 writechar block int len int device int track int sector loadblock length device seekdevice 236 outdevice 9 writechar block int len int deviceint addr Resource Abstraction loadblock length device seekdevice 236 outdevice 9 writechar block int len int device int track int sector loadblock length device seekdevice 236 outdevice 9 writechar block int len int deviceint addr fprintffileID d datum Abstract Resources User Interface Abstract Resources APD OS Resources OS Interface Hardware Resources Resource Sharing Space vs timemultiplexed sharing To control sharing must be able to isolate resources OS usually provides mechanism to isolate then selectively allows sharing How to isolate resources How to be sure that sharing is acceptable Concurrency Multipro grammin g Technique for sharing the CPU among runnable processes Process may be blocked on 10 Process may be blocked waiting for other resource While one process is blocked another should be able to run Multiprogramming OS accomplishes CPU sharing automatically Reduced time to run all processes How Multiprogramming Works Process 1 Process 2 Process 3 Timemultiplexed CPU Process 4 Spacemultiplexed Memory OS Strategies Batch processing Timesharing Personal computer amp workstations Process control amp realtime Network Distributed Batch Processing Uses multiprogramming JLZ le of OS commands prepared of ine Batch of jobs given to OS at one time OS processes jobs oneaftertheother No humancomputer interaction OS optimizes resource utilization Batch processing as an option still used today Timesharing Uses multiprogramming Support interactive computing model Illusion of multiple consoles Different scheduling amp memory allocation strategies than batch Tends to propagate processes Considerable attention to resource isolation security amp protection Tend to optimize response time Personal Computers CPU sharing among one person s processes Power of computing for personal tasks Graphics Multimedia Trend toward very small OS OS focus on resource abstraction Rapidly evolved to personal multitasking systems Process Control amp RealTime Computer is dedicated to a single purpose Classic embedded system Must respond to external stimuli in xed time Continuous media popularizing realtime techniques An area of growing interest Networks 0 LAN Local Area Network evolution 0 3Mbps 1975 gt 10 Mbps l980 gt100 Mbps 1990 0 High speed communication means new way to do computing Shared les Shared memory Distributed OS 0 Wave of the future HHHH Multiple Computers connected by a Network Evolution of Modern OS Protocols RealTime Scheduling Devices Modern OS Examples of Modern OS 0 UNIX variants have evolved since 1970 0 Windows NT has evolved since 1989 much more modern than UNIX 0 Research OS still evolving 0 Book uses Linux as main example 0 This course Will use Nachos for the programming assignments Supplementary materials available on the web Microsoft Windows NT 0 Heavily Windoworiented 0 Foundation behavior is Windows independent OS API NT Executive NT User Interface and Graphics CMPS 101 Abstract Data Types Summer 2004 Asymptotic Growth of Functions We introduce several types of asymptotic notation which are used to compare the relative performance and ef ciency of algorithms As we ve seen in comparing InsertionSort and MergeSort the asymptotic growth rate of an algorithm gives a simple and machine independent characterization of the algorithm s complexity De nition Let gn be afunction The set Ogn is de ned as Ognfn l Ecgt0En0 gt0Vi12n0 OSfnchn In other words f ne Ogn if and only if there exist positive constants c and no such that for all n 2 no the inequality 0 S fn S cgn is satis ed We say that fn is Big O of gn or that gn is an asymptotic upper bound for fn We often abuse notation slightly by writing fn Ogn to mean fne Ogn Actually fne Ogn is also an abuse of notation We should really write fe Og since we have de ned a set of functions not a set of numbers The notational convention Ogn is use ll since it allows us to refer to the set 0n3 say without having to introduce a function symbol for the polynomial n3 Observe that if f n Og 71 then f n is asymptotically non negative ie f n is nonnegative for all suf ciently large n and likewise for gn We make the blanket assumption from now on that all functions under discussion are asymptotically nonnegative In practice we will be concerned with integer valued functions of a positive integer n g Zl gt Zl However in what follows it is useful to consider 7 to be a continuous real variable taking positive values and g to be real valued function g R gt Rl Geometrically fn Ogn says CgUl f n Example 40n 100 0n2 10n 300 Observe that 0 S 40n 100 S n2 10n 300 for all n 2 20 Thus we may take no 20 and c 1 in the de nition n2 10n 300 300 40n 100 100 10 20 In fact anb Ocn2 dne for any constants 616 and more generally pn Oqn whenever pn and qn are polynomials satisfying degp S degq as we shall see De nition Let gn be a function and de ne the set Qgn to be Qgnfn Ecgt0En0 gt0Vr12n0 0chnSfn We say fn is big Omega of gn and that gn is an asymptotic lower bound for fn As before we write f n Qgn to mean f ne Qgn The geometric interpretation is flquot CgUl Example Prove that fn Ogn if and only if gn Qfn Proof If fn Ogn then there eXist positive numbers c1 n1 such that 0 S fn S clgn for all n 2711 Let c2 lc1 and r12 711 Then 0 Sczfn Sgn for all n an proving gn Qfn The converse is similar and we leave it to the reader De nition Let gn be a function and de ne the set 8gn Ogn m Qgn Equivalently 8gn fn l Elc1 gt 0 3ch gt 0 3an gt 0 Vn 2 n0 0 S clgn S fn S czgn We write fn 8gn and say the gn is an asymptotically tight bound for fn or that fn and gn are asymptotically equivalent We interpret this geometrically as ngn f n Clgn quot0 Exercise Prove that fn 8gn if and only if gn 8fn Exercise Let gn be any function and let 0 gt 0 Prove that cgn Ogn and cgn Qgn whence cgn 8gn Example Prove that n10 Proof According to the de nition we must nd positive numbers cl cl n0 such that the inequality OSc nSxn10 sagZ holds for all nZnO Pick c1 1 02 xE and n0 10 Then if n2n0 we have 10S0 and lOSn 10Sl ln and 10S2 ln 10 S1 clzn and 10 Scz2 ln cfnSn10 and n10Sczzn cfnSnlOSczzn clxnSxn10 Sczxn as required The reader may nd our choice of values for the constants cl cl n0 somewhat mysterious Adequate values for these constants can usually be obtained by working backwards algebraically from the inequality to be proved Notice that in this example there are many valid choices For instance one checks easily that c1 12 c2 x32 and n0 20 work equally well Exercise Let a b be real numbers with b gt0 Prove directly from the definition as above that n ab 80117 In what follows we learn a much easier way to prove this Example Prove that if hn Ogn and f n S hn for all suf ciently large n then fn 0gn Proof The above hypotheses say that there exist positive numbers 0 and n1 such that hn chn for all n 2 711 Also there exists 112 such that 0 S fn S hn for all n 2 n2 Recall fn is assumed to be asymptotically nonnegative Then for all 112110 maxn1n2 we have 0S f nchn showing that fn Ogn Exercise Prove that if 11101 S f nS h2 n for all suf ciently large n where 11101 Qgn and l1271 0gn then fquot 9gn Example Let k 21 be a xed integer Prove that Zik 801 11 Proof Observe that Zik S an nrnk n 001 and 11 11 21 2 21 2 Emmy Zl nZ lnZY 2n2n2k 12k nk 420 11 11n2T 11n2T By the result of the preceding exercise we may conclude Zik 801 11 When asymptotic notation appears in a formula such as Tn 2Tn 2 n we interpret 871 to stand for some anonymous function in the class 871 For example 3113 4n2 2n l 3113 8012 Here 8012 stands for 4112 2nl which belongs to the class 8012 The expression 2180 can be puzzling On the surface it stands for 818283 n which is meaningless since 8constant consists of all functions which are bounded above by some constant We interpret 8139 in this expression to stand for a single function f 139 in the class 8139 evaluated at 139 l 2 3 n Exercise Prove that 2 8139 8012 The left hand side stands for a single function f 139 summed for 139 1 2 3 n By the previous exercise it is suf cient to show that 11101 S 2 fz39 S h2 n for all suf ciently large n where 11111 2712 and h2 n 0n2 De nition ogn fn 1 VC gt 0 3an gt 0 Vn 2 no 0 S fn lt cgn We say that gn is a strictAsymptotic upper bound for f n and write f n ogn as before Lemma fn ogn if and only if ling fol 0 gn Proof Observe that fn ogn if and only if Vc gt 0 3an gt 0 Vn 2 n0 0 S f 7 lt c which is f n gn n 0 g the very de nition of the limit statement lim 71 1gn 7t Example lgn on since lim 0 Apply l Hopitals rule k Example nk obquot for any k gt0 and b gtl since lim 0 Apply l Hopitals rule l k l times In other words any polynomial grows strictly slower than any exponential By comparing de nitions of ogn and Ogn one sees immediately that ogng0gn Also no function can belong to both ogn and Qgn as is easily veri ed exercise so that 0gquot09gquot Thus 0gquot Q 0gquot 8gn De nition agn fn l Vc gt 0 3an gt 0 Vn 2 n0 0 S cgn lt fn Here we say that gn is a strict asymptotic lower bound for fn and write fn agn 00 Also prove agn m Ogn Q Exercise Prove that fn agn if and only if lim fol Hgtw n g whence agn Qgn gn The following picture emerges 0gn 9g 7 8gn Lemma If lim L where 0 SL lt oo then fn Ogn f n gn Proof The limit statement says V8gt 0 3an gt0 VnZ n0 L lt8 Since this holds for all 8 g n we may set 81 Thenfor all nZnO fnL gn fM Llt1 gn lt1 1lt amp gn fnltL139gn ltL1 Now taking 0 L 1 in the de nition ofO yields fn Ogn as claimed f n Lemma If 11m L where 0 ltL S 00 then fn Qgn quot90 g 7 Proof The limit statement implies lim gm L where L UL and hence 0 S L lt 00 By the quot90 7 previous lemma gn Ofn and therefore fn Qgn Exercise Prove that if lim fEn L where 0 lt L lt oo then fn 8gn quot90 g 7 Although ogn wgn and a certain subset of 8gn are characterized by limits the full sets Ogn Qgn and 8gn have no such characterization as the following examples show Example A Let gn n and fn 1 sinn r n 2gquot I Clearly fn Ogn but l sinn whose limit does not exist whence fn 2 ogn g n Observe also that fn 2 Qgn why Therefore fn e Ogn 8gn ogn showing that the containment ogn Ogn 8gn is strict in general f 7 Example B Let gn n and fn 2 sinn r n 3gquot f 7 gm Since nS2sinngtnS3n for all 7120 we have fn8gn but fEn g n 2 sinn whose limit does not exist Exercise Find functions fn and gn such that f ne Qgn 8gn but ling fEn does not g n exist even in the sense ofbeing in nite so that fn 2 ag 71 The preceding limit theorems and counterexamples can be summarized in the following diagram f n Here L denotes the limit L lim if it exists 71 gm 9gquot gquot L 00 In spite of the above counterexamples the preceding limit theorems are a very useful tool for establishing asymptotic comparisons between functions For instance recall the earlier exercise to show 71 ab 80117 for real numbers a and b with b gt 0 The result follows immediately from 7 17 MW 1im13 11 1 71 new 71 naw 0gn 0gn L 0 ExA since 0ltlltoo Exercise Use limits to prove the following a nlgn 0n2 here lgn denotes the base 2 logarithm of n b 7152quot an1 c If Pn is a polynomial of degree k 2 0 then Pn 8nk d For any positive real numbers a and 6 n 0n iff a lt 6 n 8n iff a 6 and n an iffagt6 e For any positive real numbers a and b a 0bquot iff a lt b a 8bquot iff a b and aquot abquot iffagtb f For any positive real numbers a and b loga n 8log n g fquot 0fquot 9070 Tle I wade B EE 257 ZDRMEWVE anm It QRA BL 49Nxuic77iL bLJ AIJJEC39A 3 1 34721 laxEm 39 nla AN Aft3mm I H D J1LihLI AL g yu 4m W 77 ampE7LL7 AAL6 1 Atty 12541114 C p fktllliali nwww I n a F quot w v i amp A QLEBAEAw zAuT H v 91DL A19 1 Emma A WAEaM t MJZLQMAIS1m kiw ELQw a Alzmrn I S Qquot u T 3M Maw R MARX LED EL W I 39 quot119quot gimp 7 CMPS 101 Algorithms and Abstract Data Types Summer 2005 Some Additional Remarks on ADTs and Modules in ANSI C Suppose you wish to implement an ADT in C The particular ADT is unimportant so let s just call it a Blah You should create the following les at minimum Blahc Blahh BlahClientc The le Blahh will contain prototypes for all exported functions which represent ADT operations It will also contain the line typedef struct Blah BlahRef This de nes BlahRef to be a pointer to some struct called Blah The le Blahc will include Blahh and will contain de nitions for all exported functions and perhaps de nitions for some private functions and structs Blahc will also contain the following typedef statement typedef struct Blah code which defines fields for the Blah ADT Blah A client module can then include Blahh giving it the ability to declare variables of type BlahRef as well as functions which either take or return BlahRef parameters However the client cannot dereference this pointer since the object it points to is not de ned in Blahh The ADT operations take BlahRef arguments so the client does not need to and is in fact unable to directly access the struct which these handles point to Therefore the client can interact with a Blah object only through the exported ADT operations This is how information hiding is accomplished in C File Blahc also contains a constructor BlahRef newBlah BlahRef B code which initializes B returnB and a destructor void freeBlahBlahRef pB ifpBNULL ampamp pBNULL free all heap memory associated with pB free pB pB NULL The destructor is called with the address of a BlahRef as follows BlahRef B newBlah do something with B freeBlahampB w 39 P quot QampW1L w F M ALLBL 2QT5AWiiDln QiCLFiz0AA AnsL7 7J4 ra 3 I I 2 CMPS 101 Algorithms and Abstract Data Types Summer 2006 Some Additional Remarks on ADTs and Modules in ANSI C Suppose you wish to implement an ADT in C The particular ADT is unimportant so let s just call it a Blah You should create the following les at minimum Blahc Blahh B1ahC1ientc The le Blahh will contain prototypes for all exported functions which represent ADT operations It will also contain the line typedef struct Blah BlahRef This de nes BlahRef to be a pointer to some struct called Blah The le Blahc will include Blahh and will contain de nitions for all exported functions and perhaps de nitions for some private functions and structs Blahc will also contain the following typedef statement typedef struct Blah code which defines fields for the Blah ADT Blah A client module can then include Blahh giving it the ability to declare variables of type BlahRef as well as functions which take BlahRef parameters However the client cannot dereference this pointer since the object it points to is not de ned in Blahh The ADT operations take BlahRef arguments so the client does not need to and is in fact unable to directly access the struct which these references point to Therefore the client can interact with a Blah object only through the exported ADT operations This is how information hiding is accomplished in C File Blahc also contains a constructor BlahRef newBlah BlahRef B code which initializes B returnB and a destructor void freeBlahBlahRef pB if pBNULL ampamp pBNULL free all heap memory associated with pB freepB pB NULL The destructor is called with the address of a BlahRef as follows BlahRef B newBlah do something with B freeBlahampB
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'