Midsemester Exam Solutions

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Mekelle University Faculty of Business & Economics

Computer Science Department

ICT241: Data Structures and Algorithms

Midsemester Exam – Solutions

Question 1 - Complexity Analysis (24 marks)

a) Why is it not a good idea to use execution time as a measure of the speed of an
algorithm? Suggest an alternative measure and explain why it is better.

Execution time includes computer speed, amount of data, language


(compiled vs. interpreted)
A better measure is computational/asymptotic complexity, because they
exclude the factors mentioned above.

b) Briefly explain the meaning of, and give the formal definitions for:

i. Ω-notation

Omega is the lower bound on the rate of growth of a function.


Definition: the function f(n) is Ω(g(n)) if there exist positive
numbers c and N such that f(n) ≥ c.g(n) for all n ≥ N

ii. little-o notation

Little-o is the upper bound when the lower bound is different.


Definition: the function f(n) is o(g(n)) if f(n) is O(g(n)) but f(n) is
not Θ(g(n))

Page 1 of 6
c) For each of the following pieces of code, state and prove the big-O complexity
in terms of the size of the data n (consider assignment statements only).
Remember that to prove the complexity you must find values for the constants
c and N that satisfy the formal mathematical definition.

i. z = 0;
for (x = 0; x < n; x++)
for (y = 0; y < n; y += 2)
z++;

f(n) = 2 + n(2 + 2(n/2)) = n2 + 2n + 2 = O(n2)


First term biggest for all n> 2
c ≥ 1 + (2/n) + (2/n2)
Therefore N=2, c=2.5

ii. for (x = 1; x < n; x++)


for (y = 0, sum = 0; y < x; y++) {
sum += b[x][y];
cout << sum << endl;
}

f(n) = 1 + 2(n-1) + 2(0+1+2+...+(n-2))


= n2 –n + 1 = O(n2)
First term biggest for all n>1
c ≥ 1 - (1/n) + (1/ n2)
Therefore N=1, c=1

iii. for (p = 0, sum = 0; p < n; p++)


for (q = 1; q < n; q = q*2)
sum += c[p][q];

f(n) = 2n log2n + 2n + 2 = O(n log n)


First term biggest for all n>2
c ≥ 2 + (2/ log2n) + (2/ n log2n)
Therefore N=2, c=5

iv. for (u = 1; u < 5; u++)


for (v = 0; v < u; v++)
sum += d[u][v];

f(n) = 21 = O(1)
Only term biggest for all n>0
c ≥ 21
Therefore N=0, c=41

Page 2 of 6
v. for (x = 0, sum = 0; x < n; x++)
for (y = 0; y < n; y++)
for (z = 0; z < n; z++)
sum += x * y * z;

f(n) = 2n3 + 2n2 + 2n + 2 = O(n3)


First term biggest for all n>1
c ≥ 2 + (2/n) + (2/n2) + (2/n3)
Therefore N=1, c=8

d) Briefly describe the meaning of amortised complexity. In what types of


situation would you need to use amortised complexity analysis?

See handout 1 for description of amortised complexity. It is used when there


is a sequence of related operations

e) A University database application contains a number of records containing


information about students. One operation required by the application is to
search for a student with a specified ID number. Assuming that the student
data is stored in a simple table, state what you would expect the big-O
complexity of the search operation to be in the best, average and worst cases.

Best case is O(1)


Average case is O(n)
Worst case is O(n)

Page 3 of 6
Question 2 - Linked Lists (21 marks)

a) Compare and contrast static and dynamic data structures. Give an example of
each.

Static - cannot change form during execution, dynamic can


Static- size must be known at compile-time
Static must be stored in continuous block of memory
Example of static - array
Example of dynamic – linked list

Below is an outline for an implementation of a singly-linked list.

class IntSLLNode {
public:
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
};

class IntSLList {
public:
IntSLList() {head = tail = 0;}
~IntSLList();
int isEmpty() {return head == 0;}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // del. head & return its info
int deleteFromTail(); // del. tail & return its info
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
private:
IntSLLNode *head, *tail;
};

b) Explain the reason for having two access pointers (head and tail) in the
IntSLList class.

It reduces the complexity of some operations (e.g. deleteFromTail)

Page 4 of 6
c) Write code for the function body of addToTail. The function should add a
new node to the end of the list, and update the tail pointer accordingly. Think
about what special cases that must be considered.

void IntSLList::addToTail(int el) {


if (tail != 0) { // if list not empty;
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}

d) Describe, using diagrams, algorithms for:

i. Deleting a node from the end of the singly-linked list.

Page 5 of 6
ii. Deleting a node from the beginning of the singly-linked list.

e) Briefly describe the operation of a skip list. You should describe, using a
diagram, how nodes are connected together, and also how the search algorithm
works. What is the advantage of a skip list over a normal linked list?

See handout 2 for description of skip list. The advantage over a normal
linked list is that the search complexity is reduced.

END OF EXAMINATION

Examination prepared by: Mekelle University Computer Science Department.

Page 6 of 6

You might also like