# Computing Spans q Given an an array X

- Slides: 26

Computing Spans q Given an an array X, the span S[i] of X[i] is q q q the maximum number of consecutive elements X[j] immediately preceding X[i] such that X[j] X[i] Spans have applications to financial analysis n E. g. , stock at 52 -week high © 2014 Goodrich, Tamassia, Goldwasser Stacks X S 6 1 3 1 4 2 5 3 2 1 1

Algorithm 1 q For each index i n Scan backward to find a higher value q Array size is N q Worst-case time complexity? © 2014 Goodrich, Tamassia, Goldwasser Stacks 2

Quadratic Algorithm spans 1(X, n) Input array X of n integers Output array S of spans of X S new array of n integers for i 0 to n 1 do s 1 while s i X[i s] X[i] s s+1 S[i] s return S # n n n 1 + 2 + …+ (n 1) n 1 Algorithm spans 1 runs in O(n 2) time © 2014 Goodrich, Tamassia, Goldwasser Stacks 3

Computing Spans with a Stack q q We keep in a stack the indices of the last element that is taller when “looking back” We scan the array from left to right n n Let i be the current index We pop indices from the stack until we find index j such that X[i] X[j] We set S[i] i j We push i onto the stack © 2014 Goodrich, Tamassia, Goldwasser Stacks 4

Algorithm 2 q Keep on the stack the last building that is taller than the current one n q Root top that can’t be seen Ie. While a building is shorter on the stack n Pop the building © 2014 Goodrich, Tamassia, Goldwasser Stacks 5

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] © 2014 Goodrich, Tamassia, Goldwasser Stacks 6

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] © 2014 Goodrich, Tamassia, Goldwasser Stacks 7

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] © 2014 Goodrich, Tamassia, Goldwasser Stacks 8

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] © 2014 Goodrich, Tamassia, Goldwasser Stacks 9

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] © 2014 Goodrich, Tamassia, Goldwasser Stacks 10

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] • 0, 2, 5 [pop 4, push 5] © 2014 Goodrich, Tamassia, Goldwasser Stacks 11

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] • 0, 2, 5 [pop 4, push 5] • 0, 6 [pop 5, 2; push 6] © 2014 Goodrich, Tamassia, Goldwasser Stacks 12

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] • 0, 2, 5 [pop 4, push 5] • 0, 6 [pop 5, 2; push 6] • 0, 6, 7 [push 7] © 2014 Goodrich, Tamassia, Goldwasser Stacks 13

Algorithm 2 Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] • 0, 2, 5 [pop 4, push 5] • 0, 6 [pop 5, 2; push 6] • 0, 6, 7 [push 7] Span 1 1 2 3 6 1 • How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks 14

Algorithm 2 Stack (top on the right) • 0 [push 0] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] • 0, 2, 5 [pop 4, push 5] • 0, 6 [pop 5, 2; push 6] • 0, 6, 7 [push 7] Span 1 1 2 3 6 1 • How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks 15

Algorithm 2 Stack (top on the right) • 0 [push 1] • 0, 1 [push 1] • 0, 2 [pop 1, push 2] • 0, 2, 3 [push 3] • 0, 2, 4 [pop 3, push 4] • 0, 2, 5 [pop 4, push 5] • 0, 6 [pop 5, 2; push 6] • 0, 6, 7 [push 7] Span 1 1 2 3 6 1 • What is the time complexity (stack operations)? © 2014 Goodrich, Tamassia, Goldwasser Stacks 16

Linear Time Algorithm q Each index of the array q q Is pushed into the stack exactly one Is popped from the stack at most once The statements in the while-loop are executed at most n times q Algorithm spans 2 runs in O(n) time q © 2014 Goodrich, Tamassia, Goldwasser Algorithm spans 2(X, n) # S new array of n integers n A new empty stack 1 for i 0 to n 1 do n while ( A. is. Empty() X[A. top()] X[i] ) do n A. pop() n if A. is. Empty() then n S[i] i + 1 n else S[i] i A. top() n A. push(i) n return S 1 Stacks 17

Worst-case Analysis N data items Algorithm 1 Algorithm 2 Time Complexity Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks 18

Worst-case Analysis N data items Algorithm 1 Algorithm 2 Time Complexity O(N 2) O(N) Space Complexity O(N) © 2014 Goodrich, Tamassia, Goldwasser Stacks 19

Which Data Structure? q Suppose that int. List is a variable of type Collection

Which Data Structure? q For each of the following scenarios choose the “best” data structure from the following list or a combination of data structures: an unsorted array, linked list, Doubly Linked List, circular Linked List, stack, queue. In each case, justify your answer briefly. © 2014 Goodrich, Tamassia, Goldwasser Stacks 21

Which Data Structure? q Suppose that a grocery store decided that customers who come first will be served first © 2014 Goodrich, Tamassia, Goldwasser Stacks 22

Which Data Structure? q A list must be maintained so that any element can be accessed randomly © 2014 Goodrich, Tamassia, Goldwasser Stacks 23

Which Data Structure? q A program needs to remember operations it performed in opposite order © 2014 Goodrich, Tamassia, Goldwasser Stacks 24

Which Data Structure? q The size of a file is unknown. The entries need to be entered as they come in. Entries must be deleted when they are no longer needed. It is important that structure has flexible memory management © 2014 Goodrich, Tamassia, Goldwasser Stacks 25

Which Data Structure? q A list must be maintained so that elements can be added to the beginning or end in O(1) © 2014 Goodrich, Tamassia, Goldwasser Stacks 26