week 3 friday what did we talk about last time asymptotic
play

Week 3 - Friday What did we talk about last time? Asymptotic - PowerPoint PPT Presentation

Week 3 - Friday What did we talk about last time? Asymptotic notation practice Abstract data types Started stacks O and have a one-to-many relationship with functions 4 n 2 + 3 is O ( n 2 ) but it is also O ( n 3 ) and O (


  1. Week 3 - Friday

  2.  What did we talk about last time?  Asymptotic notation practice  Abstract data types  Started stacks

  3.  O and Ω have a one-to-many relationship with functions  4 n 2 + 3 is O ( n 2 ) but it is also O ( n 3 ) and O ( n 4 log n )  6 n log n is Ω ( n log n ) but it is also Ω ( n )  Θ is one-to-many as well, but it has a much tighter bound  Sometimes it is hard to find Θ  Upper bounding isn't too hard, but lower bounding is difficult for many real problems

  4. 1. If f ( n ) is O ( g ( n )) and g ( n ) is O ( h ( n )), then f ( n ) is O ( h ( n )) 2. If f ( n ) is O ( h ( n )) and g ( n ) is O ( h ( n )), then f ( n ) + g ( n ) is O ( h ( n )) 3. an k is O ( n k ) 4. n k is O ( n k + j ), for any positive j 5. If f ( n ) is cg ( n ), then f ( n ) is O ( g ( n )) 6. log a n is O (log b n ) for integers a and b > 1 7. log a n is O ( n k ) for integer a > 1 and real k > 0

  5. public class ArrayStack { private int[] data; private int size; public ArrayStack() {} public void push(int value) {} public int pop() {} public int peek() {} //instead of top public int size() {} }

  6.  A queue is a simple data structure that has three basic operations (very similar to a stack)  Enqueue Put an item at the back of the queue  Dequeue Remove an item from the front of the queue  Front Return the item at the front of the queue  A queue is considered FIFO (First In First Out) or LILO (Last In Last Out)

  7.  Queues are useful whenever you want to keep track of the order of arrival  A line in a fast food restaurant  A job in a printer queue  A buffer for managing data

  8.  A queue is a little bit harder to implement than a stack with an array  The trouble is that you're enqueuing and dequeuing from different ends  Removing something from the front seems to imply that you'll need to shift over all the contents of the array  Enter the circular array!

  9.  A circular array is just a regular array  However, we keep a start index as well as a size that lets us start the array at an arbitrary point  Then, the contents of the array can go past the end of the array and wrap around  The modulus operator ( % ) is a great way to implement the wrap around

  10. Starting array Dequeue 1. 4. 3 21 9 7 18 3 21 Start Size = 3 Start Size = 4 Enqueue 9 Enqueue 14 2. 5. 7 18 3 21 9 14 3 21 9 Start Size = 5 Start Size = 4 Dequeue Dequeue 3. 6. 18 3 21 9 14 21 9 Start Size = 4 Start Size = 3

  11.  Advantages:  Dequeue is Θ (1)  Front is Θ (1)  Disadvantages  Enqueue is Θ ( n ) in the very worst case, but not in the amortized case

  12. public class ArrayQueue { private int[] data = new int[10]; private int start = 0; private int size = 0; public void enqueue(int value) {} public int dequeue() {} public int front() {} public int size() {} }

  13.  Finish queues  Linked lists

  14.  Keep reading section 1.3  Finish Assignment 1  Due tonight by midnight!

Recommend


More recommend