Download presentation
Presentation is loading. Please wait.
Published byImogene Hicks Modified over 5 years ago
1
Computing Spans Given an an array X, the span S[i] of X[i] is
the maximum number of consecutive elements X[j] immediately preceding X[i] such that X[j] X[i] Spans have applications to financial analysis E.g., stock at 52-week high X 6 3 4 5 2 1 S © 2014 Goodrich, Tamassia, Goldwasser Stacks
2
Algorithm 1 For each index i Array size is N
Scan backward to find a higher value Array size is N Worst-case time complexity? © 2014 Goodrich, Tamassia, Goldwasser Stacks
3
Quadratic Algorithm Algorithm spans1(X, n) Input array X of n integers
Output array S of spans of X # S new array of n integers n for i 0 to n 1 do n s n while s i X[i - s] X[i] …+ (n 1) s s …+ (n 1) S[i] s n return S Algorithm spans1 runs in O(n2) time © 2014 Goodrich, Tamassia, Goldwasser Stacks
4
Computing Spans with a Stack
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 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
5
Algorithm 2 Keep on the stack the last building that is taller than the current one Root top that can’t be seen Ie. While a building is shorter on the stack Pop the building © 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) [push 0] © 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) [push 0] 0, [push 1] © 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) [push 0] 0, [push 1] 0, [pop 1, push 2] © 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) [push 0] 0, [push 1] 0, [pop 1, push 2] 0,2, [push 3] © 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) [push 0] 0, [push 1] 0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] © 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) [push 0] 0, [push 1] 0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] 0,2, [pop 4, push 5] © 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) [push 0] 0, [push 1] 0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] 0,2, [pop 4, push 5] 0, [pop 5,2; push 6] © 2014 Goodrich, Tamassia, Goldwasser Stacks
13
Algorithm 2 While shorter building on the stack pop the building
Push current building Stack (top on the right) [push 0] 0, [push 1] 0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] 0,2, [pop 4, push 5] 0, [pop 5,2; push 6] 0,6, [push 7] © 2014 Goodrich, Tamassia, Goldwasser Stacks
14
Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1
0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] 0,2, [pop 4, push 5] 0, [pop 5,2; push 6] 0,6, [push 7] How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks
15
Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1
0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] 0,2, [pop 4, push 5] 0, [pop 5,2; push 6] 0,6, [push 7] How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks
16
Algorithm 2 Stack (top on the right) Span 0 [push 1] 1 0,1 [push 1] 1
0, [pop 1, push 2] 0,2, [push 3] 0,2, [pop 3, push 4] 0,2, [pop 4, push 5] 0, [pop 5,2; push 6] 0,6, [push 7] What is the time complexity (stack operations)? © 2014 Goodrich, Tamassia, Goldwasser Stacks
17
Linear Time Algorithm Each index of the array
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 Algorithm spans2 runs in O(n) time Algorithm spans2(X, n) # S new array of n integers n A new empty stack for i 0 to n 1 do n while (A.isEmpty() X[A.top()] X[i] ) do n A.pop() n if A.isEmpty() then n S[i] i n else S[i] i - A.top() n A.push(i) n return S © 2014 Goodrich, Tamassia, Goldwasser Stacks
18
Worst-case Analysis N data items Algorithm 1 Algorithm 2
Time Complexity Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks
19
Worst-case Analysis N data items Algorithm 1 Algorithm 2
Time Complexity O(N2) O(N) Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks
20
Which Data Structure? Suppose that intList is a variable of type Collection<Integer>, i.e., intList is a list of integer elements. Write a code segment that uses an iterator to compute the sum of all the integer values in the collection. Write a second code segment that does the same thing using a for-each loop. © 2014 Goodrich, Tamassia, Goldwasser Stacks
21
Which Data Structure? 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
22
Which Data Structure? Suppose that a grocery store decided that customers who come first will be served first © 2014 Goodrich, Tamassia, Goldwasser Stacks
23
Which Data Structure? A list must be maintained so that any element can be accessed randomly © 2014 Goodrich, Tamassia, Goldwasser Stacks
24
Which Data Structure? A program needs to remember operations it performed in opposite order © 2014 Goodrich, Tamassia, Goldwasser Stacks
25
Which Data Structure? 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
26
Which Data Structure? A list must be maintained so that elements can be added to the beginning or end in O(1) © 2014 Goodrich, Tamassia, Goldwasser Stacks
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.