0% found this document useful (0 votes)
33 views2 pages

Data Structures & Algorithms Worksheet

Uploaded by

Dutrei Hailu
Copyright
© All Rights Reserved
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% found this document useful (0 votes)
33 views2 pages

Data Structures & Algorithms Worksheet

Uploaded by

Dutrei Hailu
Copyright
© All Rights Reserved
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

Data Structures and Algorithms

Worksheet
Exercise 1
1. Define the following terms
a. Algorithm
b. Data structures
c. Complexity Analysis
d. Big-Oh Notation

2. Give a Big-Oh , in terms of n, of the running time of each of the following methods.

a. int ExA( int n ) d. int ExD( int n )


{ int a; { int a;
for ( int i = 0; i < n; i += 2 ) for ( int i = 0; i < n * n; i++ )
a = i; for ( int j = 0; j <= n; j++ )
} a = i;
}
b. int ExB( int n )
{ int a; e. int ExE( int n )
for ( int i = 0; i < n * n; i++ ) { int a;
a = i; for ( int i = 0; i < n; i++ )
} for ( int j = 0; j <= n - i; j++ )
a = i;
}
c. int ExC( int n ) {
int a; f. int ExF( int n )
for ( int i = 0; i < n; i++ ) { int a;
for ( int j = 0; j <= i; j++ ) for ( int i = 1; i < n; i *= 2 )
a = i; a = i;
} }

3. Put the followings in order of their complexity

a. 6n log n + 2n e. 1 / n i. n! [That's n factorial.]


b. ( 1 / 3 )n f. 210 j.
2log n
c. n n g. n - n 3 + 7n 5 k. 2n
d. log log n h. n / log n l. 3n + 100 log n

Exercise 2
1) For each of the following two
loops:
 Write an expression f(n) that defines the number of assignment operations executed
by the code
 State what the big-O complexity of the code is
 Using definition 3, suggest reasonable values for c and N

1
i. for (cnt1=0, i=1; i<=n; i++)
for (j=1; j<=n; j++)
cnt1++;

ii. for (cnt2=0, i=1; i<=n; i++)


for (j=1; j<=i; j++)
cnt2++;

2) For each of the following two loops, state what the big-O complexity of the code is:

i. for (cnt3=0, i=1; i<n; i*=2)


for (j=1; j<=n; j++)
cnt3++;

ii. for (cnt4=0, i=1; i<=n; i*=2)


for (j=1; j<=i; j++)
cnt4++;

3) The following conversation was overheard between a programmer and a computer scientist:
Programmer: I just wrote a really fast program to solve the WOW problem. I wrote it in
assembler, optimized every loop, and used every special instruction available on the Quadrium-7
processor.
Computer Scientist: How fast is it?
Programmer: Well, for 10 BogoUnits, it took 10 seconds to find the optimum, and for 11, it took
20 seconds, and for 12, it took 40 seconds, and for 13, it took 80 seconds, and so on.
Computer Scientist: But that's really slow! I have a O( n3 ) algorithm, which is much faster.
Programmer: Faster? I tried your program and it took 1000 seconds for 10 BogoUnits. How
can you claim your method is faster?
a. Estimate how long it would take the Computer Scientist's algorithm to solve the problem
for 20 BogoUnits.
b. Estimate how long it would take the Programmer's program to solve the problem for 20
BogoUnits.
c. What additional information do you need to decide which solution is in fact better for the
actual problem?

Common questions

Powered by AI

The Computer Scientist's O(n^3) algorithm is likely more scalable as input size increases, even if the Programmer’s approach is better for smaller sizes. This is because O(n^3) denotes polynomial growth while the Programmer’s results suggest exponential growth, evidenced by the doubling time indicating a pattern of approximately 2^n. As n grows, the exponential growth would lead to intractable runtime as opposed to polynomial growth, which, while not ideal, remains more manageable for larger inputs .

The complexity of a nested loop where the outer loop runs from 1 to n and the inner loop from 1 to i is O(n^2). This is because the inner loop's range depends on the current iteration of the outer loop (grows linearly from 1 through n), leading to 1 + 2 + ... + n operations, which sums to about n(n+1)/2 = O(n^2). This provides insight into the impact of loop dependencies, illustrating how increasing dependencies (like inner loop boundaries tied to outer loop counters) significantly affect overall complexity .

The conversation highlights how theoretical efficiency (Big-O notation) doesn’t always translate to practical efficiency. The Programmer’s solution, although theoretically less efficient (apparent doubling time), performs better on smaller input sizes (e.g., 10 BogoUnits). Meanwhile, the Computer Scientist's O(n^3) algorithm is theoretically faster but performs worse in practice for small n due to constant factors or lower order terms not considered in Big-O analysis. Such scenarios emphasize the importance of understanding both theoretical complexity and practical overheads .

An 'Algorithm' is a step-by-step procedure or formula for solving a problem, while 'Data Structures' are ways of organizing and storing data so that it can be accessed and modified efficiently . These concepts are fundamental as algorithms dictate the logic of problem-solving, and data structures define how data is organized for processing, both of which are key for creating efficient computer programs.

Complexity analysis is crucial for understanding the efficiency of an algorithm in terms of time and space consumed as the input size grows. Big-Oh notation helps in representing the upper bound of an algorithm’s running time, which aids in analyzing and comparing the performance of different algorithms . It provides a mathematical way to describe the worst-case scenario of an algorithm's growth rate, making it easier to predict and evaluate performance.

To estimate the Programmer's exponential run-time pattern, note the doubling relation: 10 BogoUnits = 10 seconds, 11 BogoUnits = 20 seconds, up to 13 BogoUnits = 80 seconds. For 20 BogoUnits, if the pattern continues, you expect it to take approximately 10 * 2^(20-10) quires = 10 * 2^10 = 10240 seconds. This calculation follows the same exponential growth doubling notion observed in smaller unit steps .

The order of complexity from lowest to highest is: log log n, (1/3)n, 6n log n + 2n, n^n. 'log log n' grows very slowly and thus has the least complexity. '(1/3)n' is a linear function with a constant coefficient, which grows faster than poly-logarithmic functions like 'log log n'. '6n log n + 2n' is higher due to the n log n term, typical of divide-and-conquer algorithms. Finally, 'n^n' is exponential, growing extremely fast with increasing n .

For a simple loop 'for i = 1 to n; a = i;', the number of assignments is effectively n, which corresponds directly to the loop count. The Big-O complexity for such a loop is O(n) since each iteration involves a constant time operation—assigning the value i to variable a—and this operation is repeated n times .

Additional information required includes the specific input size range expected in practical use, the significance of setup and constant factors in performance, available computational resources, and any constraints on execution time. Also, understanding input variability—how often small vs. large inputs will be processed—can decisively influence which algorithm is more efficient under real-world conditions .

The Big-O complexity of a nested loop is determined by multiplying the complexity of each individual loop. If an outer loop runs n times and the inner loop runs m times, the complexity is O(n*m). For example, in a loop structure like 'for i in 1..n; for j in 1..n;', each nested loop runs n times, resulting in a total complexity of O(n*n) = O(n^2). This reflects the total number of operations executed as a product of the loop iterations.

You might also like