Slide by matt stallmann included with permission

Info icon This preview shows pages 17–21. Sign up to view the full content.

View Full Document Right Arrow Icon
Slide by Matt Stallmann  included with permission.
Image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks 18 Computing Spans (not in book) Using a stack as an auxiliary  data structure in an algorithm 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 ] and such that  X [ j ] X [ i ]   Spans have applications to  financial analysis E.g., stock at 52-week high 6 3 4 5 2 1 1 2 3 1 X S 0 1 2 3 4 5 6 7 0 1 2 3 4 ©  2010 Goodrich, Tamassia
Image of page 18
Stacks 19 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 1 n while s i X [ i - s ] X [ i ] 1 + 2 + + ( n - 1) s s + 1 1 + 2 + + ( n - 1) S [ i ] s n return S 1 Algorithm  spans1 runs in  O ( n 2 ) time  ©  2010 Goodrich, Tamassia
Image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks 20 Computing Spans with a  Stack We keep in a stack the  indices of the elements  visible 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  x  onto the stack 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 ©  2010 Goodrich, Tamassia
Image of page 20
Stacks 21 Linear Algorithm Algorithm spans2 ( X, n ) # S new array of n integers n A new empty stack 1 for i 0 to n - 1 do n while ( ¬ A . empty () X [ A.top ()] X [ i ] ) do n A.pop () n if A . empty () then n S [ i ] i + 1 n else S [ i ] i - A.top () n A . push ( i ) n return S 1 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  ©  2010 Goodrich, Tamassia
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern