Download
abstract data types stack queue amortized analysis n.
Skip this Video
Loading SlideShow in 5 Seconds..
Abstract Data Types Stack, Queue Amortized analysis PowerPoint Presentation
Download Presentation
Abstract Data Types Stack, Queue Amortized analysis

Abstract Data Types Stack, Queue Amortized analysis

128 Views Download Presentation
Download Presentation

Abstract Data Types Stack, Queue Amortized analysis

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Abstract Data TypesStack, QueueAmortized analysis Cormen: Ch 10, 17 (11, 18)

  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. Application חוזה בין מתכנת האפליקציה ומיישם מבנה הנתונים ממשק Implementation of the Data structure ADT

  4. ADT : how to work with • Advantage: • Application programming can be done INDEPENDENTLY of implementation • If care about efficiency (complexity): • BE CAREFUL!! • Using the ADT the “wrong way” might become quite inefficient!!!

  5. Example: Stacks • Push(x,S) : Insert element x into S • Pop(S) : Delete the last (time) 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()

  6. push push push The Stack Data Abstraction

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

  8. The Stack: Applications?? • REAL-LIFE: • Rifle • Some document handling? Last in, First out. • Computers/ Communications: • In some applications – LIFO preferred on FIFO • Program control + algorithm == a KEY

  9. Application חוזה בין מתכנת האפליקציה ומיישם מבנה הנתונים ממשק Implementation 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

  10. Application חוזה בין מתכנת האפליקציה ומיישם מבנה הנתונים ממשק Implementation A stack application 2 3 + 5 * 3 2

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

  12. A stack application 2 3 + 5 * 25

  13. A Word on Pseudo-code Pseudo = כאילו Combination of: • Programming (like) commands • “free style English” Allows to conveniently express and algorithm. See Cormen (one page) for his language constructs. No need for FORMAL syntax. (can deviate Cormen)

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

  15. Application חוזה בין מתכנת האפליקציה ומיישם מבנה הנתונים ממשק Implementation Implementation • We will be interested in algorithms to implement the ADT.. • And their efficiency..

  16. 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

  17. 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

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

  19. 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)

  20. 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)

  21. 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)

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

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

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

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

  26. 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)

  27. 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)

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

  29. 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)

  30. 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)

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

  32. 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)

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

  34. Why order of growth • Precise cost can change from computer to computer • From Programmer to programmer • Anyhow MOORE  may go down by factor of 1.5 -2 next year

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

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

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

  38. Stacks via extendable arrays • Array implementation difficulties: • Pick large N: wasted space  • Pick small N: stack is limited  • Solution: • Pick moderate N, and: • When the array is full we will double its size (N) (DOUBLING)

  39. Push t 12 1 A A[1] A[N-1] A[0] push(x,S): /* N is size of array*/ 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

  40. 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)

  41. 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)

  42. 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)

  43. 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)

  44. 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): /* N is array size */ 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)

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

  46. 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 ?

  47. x

  48. x x

  49. x x x x x