Review and Prepare for Test 1 Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content derived/taken from: http://www.stroustrup.com/Programming/ Points of Note • Assignments 1, 2, 3, 4, and 5 are now graded • Assignment 6 is posted • Next class, there is a Test • More on this shortly Previously • Algorithms • Analysis of Algorithms • Big Oh • and the “Take Home / Online” Quiz began Today’s Plan • Quickly go over the homework • Review for Test 1 • Format • Material to Expect A01: MoneyChanger A02: BestGuess A03: Movies A03: Movies A04: Pointers • Arrays and Pointers part A04: Pointers • StudentCheck A04: Pointers • StudentClub.H A04: Pointers • StudentClub.cpp A05: DynMemInherit • DynSort A05: DynMemInherit • vlistTest A05: DynMemInherit • Car • Truck End Assignment Review • Any Questions? • Next • Quick Big-Oh Review • Prep for Test Big Oh – Again • Because you can never get enough • We will briefly go over some things again • Review some rules first 1st Thing to Remember – Order of Classes of Functions 2nd Thing to Remember – Rules of Big-Oh • If f(n) is a polynomial of degree d, then f(n) is O(nd), 1. Drop lower-order terms 2. Drop constant factors • The idea is to: • Use the smallest possible class of functions • So “2n is O(n)” instead of “2n is O(n2)” • Use the simplest expression of the class • So “3n + 5 is O(n)” instead of “3n + 5 is O(3n)” 3rd Thing - Analyze the Code Correctly • We have seen a lot of implementations • Let’s make a more generic algorithm to do it Given a C++ function • For each statement in the function • Assign the worst case number of operations it will use • i.e. maximum number of times it executes • Many will be a constant number of operations • Assign these a 1 O(1) • Assuming NOT in a loop then they execute only once cout << value int sum = 0; total = total + current; if (count < 20) { area = (1.0 / 2.0) * base * height; O(1) Given a C++ function • For each statement in the function • Assign the worst case number of operations it will use • i.e. maximum number of times it executes • Some will require n operations • Assign these n O(n) • This includes constant stuff inside a loop iterating n times for (int j = 1; j <= n; j++) { } n cout << “this prints n times” << endl; n ( 1 thing n times) O(1) Given a C++ function O(n) • For each statement in the function • Assign the worst case number of operations it will use • i.e. maximum number of times it executes • Some will require n2 operations • Assign these n2 O(n2) • Typically these are nested loops and constants in nested loops for (int j = 1; j <= n; j++) still just n { for (int k = 1; k <= n; k++) { } } n2 (n things n times) cout << “this prints n^2 times” << endl; n2 O(1) Given a C++ function O(n) O(n2) • For each statement in the function • Assign the worst case number of operations it will use • i.e. maximum number of times it executes • Some get tricky and require math skills • Like n1/2 (or sqrt(n) ) recall working with positive integers O(n1/2) for (int j = 1; j <= j*jsqrt(n) <= n; j++) n1/2 { Tables may help j*j <= n; cout << “this prints times” << endl; to verify this 𝑗 ∗ 𝑗 ≤sqrt(n) 𝑛 OR } 𝑗∗𝑗 ≤ 𝑛 inspire the thought to 𝑗≤ 𝑛 apply math to make the j <= sqrt(n); problem easier n1/2 O(1) Given a C++ function O(n) O(n2) O(n1/2) • For each statement in the function • Assign the worst case number of operations it will use • i.e. maximum number of times it executes • Others require understanding of math stuff like logs • log2 n … thinking in terms of integers gives the number of times n can be divided by 2 O(lg n) for (int j = n; j > 0; j /= 2) { lg n cout << “this prints lg n times” << endl; lg n Again… } Tables may help to verify this OR inspire the thought to apply math to make the problem easier Given a C++ function • For each statement in the function • Assign the worst case number of operations it will use • i.e. maximum number of times it executes • Sum the operations for each statement • Reduce per the Big-Oh rules for (int j = 1; j <= n; j++) n { + n2 for (int k = 1; k <= n; k++) { cout << “this prints n^2 times” << endl; + n2 } = n + n2 + n 2 } • Drop lower order terms • Drop constants • i.e. Effectively pick the line that executes the greatest number of operations (in the worst case) = n + 2 n2 • This gives us the “worst case” for the entire function • And we arrive at our Big-Oh for the function O(n2) Why Summations Before? • All of the previous slides are relatively straight forward cases. • More difficult cases require more sophisticated tools. • As it turns out • Math again comes to the rescue Summations and Loops • For the “easy” case of 1 loop for (int j = 1; j <= n; j++) Human brain thinks: 1 + 1 + ... + 1 there are n additions that means n things Math brain thinks: 𝒏 𝟏=𝒏 𝒋=𝟏 Summations and Loops • Nested Loops for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { … } } Math brain thinks: 𝒏 𝒏 𝒏 𝒏 = 𝒏𝟐 𝟏= 𝒋=𝟏 𝒌=𝟏 𝒋=𝟏 And Math Wins • For cases like for (int j = 1; j <= n; j++) 𝒏 { for (int k = 1; k <= j; k++) 1 + 2 + … + 𝒏 { … } } Math brain Human brainthinks: thinks: 𝒏 + ... 𝒋 + n 1 + 2 𝟏= uh... 𝒋=𝟏 𝒌=𝟏 𝒏 𝒋= 𝒋=𝟏 𝒏(𝒏 + 𝟏) 𝟏 𝟐 𝟏 = 𝒏 + 𝒏 𝟐 𝟐 𝟐 And Math Wins • For cases like for (int j = 1; j <= n; j++) 𝒏 { for (int k = 1; k <= j; k++) 𝟏 𝟏 𝟐+ 𝒏 𝒏 { 𝟐 𝟐 … } } Math brain thinks: 𝒏 𝒋 𝒏 𝟏= 𝒋=𝟏 𝒌=𝟏 𝒋= 𝒋=𝟏 𝒏(𝒏 + 𝟏) 𝟏 𝟐 𝟏 = 𝒏 + 𝒏 𝟐 𝟐 𝟐 And Math Wins • For cases like for (int j = 1; j <= n; j++) 𝒏 { for (int k = 1; k <= j; k++) 𝟏 𝟏 𝟐+ 𝒏 𝒏 { 𝟐 𝟐 … } } =𝒏 + O(n2) 𝟏 𝟐 𝟏 𝒏 + 𝒏 𝟐 𝟐 𝟏 𝟐 𝟑 = 𝒏 + 𝒏 𝟐 𝟐 Enough Big-Oh • Get to the test already. =) Game Plan for Test 1 • Closed Book • No notes of any kind • Closed Computer • No electronic devices (pace makers or other life sustaining devices maybe) • Nothing but a pencil, eraser, brain and one functioning hand needed / required • this time • Any attempts to cheat will result in beheading umm… maximal punishment allowed per University regulations • Do NOT give me any reason to believe you are or even might be cheating Layout for Test 1 • Some Short answer / coding questions • Circa 10 • Some true false • Circa 10 • Some Multiple choice • Circa 10 to 15 • There will be THREE (or more) versions of the test • Similar but not identical in content Topics for Test 1 • General C++ • C++ Classes • UML • Abstract Data Types (ADTs) • • • • • • • • Standard Template Library (STL) Inheritance Templates Searching / Sorting Looping and Recursion Linked Lists Big Oh Math Stuff General C++ • What is the g++ command to compile a file? a program? • Can you write functions in C++ • global functions and class member functions • Things to know about • Return values / types • Pointers • dereferencing pointers • address operator • things like char * • Pass by ??? • Constant functions • Built in types • char, int, double, etc… • Enumerated types • Operator Overloading • new, delete, delete [ ], allocation on the stack vs. on the free store (heap) C++ Classes • C++ Class Declaration / Implementation • Can you write a C++ header file (.h) • In general and from a UML specification • Can you write a C++ implementation file (.cpp) • In general and from a UML specification • How do classes relate to objects • How do classes relate to Abstract Data Types • File Usage • What does a header file do? • What does a cpp file do? UML • UML stands for ______ • How does UML relate to ADTs and classes? • Given an ADT description • can you relate it to a UML diagram • to a C++ class ADTs • ADT stands for what • How do ADTs relate to UML and C++ classes Standard Template Library (STL) • using namespace std • std::string • some of its functions • be able to read • string s; s.length() returns what? • std::vector • what does it allow you to do? • It’s like an array, but better • vector <int> v; is a vector of what ? integers. • std::cout • How do you use this function? • std::cin • And how does this work? Inheritance • How does this relate to UML? • What does it have to do with C++ classes • Can you write the code to derive one class from another • How does inheritance affect access to member variables • what data can a child see that is declared in it’s parent’s class • what data can it access • what “types” of data access are there in C++ Templates • • • • Have some examples handy Can you template a class Can you template a function What is the purpose of templates Searching / Sorting • • • • Linear search Binary search Insertion sort Selection sort • Describe • Walk through • Maybe implement • at least partial Looping • Understand looping • • • • • • particularly with regard to Big-Oh for-loops while-do loops do-while loops any others break Linked Lists • How are they implemented in C++ • Node and pointer • Mostly Singly Linked • For extra may want to know about doubly linked lists • Be able to read code • Use pointers • Allocate and De-allocate nodes Big-Oh • See all of earlier slides and homework and quizzes Math Stuff • There will not be any rigorous proofs • but likely a “show or explain” • Summation Stuff • Properties of logs • Concept of Asymptotic (limits, etc) • Implicitly likely more (and perhaps explicitly) What to do to Prepare • Be sure to sleep sometime before the test • NOT during • Review the homework • Review the quizzes • Think about the topics list just provided • What questions would you ask about those topics? • Read the book • Chapters 3 and 4, but glance at 1 and 2 too • Write some C++ code What about the Lectures • Read/review them • There were a lot of examples • If you didn’t understand some of them • • • • work them out run the code change the code run it again And the Homework • All good to look at Questions • Any questions • that have not yet been asked • and or answered The End • It says, The End • But there is likely time left over • Could take the online quiz…