Lecture08

Lecture08 - 4/20/10 CMPSC 24: Lecture 8 Abstract Data Type:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4/20/10 CMPSC 24: Lecture 8 Abstract Data Type: Lists Divyakant Agrawal Department of Computer Science UC Santa Barbara 4/20/10 1 Recap •  Abstract Data Types –  SpecificaIon (What?) –  ImplementaIon (How?) •  Unsorted lists –  Array‐based implementaIon –  Pointer‐based implementaIon •  QuesIons –  Reference vs. value type –  Comparing enumerated types •  ExcepIons (TODAY) 2 Lecture Plan •  Linked list implementaIon of unsorted list •  Time complexity of algorithms –  Big Oh 3 1 4/20/10 ADT Unsorted List •  Transformers –  MakeEmpty –  InsertItem –  DeleteItem change state •  Observers observe state –  IsFull –  GetLength •  –  RetrieveItem process all •  Iterators –  ResetList –  GetNextItem 4 SpecificaIon // SPECIFICATION FILE #include “ItemType.h” struct NodeType; ( unsortedType.h ) class UnsortedType // declares a class data type { public : // 8 public member functions UnsortedType(); void MakeEmpty( ); bool IsFull( ) const; int GetLength( ) const; // returns length of list void RetrieveItem( ItemType& item, bool& found ); void InsertItem( ItemType item ); void DeleteItem( ItemType item ); void ResetList( ); void GetNextItem( ItemType& item ); 5 Linked List ImplementaIon (private part) private: NodeType* listData; int length; NodeType* currentPos; struct NodeType { ItemType Info; NodeType *next; } List with two items 6 2 4/20/10 Linked List ImplementaIon How do you know that a linked list is empty? listData is NULL What should the constructor do? Set length to 0 Set listData to NULL What about currentPos? We let ResetList take care of iniIalizing currentPos 7 Linked List ImplementaIon What about the observers IsFull and GetLength? GetLength just returns length Can a linked list ever be full? Yes, if you run out of memory Ask for a new node within a try/catch 8 Linked List ImplementaIon of IsFull bool Unsortedtype::IsFull() const { NodeType* location; try { location = new NodeType; delete location; return false; } catch (std::bad_alloc exception) What about MakeEmpty? { return true; } } 9 3 4/20/10 Linked List ImplementaIon of MakeEmpty void Unsortedtype::MakeEmpty() { NodeType* tempPtr; while (listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; } length = 0; } Why can’t we just set listData to NULL? 10 C++ concepts to come •  •  •  •  ExcepIons Deep vs. shallow copying Constructor, destructor, copy constructor Dynamically allocated arrays 11 Order of Magnitude of a FuncIon •  The order of magnitude, or Big‐O, of a funcIon expresses an upper bound to the growth of a funcIon relaIve to its parameters. •  Used to analyze the space and Ime complexity of algorithms/programs. 12 4 4/20/10 AsymptoIc Analysis Example: The big ‐oh ( O) N otaD on 13 Big‐Oh Defined T(n) = O(f(n)) if there are constants c and n0 such that T(n) ≤ c*f(n) when n > n0 c*f(n) c*f(n) is an upper bound for T(n) * Ignoring absolute values in the definiIons n0 T(n) n 14 Big‐O NotaIon •  T(n) = O(f(n)) if there are constants c and n0 such that T(n) ≤ c*f(n) when n > n0 •  If f(n)=1000n and g(n)=n2, n0 = 999 and c = 1, then f(n) < 1*g(n) where n > n0 and we say that f(n) = O(g(n)) •  The O notation indicates' bounded above by a constant multiple of. ' 15 5 4/20/10 Big‐Oh ProperIes •  Fastestgrowing funcIon dominates a sum –  O(f(n)+g(n)) is O(max{f(n), g(n)}) •  Product of upper bounds is upper bound for the product –  If f is O(g) and h is O(r) then H is O(gr) •  f is O(g) is transiIve –  If f is O(g) and g is O(h) then f is O(h) •  Hierarchy of funcIons –  O(1), O(logn), O(n1/2), O(nlogn), O(n2), O(2n), O(n!) 16 Some Big‐Oh’s are not reasonable •  Polynomial Time algorithms –  An algorithm is said to be polynomial if it is O( nc ), c >1 –  Polynomial algorithms are said to be reasonable •  They solve problems in reasonable Imes! •  Coefficients, constants or low-order terms are ignored e.g. if f(n) = 2n2 then f(n) = O(n2) •  ExponenIal Time algorithms –  An algorithm is said to be exponenIal if it is O( rn ), r > 1 –  ExponenIal algorithms are said to be unreasonable 17 Can we jusIfy Big O notaIon? •  Big O notaIon is a huge simplificaIon; can we jusIfy it? –  It only makes sense for large problem sizes –  For sufficiently large problem sizes, the highest-order term swamps all the rest! 18 6 4/20/10 Classifying Algorithms based on Big‐Oh •  A function f(n) is said to be of at most logarithmic growth if f(n) = O(log n) •  A function f(n) is said to be of at most quadratic growth if f(n) = O(n2) •  A function f(n) is said to be of at most polynomial growth if f(n) = O(nk), for some natural number k > 1 •  A function f(n) is said to be of at most exponential growth if there is a constant c, such that f(n) = O(cn), and c > 1 •  A function f(n) is said to be of at most factorial growth if f(n) = O(n!). •  A function f(n) is said to have constant running time if the size of the input n has no eect on the running time of the algorithm (e.g., assignment of a value to a variable). The equation for this algorithm is f(n) = c •  Other logarithmic classifications: f(n) = O(n log n) f(n) = O(log log n) 19 Names of Orders of Magnitude O(1) bounded (by a constant) Dme O(log2N) logarithmic Dme O(N) linear Dme O(N*log2N) N*log2N Dme quadraDc Dme O(N2) O( 2N ) exponenDal Dme 20 How do various functions grow? N log2N N*log2N N2 2N 1 0 0 1 2 2 1 2 4 4 4 2 8 16 16 8 3 24 64 256 16 4 64 256 65,536 32 5 160 1024 4,294,967,296 64 6 384 4096 128 7 896 16,384 21 7 4/20/10 Big‐O Comparison of List OperaIons Array-based Pointer-based Constructor O(1) O(1) IsFull O(1) O(1) GetLength O(1) O(1) RetrieveItem O(N) O(N) MakeEmpty O(1) O(N) InsertItem O(1) O(1) DeleteItem O(N) O(N) ResetList O(1) O(1) GetNextItem O(1) O(1) 22 8 ...
View Full Document

Ask a homework question - tutors are online