CSC 406 Question2
CSC 406 Question2
SECTION A
i)Time complexity of an algorithm: this refers to the time needed/required by an algorithm which
is usually expressed as a function of the size of a problem. (2 marks)
ii) Asymptotic time complexity: this refers to the limiting behaviour of the complexity as size
increases. The asymptotic time complexity determines the size of problems that can be solved by
an algorithm. (2 marks)
iii) Analysis of space complexity of an algorithm or program is the amount of memory it needs
to run to completion. (2 marks)
b) i)Assuming that programs can be evaluated by comparing their running time function, with
the constant of proportionality neglected. Under this assumption, a program with running time θ(
2 3
n ) is better that one with running time θ(n ). Therefore, as n gets large, the ratio of the running
3
n n2
times, which is 2 = gets arbitrarily large. Thus as the size of input n increases, the 0(n¿¿ 3)¿
n 1
program will take significantly more time than the 0( n¿¿ 2)¿ program. (5 marks)
ii) 1. It contains no effort to improve the programming methodology. Big Oh Notation does not
discuss the way and means to improve the efficiency of the program, but it helps to analyze and
calculate the efficiency (by finding time complexity) of the program. (2 marks)
2. It does not exhibit the potential of the constants. For example, one algorithm is taking 1000n2
time to execute and the other n3 time. The first algorithm is O(n2), which implies that it will take
less time than the other algorithm which is O(n3). However in actual execution the second
algorithm will be faster for n < 1000. (2 marks)
c) . i) Constant time = O(1) ii) Logarithmic time = Olog(n) iii) Linear time = O(n) iv) Polynomial
time = O(nc) v) Exponential time = O(cn) Where c > 1 (5 marks, 1 each)
Section
1) A) i) Lists: Mathematically speaking, list is a finite sequences of items drawn from some
set pertinent to the application at hand. That is the collection of a finite set of elements.
( (3 marks)
ii)Stack: A list may be manipulated in a very restricted manner. Items might be added or
deleted only at the end of list. That is items are inserted and deleted in a last-in, first-out
fashion. In this case the list is referred to as stack or push down store. (3 marks)
iii)Queue: a list in which items are always added to one end known as front and removed
from the other known as rear. (3 marks)
b) A graph G= (V, E) consists of a finite nonempty set of vertices V and a set of edges E. (1
mark)
ii) if the edges are ordered pairs (v, w) of vertices, then the graph is said to be directed, v is
called the tail and w the head of the edge (v, w). if the edges are unordered pairs of distinct
vertices, denoted by (v, w) then the graph is said to be undirected. (4 marks)
1)The degree of a vertex (2 marks): the degree of a given vertex is the number of vertices
adjacent to it. (2 marks)
2)The path (2 marks): a path in a directed or undirected graph is a sequence of edges of the form
(v1, v2), (v2, v3),…………….., (vn-1, vn), (2 marks)
3)Adjacency matrix (2 marks) : it is a ||V|| ₓ||V|| matrix of 0’s and 1’s where the ijth element, A[I,
j] is 1 iff there is an edge from vertex I to vertex j. ( 2 marks)
Que 2
a) Sorting is used to arrange names and numbers in meaningful ways. For example Let A be
a list of n elements A1, A2, ....... An in memory. Sorting of list A refers to the operation
of rearranging the contents of A so that they are in increasing (or decreasing) order
(numerically or lexicographically); A1 < A2 < A3 < ...... < An. (4 marks)
b) i) In bubble sort, each element is compared with its adjacent element. If the first element
is larger than the second one, then the positions of the elements are interchanged,
otherwise it is not changed. Then next element is compared with its adjacent element and
the same process is repeated for all the elements in the array until we get a sorted array.
(4 marks)
ii) ALGORITHM
Let A be a linear array of n numbers. Swap is a temporary variable for swapping (or
6. Exit. (8 marks)
iii) Best Case: f (n) = O(n) Worst Case: f(n) = (n(n – 1))/2 = O(n2) (4 marks)
Ques 3
Let A be a collection of data elements, i.e., A is a linear array of say n elements. If we want
to find the presence of an element “data” in A, then we have to search for it. The search is
b)
searched. Then this algorithm will find the location “loc” of data in A. Set loc = – 1,if the
search is unsuccessful.
3. If (data = A[i])
(a) loc = i
4. If (loc > 0)
6. Exit (8 marks)
PROCEDURE SUM[bin_tree];
Begin
RETURN[bin_tree];
RETURN[tree-root[bin_tree];
+ [SUM[tree-left_son[bin_tree]]]
+ [SUM[tree-right_son[bin_tree]]]];
End (4 marks)
Que 5
the given array is a sorted one, otherwise first we have to sort the array elements.
1. Find the middle element of the array (i.e., n/2 is the middle element if the array
2. Compare the middle element with the data to be searched, then there are following
three cases.
(b) If it is less than desired data, then search only the first half of the array, i.e.,
the elements which come to the left side of the middle element.
(c) If it is greater than the desired data, then search only the second half of the
array, i.e., the elements which come to the right side of the middle element.
Repeat the same steps until an element is found or exhaust the search area (3 marks)
ii)
searched. “mid” denotes the middle location of a segment (or array or sub-array) of the
element of A. LB and UB is the lower and upper bound of the array which is under consideration
3. Repeat step 4 and 5 while (LB <= UB) and (A[mid] ! = data)
(a) UB = mid–1
5. Else
(a) LB = mid + 1
7. If (A[mid]== data)
8. Else
(a) Display “the data is not found”
9. Exit (7 marks)
b)Given that x = 3141 and y =5927, let x = ab where a = 31 and b = 41, also let c = cd where c =
59 and d= 27.
A + b = 72 and c + d = 86
Let u = (a + b)(c + d) = 72 * 86 = 6192 (2 marks)
Let v = ac = 31 * 59 = 1829, w = bd = 41 * 27 = 1107 (2 marks)
Xy = 1829000 + (6192 – 1829 -1107)*100 +1107
= 18616707 (2 marks)
c)
Selection sort algorithm finds the smallest element of the array and interchanges it with the
element in the first position of the array. Then it finds the second smallest element from the
remaining elements in the array and places it in the second position of the array and so on. (4
marks)