CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues - - PowerPoint PPT Presentation

cpsc 221 algorithms and data structures adts stacks and
SMART_READER_LITE
LIVE PREVIEW

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues - - PowerPoint PPT Presentation

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 available very soon! Instructions for Lab 1


slide-1
SLIDE 1

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221

1

slide-2
SLIDE 2

Lab 1 available very soon!

  • Instructions for Lab 1 will be posted on course

webpage very soon: http://www.ugrad.cs.ubc.ca/~cs221

  • Labs start on Monday.
  • Read instructions and do any pre-labs before your

lab section.

slide-3
SLIDE 3

Today’s Outline

  • Abstract Data Types and Data Structures
  • Queues
  • Stacks
  • Abstract Data Types vs. Data Structures

3

slide-4
SLIDE 4

What is an Abstract Data Type?

Abstract Data Type (ADT) - Formally: Mathematical description of an object and the set

  • f operations on the object

In Practice: The interface of a data structure, without any information about the implementation

4

slide-5
SLIDE 5

Data Structures

  • Algorithm

– A high level, language independent description of a step-by-step process for solving a problem

  • Data Structure

– A set of algorithms which implement an ADT

  • Don’t get too obsessed with this distinction.
  • Let’s look at some examples…

5

slide-6
SLIDE 6

Queue ADT

  • Queue operations

– create – destroy – enqueue – dequeue – is_empty

  • Queue property:

if x is enqueued before y is enqueued, then x will be dequeued before y is dequeued. FIFO: First In First Out

F E D C B

enqueue dequeue

G A

6

slide-7
SLIDE 7

Why is it called a “queue”?

slide-8
SLIDE 8

Applications of Queues

  • Hold jobs for a printer
  • Store packets on network routers
  • Make waitlists fair
  • Breadth first search
  • Etc. etc. etc.
  • Basically, any time you need to hold a bunch of

stuff for a bit, where you want to keep them in

  • rder.

8

slide-9
SLIDE 9

Abstract Queue Example

enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue dequeue enqueue E dequeue

9

slide-10
SLIDE 10

Implementing Queues

  • Many different ways to do this!
  • What would you do?
slide-11
SLIDE 11

Circular Array Q Data Structure

void enqueue(Object x) { Q[back] = x back = (back + 1) % size } Object dequeue() { x = Q[front] front = (front + 1) % size return x }

b c d e f

Q

size - 1 front back bool is_empty() { return (front == back) } bool is_full() { return front == (back + 1) % size }

This is pseudocode. Do not correct my semicolons 

11

slide-12
SLIDE 12

Circular Array Q Example

enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue dequeue enqueue E dequeue What are the final contents of the array?

12

slide-13
SLIDE 13

Circular Array Q Example

enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue dequeue enqueue E dequeue What are the final contents of the array? Assuming we can distinguish full and empty (could add a boolean)…

13

slide-14
SLIDE 14

Linked List Q Data Structure

b c d e f

front back void enqueue(Object x) { if (is_empty()) front = back = new Node(x) else back->next = new Node(x) back = back->next } Object dequeue() { assert(!is_empty) return_data = front->data temp = front front = front->next delete temp return return_data } bool is_empty() { return front == null }

14

slide-15
SLIDE 15

Linked List Q Data Structure

b c d e f

front back void enqueue(Object x) { if (is_empty()) front = back = new Node(x) else back->next = new Node(x) back = back->next } Object dequeue() { assert(!is_empty) return_data = front->data temp = front front = front->next delete temp return return_data } bool is_empty() { return front == null }

What’s with the red text?

15

slide-16
SLIDE 16

Circular Array vs. Linked List

  • Which is better? Why?

16

slide-17
SLIDE 17

Circular Array vs. Linked List

  • Which is better? Why?

They both have plusses and minuses! In general, many different data structures can implement an ADT, each with different trade-offs. You must pick the best for your needs.

17

slide-18
SLIDE 18

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

18

slide-19
SLIDE 19

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

push F F

19

slide-20
SLIDE 20

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

push E E F

20

slide-21
SLIDE 21

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

push D D E F

21

slide-22
SLIDE 22

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

push C C D E F

22

slide-23
SLIDE 23

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

pop D E F

23

C

slide-24
SLIDE 24

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

pop E F

24

D

slide-25
SLIDE 25

Stack ADT

  • Stack operations

– create – destroy – push – pop – top – is_empty

  • Stack property: if x is pushed before y is pushed,

then x will be popped after y is popped LIFO: Last In First Out

pop F

25

E

slide-26
SLIDE 26

Why use a stack?

Can you think of anything in real life where you want LIFO instead of FIFO?

slide-27
SLIDE 27

Why use a stack?

Can you think of anything in real life where you want LIFO instead of FIFO? Handling interruptions? Reversing the order of things?

slide-28
SLIDE 28

Stacks in Practice

  • Function call stack
  • Removing recursion
  • Balancing symbols (parentheses)
  • Depth first search

28

slide-29
SLIDE 29

Array Stack Data Structure

S

size - 1

f e d c b

void push(Object x) { assert(!is_full()) S[back] = x back++ } Object top() { assert(!is_empty()) return S[back - 1] } back Object pop() { assert(!is_empty()) back-- return S[back] } bool is_empty() { return back == 0 } bool is_full() { return back == size }

29

slide-30
SLIDE 30

Linked List Stack Data Structure

b c d e f

back void push(Object x) { temp = back back = new Node(x) back->next = temp } Object top() { assert(!is_empty()) return back->data } Object pop() { assert(!is_empty()) return_data = back->data temp = back back = back->next delete temp return return_data } bool is_empty() { return back == null }

30

slide-31
SLIDE 31

Data structures you should already know (a bit)

  • Arrays
  • Linked lists
  • Trees
  • Queues
  • Stacks

31

slide-32
SLIDE 32

Abstract Data Types vs. Data Structures

  • As mentioned before, ADT tells you what
  • perations are available, but does not say anything

about how implemented.

  • Data structure consists of algorithms and memory

layout to implement the ADT.

  • Algorithms are language-independent. How does

this map onto code?

32

slide-33
SLIDE 33

ADTs vs. Data Structures in Code Implementation

  • Theoretically

– abstract base class (or Java interface) describes ADT – inherited implementations implement data structures – can change data structures transparently (to client code)

  • Practice

– different implementations sometimes suggest different interfaces (generality vs. simplicity) – performance of a data structure may influence form of client code (time vs. space, one operation vs. another)

33

slide-34
SLIDE 34

Why so many data structures?

Ideal data structure: fast, elegant, memory efficient Generates tensions:

– time vs. space – performance vs. elegance – generality vs. simplicity – one operation’s performance vs. another’s

“Dictionary” ADT

– list – binary search tree – AVL tree – Splay tree – B tree – Red-Black tree – hash table – …

34

slide-35
SLIDE 35

CS 221 ADT Presentation Algorithm

  • Present an ADT
  • Motivate with some applications
  • Repeat a bunch of times:

– develop a data structure for the ADT – analyze its properties

  • efficiency
  • correctness
  • limitations
  • ease of programming
  • Contrast data structure’s strengths and weaknesses

– understand when to use each one

35

slide-36
SLIDE 36

Coming Up

  • Asymptotic Analysis

36