Question

In: Computer Science

Consider the append() operation for a Dynamic Array. In the best case, the operation is O(1)....

Consider the append() operation for a Dynamic Array. In the best case, the operation is O(1). This corresponds to the case where there was room in the space we have already allocated for the array. However, in the worst case, this operation slows down to O(n). This corresponds to the case where the allocated space was full and we must copy each element of the array into a new (larger) array. This problem is designed to discover runtime bounds on the average case when various array expansion strategies are used, but first some information on how to perform an amortized analysis is necessary.

1. Each time an item is added to the array without requiring reallocation, count 1 unit of cost. This cost will cover the assignment which actually puts the item in the array.

2. Each time an item is added and requires reallocation, count X + 1 units of cost, where X is the number of items currently in the array. This cost will cover the X assignments which are necessary to copy the contents of the full array into a new (larger) array, and the additional assignment to put the item which did not fit originally.

To make this more concrete, if the array has 4 spaces and is holding 3 items, adding the forth will cost 1. However, if the array has 4 spaces and is holding 4 items, adding the fifth will cost 5 (4 to move the existing items + 1 to assign the fifth item once space is available).

When we can bound an average cost of an operation in this fashion, but not bound the worst case execution time, we call it amortized execution time.

In a file called amortizedAnalysis.txt , please provide answers to the following questions:

1. How many cost units are spent in the entire process of performing 40 consecutive append operations on an empty array which starts out at capacity 4, assuming that the array will double in capacity each time a new item is added to an already full dynamic array? As N (i.e., the number of appends) grows large, under this strategy for resizing, what is the average big-O complexity for an append?

2. How many cost units are spent in the entire process of performing 40 consecutive append operations on an empty array which starts out at capacity 4, assuming that the array will grow by a constant 2 spaces each time a new item is added to an already full dynamic array? As N (i.e., the number of appends) grows large, under this strategy for resizing, what is the average complexity for an append?

Solutions

Expert Solution

1). Array has Initial size of 8

For adding 16 elements

1. So Cost for first 8 element its 8.

2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 16

3. Then for 10th,11th,12th,.......,16th element Cost is 7.

So total cost is 8 +9 + 7 = 24 ..So for adding 16 elements Cost is 24 units.

For adding 32 elements

1. So Cost for first 8 element its 8.

2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 16

3. Then for 10th,11th,12th,.......,16th element total Cost is 7.

4. On adding 17th element cost is (16+1) = 17 and array is resized to size of 32.

5. Then for 18th,19th,20th,.......,32th element total Cost is 15.

So total cost is 8 +9 + 7 +17+15= 56 ..So for adding 32 elements Cost is 56 units.

We are Intrested in finding the Amortized Cost i.e Averga cost over large number of insertion:-

When the array size is doubled:-

N operation of N pushes will have cost of 1+2+4+8+16+.. + 2k where 2k < N, Summing upo the series we have

2k-1 - 2 . But 2k <=n <= 2k-1 So,

The total cost of the sequence of n insertions will be O(n) .

Since the Amortized Cost is the Average of total Cost = Total Cost/n = O(1)

2) resize by increasing the size by 2 elements

For adding 16 elements

1. So Cost for first 8 element its 8.

2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 10

3. Then for 10th element Cost is 1

4. Then for 11th element Cost is 11

5.Then for 12th element Cost is 1

6.Then for 13th element Cost is 13

6.Then for 14th element Cost is 1

6.Then for 15th element Cost is 15

6.Then for 16th element Cost is 1

So total cost is 60 ..So for adding 16 elements Cost is 60 units.

For adding 32 elements

1. So Cost for first 8 element its 8.

2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 10

3. Then for 10th element Cost is 1

4. Then for 11th element Cost is 11

5.Then for 12th element Cost is 1

7.Then for 13th element Cost is 13

8.Then for 14th element Cost is 1

9.Then for 15th element Cost is 15

10.Then for 16th element Cost is 1

11. Then for 17th element Cost is 17

12. Then for 18th element Cost is 1

13.Then for 19th element Cost is 19

14.Then for 20th element Cost is 1

15.Then for 21th element Cost is 21

16.Then for 22th element Cost is 1

17.Then for 23th element Cost is 23 .. . . .. .. Then for 31th element Cost is 31 .. . . . . . . . . .. Then for 32th element Cost is 1 .

So total cost is 260 ..So for adding 32 elements Cost is 260 units.

N operation of N pushes will have cost of 1+2+3+4+5+.. + N-1 = N(N-1)/2

The total cost of the sequence of n insertions will be O(n2) .

Since the Amortized Cost is the Average of total Cost = Total Cost/n = O(n)


Related Solutions

java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements are distinct (D[i] ̸= D[j], for every i ̸= j ∈ {1, ..., n}) and are taken from the set {1, 2, ..., 2n}.
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
Why do we need a dynamic stack and How to implement a dynamic array stack? (...
Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)
Walmart- Case Questions 1) Consider the history of Walmart. Is Walmart's success best described as an...
Walmart- Case Questions 1) Consider the history of Walmart. Is Walmart's success best described as an intended or emergent strategy? 2) Evaluate changes in Walmart's general environment (e.g., PESTEL analysis) to identify trends that represent both opportunities and challenges. What trends are most important for Walmart's future? 3) Evaluate the competitive forces for the retail industry to identify the forces controlling its profitability (e.g., five forces analysis). Is Walmart positioned for long term profitability? What changes can Walmart make to...
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Consider the case of Timmy, a consumer with preferences over oranges (O) and Sauerkraut (S) that...
Consider the case of Timmy, a consumer with preferences over oranges (O) and Sauerkraut (S) that give him a utility function U=ln(O) + S. A. Given that Timmy has an allowance of I and faces prices of Po and Ps, use the Lagrangian Multiplier method to find his optimal consumption of O and S. B. Demonstrate whether Sauerkraut is a normal or inferior good for Timmy. C. Find Timmy’s indirect utility function. In a world where I=20, Po = 1...
how to read, write and append a text file using a switch case in java.I need...
how to read, write and append a text file using a switch case in java.I need help with the code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT