Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Advertisements

1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSE332: Data Abstractions Section 2 HyeIn Kim Spring 2013.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Week 15 – Monday.  What did we talk about last time?  Tries.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
September 29 – Stacks and queues
Recitation 10 Prelim Review.
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Week 15 – Monday CS221.
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
Algorithm Analysis and Modeling
Data Structures and Algorithms
Lecture 3: Working with Data Structures
Stacks and Queues.
Building Java Programs
Introduction to Data Structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Road Map CS Concepts Data Structures Java Language Java Collections
Building Java Programs
Lecture 1: Welcome to CSE 373
Lecture 1: Welcome to CSE 373
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Lecture 2: Implementing ADTs
Lecture 2: Implementing ADTs
structures and their relationships." - Linus Torvalds
Data Structures and Algorithms
Algorithm design and Analysis
Stacks and Queues.
Lecture 3: Queues, Testing, and Working with Code
B-Tree Insertions, Intro to Heaps
ITEC 2620M Introduction to Data Structures
Lecture 5: Master Theorem, Maps, and Iterators
Building Java Programs
Building Java Programs
ITEC 2620M Introduction to Data Structures
Implementing Hash and AVL
Stacks and Queues CLRS, Section 10.1.
slides created by Marty Stepp
Based on slides by Alyssa Harding & Marty Stepp
CSE 373: Data Structures and Algorithms
Lecture 2: Stacks and Queues
Lecture 3: Maps and Iterators
Lecture 14: Midterm Review
Lecture 4: Introduction to Code Analysis
CS210- Lecture 3 Jun 6, 2005 Announcements
Lecture 3: Maps and Iterators
Complexity Analysis (Part II)
Lecture 14: Midterm Review
Lecture 9: Intro to Trees
Lecture 2: Stacks and Queues
structures and their relationships." - Linus Torvalds
Stacks and Queues.
CMPT 225 Lecture 8 – Queue.
Presentation transcript:

Data Structures and Algorithms Midterm Review Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

Warm Up Imagine you are asked to test a method in a class that implements a Binary Search Tree. This method should be able to take in any node and then return back the height of the given tree. public int treeHeight(TreeNode node) What are some unit tests you would write to validate this method works like it should? What would you name those unit tests? What are some examples of trees you would pass into the method to test if it can handle all appropriate cases? CSE 373 SP 18 - Kasey Champion

Logistics 50 minutes 8.5 x 11 in note page, front and back Review tonight 4:30-6:30 in MLR 301 Extra TA office hours No office hours on Friday CSE 373 SP 18 - Kasey Champion

ADTs vs Data Structures A way of organizing and storing related data points An object that implements the functionality of a specified ADT Describes exactly how the collection will perform the required operations Examples: LinkedIntList, ArrayIntList Algorithm A series of precise instructions used to perform a task Examples from CSE 14X: binary search, merge sort, recursive backtracking Abstract Data Type (ADT) A definition for expected operations and behavior A mathematical description of a collection with a set of supported operations and how they should behave when called upon Describes what a collection does, not how it does it Can be expressed as an interface Examples: List, Map, Set CSE 373 SP 18 - Kasey Champion

List ADT list: stores an ordered sequence of information. Each item is accessible by an index. Lists have a variable size as items can be added and removed Supported Operations: get(index): returns the item at the given index set(value, index): sets the item at the given index to the given value append(value): adds the given item to the end of the list insert(value, index): insert the given item at the given index maintaining order delete(index): removes the item at the given index maintaining order size(): returns the number of elements in the list CSE 373 SP 18 - Kasey Champion

Stack ADT stack: A collection based on the principle of adding elements and retrieving them in the opposite order. Last-In, First-Out ("LIFO") Elements are stored in order of insertion. We do not think of them as having indexes. Client can only add/remove/examine the last element added (the "top"). basic stack operations: push(item): Add an element to the top of stack pop(): Remove the top element and returns it peek(): Examine the top element without removing it size(): how many items are in the stack? isEmpty(): true if there are 1 or more items in stack, false otherwise push pop, peek top 3 2 bottom 1 stack CSE 373 SP 18 - Kasey Champion

Queue ADT queue: Retrieves elements in the order they were added. First-In, First-Out ("FIFO") Elements are stored in order of insertion but don't have indexes. Client can only add to the end of the queue, and can only examine/remove the front of the queue. basic queue operations: add(item): aka “enqueue” add an element to the back. remove(): aka “dequeue” Remove the front element and return. peek(): Examine the front element without removing it. size(): how many items are stored in the queue? isEmpty(): if 1 or more items in the queue returns true, false otherwise front back 1 2 3 remove, peek add queue CSE 373 SP 18 - Kasey Champion

Map ADT map: Holds a set of unique keys and a collection of values, where each key is associated with one value. a.k.a. "dictionary", "associative array", "hash" operations: put(key, value ): Adds a mapping from a key to a value. get(key ): Retrieves the value mapped to the key. remove(key ): Removes the given key and its mapped value. key value “at" 43 key value “in" 37 key value “you" 22 key value “the" 56 map.get("the") 56 CSE 373 SP 18 - Kasey Champion

Iterators iterator: a Java interface that dictates how a collection of data should be traversed. Behaviors: hasNext() – returns true if the iteration has more elements next() – returns the next element in the iteration while (iterator.hasNext()) { T item = iterator.next(); } CSE 373 SP 18 - Kasey Champion

Testing and Debugging Expected behavior Forbidden Input Empty/Null Black Box Behavior only – ADT requirements From an outside point of view Does your code uphold its contracts with its users? Performance/efficiency White Box Includes an understanding of the implementation Written by the author as they develop their code Break apart requirements into smaller steps “unit tests” break implementation into single assertions Expected behavior The main use case scenario Does your code do what it should given friendly conditions? Forbidden Input What are all the ways the user can mess up? Empty/Null Protect yourself! How do things get started? Boundary/Edge Cases First last Scale Is there a difference between 10, 100, 1000, 10000 items? CSE 373 SP 18 - Kasey Champion

Asymptotic Analysis asymptotic analysis: how the runtime of an algorithm grows as the data set grows n and 4n look very different up close n and 4n look the same over time n2 doesn’t start off dominating the linear functions It eventually takes over… n2 eventually dominates n A function f(n) is dominated by g(n) when there exists two constants c > 0 and n0 > 0 such that for all values of n ≥ n0 f(n) ≤ c * g(n) CSE 373 SP 18 - Kasey Champion

Domination in Graphs Dominates f(n) ∈ Ω(g(n)) Dominated by f(n) ∈ O(g(n)) CSE 373 SP 18 - Kasey Champion

O, Ω, Θ Definitions O(f(n)) is the “family” or “set” of all functions that are dominated by f(n) f(n) ∈ O(g(n)) when f(n) <= g(n) Ω(f(n)) is the family of all functions that dominate f(n) f(n) ∈ Ω(g(n)) when f(n) >= g(n) Θ(f(n)) is the family of functions that are equivalent to f(n) We say f(n) ∈ Θ(g(n)) when both f(n) ∈ O(g(n)) and f(n) ∈ Ω (g(n)) are true For in-depth explanation see https://piazza.com/class/jfbjq4r4em21sn?cid=188 CSE 373 SP 18 - Kasey Champion

Proving Domination f(n) = 5(n + 2) g(n) = 2n2 Find a c and n0 that show that f(n) ∈ O(g(n)). f(n) = 5n + 10 5n + 10 <= 5n2 + 10n2 5n + 10 <= 15n2 for n>= 1 15n2 <= c * 2n2 c = 15/2 = 7.5 n0 = 1 CSE 373 SP 18 - Kasey Champion

O, Ω, Θ Examples For the following functions give the simplest tight O bound a(n) = 10logn + 5 b(n) = 3n – 4n c(n) = 𝑛 2 For the above functions indicate whether the following are true or false a(n) ∈ O(b(n)) a(n) ∈ O(c(n)) a(n) ∈ Ω(b(n)) a(n) ∈ Ω(c(n)) a(n) ∈ Θ(b(n)) a(n) ∈ Θ(c(n)) a(n) ∈ Θ(a(n)) O(logn) O(3n) O(n) TRUE FALSE b(n) ∈ O(a(n)) b(n) ∈ O(c(n)) b(n) ∈ Ω(a(n)) b(n) ∈ Ω(c(n)) b(n) ∈ Θ(a(n)) b(n) ∈ Θ(c(n)) b(n) ∈ Θ(b(n)) FALSE TRUE c(n) ∈ O(b(n)) c(n) ∈ O(a(n)) c(n) ∈ Ω(b(n)) c(n) ∈ Ω(a(n)) c(n) ∈ Θ(b(n)) c(n) ∈ Θ(a(n)) c(n) ∈ Θ(c(n)) TRUE FALSE CSE 373 SP 18 - Kasey Champion

Function Modeling public int mystery(int n) { int result = 0; Create a mathematical model of a piece of code’s runtime in relation to the given input. You can assume basic operations all take the same constant time. You can ignore the runtime of the operations in a for loop’s header and just include how many times the for loop iterates. public int mystery(int n) { int result = 0; for (int i = 0; i < n/2; i++) { result++; } for (int i = 0; i < n/2; i+=2) { result * 10; return result; 𝑓 𝑛 =3+ 3 4 𝑛 CSE 373 SP 18 - Kasey Champion

Modeling Complex Loops for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { System.out.println(“Hello!”); } +c +1 0 + 1 + 2 + 3 +…+ n-1 n 𝑖=1 𝑛 𝑖 Summation 1 + 2 + 3 + 4 +… + n = = f(a) + f(a + 1) + f(a + 2) + … + f(b-2) + f(b-1) + f(b) Definition: Summation 𝑖=𝑎 𝑏 𝑓(𝑖) 𝑖=0 𝑛−1 𝑗=0 𝑖−1 𝑐 T(n) = CSE 373 SP 18 - Kasey Champion

Function Modeling: Recursion public int factorial(int n) { if (n == 0 || n == 1) { return 1; } else { return n * factorial(n – 1); } +c1 +T(n-1) +c2 C1 C2 + T(n-1) when n = 0 or 1 otherwise T(n) = Mathematical equivalent of an if/else statement f(n) = Definition: Recurrence &𝑟𝑢𝑛𝑡𝑖𝑚𝑒 𝑜𝑓 𝑏𝑎𝑠𝑒 𝑐𝑎𝑠𝑒 𝑤ℎ𝑒𝑛 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛𝑎𝑙 &𝑟𝑢𝑛𝑡𝑖𝑚𝑒 𝑜𝑓 𝑟𝑒𝑐𝑢𝑟𝑠𝑖𝑣𝑒 𝑐𝑎𝑠𝑒 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 CSE 373 SP 18 - Kasey Champion