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
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Week 3 - Friday

slide-2
SLIDE 2

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

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
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
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 10
slide-11
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 12
slide-13
SLIDE 13
slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
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
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
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
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
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
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
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 25
slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28
slide-29
SLIDE 29
slide-30
SLIDE 30

 Finish queues  Linked lists

slide-31
SLIDE 31

 Keep reading section 1.3  Finish Assignment 1

  • Due tonight by midnight!