Download presentation
Presentation is loading. Please wait.
Published byWillis Woods Modified over 8 years ago
1
CS 221 Analysis of Algorithms Data Structures
2
Portions of the following slides are from Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002 and Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book
3
Remember this “Algorithms + Data Structures = Programs” Niklaus Wirth, Swiss Computer Scientist Or, perhaps- Programs = Programming_language(Algoritms + Data Structures)
4
Data Structures What are they and Why do we care?
5
Stacks Stack – container for a collection of objects where you add objects to one end and retrieve those objects from the same end -- add object to the top of the stack and must retrieve from the top of the stack
6
Stacks Last-In-First-Out -- LIFO Think Stack of dishes in the cafeteria Put a plate on top take a plate from the top Stack of poker chips Stack of anything Bread-crumbs
7
Stacks Algorithms - Stack Operations Push(o) Pop()
8
Stacks Other Stack Operations size() how many objects are on the stack? isEmpty() boolean is the stack empty? top() what is on top of stack? but don’t pop it
9
Stacks What do we use stacks for? Web – Browsers session visit history Text Editors – Word Processors edit/undo Procedure Calls transfer of execution/return
10
Stacks What do we use stacks for? Web – Browsers session visit history Text Editors – Word Processors edit/undo Procedure Calls transfer of execution/return
11
Stack - Array implementation Algorithm push(o) if t = S.length 1 then throw “StackFullError” else t t + 1 S[t] o A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t from: Goodrich and Tamassia, 2002
12
Stack - Array implementation Algorithm pop() if isEmpty() then throw “StackEmptyError” else t t 1 return S[t + 1] A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t from: Goodrich and Tamassia, 2002
13
Stacks for procedure calls To call a procedure Push state of procedure (local variables and PC) on stack To return from a procedure Pop state of calling procedure from stack main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 from: Goodrich and Tamassia, 2002
14
Stack – array implementation Issues run-time? size() isEmpty() top() push(o) pop() Algorithm push(o) if t = S.length 1 then throw “StackFullError” else t t + 1 S[t] o Algorithm pop() if isEmpty() then throw “StackEmptyError” else t t 1 return S[t + 1]
15
Stack – array implementation Memory limitations? growth? How would you handle this? Algorithm push(o) if t = S.length 1 then throw “StackFullError” else t t + 1 S[t] o Algorithm pop() if isEmpty() then throw “StackEmptyError” else t t 1 return S[t + 1]
16
Queues Queue – container for objects Objects are added/retrieved from queue on First-In-First-Out rule FIFO That is, oldest thing in container must be removed first, then next oldest thing, etc. Objects are added to the end of queue, retrieved from front of queue
17
Queues Queue – conceptually parts on a conveyer belt standing line to buy movie tickets In Computer systems Printer queues job scheduling process scheduling Web page request service
18
Queues Queue algorithms – main operations enqueue(o) – placing object at end of queue dequeue() – retreiving object from front of queue Other queue operations front(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored
19
Queues Array-based Queue Algorithm enqueue(o) if size() = N-1 then throw “QueueFullError” Q[r] <- o r <- r + 1 Q 012rf queue
20
Queues Array-based Queue Algorithm dequeue(o) if isEmpty() then throw “QueueEmptyError” temp <- Q[f] Q[f] <- Null f <- f + 1 return temp Q 012rf queue
21
Queues Array-based Queue Algorithm dequeue(o) if isEmpty() then throw “QueueEmptyError” temp <- Q[f] Q[f] <- Null f <- f + 1 return temp Q 012rf queue Algorithm enqueue(o) if size() = N-1 then throw “QueueFullError” Q[r] <- o r <- r + 1 Issues? Run-time? Memory?
22
Queues Array-based Queue circular Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty Q 012rf normal configuration Q 012fr wrapped-around configuration
23
Queue - circular We use the modulo operator (remainder of division) Algorithm size() return (N f + r) mod N Algorithm isEmpty() return (f r) Q 012rf Q 012fr
24
Queue - circular Algorithm enqueue(o) if size() = N 1 then throw FullQueueException else Q[r] o r (r + 1) mod N Operation enqueue throws an exception if the array is full Q 012rf Q 012fr
25
Queue - circular Operation dequeue throws an exception if the queue is empty Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o Q[f] f (f + 1) mod N return o Q 012rf Q 012fr
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.