1 / 113

Abstract Data Types Stack, Queue Amortized analysis

This article explains the abstract data types (ADTs) Stack and Queue, their application, implementation, and efficiency analysis using arrays and linked lists.

fgallagher
Download Presentation

Abstract Data Types Stack, Queue Amortized analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Data TypesStack, QueueAmortized analysis

  2. ADT is an interface • It defines • the type of the data stored • operations, what each operation does (not how) • parameters of each operation

  3. ADT Application חוזה בין מתכנת האפליקציה ומיישם מבנה הנתונים ממשק Implementation of the Data structure

  4. Example: Stacks • Push(x,S) : Insert element x into S • Pop(S) : Delete the last element inserted into S • Empty?(S): Return yes if S is empty • Top(S): Return the last element inserted into S • Size(S) • Make-stack()

  5. push push push The Stack Data Abstraction

  6. push The Stack Data Abstraction push push Last in, First out. push pop

  7. A stack application InfixPostfix (2+ 3) * 5 2 3 + 5 * ( (5 * (7 / 3) ) – (2 * 7) ) 5 7 3 / * 2 7 *- • Evaluate an expression in postfix or Reverse Polish Notation

  8. A stack application 2 3 + 5 * 3 2

  9. A stack application 2 3 + 5 * 5 5

  10. A stack application 2 3 + 5 * 25

  11. Pseudo-code S ← make-stack() while ( not eof ) do B ← read the next data; if B is an operand thenpush(B,S) else X ← pop(S) Y ← pop(S) Z ← Apply the operation B on X and Y push(Z,S) return(top(S))

  12. Implementation • We will be interested in algorithms to implement the ADT.. • And their efficiency..

  13. Using an array t 12 1 3 A A[2] A[1] A[N-1] A[0] The stack is represented by the array A and variable t 3 1 12

  14. Using an array t 12 1 3 A A[2] A[1] A[N-1] A[0] The stack is represented by the array A and variable t make-stack(): Allocates the array A, which is of some fixed size N, sets t ← -1

  15. Operations t 12 1 3 A A[2] A[1] A[N-1] A[0] size(S): return (t+1) empty?(S): return (t < 0) top(S): ifempty?(S) then error else return A[t]

  16. Pop t 12 1 3 A A[2] A[1] A[N-1] A[0] pop(S): ifempty?(S) then error else e ←A[t] t ← t – 1 return (e) pop(S)

  17. Pop t 12 1 3 A A[2] A[1] A[N-1] A[0] pop(S): ifempty?(S) then error else e ←A[t] t ← t – 1 return (e) pop(S)

  18. Push t 12 1 3 A A[2] A[1] A[N-1] A[0] push(x,S): ifsize(S) = N then error else t ←t+1 A[t] ← x push(5,S)

  19. Push t 12 1 5 A A[2] A[1] A[N-1] A[0] push(x,S): ifsize(S) = N then error else t ←t+1 A[t] ← x push(5,S)

  20. x x.next x.element Implementation with lists top size=3 5 1 12

  21. Implementation with lists top size=3 5 1 12 make-stack(): top ← null size ← 0

  22. Operations top size=3 5 1 12 size(S): return (size) empty?(S): return (top = null) top(S): ifempty?(S) then error else return top.element

  23. Pop top size=3 5 1 12 pop(S): ifempty?(S) then error else e ←top.element top ← top.next size ← size-1 return (e) pop(S)

  24. Pop top size=2 5 1 12 pop(S): ifempty?(S) then error else e ←top.element top ← top.next size ← size-1 return (e) pop(S)

  25. Garbage collection top size=2 5 1 pop(S): ifempty?(S) then error else e ←top.element top ← top.next size ← size-1 return (e) pop(S)

  26. Push top size=2 5 1 push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)

  27. Push top size=2 5 1 5 push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)

  28. Push top size=2 5 1 5 push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)

  29. Push top size=3 5 1 5 push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)

  30. Analysis • Bound the running time of an operation on the worst-case • As a function of the “size”, n, of the data structure • T(n) < 4n+7 • Too detailed, we are just interested in the order of growth

  31. Big-O - קיים cו- כך ש: דוגמא:

  32. cg(n) f(n) n0 Big-O

  33. The running time of our stack and queue operations • Each operation takes O(1) time

  34. Stacks via extendable arrays • We do not want our implementation using arrays to be limited to only N elements • When the array is full we will double its size

  35. Push t 12 1 A A[1] A[N-1] A[0] push(x,S): ifsize(S) = N then {allocate a new array of size 2N copy the old array to the new one; N ← 2N } t ←t+1 A[t] ← x

  36. Push t 12 1 3 3 4 5 7 3 2 8 1 A A[1] A[N-1] A[0] push(x,S): ifsize(S) = N then {allocate a new array of size 2N copy the old array to the new one; N ← 2N } t ←t+1 A[t] ← x push(5,S)

  37. Push t 12 1 3 3 4 5 7 3 2 8 1 A A[1] A[0] push(x,S): ifsize(S) = N then {allocate a new array of size 2N copy the old array to the new one; N ← 2N } t ←t+1 A[t] ← x push(5,S)

  38. Push 12 1 3 3 4 5 7 3 2 8 1 t 12 1 3 3 4 5 7 3 2 8 1 A A[1] A[0] push(x,S): ifsize(S) = N then {allocate a new array of size 2N copy the old array to the new one; N ← 2N } t ←t+1 A[t] ← x push(5,S)

  39. Push t 12 1 3 3 4 5 7 3 2 8 1 A A[1] A[2N-1] A[0] push(x,S): ifsize(S) = N then {allocate a new array of size 2N copy the old array to the new one; N ← 2N } t ←t+1 A[t] ← x push(5,S)

  40. Push t 12 1 3 3 4 5 7 3 2 8 1 5 A A[1] A[2N-1] A[0] push(x,S): ifsize(S) = N then {allocate a new array of size 2N copy the old array to the new one; N ← 2N } t ←t+1 A[t] ← x push(5,S)

  41. Analysis • An operation may take O(n) worst case time ! • But that cannot happen often..

  42. Amortized Analysis • How long it takes to do m operations in the worst case ? • Well, O(nm) • Yes, but can it really take that long ?

  43. x

  44. x x

  45. x x x x x

  46. x x x x x x

  47. x x x x x x x x x x x

  48. x x x x x x x x x x x x

  49. x x x x x x x x x x x x x

More Related