Question

In: Computer Science

Suppose we perform a sequence of n operations on a data structure in which the ith...

Suppose we perform a sequence of n operations on a data structure in which the ith operation costs logi if
i is an exact power of 2, and 1 otherwise. Use two different methods to determine the amortized cost per
operation.

Solutions

Expert Solution

Design and Analysis of Algorithms:

1. If the set of stack operations included a MULTIPUSH operation, which pushes k items onto the stack, would the O(1) bound on the amortized cost of stack operations continue to hold? No. The time complexity of such a series of operations depends on the number of pushes (pops vise versa) could be made. Since one MULTIPUSH needs Θ(k) time, performing n MULTIPUSH operations, each with k elements, would take Θ(kn) time, leading to amortized cost of Θ(k).

2. Suppose we perform a sequence of n operations on a data structure in which the ith operation costs i if i is an exact power of 2, and 1 otherwise. Use aggregate analysis to determine the amortized cost per operation. In a sequence of n operations there are blog2 nc+1 exact powers of 2, namely 1,2,4,...,2 blog2 nc . The total cost of these is a geometric sum ∑ blog2 nc i=0 2 i = 2 blog2 nc+1 −1 ≤ 2 log2 n+1 = 2n. The rest of the operations are cheap, each having a cost of 1, and there are less than n such operations. The total cost of all operations is thus T(n) ≤ 2n+n = 3n = O(n), which means O(1) amortized cost per operation.

IV-3 (CLRS 17.2-2 and CLRS 17.3-2) Redo the previous exercise using (a) an accounting method of analysis and (b) a potential method of analysis.

(a) The actual cost of the ith operation is

ci = ( i if i is an exact power of 2

  1 otherwise.)

We assign the amortized costs as follows:

cˆi = ( 2 if i is an exact power of 2

3 otherwise.)

Now an operation, which is an exact power of 2 uses all previously accumulated credit plus one unit of its own amortized cost to pay its true cost. It then assigns the remaining unit as credit. On the other hand, if i is not an exact power of 2, then the operation uses one unit to pay its actual cost and assigns the remaining two units as credit. This covers the actual cost of the operation. We still have to show that there will always be enough credit to pay the cost of any power-of-2 operation. Clearly this is the case for the first operation (i = 1), which happens to be an exact power of two. Let now j > 0. After 2j−1 :th operation there is one unit of credit. Between operations 2j−1 and 2j there are 2j−1 −1 operations none of which is an exact power of 2. Each assigns two units as credit resulting to total of 1+2 ·(2 j−1 −1) = 2 j −1 accumulated credit before 2j th operation. This, together with one unit by its own, is just enough to cover its true cost. Therefore the amount of credit stays nonnegative all the time, and the total amorized cost is an upper bound for the total actual cost.

(b) We define the potential function as follows:

Φ(D0) = 0 and Φ(Di) = 2i−2 blog2 ic+1 +1 for i > 0. (Note that Φ(Di) actually equals to the amount of credit after ith operation in previous exercise.) This potential is always nonnegative, so we have Φ(Di) ≥ 0 = Φ(D0) for all i. Now • For i, which is an exact power of 2, the potential difference is Φ(Di)−Φ(Di−1) = (2i−2i+1)−(2(i−1)−i+1) = 2−i Note that this also holds for i = 1. Thus, the amortized cost of ith operation is cˆi = ci +Φ(Di)−Φ(Di−1) = i+2−i = 2. 1 • For i, which is not an exact power of 2, the potential difference is Φ(Di)−Φ(Di−1) = (2i−i+1)−(2(i−1)−i+1) = 2. Thus, the amortized cost of ith operation is cˆi = ci +Φ(Di)−Φ(Di−1) = 1+2 = 3. As seen above, the amortized cost of each operation is O(1).

4. What is the total cost of executing n of the stack operations PUSH, POP, and MULTIPOP, assuming that the stack begins with s0 objects and finishes with sn objects? Let Φ be the potential function that returns the number of elements in the stack. We know that for this potential function, we have amortized cost 2 for PUSH operation and amortized cost 0 for POP and MULTIPOP operations. The total amortized cost is

Using the potential function and the known amortized costs, we can rewrite the equation as

  

which gives us the total cost of O(n + (s0 − sn)). If sn ≥ s0, then this equals to O(n), that is, if the stack grows, then the work done is limited by the number of operations. (Note that it does not matter here that the potential may go below the starting potential. The condition Φ(Dn) ≥ Φ(D0) for all n is only required to have , but we do not need for that to hold in this application.)

5.   Suppose that instead of contracting a table by halving its size when its load factor drops below 1/4, we contract it by multiplying its size by 2/3 when its load factor drops below 1/3. Using the potential function Φ(T) = |2 ·N(T) − S(T)|, show that the amortized cost of a TABLE-DELETE that uses this strategy is bounded above by a constant. Here N(T) and S(T) denote the number of items stored in table T and the size of T, respectively.

Let ci denote the actual cost of the ith operation, ˆci its amortized cost and ni , si and Φi the number of items stored in the table, the size of the table and the potential after the ith operation, respectively. The potential Φi cannot get negative values and we have Φ0 = 0. Therefore Φi ≥ Φ0 for all i and the total amortized cost provides an upper bound on the actual cost.

Now if the ith operation is TABLE-DELETE, then ni = ni−1 −1. Consider first the case when the load factor does not drop below 1/3, i.e. ni si−1 ≥ 1 3 . Then the table isn’t contracted and si = si−1. We pay ci = 1 for deleting one item. Thus

   = 1+|2ni −si | −|2ni−1 −si−1|

= 1+|2ni−1 −si−1 −2| −|2ni−1 −si−1| ni = ni−1 −1, si = si−1

≤ 1+|(2ni−1 −si−1 −2)−(2ni−1 −si−1)| (reverse) triangle inequality

= 1+2 = 3

Now consider the case when the load factor drops below 1/3, i.e.

ni/( si−1) < 1/ 3 ≤ ni−1/ si−1

⇒ 2ni < (2/ 3) si−1 ≤ 2ni +2. (1)

Now we contract the table and have

si = [(2/3) ·si−1] ⇒( 2/ 3) si−1 −1 ≤ si ≤ (2/ 3) si−1. (2)

By combining (1) and (2) we get 2ni −1 ≤ si ≤ 2ni +2 and thus |2ni −si | ≤ 2. Furthermore, from (1) we get si−1 > 3ni and thus |2ni−1 −si−1| = −2ni−1 +si−1 ≥ ni−1. We pay ci = ni +1 for deleting one item and moving the remaining ni items into the contracted table. Then,

cˆi = ci +Φi −Φi−1

= (ni +1) +|2ni −si | −|2ni−1 −si−1|

≤ (ni +1) +2−ni = 3

In both cases the amortized cost is at most 3 and thus bounded above by a constant.


Related Solutions

Suppose that we wish to test a claim that a sequence of sample data was produced...
Suppose that we wish to test a claim that a sequence of sample data was produced in a random manner, and suppose that each data value belongs to one of two categories. Let n1n1 be the number of elements in the sequence that belong to the first category, n2n2 be the number of elements in the sequence that belong to the second category, and GG be the number of runs in such a sequence. Answer each of the following questions...
Suppose that we wish to test a claim that a sequence of sample data was produced...
Suppose that we wish to test a claim that a sequence of sample data was produced in a random manner, and suppose that each data value belongs to one of two categories. Let n1 be the number of elements in the sequence that belong to the first category, n2 be the number of elements in the sequence that belong to the second category, and G be the number of runs in such a sequence. Answer each of the following questions...
Suppose that we wish to test a claim that a sequence of sample data was produced...
Suppose that we wish to test a claim that a sequence of sample data was produced in a random manner, and suppose that each data value belongs to one of two categories. Let n1n1 be the number of elements in the sequence that belong to the first category, n2n2 be the number of elements in the sequence that belong to the second category, and GG be the number of runs in such a sequence. Answer each of the following questions...
Give O(n) sequence of Fibonacci-heap operations, which creates a Fibonacciheap which has more number of marked...
Give O(n) sequence of Fibonacci-heap operations, which creates a Fibonacciheap which has more number of marked nodes than the number of nodes that are not marked.
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and...
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and Dequeue(): Enqueue(element): add a new element at the tail of the queue; Dequeue(): delete the element at the head of the queue. Show how to implement a stack using two queues. Analyze the running time of the stack operations: Push and Pop.
***Use R/STATA to perform the following analysis Data: ShareValue.xlsx contains data on N=309 firms which sold...
***Use R/STATA to perform the following analysis Data: ShareValue.xlsx contains data on N=309 firms which sold new shares. Data on the following variables is provided. All variables are measured in millions of US dollars. ShrVal is the dependent variable and the all the remaining variables are the explanatory variables. ShareValue: the total value of all shares outstanding, calculated as the price per share times the number of shares outstanding. FirmDebt: firm’s long-term debt TotalSales: sales of the firm. Net_Income: net...
Suppose you have N items with values xi from which we want to sample n items...
Suppose you have N items with values xi from which we want to sample n items with replacement. Each item has its own probability pi of being selected across all times we pick items. What's the estimated variance of the Horvitz-Thompson estimator of T? Let T = > 1, 1=1
Suppose you have N items with values xi from which we want to sample n items...
Suppose you have N items with values xi from which we want to sample n items with replacement. Each item has its own probability pi of being selected across all times we pick items. What's the bias for the following estimator of T: Let T = > 1, 1=1
In this problem, we will perform multiple regression on the Boston housing data. The data contains...
In this problem, we will perform multiple regression on the Boston housing data. The data contains 506 records with 14 variables. The variable medv is the response variable. Solve the following problems in R and print out the commands and outputs : To assess the data use library(MASS) data(Boston) (a) First perform a multiple regression with all the variables, what can you say about the significance of the variables based on only the p-values. Next use the ”step” function to...
Suppose we have the following annual sales data for an automobile dealership: Year                Sales            &n
Suppose we have the following annual sales data for an automobile dealership: Year                Sales                Trend 2009                121                     1 2010                187                     2 2011                165                     3 2012                134                     4 2013                155                     5 2014                167                     6 2015                200                     7 2016                206                     8 2017                221                    9 2018                231                 10 We want to forecast sales for 2019 and 2020 using either a simple trend model or a quadratic trend model. Use a within sample forecasting technique to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT