Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture19.pdf

Avatar for William Albritton William Albritton
September 14, 2014
98

 lecture19.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

Transcript

  1. Memory Upload • Array-based queue • Methods that are private

    • Algorithms for queue methods • Big-O for queue methods • A queue object in memory • Palindromes
  2. What Is a Queue? • A queue is a fancy

    word for a line • A daily life example of a queue is a line at a movie theatre • First person in line is first to buy a ticket • Last person in line is last to buy a ticket • Breaking in line is NOT cool
  3. What Is a Queue? • A computer science example of

    a queue is a data structure with a FIFO (first-in, first-out) property • For example, documents waiting in line to be printed on a printer
  4. What Is a Queue? • Queues are useful in simulating

    people waiting in lines • Compute average waiting time over many runs with random arrivals, random service times, and one or more lines • For example, a bank might run a simulation to see how many tellers they need to keep lines to a minimum
  5. Queue ADT • An ADT (Abstract Data Type) is a

    list of operations on an organized set of data • In Java, an ADT is represented by an interface • See QueueInterface.java on the class web page for the queue interface
  6. Queue Methods • boolean empty() • Is the queue empty?

    • boolean offer(T element) • Adds an element to the end of the queue • Returns true if added successfully, otherwise false
  7. Queue Methods • T poll() • Retrieves and removes the

    element in the front of queue • If queue is empty, returns null • T peek() • Retrieves, but does not remove, the element in front of queue • If queue is empty, returns null
  8. Implementing a Queue • We can either implement a queue

    using an array or linked nodes • Array implementation: • Use a circular array by using the modulus operator (%) • Modulus is the remainder of a division • 7 / 3 = 2, but 7 % 3 = 1 (remainder)
  9. Example Code • See files QueueInterface.java and ArrayQueue.java • See

    the main() method for an example of using the QueueInterface to declare a queue variable and ArrayQueue to instantiate a queue object QueueInterface<Integer> queue = new QueueStack<Integer>(5);
  10. Array-Based Queue • To offer (add) a new element to

    the end of the queue, we place the element at the end of the array
  11. Array-Based Queue • To poll (remove) an element from the

    front of the queue, we have to remove it from the front of the array • To do this, we have 2 choices: 1. Slide (copy) all elements to the left in the array, so we have no empty spaces at the beginning of the array 2. Use a variable to keep track of index of the beginning element in the array
  12. Big-O Break • So which do we choose? 1. Slide

    (copy) all elements to the left in the array will take O(n), as all elements have to be copied 2. Using a variable to keep track of the index will take O(1), because we are simply assigning a value to a variable
  13. Big-O Break • Since O(1) is much faster than O(n),

    we choose to implement choice #2
  14. Front and End Indexes • The ArrayQueue has integer variable

    frontIndex to keep track of the index of the front element in queue • For the index of the element at the end of queue, we have integer variable endIndex
  15. Problem with Arrays • As elements are polled (added) to

    and offered (removed) from the array, we will have empty spaces at the beginning of the array and run out of empty spaces at the end of the array
  16. Solution to Arrays • However, this is easily solved by

    storing elements at the beginning of the array again, making this a “circular” array • The values for endIndex and frontIndex keep increasing until reaching a maximum size, and then return back to zero (0)
  17. Circular Arrays and Modulus • We can use modulus to

    make endIndex and frontIndex “wrap around” the array endIndex = (endIndex+1) % maxSize; • If endIndex is 4 and maxSize is 5 • 4 + 1 = 5 • 5 % 5 = 0 (5/5 = 1 remainder 0) • So the index can wrap around to 0
  18. Compiler Warning • An array instantiation of generic type T

    cannot be type-checked, so it gives a compiler warning • This code in ArrayQueue constructor gives a compiler warning array = (T[])new Object[maxSize]; • You can ignore this warning, as this is the only way in Java to create generic arrays
  19. Private Methods • Sometimes we may want to use private

    methods as opposed to public methods • For example, class ArrayQueue has this private method full() private boolean full(){ boolean fullArray = (currentSize == maxSize); return fullArray; }
  20. Private Methods • The scope of private methods is limited

    to methods of the same class • We can use method full() in class method offer() if(!this.full()){ //OK! • But using method full() in static method main() will NOT compile queue.full(); //ERROR!
  21. Private Methods • Private methods are NOT listed in the

    interface for a class • This is because private methods are used only within the class methods of a class definition • The public methods listed in an interface can be used outside the class definition
  22. Offer (Add) Algorithm 1. If the queue is not full:

    a. Add 1 to endIndex and take modulus to wrap around the array b. Assign element to array at endIndex c. Add 1 to current size of array d. Return true 2. If the queue is full, return false
  23. Offer (Add) Big-O • Big-O is O(1), as all steps

    are O(1) 1. If the queue is not full: O(1) a. Add 1 to endIndex and take modulus to wrap around the array O(1) b. Assign element at endIndex O(1) c. Add 1 to current size of array O(1) d. Return true O(1) 2. If the queue is full, return false O(1)
  24. ArrayQueue Object • Instantiate a queue of Integers QueueInterface<Integer> queue

    = new ArrayQueue<Integer>(5); queue 0 1 2 3 4 frontIndex endIndex currentSize maxSize array 5 0 -1 0 null null null null null
  25. Integer number = new Integer(10); queue.offer(number); queue 0 1 2

    3 4 number Offer (Add) 10 • Offer integer 10 to front of queue frontIndex endIndex currentSize maxSize array 5 0 0 1 null null null null 10
  26. • Offer 20, so add 1 to endIndex Offer (Add)

    20 frontIndex endIndex currentSize maxSize array 5 0 1 2 null null null 10 20 number = new Integer(20); queue.offer(number); queue 0 1 2 3 4 number
  27. number = new Integer(30); queue.offer(number); queue 0 1 2 3

    4 number Offer (Add) 30 • Add 1 to endIndex and currentSize frontIndex endIndex currentSize maxSize array 5 0 2 3 null null 10 20 30
  28. number = new Integer(40); queue.offer(number); queue 0 1 2 3

    4 number Offer (Add) 40 • frontIndex=0 and endIndex=3 frontIndex endIndex currentSize maxSize array 5 0 3 4 null 10 20 30 40
  29. Poll (Remove) Algorithm 1. Initialize variable element to null 2.

    If the queue is not empty: a. Assign the address to the element at the frontIndex to the element variable b. Add 1 to frontIndex and take modulus to wrap around the array c. Subtract 1 from current size of array 3. Return the element variable
  30. Poll (Remove) Big-O • Since all the steps of the

    algorithm are O(1), the Big-O is O(1) 1. Initialize element to null O(1) 2. If the queue is not empty: O(1) a. Assign frontIndex to element O(1) b. Add 1 to frontIndex O(1) c. Subtract 1 from size of array O(1) 3. Return the element variable O(1)
  31. number = queue.poll(); number queue 0 1 2 3 4

    Poll (Remove) in Memory • Poll from front of queue, number=10 frontIndex endIndex currentSize maxSize array 5 1 3 3 null 10 20 30 40
  32. frontIndex endIndex currentSize maxSize array number = queue.poll(); number queue

    0 1 2 3 4 Poll (Remove) in Memory • number=20, add 1 to frontIndex 5 2 3 2 null 10 20 30 40
  33. number = queue.poll(); //number=30 number queue 0 1 2 3

    4 frontIndex endIndex currentSize maxSize array Poll (Remove) in Memory • frontIndex=3 and endIndex=3 5 3 3 1 null 10 20 30 40
  34. number = new Integer(50); queue.offer(number); queue 0 1 2 3

    4 number Offer (Add) 50 • Offer integer 50 to end of queue frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50
  35. number = new Integer(60); queue.offer(number); queue 0 1 2 3

    4 number Offer (Add) 60 • Offer integer 60 to end of queue frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 60
  36. • Jump to offer() method definition public boolean offer(T element){

    element queue 0 1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 60
  37. • Queue is NOT full if (!this.full()) { element queue

    0 1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 60
  38. endIndex=(endIndex+1)%maxSize; element queue 0 1 2 3 4 number The

    Wrap! • Wrap around array: 0=(4+1)%5 frontIndex endIndex currentSize maxSize array 5 3 0 2 10 20 30 40 50 60
  39. array[endIndex] = element; element queue 0 1 2 3 4

    number Offer (Add) 60 • Assign element to array at endIndex frontIndex endIndex currentSize maxSize array 5 3 0 2 20 30 40 50 60
  40. • Add 1 to current size of array currentSize++; element

    queue 0 1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 0 3 20 30 40 50 60
  41. • Successful offer(),so return true return true; element queue 0

    1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 0 3 20 30 40 50 60
  42. • Return to line following offer() queue 0 1 2

    3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 0 3 20 30 40 50 60 number = new Integer(60); queue.offer(number);
  43. • Offer 70 to the end of queue queue 0

    1 2 3 4 number number = new Integer(70); queue.offer(number); Offer (Add) 70 frontIndex endIndex currentSize maxSize array 5 3 1 4 70 30 40 50 60
  44. • Queue has 4 elements, from front to end they

    are: 40, 50, 60, 70 queue 0 1 2 3 4 number ArrayQueue Object frontIndex endIndex currentSize maxSize array 5 3 1 4 30 40 50 60 70
  45. Peek Algorithm 1. Initialize variable element to null 2. If

    the queue is not empty, assign the address to the element at the frontIndex to the element variable 3. Return the element variable
  46. Peek Big-O • Big-O is O(1), because O(1) + O(1)

    + O(1) = O(1) • Initialize element to null O(1) • If the queue is not empty, assign the address to element at the frontIndex to the element variable O(1) • Return the element variable O(1)
  47. number = queue.peek(); queue 0 1 2 3 4 number

    Peek in Memory • Peek does not change frontIndex, endIndex, or currentSize frontIndex endIndex currentSize maxSize array 5 3 1 4 30 40 50 60 70
  48. number = queue.peek(); queue 0 1 2 3 4 number

    Peek in Memory frontIndex endIndex currentSize maxSize array 5 3 1 4 30 40 50 60 70 • Peek only returns address (pointer) to element in front of queue
  49. Palindromes • A palindrome is a string that reads the

    same forwards and backwards • Example palindrome words are: radar, racecar, kook, ono, Ogopogo (a lake monster in British Columbia), tattarrattat (English for "knocking on a door"), saippuakauppias (Finnish for "soap vendor"), and aibohphobia
  50. Algorithm • Algorithm to recognize a palindrome 1.Loop for each

    character of a string a. Offer (add) each character to a queue b. Push (add) each character to a stack 2.Loop for each character of a string a. Remove character from queue & stack b. If any characters different, return false c. Otherwise, return true
  51. Memory Defragmenter • Implementing a queue with an array •

    Private methods • Algorithms for ArrayQueue methods • Big-O for ArrayQueue methods • Memory for an ArrayQueue object • Palindromes
  52. Task Manager • Before the next class, you need to:

    1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture