Stacks - Courses

Download Report

Transcript Stacks - Courses

Foundations of Software Design
Fall 2002
Marti Hearst
Lecture 12: Stacks and Queues
1
Lingering Question
• Why is the expected value of accessing an
array of length n going to take time n/2?
• The expected value is the number of steps
you’d expect to take on average.
• The average number of steps you’ll have to
take walking through an array of length n is:
– (1 + 2 + 3 + … + n)/n
– This is (n(n+1)/2)/n = (n+1)/2
– This is O(n/2) which is O(n)
2
Lingering Question
• Why is O(n/2) equivalent to O(n)?
• Recall the definition of O(g(n)):
f(n) is (g(n)) if there exist positive constants n0
and c such that for all n>=n0, f(n) <= c*g(n)
– In the case of n/2 vs. n, there is a constant: 2
–
–
–
–
f(n) = n/2
g(n) = n
f(n) <= 2*g(n)
Thus f(n) is O(g(n))
3
Lingering Question
• Why is O(n) not equivalent to O(n 2 )?
• Recall the defintion of O(g(n)):
f(n) is (g(n)) if there exist positive constants n0
and c such that for all n>=n0, f(n) <= c*g(n)
2
– In the case of n vs. n , there is no constant that
satisfies the condition for all values of n.
– Example: Set c to 100,000
• Say n =10,000
2
• Then n = 100,000,000
– Thus c is too small. We just can’t pick a large
enough c.
4
Definition of Big-Oh
A running time is O(g(n)) if
there exist constants n0 > 0
and c > 0 such that for all
problem sizes n > n0, the
running time for a problem
of size n is at most c(g(n)).
In other words, c(g(n)) is
an upper bound on the
running time for sufficiently
large n.
http://www.cs.dartmouth.edu/~farid/teaching/cs15/cs5/lectures/0519/0519.html
c g(n)
5
What is a Data Structure?
• A systematic way of organizing and accessing
data. (Goodrich & Tamassia)
• An organization of information, usually in
memory, for better algorithm efficiency, such
as a queue, stack linked list, heap, and tree. It
may include redundant information, such as
length of the list or number of nodes in a tree.
(http://hissa.nist.gov/dads/terms.html)
6
Stacks
7
Stack
• Container of objects that are inserted and
removed according to the principle of
– Last-in-first-out
– LIFO
• Objects can be inserted at any time, but only
the most recently inserted can be removed at
any time.
• Operations:
– Pop: remove item from stack
– Push: enter item onto stack
8
Why Stacks?
• The Undo facility in software applications
– Is LIFO a good model for this? Why or why not?
• The Back button facility in web browsers
– Is LIFO a good model? Why or why not
• Certain mathematical calculators (operand stack)
– Makes it easy to do parenthesized expressions
– Example:
•
•
•
•
10 Enter
30 Enter
15 Enter
Plus
• Plus
(pushes 10)
(pushes 30)
(pushes 15)
(pops and adds the most recent pair; then
pushes the result onto the top of the stack)
(same as above; end up with 55 as only entry)
9
Push notes onto the stack
•
•
•
•
•
•
•
•
•
•
•
•
(empty stack)
Push do
Push re
Push mi
Push fa
Push so
Push la
Push ti
Push do
--Pop (8 times)
Halt.
Top of stack
Top of stack
-do
re do
mi re do
fa mi re do
so fa mi re do
la so fa mi re do
ti la so fa mi re do
do ti la so fa mi re do
What do the pops sound like?
10
What do the pops sound like?
(Sing the note each time you do a pop operation)
•
•
•
•
•
•
•
•
•
•
•
Push
Push
Push
Pop
Pop
Push
Push
Pop
Pop
Pop
Halt.
do
re
mi
fa
so
Top of stack
do
re do
mi re do
re do
do
fa do
so fa do
fa do
do
--
11
From Goodrich & Tamassia
12
Stack Running Times
• What is the running time of each operation?
• Push
O(1)
• Pop
O(1)
• isEmpty()
O(1)
13
More Definitions
• What is an ADT?
– Abstract data type
– A model of a data structure that specifies
• The type of data stored
• The operations supported
• The types of input and output parameters
– Specifies what the program does, but not how it does it
• What is an API?
– Application Programming Interface
• The names of the methods supported by an ADT
• How those methods are to be declared and used
– e.g., what order the parameters are listed in
• The API specifies the programming details of the ADT
14
From Goodrich & Tamassia
15
From Goodrich & Tamassia
16
Stack Example
• Backtracking
• 8 Queens Example
17
Stacks in the Java VM
• Each process running in a java program has a
java method stack
• Each time a method is called, its state is
pushed onto the method stack
– The local variables and necessary object references
(pointers) are also stored with the method
– All this info together is called a stack frame.
• This is useful for
– Printing error messages (showing the stack trace)
– Doing recursive method calls
18
From Goodrich & Tamassia
19
Memory usage in the Java VM
Makes memory available for new objects
– This is called “the heap”
– When the process calls “new”, we allocate the
appropriate amount of memory
• The heap can be implemented as a queue of blocks of
memory
Program code
Doesn’t grow
Java stack
Stores method
call frames
Free
Memory
Heap
Stores objects
20
Queues
http://www.lib.uconn.edu/DoddCenter/ASC/Wilcox/Students.htm
21
Queue
• Container of objects that are inserted and
removed according to the principle of
– First-in-first-out
– FIFO
• Objects can be inserted at any time, but only
the least recently inserted can be removed at
any time.
• Operations:
– Enqueue: put item onto queue
– Dequeue: remove item from queue
22
Queue Running Times
• What is the running time of each operation?
• Enqueue
O(1)
• Dequeue
O(1)
• isEmpty()
O(1)
23
24
How are Queues Used?
• Queues are used extensively in
– The OS
• For scheduling processes to receive resources
– Computer networking
• For keeping storing and sending network packets
25
Use of Queues in the OS
Processes waiting
in a queue
http://courses.cs.vt.edu/~csonline/OS/Lessons/Processes/index.html
26
Use of Queues
in Distributed
Systems
Animation by
Remzi Arpaci-Dusseau
27
Sequences, Lists, & Vectors
• There are many ways to implement sequences
of items
• In order of abstractness:
– Sequence > List > Vector
• Know about Vectors in Java
– Can be more intuitive to program with
– But can be less efficient
– Implements the Enumeration interface
28
Java
Vector
API
29
Java
Vector
API
30
Vectors in Java
• How do they differ from arrays?
– Can grow the length dynamically
– Can insert items into any position
– Have a different API (set of method calls)
• What are the running times of the operations?
– boolean isEmpty()
• O(1)
– void copyInto(Object[] anArray)
• O(n)
– Object firstElement()
• O(1)
– boolean contains(Object elem)
• O(n)
– Object elementAt(int index)
• O(1)
31