SLIDE 1
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 - - 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 (
SLIDE 2
SLIDE 3
SLIDE 4
SLIDE 5
SLIDE 6
SLIDE 7
SLIDE 8
O and Ω have a one-to-many relationship with functions
- 4n2+ 3 is O(n2) but it is also O(n3) and O(n4 log n)
- 6n 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
SLIDE 9
- 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. ank is O(nk)
- 4. nk is O(nk+j), for any positive j
- 5. If f(n) is cg(n), then f(n) is O(g(n))
- 6. loga n is O(logb n) for integers a and b > 1
- 7. loga n is O(nk) for integer a > 1 and real k > 0
SLIDE 10
SLIDE 11
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() {} }
SLIDE 12
SLIDE 13
SLIDE 14
SLIDE 15
SLIDE 16
SLIDE 17
SLIDE 18
A queue is a simple data structure that has three basic
- perations (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)
SLIDE 19
Queues are useful whenever you want to keep track of the
- rder of arrival
- A line in a fast food restaurant
- A job in a printer queue
- A buffer for managing data
SLIDE 20
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!
SLIDE 21
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
SLIDE 22
1.
Starting array
2.
Enqueue 9
3.
Dequeue
4.
Dequeue
5.
Enqueue 14
6.
Dequeue
18 3 21 9
Start Size = 4
7 18 3 21 9
Start Size = 5
7 18 3 21
Start Size = 4
14 3 21 9
Start Size = 4
14 21 9
Start Size = 3
3 21 9
Start Size = 3
SLIDE 23
Advantages:
- Dequeue is Θ(1)
- Front is Θ(1)
Disadvantages
- Enqueue is Θ(n) in the very worst case, but not in the amortized case
SLIDE 24
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() {} }
SLIDE 25
SLIDE 26
SLIDE 27
SLIDE 28
SLIDE 29
SLIDE 30
Finish queues Linked lists
SLIDE 31
Keep reading section 1.3 Finish Assignment 1
- Due tonight by midnight!