Potential Method Dynamic Tables Readings Running Time of the MULTIHOP Stack

Potential method dynamic tables readings running time

This preview shows page 8 - 14 out of 41 pages.

Potential Method . . . . . . . . . . . . . Dynamic Tables . Readings Running Time of the MULTIHOP Stack Operation: Analysis A Assuming that a POP operation costs 1 unit, then, a single MULTIPOP operation will cost: min { k , length ( S ) } Consider a sequence of n PUSH, POP, and MULTIPOP operations on an initially empty stack. Stack size is at most n the worst case of a MULTIPOP operation is O ( n ) . a sequence of n operations costs O ( n 2 ) , since we could have n MULTIPOP operations costing O ( n ) each. COMP6651- Algorithm Design - Fall 2013 8
Image of page 8
. . Definitions . . . . . . . . . . Aggregate Analysis Method . . . . . . Accounting Method . . . . . . . Potential Method . . . . . . . . . . . . . Dynamic Tables . Readings Running Time of the MULTIHOP Stack Operation: Analysis 2 Analysis A overestimates the cost of n PUSH, POP, and MULTIPOP operations. Each object can be popped at most once for each time it is pushed. number of times that POP can be called on a nonempty stack, including calls within MULTIPOP, is at most the number of PUSH operations: at most n . any sequence of n PUSH, POP, and MULTIPOP operations takes a total of O ( n ) time. average cost of an operation is O ( n ) / n = O ( 1 ) . COMP6651- Algorithm Design - Fall 2013 9
Image of page 9
. . Definitions . . . . . . . . . . Aggregate Analysis Method . . . . . . Accounting Method . . . . . . . Potential Method . . . . . . . . . . . . . Dynamic Tables . Readings Incrementing a binary counter(1/5) Implementing a k -bit binary counter that counts upward from 0. We use an array A[0... k - 1] of bits, where length[A] = k , as the counter. A binary number x that is stored in the counter has its lowest-order bit in A [ 0 ] and its highest order in A [ k - 1 ] , so that x = k - 1 i = 0 A [ i ] . 2 i , for (x=5) x = 1 × 2 0 + 0 × 2 1 + 1 × 2 2 . can be represented by 101. COMP6651- Algorithm Design - Fall 2013 10
Image of page 10
. . Definitions . . . . . . . . . . Aggregate Analysis Method . . . . . . Accounting Method . . . . . . . Potential Method . . . . . . . . . . . . . Dynamic Tables . Readings Incrementing a binary counter(2/5) . INCREMENT(A) . . . . . 1 i 0 2 while i < length [ A ] and A [ i ] = 1 3 do A [ i ] 0 4 i i + 1 5 if i < length [ A ] 6 then A [ i ] 1 If A [ i ] = 1, then adding 1 flips the bit to 0 in position i and yields a carry of 1, to be added into position i + 1 on the next iteration of the loop. Else ( A [ i ] = 0), then adding 1 flips the bit 0 to 1 in position i . The cost of each INCREMENT operation is linear in the number of bits flipped . COMP6651- Algorithm Design - Fall 2013 11
Image of page 11
. . Definitions . . . . . . . . . . Aggregate Analysis Method . . . . . . Accounting Method . . . . . . . Potential Method . . . . . . . . . . . . . Dynamic Tables . Readings COMP6651- Algorithm Design - Fall 2013 12
Image of page 12
. . Definitions . . . . . . . . . . Aggregate Analysis Method . . . . . . Accounting Method . . . . . . . Potential Method . . . . . . . . . . . . . Dynamic Tables . Readings Incrementing a binary counter(4/5) Analysis 1 A single execution of INCREMENT: Θ( k ) in worst case, if array A contains all 1’s a sequence of n INCREMENT operations on an initially zero counter takes time O ( nk ) in worst case. Analysis 2 Tighten the analysis to yield a worst-case cost of O ( n ) for a sequence of n INCREMENT operations, since not all bits flip at each time INCREMENT is called. A[0] flips each time INCREMENT is called, A [ 1 ] flips each n / 2 times , A [ 2 ] flips each n / 4 times , A [ i ] flips each n / 2 i times , in a sequence of n INCREMENT operations on an initially zero counter.
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 41 pages?

  • Fall '09
  • Analysis of algorithms, Multipop, Stack Operations

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes