Go Back   TestMagic Forums > Test preparation > GRE Subject Tests > GRE Computer Science
Register Forum Rules FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 2009 November 5th, 03:51 AM   #1 (permalink)
I JUST got here.
 
Join Date: Nov 2009
Posts: 2
ckbest just joined TestMagic.
questions from time complexity

any idea about the time complexity of following code?
1)
-------------------------------
i=1;
while(i <=n )
{
x = 2 * x;
i = i +2;
}

1) O(X^2)
2) O(X^n)
3) O(X^2/n)

-------------------------------

2) Let “rest(i)” return a sub array that is an part of array from i+1th elements to the end of original array. Then how many times will the following function be called in worst case?

Int Function( int[] a, int[] b){

Int i=0, j=0;
If( a[i] == b[j])
Return Function( rest(i) , rest(j)) +1;
Else
Return max(function(rest(i), rest(j+1)), function(rest(i+1),rest(j));
}

A. O(N) B. 0(nlogn) C. D E. O(2^n)
ckbest is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 November 7th, 09:06 AM   #2 (permalink)
I JUST got here.
 
Join Date: Oct 2009
Posts: 6
bahri just joined TestMagic.
q1 loop executes n/2 times so O(n/2) equals O(n) but i dont understant x,

is it a variable ? if so it must not have an effect to complexity
bahri is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Old 2009 November 7th, 09:51 AM   #3 (permalink)
I JUST got here.
 
Join Date: Oct 2009
Posts: 6
bahri just joined TestMagic.
for q2) think T(0) as 1, and if n is the sum of the length of array a and array b,

and T(n) is running time of function([a],[b]) then there are two cases.

--------------------------------------------------------------

on the first Return Function( rest(i) , rest(j))

T(n) = T(n-2) + very small time (vst)

T(n) = (T(n-4) + vst) + very small time

T(n) = ((T(n-6) + vst) + vst ) + very small time

.
.
.
.

T(0) = (T(n - n) + vst .........


so T(n) = [(n/2) * vst] equals O(n/2) equals O(n)

--------------------------------------------------------------

on the second return function

Return max(function(rest(i), rest(j+1)), function(rest(i+1),rest(j));

T(n) = 2 T(n-1)

so

T(n) = 2 (2 T(n-2))
T(n) = 2(2(T(n-3)))
.
.
.
.
T(n-n) = (2^n) T(0) = O(2^n)

----------------------------------------------

if function 2 return always the worst runtime is exponential one 2^n
bahri is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Google Bookmark this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

What you can do
You cannot post new threads
You cannot post replies
You cannot post attachments
You cannot edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:20 AM.

Contact TestMagic   TestMagic Forums      Archive   Privacy Statement

TestMagic Locations   Legal   Privacy


SEO by vBSEO 3.2.0
Copyright © 2009 TestMagic
Ad Management by RedTyger

Scroll Up