lecture 2 stacks and
play

Lecture 2: Stacks and CSE 373: Data Structures and Queues - PowerPoint PPT Presentation

Lecture 2: Stacks and CSE 373: Data Structures and Queues Algorithms CSE 373 19 SP - KASEY CHAMPION 1 Q: Which data structure implementation of the Warm Up List ADT would you choose to optimize for the delete function? Instructions


  1. Lecture 2: Stacks and CSE 373: Data Structures and Queues Algorithms CSE 373 19 SP - KASEY CHAMPION 1

  2. Q: Which data structure implementation of the Warm Up List ADT would you choose to optimize for the “delete” function? Instructions Ar ArrayList Linked Lin edLis List Ta Take 3 Minutes uses an Array as underlying storage uses nodes as underlying storage 1. Introduce yourself ArrayList<E> LinkedList<E> to your neighbors J state state data[] 2. Discuss your Node front size size behavior answers List ADT behavior get return data[index] set data[index] = value get loop until index, state st 3. Log onto Poll append data[size] = return node’s value Set of ordered items value, if out of space set loop until index, Everywhere Count of items update node’s value grow data be behavior insert shift values to append create new 1. Go to get(index) return item at index node, update next of make hole at index, PollEv.com/champk set(item, index) replace item at index data[index] = value, if last node append(item) add item to end of list insert create new out of space grow data 2. Text CHAMPK to 22333 insert(item, index) add item at index delete shift following node, loop until to join session, text “1” delete(index) delete item at index values forward index, update next or “2” to select your size() count of items size return size fields answer delete loop until 0 1 2 3 4 index, skip node size return size 4. Get extra credit! 88.6 26.1 94.4 0 0 94.4 88.6 26.1 free space list CSE 373 19 SP - KASEY CHAMPION 2

  3. Administrivia Course Stuff - Class webpage: cs.washington.edu/373 - Piazza: piazza.com/washington/spring2019/cse373 Homework 1 Live! - Individual assignment - 14x content review - GitLab/IntelliJ setup - You will be created a git lab repo (TODAY) Important Dates - Midterm – Friday May 4 th in class - Final – Tuesday June 11 th 8:30-10:20am Homework 2 out next week, partner project - You are responsible for finding your own partner - Lecture, section, piazza, office hours - Fill out partner form so we can generate repos CSE 373 19 WI - KASEY CHAMPION 3

  4. Review: “Big Oh” efficiency : measure of computing resources used by code. - can be relative to speed (time), memory (space), etc. - most commonly refers to run time Assume the following: - Any single Java statement takes same amount of time to run. - A method call's runtime is measured by the total of the statements inside the method's body. - A loop's runtime, if the loop repeats N times, is N times the runtime of the statements in its body. We measure runtime in proportion to the input data size, N. - growth rate : Change in runtime as N gets bigger. How does this algorithm perform with larger and larger sets of data? b = c + 10; This algorithm runs 2N 2 + N + 1 statements. for (int i = 0; i < N; i++) { - We ignore constants like 2 because they are tiny next to N. for (int j = 0; j < N; j++) { dataTwo[j][i] = dataOne[i][j]; - The highest-order term (N 2 ) “dominates” the overall runtime. dataOne[i][j] = 0; } - We say that this algorithm runs "on the order of" N 2 . } - or O(N 2 ) for short (" Big-Oh of N squared") for (int i = 0; i < N; i++) { dataThree[i] = b; } CSE 373 18 AU – SHRI MARE 4

  5. Review: Complexity Class complexity class: A category of algorithm efficiency based on the algorithm's relationship to the input size N. Complexity Big-O Runtime if you Example Algorithm Class double N constant O(1) unchanged Accessing an index of an array logarithmic O(log 2 N) increases slightly Binary search linear O(N) doubles Looping over an array log-linear O(N log 2 N) slightly more than Merge sort algorithm doubles quadratic O(N 2 ) quadruples Nested loops! ... ... ... ... exponential O(2 N ) multiplies drastically Fibonacci with recursion CSE 373 19 WI - KASEY CHAMPION 5

  6. List ADT tradeoffs Time needed to access i-th element: - Array: O(1) constant time char[] myArr = new char[5] - LinkedList: O(n) linear time 0 1 2 3 4 Time needed to insert at i-th element ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ - Array: O(n) linear time - LinkedList: O(n) linear time Amount of space used overall LinkedList<Character> myLl = new LinkedList<Character>(); - Array: sometimes wasted space - LinkedList: compact front ‘h’ ‘o’ / ‘e’ ‘l’ ‘l’ Amount of space used per element - Array: minimal - LinkedList: tiny extra CSE 373 19 WI - KASEY CHAMPION 6

  7. Design Decisions Ta Take 3 Minutes Discuss with your neighbors: How would you implement the List ADT for each of the following situations? For each consider the most important functions to optimize. Situation #1: Write a data structure that implements the List ADT that will be used to store a list of songs in a playlist. LinkedList – optimize for growth of list and movement of songs Situation #2: Write a data structure that implements the List ADT that will be used to store the history of a bank customer’s transactions. ArrayList – optimize for addition to back and accessing of elements Situation #3: Write a data structure that implements the List ADT that will be used to store the order of students waiting to speak to a TA at a tutoring center LinkedList - optimize for removal from front ArrayList – optimize for addition to back CSE 373 19 WI - KASEY CHAMPION 7

  8. Review: What is a Stack? stack : A collection based on the principle of adding elements and retrieving them in the opposite order. - Last-In, First-Out ("LIFO") push pop, peek - 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"). top 3 2 Stack ADT bottom 1 st state supported operations: Set of ordered items - push(item) : Add an element to the top of stack Number of items be behavi vior - pop() : Remove the top element and returns it push(item) add item to top - peek() : Examine the top element without removing it pop() return and remove item at top - size(): how many items are in the stack? peek() look at item at top - isEmpty(): true if there are 1 or more items in stack, false otherwise size() count of items isEmpty() count of items is 0? CSE 143 SP 17 – ZORA FUNG 8

  9. Implementing a Stack with an Array Stack ADT ArrayStack<E> Bi Big O O A Ana nalysis O(1) Constant state st state pop() data[] Set of ordered items O(1) Constant size Number of items peek() behavior behavi be vior O(1) Constant push data[size] = value, if size() push(item) add item to top out of room grow data pop() return and remove pop return data[size - 1], O(1) Constant item at top isEmpty() size-1 peek() look at item at top peek return data[size - 1] O(1) Constant or size() count of items push() size return size isEmpty() count of items is 0? worst case O(N) linear isEmpty return size == 0 0 1 2 3 push(3) 3 5 4 push(4) pop() numberOfItems = 0 1 2 push(5) CSE 373 19 WI - KASEY CHAMPION 9

  10. Implementing a Stack with Nodes Stack ADT LinkedStack<E> Bi Big O O A Ana nalysis state st O(1) Constant state pop() Node top Set of ordered items O(1) Constant size Number of items peek() behavior be behavi vior push add new node at top O(1) Constant size() push(item) add item to top pop return and remove node at pop() return and remove top O(1) Constant item at top isEmpty() peek return node at top peek() look at item at top size return size O(1) Constant size() count of items push() isEmpty return size == 0 isEmpty() count of items is 0? 4 push(3) front 3 push(4) pop() numberOfItems = 0 1 2 CSE 373 19 WI - KASEY CHAMPION 10

  11. Review: What is a Queue? 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. front back remove, peek add 1 2 3 Queue ADT supported operations: st state Set of ordered items - add(item): aka “enqueue” add an element to the back. Number of items be behavi vior - remove(): aka “dequeue” Remove the front element and return. add(item) add item to back - peek() : Examine the front element without removing it. remove() remove and return item at front - size(): how many items are stored in the queue? peek() return item at front size() count of items - isEmpty(): if 1 or more items in the queue returns true, false otherwise isEmpty() count of items is 0? CSE 143 SP 17 – ZORA FUNG 11

  12. Implementing a Queue with an Array Queue ADT ArrayQueue<E> Big O Bi O A Ana nalysis O(1) Constant st state state remove() data[] Set of ordered items O(1) Constant Size Number of items peek() front index behavi be vior back index O(1) Constant size() add(item) add item to back behavior remove() remove and return add – data[size] = value, if O(1) Constant item at front isEmpty() out of room grow data peek() return item at front remove – return data[size - O(1) Constant or size() count of items add() 1], size-1 isEmpty() count of items is 0? worst case O(N) linear peek – return data[size - 1] size – return size isEmpty – return size == 0 0 1 2 3 4 add(5) 5 8 9 add(8) numberOfItems = 0 1 2 3 add(9) 1 front = 0 remove() back = 0 1 2 CSE 373 19 WI - KASEY CHAMPION 12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend