ࡱ> Nn  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMlQRSTUVWXYZ[\]^_`abcdefghijkmRdO)CxOPowerPoint Document(SummaryInformation(Ph6DocumentSummaryInformation8 K(   +6"Wagner@cs.ucr.eduN:http://www.cs.ucr.edu/~wagnerH4http://www.cs.ucr.edu/cs12/ 00DTimes New Roman0`W 'K DArialNew Roman0`W 'K  DWingdingsRoman0`W 'K 0DProFontsRoman0`W 'K  ` .  @n?" dd@  @@`` (p       c $ n3f3@8d >$ʚ;+4ʚ;g4QdQd@t@ppp@ <4!d!d-gX<4dddd-gXg4`d`d4@@8p@ pp>___PPT9 h___PPT2001D<4X?-"2003 W L Truppel hCS 14: Intro. Data Structures & Algs. " Lectures 3-4O =?%Intro to Data Structures & AlgorithmsWagner Truppel Lecturer, Dept. of Computer Science & Engineering UC Riverside wagner@cs.ucr.edu http://www.cs.ucr.edu/~wagner http://www.cs.ucr.edu/cs14zO  O`a~Today s Topics[Testing ADTs Namespaces C++ review Templates Exceptions Algorithm Quality Complexity Theory6#$#$Om Testing ADTs<Suppose I give you some code and claim that it performs some function. How can you verify my claim ? Example: I give you a Counter class and I claim it does indeed implement the Counter interface. How can you prove that it does ? You can look at the source code What if the source code isn t available ? You can verify adherence to an interface but that isn t enough Verifying adherence to an ADT is more difficult\Z Z*ZoZ *ot  Testing ADTsTo verify adherence to an ADT you need to test the axioms defined by the ADT Example: Counter class get(new()) = 0 get(inc(c)) = get(c) + 1 How do you test the axioms ? Counter c = Counter(); if (c.get() == 0) print  Fine. ; else print  Test failed. ; int old = c.get(); c.inc(); int new = c.get(); if (old + 1 == new) print  Fine. ; else print  Test failed. ;dZ(ZZZ*+',Ou  Testing ADTsVStill not enough, however& Testing can only show the presence of bugs, not their absence ! The more you test, the more confident you become Two kinds of test environments Black-box test Use only ADT axioms to come up with test cases White-box test Look at the source code to come up with test cases Z@ZPZZ/ZZ4Z  P/  4n NamespacesA long time ago& & people used to write one large piece of code. Then procedural programming came along& & and, more recently, OOP. The trend has been to write software components Chunks of reusable and self-contained code performing unique and well-defined functionsX   v  NamespacesHSoftware components Chunks of reusable and self-contained code performing unique and well-defined functions Problem: what if you re using two software components developed by different people but named the same ? Solution: collect groups of related components into namespaces Examples: MS::Stack Cs14::Stack Still not a perfect solution& Two namespaces could have the same name Java uses a neat idea: com.wltruppel.stack"ZXZZZZ?ZZ     ?   pC++: TemplatesSuppose you want to find the maximum among two int values int max(int a, int b) { if (a > b) return a; else return b; } Suppose you want to find the maximum among two double values double max(double a, double b) { if (a > b) return a; else return b; } Suppose you want to find the maximum among two Speed values Speed max(Speed a, Speed b) { if (a > b) return a; else return b; } Bored yet ?PZ)Z\Z)ZXZ)Z Z/v9P.>w C++: TemplatesObviously, the operation of finding the maximum among values applies to many data types in exactly the same way Provided those data types have a comparison operator (> or <) Can we not write the code for a max function only once and use it for many kinds of data types ? C++ lets you do that with Templates Template T max(T a, T b) { if (a > b) return a; else return b; }|pZ>ZZ=Zp> X P,F(qC++: ExceptionsvSay that you re writing a program that reads and writes files When the program is running, it could encounter the following problems can t read a file can t write to a file b/c it s locked can t even find the darn file Since you know these problems could happen, you safeguard your program by adding error handling^W`CQxC++: ExceptionsError handling in the past set an error code and check it elsewhere return an error code and check it at the caller level just ignore the error and pray that things will work What s the real problem ? If you get a problem (expected or not) and you know how to fix it, then fix it right there and then ! If you get a problem (expected or not) and you don t know how to fix it, pass it to the caller Ok& but how do you pass an error ? Easy& if errors are represented as data types, you throw themZZZ&Z  9 6{C++: Exceptionsdouble f(double a, double b) { if (b == 0.0d) throw std::domain_error( Div by zero! ); else return (a / b); } You should always throw exceptions when you expect potential problems whose fault isn t your own You should throw exceptions only for exceptional cases Exceptions in C++ and in Java belong to a class hierarchy For now, use std::domain_error:uZZp/5yC++: ExceptionsXtry { & // code that might raise an exception // such as opening a file that doesn t exist, etc } catch (std::exception &e) { // here you can fix the problem if you know how // and have enough information to do so // you can also get information on the exception std::cout << e.what() << std::endl; }~ZZZZZZZ, rAlgorithm Quality@Effectiveness we can actually use them ! Correctness hey, if it s broken, why use it ? Finiteness it d better terminate ! Efficiency memory and time are precious Understandability if no one can understand your algorithm, what s the point in writing it ? Maintenance someone s gotta do it& ZZ Z"Z ZZ ZZZJZ ZZ "     J sComplexity TheoryDeals with efficiency Simple instructions are assumed to take constant time Additions & subtractions Assignments Simple comparisons We re not saying that they all take the exact same amount of time  they probably don t !RL8Z (8Z|Complexity TheoryWe re not saying that they all take the exact same amount of time  they probably don t ! All we re saying is that the time those operations take does not grow with the size of the input Example: For (int i = 0; i < n; i++) { a[i] = 0; } O(n)  Order means we don t care about specific numbers but only about general behavior*.XJ>f~Complexity Theoryint i = 0; O(1) steps constant time binary search O(log2n) steps logarithmic growth array init O(n) steps linear growth O(1) is faster than O(log2n) which is faster than O(n) Always ? No& for small values of n, it may be the case that the order is violated. But we never really care about small n values. Three cases: worst case, best case, average casef8 Z bComplexity TheoryExample: linear search Best case: you get lucky and find it the very first time you try& O(1) ! Worst case: you get unlucky and have to check every element& O(n) Average case: you only have to check half the number of elements& O(n/2) = O(n) (b/c constants don t matter& )EgComplexity TheoryWhat we d always like to have: O(1) Yeah, dream on& what we often get (these are acceptable) O(n), O(n log2n) Sometimes, we also get (great !) O(log2n) But other times, we have to bite the bullet and use algs that take O(n2), O(n3), or even worseZZ9ZZ!Z ZCZZ9 !C '/PsxHH@Rg(HH(dh   ` www3ff` 3fff` ___>?" dF@,?n<d@uA @ " d`  n?" dd@   @@``PR   @ ` `P0p>> -"2003 W L Truppel hCS 14: Intro. Data Structures & Algs. " Lectures 3-4 j( = 2  N`e0?"! :    Nd0gֳgֳ ?"P  T Click to edit Master title style! !<  Hf0gֳgֳ ?"  RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S  N@g0gֳgֳ ?"`p  Z*    Nh0gֳgֳ ?"`   n*2    Ni0gֳgֳ ?"`@  \*   N  6n޽h? ? www3ff $Blank PresentationS  -"2003 W L Truppel hCS 14: Intro. Data Structures & Algs. " Lectures 3-4 =5  ( |  B  N1?"442  Nk0?"! :    N@j0gֳgֳ ?"` D T Click to edit Master title style! !  H l0gֳgֳ ?"P  D W#Click to edit Master subtitle style$ $  N@m0gֳgֳ ?"`p D Z*    N gֳgֳ ?"`@ D \*     N gֳgֳ ?"`  D n*2  N  6n޽h? ?  www3ff0 `<f(  < < 0 "P   ה R*   < 0 "   ה T*  r < c $ ?"  ה$ < 0V " @ ה RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S < 6U "`P  ה R*   < 6W "`  ה T*  H < 0޽h ? ̙33H D( @ $  D D 0P[ "P   ה FCS 14 Lectures 3 & 4  D 0, "   ה T*   D 6P, "`P  ה FCS 14 Lectures 3 & 4  D 6p, "`  ה T*  H D 0޽h ? ̙33  d\@ (  P   c $ "P D   c $ "P  D    <D :  B  s *޽h ? 3ff   p@(    S 0]"P  D   S Z"  D B  s *޽h ? 33̙ffJ   x(    c $P^"P  D   c $]"<$  D B  s *޽h ? 33̙ffJ  m x( 00//   c $`"  D   c $Pa"`<$  D B  s *޽h ? 33̙ffJ  t x( 00//   c $`"  D   c $0`"`<$  D B  s *޽h ? 33̙ff  t L( 00//   c $)"P  D   c $p)"  D B  s *޽h ? 33̙ff  u L(     c $p)"  D   c $)"0 D B  s *޽h ? 33̙ff  t L(    c $)"  D   c $P)" D B  s *޽h ? 33̙ff  v L( @   c $)"  D   c $p)" D B  s *޽h ? 33̙ff  t L(    c $P5*"P  D   c $4*"  D B  s *޽h ? 33̙ff  w L( ff   c $9*"p  D   c $p9*" D B  s *޽h ? 33̙ff  x L( 00//   c $07*"p  D   c $7*" D B  s *޽h ? 33̙ff  {  L( :   c $8*"p  D   c $4*" D B  s *޽h ? 33̙ff  t 0L(    c $;*"  D   c $<*"` D B  s *޽h ? 33̙ff  t @L( ni   c $P;*"P  D   c $:*"  D B  s *޽h ? 33̙ff  { PL(    c $)"P  D   c $)" D B  s *޽h ? 33̙ff  | `L( 00//   c $)"P  D   c $P)" D B  s *޽h ? 33̙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~0wn~͆0׆~n~1Ćn~1n~1n~1n~s1n~c1n~S1tn~F2kn~<2cn~32[n~)222222222Snnnnnnnnnnnnnnnnnnn%~2222222222222nnnnnnnnnnnnnnnnnnnnnnnnnn~2222222 D nnnnnnnnnnnnnnnnn7~222?nn~ 2=ņn~ 2:Ɔn~27Ɇn~25Ɇn~2ˆn ADTs Testing ADTs Namespaces NamespacesC++: TemplatesC++: TemplatesC++: ExceptionsC++: ExceptionsC++: ExceptionsC++: ExceptionsAlgorithm QualityComplexity TheoryComplexity TheoryComplexity TheoryComplexity TheoryComplexity Theory  Fonts UsedDesign Template Slide Titles 8@ _PID_HLINKS'AHmailto:Wagner@cs.ucr.eduhttp://www.cs.ucr.edu/~wagnerhttp://www.cs.ucr.edu/cs12&_qCTWagner TruppelCurrent User.