This document contains an assignment on data structures and algorithms. It includes 5 questions analyzing the time complexity of various algorithms using Big O notation. For each question, the student provided analyses of algorithms with running times including linear, quadratic, cubic, N log N, and N^5. The student also implemented codes applying different input sizes and comparing the actual running times to the theoretical analyses.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
289 views
DSA Assignment # 2
This document contains an assignment on data structures and algorithms. It includes 5 questions analyzing the time complexity of various algorithms using Big O notation. For each question, the student provided analyses of algorithms with running times including linear, quadratic, cubic, N log N, and N^5. The student also implemented codes applying different input sizes and comparing the actual running times to the theoretical analyses.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5
DATA STRUCTURE AND ALGORITHM
BSCS 4B SPRING 2014
ASSIGNMENT # 2
DEPARTMENT OF COMPUTER SCIENCE BAHRIA UNIVERSITY, KARACHI CAMPUS Name : Musab Hussain Reg # 34675 Submitted To : Sir Shahid Date : 13 11 2014
Q1 : Order the following functions by growth rate: N, N, N 1.5 , N 2 , N log N, N log log N , N log 2 N, N log(N 2 ), 2/N, 2 N , 2 N/2 , 37, N 2 log N, N 3 . Indicate which functions grow at the same rate .
if we rearrange above listed function in increasing order of growth then : 2/N < 37 < N < N < N log log N < N log N N log(N 2 ) < N log 2 N <N1.5 < N 2 < N 2 log N < N 3 < 2 N/2 < 2N only N log N and N log(N 2 ) functions will grow with same rate .. as N Log N = N Log ( N 2 ) N Log N = 2 N Log N so it becomes in Big O notation O ( N Log N) = O ( N Log N ) .. .. 2 is constant thats why neglected Q2 : For each of the following six program fragments: (a) Give an analysis of the running time (Big-Oh will do) (b) Implement the code in the language of your choice, and values of N (c) Compare your analysis with the actual running times
Program # 1 sum=0 for(int i=0;i<n;i++) sum++;
BIG-Oh Analysis : Running time is O ( N ) Implementation in code and applying values of n : Size (N ) Time growth rate 64 0.0048 ms 128 0.0051 ms 1.0601 256 0.0057 ms 1.1136 512 0.0068 ms 1.1955 Comparing Analysis : growth rate is O (N) , as we doubled the input size the rate approach to 2 .. Program # 2 BIG-Oh Analysis : Running time is O ( N 2 ) Implementation in code and applying values of n : Size (N ) Time growth rate 64 0.0248 ms 128 0.0831 ms 3.3482 256 0.3145 ms 3.7836 512 1.2706 ms 4.0407 1024 4.9505 ms 3.8960 2048 19.6078 ms 3.9608 Program # 3 BIG-Oh Analysis : Running time is O ( N 3 ) Implementation in code and applying values of n : Size (N ) Time growth rate 32 0.1513 ms 64 1.1723 ms 7.7491 128 9.3458 ms 7.9720 256 76.9231 ms 8.2308 Comparing Analysis : growth rate is O (N 2 ) , as we doubled the input size the rate approach to 2 2
Program # 4 BIG-Oh Analysis : Running time is O ( N 2 ) Implementation in code and applying values of n : Size (N ) Time growth rate 64 0.0151 ms 128 0.0435 ms 2.8865 256 0.1570 ms 3.6095 512 0.6098 ms 3.8848 1024 2.4272 ms 3.9806 Comparing Analysis : growth rate is O (N 2 ) , as we doubled the input size the rate approach to 2 2
Program # 5 BIG-Oh Analysis : j can be as large as i 2 , which could be as large as N 2 . k can be as large as j , which is N 2 . The running time is thus proportional to N N 2 N 2 , which is O(N5). Implementation in code and applying values of n : Size (N ) Time growth rate 16 0.1079 ms 32 3.1056 ms 28.7733 64 100.0000 ms 32.2000 Comparing Analysis : growth rate is O (N 5 ) , as we doubled the input size the rate approach to 2 5
Program # 6 BIG-Oh Analysis : Running time is O ( N 4 ) Implementation in code and applying values of n : Size (N ) Time growth rate 16 0.0467 ms 32 0.6596 ms 14.1194 64 10.2041 ms 15.4694 128 166.6667 ms 16.3333 Comparing Analysis : growth rate is O (N 4 ) , as we doubled the input size the rate approach to 2 4
Q3 : An algorithm takes 0.5 ms for input size 100. How long will it take for input size 500 . if the running time is the following (assume low-order terms are negligible) (a) linear (b) O(N log N ) (c) quadratic (d) cubic
a ) Linear => 500 / 100 = N / 0.5 and solving for N , 2.5 ms b) O(N Log N) = > 500 Log 500 / 100 log 100 = N/0.5 and solving for N , 3.3737 ms c) quadratic => 500 2 / 100 2 = N / 2 and solving for N , 12.5 ms d) cubic => 500 3 / 100 3 = N / 2 and solving for N , 62.5 ms
Q4 : An algorithm takes 0.5 ms for input size 100. How large a problem can be solved in 1 min if the running time is the following (assume low-order terms are negligible) (a) linear (b) O(N log N ) (c) quadratic (d) cubic
* 1 min = 60 s = 60000 ms. a ) Linear => x / 100 = 60000 / 0.5 and solving for x then input size is , 12000000 b) O(N Log N) = > x Log x / 100 log 100 = 60000/0.5 and solving for x , input size is 3656807 c) quadratic => x 2 / 100 2 = 60000 / 2 and solving for x ,input size is 34,641 d) cubic => x 3 / 100 3 = 60000 / 2 and solving for x , input size is 4932
Q5 : In a recent court case, a judge cited a city for contempt and ordered a fine of 2 for the first day. Each subsequent day, until the city followed the judges order, the fine was squared (that is, the fine progressed 2$, 4$, 16$, 256$,65$,536$, ...).
What Would be The Fine on N Day ? if we look on progression : 2 = 2 1 = 2 (2^0)
4 = 2 2 = 2 (2^1)
16 = 2 4 = 2 (2^2)
256 = 2 8 = 2 (2^3)
65536 = 2 16 = 2 (2^4)
fine on N day will 2 (2^(n-1)) How Many Days would take to reach D Dollar ? x is the days to reach D dollar fine x = log2 (log2 D) + 1 int days=5;//suppose n= 5 int fine = 2; for(int i=0; i<days-1; i++) fine = fine * fine;