0% found this document useful (0 votes)
16 views

CSC 406 Question2

The document discusses different algorithms and their time complexities. It covers topics like asymptotic time complexity, different algorithm time complexities like constant, logarithmic, linear, and polynomial time. Binary search and selection sort algorithms are also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CSC 406 Question2

The document discusses different algorithms and their time complexities. It covers topics like asymptotic time complexity, different algorithm time complexities like constant, logarithmic, linear, and polynomial time. Binary search and selection sort algorithms are also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Marking Scheme for Algorithm and Complexity

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

interchange) the position of the numbers.

1. Input n numbers of an array A

2. Initialise i = 0 and repeat through step 4 if (i < n)

3. Initialize j = 0 and repeat through step 4 if (j < n – i – 1)

4. If (A[j] > A[j + 1])


(a) Swap = A[j]

(b) A[j] = A[j + 1]

(c) A[j + 1] = Swap

5. Display the sorted numbers of array A

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

a)Searching is a process of checking and finding an element from a list of elements.

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

successful if data does appear in A and unsuccessful if otherwise (2 marks)

b)

ALGORITHM FOR LINEAR SEARCH

Let A be an array of n elements, A[1],A[2],A[3], ...... A[n]. “data” is the element to be

searched. Then this algorithm will find the location “loc” of data in A. Set loc = – 1,if the

search is unsuccessful.

1. Input an array A of n elements and “data” to be searched and initialise loc = – 1.

2. Initialise i = 0; and repeat through step 3 if (i < n) by incrementing i by one .

3. If (data = A[i])

(a) loc = i

(b) GOTO step 4

4. If (loc > 0)

(a) Display “data is found and searching is successful”


5. Else

(a) Display “data is not found and searching is unsuccessful”

6. Exit (8 marks)

ii) Best Case :So f (n) = O(1). (2 marks)

Average Case: f (n) = O[(n + 1)/2]. (2 marks)

Worst Case: f (n) = O(n + 1). (2 marks)

c) Sum of the children of a binary tree

PROCEDURE SUM[bin_tree];

Begin

If bin_tree = number then

RETURN[bin_tree];

If bin_tree = tree then

RETURN[tree-root[bin_tree];

+ [SUM[tree-left_son[bin_tree]]]

+ [SUM[tree-right_son[bin_tree]]]];

End (4 marks)

Que 5

Binary search technique searches “data” in minimum possible comparisons. Suppose

the given array is a sorted one, otherwise first we have to sort the array elements.

Then apply the following conditions to search a “data”.

1. Find the middle element of the array (i.e., n/2 is the middle element if the array

or the sub-array contains n elements).

2. Compare the middle element with the data to be searched, then there are following
three cases.

(a) If it is a desired element, then search is successful.

(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)

ALGORITHM FOR BINARY SEARCH

Let A be an array of n elements A[1],A[2],A[3],...... A[n]. “Data” is an element to be

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

1. Input an array A of n elements and “data” to be sorted

2. LB = 0, UB = n; mid = int ((LB+UB)/2)

3. Repeat step 4 and 5 while (LB <= UB) and (A[mid] ! = data)

4. If (data < A[mid])

(a) UB = mid–1

5. Else

(a) LB = mid + 1

6. Mid = int ((LB + UB)/2)

7. If (A[mid]== data)

(a) Display “the data found”

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)

You might also like