Docsity

Data Structures and Algorithms Exam - BSc (Hons) Computer Science, University of Mauritius, Exams of Applications of Computer Sciences

Toggle sidebar
bg1
UNIVERSITY OF MAURITIUS
FACULTY OF ENGINEERING
SECOND SEMESTER/YEARLY EXAMINATIONS
MAY 2011
PROGRAMME BSc (Hons) Computer Science
Exchange Programme
MODULE NAME Data Structures and Algorithms
DATE Monday
9 May 2011
MODULE CODE CSE2032Y(3)
TIME 09:30 – 12:30 Hrs DURATION 3 hours
NO. OF
QUESTIONS SET
5 NO. OF QUESTIONS
TO BE ATTEMPTED
5
INSTRUCTIONS TO CANDIDATES
Answer all Five (5) questions.
Note: All programs in this paper must be written in the Java Programming Language.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Data Structures and Algorithms Exam - BSc (Hons) Computer Science, University of Mauritius and more Exams Applications of Computer Sciences in PDF only on Docsity!

UNIVERSITY OF MAURITIUS

FACULTY OF ENGINEERING

SECOND SEMESTER/YEARLY EXAMINATIONS

MAY 2011

PROGRAMME BSc (Hons) Computer Science

Exchange Programme

MODULE NAME Data Structures and Algorithms

DATE Monday

9 May 2011

MODULE CODE CSE2032Y(3)

TIME 09:30 – 12:30 Hrs DURATION 3 hours

NO. OF QUESTIONS SET

5 NO. OF QUESTIONS

TO BE ATTEMPTED

5

INSTRUCTIONS TO CANDIDATES

Answer all Five (5) questions.

Note: All programs in this paper must be written in the Java Programming Language.

ANSWER ALL QUESTIONS

Question 1 - [Total 20 Marks]

(a) Suppose that a particular algorithm has time complexity T(n) = 7 * 3 n, and executing an implementation of it on a particular machine takes T seconds for n inputs. Now suppose that we are presented with a machine that is 81 times faster. How many inputs would we process on the new machine in T seconds? [3 marks]

(b) Given the following complexities for two sorting algorithms, sortingAlgo1 and sortingAlgo2 , which one would you select to sort a large number of elements in a random order? Justify your answer.

sortingAlgo1:- Best-case: O(n log (n)) Average-case: O(n 2 ) Worst-case: O (n ln(n))

sortingAlgo2:- Best-case: O(n log (n)) Average-case: O(n log (n)) Worst-case: O(n log (n)) [2 marks]

(c) Sort the following array of integers using selection sort, clearly showing all the steps. 77 45 22 34 23 13 37 [3 marks]

(d) Write a recursive function recursiveSelection that will sort an integer array using the selection sort algorithm. [5 marks]

(e) Determine the recurrence relation for your recursive function of Question 1(d) above. [2 marks]

(f) Solve the following recurrence relation using the substitution method: T(N) = 2T(N-1) + 1 where T(1)= [5 marks]

Question 2 - [Total 20 Marks]

(a) The class Pizza is used to store details about pizzas. The attributes of the class Pizza are name , diameter (in cms), toppings , type (thick, thin or mia). Assume the following definition for the class Pizza:

public class Pizza { String name; float diameter; String toppings; String type;

…/Cont’d next page

Question 2 [Cont’d]

(d) (i) Apply the basic Quicksort algorithm to sort the alphabetic keys “K , E, Y, S, T, O, S, O, R, T”. Use the rightmost element as the partitioning element and show the sub-lists created during the partitioning process. [7 marks]

(ii) Name two (2) improvements that can be applied to the basic Quicksort algorithm in order to improve its partitioning process thus reducing the number of comparisons. [2 marks]

Question 3 - [Total 20 Marks]

(a) An array and a linked list are both examples of list data structures. Briefly explain the differences between an array and a linked list. [2 marks]

(b) The following class definition can be used to represent nodes in a linked list of student-marks. The nodes contain the ID of students and their marks in the “Data Structures and Algorithms” module.

class StudentNode { private int ID; // Key item private double Marks; // data item private StudentNode next; // next node in list

public StudentNode( int id, double mks) // constructor { ID = id; // initialize Key and Data Marks = mks; // (‘next’ is automatically set to null) next= null ; } void set_ID( int id) { ID = id; } int get_ID() { return ID; } void set_Marks( double mks) { Marks=mks; } double get_Marks() { return Marks; } public void setnext(StudentNode newnode) { next=newnode; } StudentNode getnext() { return next; } …/Cont’d next page

Question 3 [Cont’d]

public void displayNode() // display ourself { System. out .print("{" + ID + ", " + Marks + "} "); } } // end class Node

The class for the linked-list of student marks can be created using the following class definition. class LinkedList { private Node head; // ref to first link on list // ------------------------------------------------------------- public LinkedList() // constructor { head = null; // no items on list yet } // To check for empty list public boolean isEmpty() // true if list is empty { return (head==null); } public void insertLast(int id, double mks) { /* To insert a node at the end of a linked list / } public Node find(int k) // find Node with given key { / To find if a node with student id K exists and returns that node or null if it does not exist. / } public Node deleteNode(int k) // delete node with given key { / finds a node with ID=k and, deletes and returns that node / } public void displayList() { / Display the contents of the linked list from first to last element */ } }

public long peek() { return stackArray[top]; }

}

A class StackApp can be used to create and manipulate Stack objects. It consists of the method main () and a method mergeStack to merge two given stack objects into a third one and return the latter. The class StackApp can be defined as follows:

Class StackApp { public static Stack mergeStack ( Stack S1, Stack S2) { /* take the contents of stacks S1 and S2 and create a third stack S3 by merging the contents of S2 and S1 */ } public static void main(String[] args) { Stack Stack1=new Stack(10); Stack Stack2=new Stack(5); //input the elements for S1 and S Stack3=mergeStack(Stack1,Stack2); } } Implement the method mergeStack.

A sample output of creating two stacks Stack1 and Stack2, and applying the method mergeStack () is given below:

number of items to input on Stack1: 3 Input 3 long values:3 2 1 number of items to input on stack2: 4 Input 4 long values:7 6 5 4

Stack1 contains: 1 2 3 Stack2 contains: 4 5 6 7 After merging Stack1 and Stack2 we get Stack3, and Stack3 contains: 1 2 3 4 5 6 7 [4 marks]

(d) Apply the heapsort algorithm to sort the numbers 6, 4, 2, 1, 10. Show your workings. [5 marks]

Question 4 - [Total 20 Marks]

(a) (i) Using a simple example, briefly explain why there is a need to balance binary search trees (BSTs). [2 marks]

(ii) Briefly explain how 2-3-4 trees can be used to balance BSTs, using a simple example to support your explanation. [3 marks]

(iii) Draw the red-black tree that results from inserting the alphabetic keys “A N E A S Y Q U E S T I O N”. Show your workings. [6 marks]

(b) Using 5 bit-representations and the integers 1 for A, 2 for B, ...26 for Z, apply the radix-exchange sort to sort the alphabetic keys “S I M P L E T E X T”. [5 marks]

(c) Draw the radix search trie that results from inserting the alphabetic keys “S I M P L E T X “. Again, use 5 bit-representations and the integers 1 for A, 2 for B, ... for Z. [4 marks]

Question 5 [Total 20 Marks]

(a) Assume that the text to be encoded, using Huffman coding , contains the following characters and associated frequencies as given in table 5.1 below.

Table 5.1: Character in text to be encoded and their associated frequencies Character i a l b s t d Frequency 12 13 7 15 20 17 9

(i) Determine the Huffman codes for each character. You are required to show all your workings.

(ii) Decode the following sequence of bits, given that it has been encoded using the codes from part (i) above: 00101110111011 [4 + 2 marks]

(b) Assume that the pattern “ACGTACTG” has to be searched in the string “CTGACCTACGTACTGACCGTAC” using the Knuth-Morris-Pratt (KMP) searching algorithm. Using failure function for KMP, fill in the following table for the given pattern:

j 1 2 3 4 5 6 7 k 0 1 2 3 4 5 6 failure(k)

[3 marks]

…/Cont’d next page