| | |
How do you prove the following is a bad idea?
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 35
Reputation:
Solved Threads: 0
I have a class that creates a stack object implemented with an array:
So I have a method contractStack() which is called when the length of an array of the same class is greater than INITIAL_CAPACITY of 8, and the size of the stack (contents within the array) is less than or equal to one-quarter of the array's length.
i.e. top <= (Array.length / 4)
Now, I have to prove that it is a bad idea to call this method when the size is less than one-half instead of one-quarter of the array's length.
i.e. top <= (Array.length /2)
The instructions say:
"In order to see this is a bad idea, show the worst case amortized cost of a sequence of n operations on the stack would be Big Omega(n) instead of O(1), if contractStack was used like this.
You should do this by finding some constant c that is greater than 0 and proving that for every inter n >= N0 = 32, there is a sequence of n operations on a stack (following the use of the constructor to initialize it as an empty stack) whose execution requires at least cn^e steps"
I understand the question, but I don't understand how the cost of the operations would be more expensive. Anyone have a good guess at what i'm supposed to do?
My implementation of contractStack is:
I have found bounds for this operation in the function: 3n + 4, which means there are 3 units of cost in the loop, and 4 constant units for the method in general. Which means the worst case cost of this function should be Theta(n).
So I have a method contractStack() which is called when the length of an array of the same class is greater than INITIAL_CAPACITY of 8, and the size of the stack (contents within the array) is less than or equal to one-quarter of the array's length.
i.e. top <= (Array.length / 4)
Now, I have to prove that it is a bad idea to call this method when the size is less than one-half instead of one-quarter of the array's length.
i.e. top <= (Array.length /2)
The instructions say:
"In order to see this is a bad idea, show the worst case amortized cost of a sequence of n operations on the stack would be Big Omega(n) instead of O(1), if contractStack was used like this.
You should do this by finding some constant c that is greater than 0 and proving that for every inter n >= N0 = 32, there is a sequence of n operations on a stack (following the use of the constructor to initialize it as an empty stack) whose execution requires at least cn^e steps"
I understand the question, but I don't understand how the cost of the operations would be more expensive. Anyone have a good guess at what i'm supposed to do?
My implementation of contractStack is:
private void contractStack () {
int[] tempArray = new int[(StackContents.length /2)]; // creates new array of half size
for (int i = 0; i <= top; i++) {
tempArray[i] = StackContents[i]; // places contents of stack into new array
}
StackContents = tempArray; // Set StackContents to the new smaller array.
}I have found bounds for this operation in the function: 3n + 4, which means there are 3 units of cost in the loop, and 4 constant units for the method in general. Which means the worst case cost of this function should be Theta(n).
Last edited by iamsmooth; Oct 17th, 2009 at 6:45 pm.
•
•
Join Date: Jul 2009
Posts: 35
Reputation:
Solved Threads: 0
0
#2 Oct 17th, 2009
Oh, I think I know the worst case. You have to consider expandStack() which expands the array by 2 times when the size of stack is the same as array. So at stack size 32, and array size of 32, when we push an object into the stack, the method finds that the stack is equal to array size so it is multiplied to 64. Immediately after, we pop an object out which means it is no longer 1/2 of the array again so immediately it contracts it again. Whereas if it only contracted it when it was 1/4, this would not happen.
This should cause extra usage in the time of the algorithm. However, I don't know how to convey this on paper with asymptotic notation
Hmm...
This should cause extra usage in the time of the algorithm. However, I don't know how to convey this on paper with asymptotic notation
Hmm... •
•
Join Date: Sep 2008
Posts: 1,606
Reputation:
Solved Threads: 204
0
#3 Oct 17th, 2009
Just some thoughts. .
1) because with the 1/2 condition, if you downsize the array, it will be much more likely that you will then have to subsequently resize the array later (due to having more elements than can fit in the new array you created in contractStack).
2) You should also consider that if you downsize the array based on the < 1/2 condition, then you will be resizing the array more often than you would be with the < 1/4 condition.
1) because with the 1/2 condition, if you downsize the array, it will be much more likely that you will then have to subsequently resize the array later (due to having more elements than can fit in the new array you created in contractStack).
2) You should also consider that if you downsize the array based on the < 1/2 condition, then you will be resizing the array more often than you would be with the < 1/4 condition.
Out.
3
#4 Oct 18th, 2009
You would just write that each push and pop wolud take Theta(n) time. (And so the average time per operation is Theta(n) which is bad because we'd like it to be O(1).)
All my posts may be redistributed under the GNU Free Documentation License.
4
#6 Oct 18th, 2009
No, when n is the size of the array, resizing takes Theta(n) time because you have to copy all the elements.
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Jul 2009
Posts: 35
Reputation:
Solved Threads: 0
0
#7 Oct 19th, 2009
"You should do this by finding some constant c that is greater than 0 and proving that for every inter n >= N0 = 32, there is a sequence of n operations on a stack (following the use of the constructor to initialize it as an empty stack) whose execution requires at least cn^2 steps"
Okay, I understand how it would be Theta(n) if it was to resize on 1/2 needlessly when it could be a constant number of steps without the resize. But what does it mean when it says cn^2, does that imply it's somehow quadratic? I've talked to my other classmates and we all agree it's linear but don't understand how it's n^2.
How do I use cn^2 as opposed to cn?
Okay, I understand how it would be Theta(n) if it was to resize on 1/2 needlessly when it could be a constant number of steps without the resize. But what does it mean when it says cn^2, does that imply it's somehow quadratic? I've talked to my other classmates and we all agree it's linear but don't understand how it's n^2.
How do I use cn^2 as opposed to cn?
![]() |
Similar Threads
- News Story: Broadband over Powerline (BPL) is a Bad Idea (Upcoming News Stories)
- Gasoline Tax Holiday: Good or Bad Idea? (Post your Resume)
- Clean Your Prefetch to Improve Performance (Windows tips 'n' tweaks)
- Why inventor of Linux against another open soruce movement? (IT Professionals' Lounge)
- Don't pump gas on May 15th (Geeks' Lounge)
- omg this is a bad hijack log! (Viruses, Spyware and other Nasties)
- Is using a laptop in a car bad for its hard drive? (Storage)
- On your site - How many ads are too many? (Advertising Sales Strategies)
Other Threads in the Computer Science Forum
- Previous Thread: Greedy algoritm problem
- Next Thread: We only give homework help to those who show effort
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bizarre bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware conversion csc data dataanalysis dataintepretation development dfa dissertation dissertations dissertationthesis dissertationtopic ebook employment energy extensions floatingpoint foreclosuresoftware fuel gadgets geeks givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod itcontracts jobs kindle laser laws lsmeans mainframes marketing mining mobileapplication netbeans networking news os p2p piracy piratebay programming rasterizer research sas science security sex simulation software spying sql study supercomputer supercomputing sweden technology textfield turing turingtest two'scompliment uk virus warehouse ww2






