Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.

Similar presentations


Presentation on theme: "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues."— Presentation transcript:

1 1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues

2 2 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)

3 3 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))

4 4 Lingering Question Why is O(n) not equivalent to O( )? 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) –In the case of n vs., there is no constant that satisfies the condition for all values of n. –Example: Set c to 100,000 Say n =10,000 Then = 100,000,000 –Thus c is too small. We just can’t pick a large enough c.

5 5 Definition of Big-Oh A running time is O(g(n)) if there exist constants n 0 > 0 and c > 0 such that for all problem sizes n > n 0, 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)

6 6 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)

7 7 Stacks

8 8 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

9 9 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 (pushes 10) 30 Enter(pushes 30) 15 Enter (pushes 15) Plus (pops and adds the most recent pair; then pushes the result onto the top of the stack) Plus(same as above; end up with 55 as only entry)

10 10 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. -- 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 Top of stack What do the pops sound like?

11 11 What do the pops sound like? (Sing the note each time you do a pop operation) Push do Push re Push mi Pop Push fa Push so Pop Halt. do re do mi re do re do do fa do so fa do fa do do -- Top of stack

12 12 From Goodrich & Tamassia

13 13 Stack Running Times What is the running time of each operation? Push O(1) Pop O(1) isEmpty() O(1)

14 14 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

15 15 From Goodrich & Tamassia

16 16 From Goodrich & Tamassia

17 17 Stack Example Backtracking 8 Queens Example

18 18 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

19 19 From Goodrich & Tamassia

20 20 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 codeJava stack Free Memory Heap Doesn’t grow Stores method call frames Stores objects

21 21 Queues http://www.lib.uconn.edu/DoddCenter/ASC/Wilcox/Students.htm

22 22 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

23 23 Queue Running Times What is the running time of each operation? Enqueue O(1) Dequeue O(1) isEmpty() O(1)

24 24

25 25 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

26 26 Use of Queues in the OS http://courses.cs.vt.edu/~csonline/OS/Lessons/Processes/index.html Processes waiting in a queue

27 27 Use of Queues in Distributed Systems Animation by Remzi Arpaci-Dusseau

28 28 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

29 29 Java Vector API

30 30 Java Vector API

31 31 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)


Download ppt "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues."

Similar presentations


Ads by Google