You are on page 1of 10

UNIVERSITY OF MAURITIUS

FACULTY OF ENGINEERING

SECOND SEMESTER/YEARLY EXAMINATIONS

MAY 2009

PROGRAMME

BSc (Hons) Computer Science and Engineering BSc (Hons) Electronics and Computer Science BSc (Hons) Information and Communication Technologies

MODULE NAME DATE

Programming Languages and Algorithms Friday 22 May 2009 MODULE CODE CSE 2004Y(3)

TIME NO. OF QUESTIONS SET

9.30 12.30 hrs 8

DURATION NO. OF QUESTIONS TO BE ATTEMPTED

3 hrs 5

INSTRUCTIONS TO CANDIDATES Answer 5 questions in all with at least TWO questions from each section All questions carry equal marks.

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

SECTION A Answer 5 questions in all with at least TWO questions from each section Question 1 [20 marks] (a) What is an algorithm? Can we consider these shampoo instructions to be an algorithm? Justify your answer. shampoo instructions Lather. Rinse. Repeat. [3 marks] (b) Algorithms can be categorized within many types, including Simple recursive algorithms and Dynamic programming algorithms. Using the example of the Fibonacci function (to calculate the nth Fibonacci number), briefly explain the difference between these two types of algorithms. [8 marks] Solve the following recurrence relation using recursion tree. T(n) = T(n/3) + n. Study the following piece of C++ code: int mystery (int n) { int r=0; for (int i=0; i<=n-1; i++) for (int j=i+1; j<=n; j++) r=r+1; return (r); } Determine the worst-case running time (using the Big-Oh notation) of the function mystery. [3 marks] (e) Given the Insertion Sort and the Selection Sort, which is a better sorting algorithm and why? [2 marks]

(c)

[4 marks]

(d)

Page 1 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 2 [20 marks] (a) Assuming that a partitioning algorithm exists (which partitions an array based on a pivot), write the pseudocode for the basic recursive Quicksort algorithm. [2 marks] Sort the following numbers using the basic recursive Quicksort algorithm: 37, 18, 26, 17, 30, 43, 21 (Note: Use the rightmost number as the pivot.) [5 marks] (c) Find the complexity of the quicksort algorithm in the worst-case. (Note: You are required to show all your workings.) [3 marks] (d) Using the keys 5, 10, 1, 7, 4, illustrate how a heap can be used for sorting purposes. [4 marks] Sort the alphabetic keys S, O, R, T, I, N, G using straight radix sorting algorithm. (Note: Assume that letter A is represented by 1, letter B is represented by 2, , letter Z is represented by 26). [6 marks]

(b)

(e)

Question 3 [20 marks] (a) Draw the resulting top-down 2-3-4 tree after inserting the alphabetic keys E, A, S, Y, Q, U, E, S, T, I, O, N, in the given order, into an initially empty tree. [5 marks] Draw the resulting 2-3-4 tree after deleting the alphabetic keys O, Y, I, in the given order, from the resulting tree from Question 3(a) above. [4 marks]

(b)

/contd next page

Page 2 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 3 (continued) (c) Given the red-black tree in figure 3.1 below, with keys 1, 2, 3 and 4, draw the resulting tree after inserting the keys 5, 6, 7, in the given order. [3 marks]
1 Black Link 2 Black Link 3 Red Link 4

Figure 3.1: Red-Black Tree (d) Given the radix search trie in figure 3.2 below, with alphabetic keys F, A, V and O, draw the resulting trie after inserting the keys U, R, I, T, X, in the given order. (Note: Assume that letter A is represented by 1, letter B is represented by 2, , letter Z is represented by 26).
0 0 0
A

1 1
V O

1
F

Figure 3.2: Radix Search Trie [4 marks] (e) Show the content of the hash table that results when the alphabetic keys E, A, S, Y, Q, U, E, S, T, I, O, N are inserted in that order into an empty table using linear probing. (Note: Assume that letter A is represented by 1, letter B is represented by 2, , letter Z is represented by 26). [4 marks]

Page 3 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 4 [20 marks] (a) Perform a search for the pattern A C T A C G T in the target string A C T A C A T A T A G G A C T A C G T A C C using: (i) Brute-force search (ii) Knuth-Morris-Pratt (KMP) search (Note: Show all your workings.) [3 + 5 marks] (b) Differentiate between lossy and lossless compression techniques and give one (1) example of each. [3 marks] Encode the following text using run-length coding: AAABHHHHWWWWWWWWEEEEEEEEEHHKLXFFF [2 marks] (d) Suppose that the text to be encoded, using Huffman coding, contains the characters and associated frequencies given in the table below. Character Frequency (i) (ii) (iii) (iv) a 22 b 45 e 13 h 15 p 23 v 14 r 54 y 26

(c)

Construct the Huffman tree. Write the codes for each character. Encode the text happy. Decode the following sequence of bits: 110111001001111 [3 + 2 + 1 + 1 marks]

Page 4 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 5 [20 marks] (a) (b) Differentiate between a sparse and a dense graph. Refer to the following undirected graph as Graph1.
A 1 B 2 C 1 D

[2 marks]

(i) (ii)

Draw the adjacency matrix of Graph1.

[3 marks]

Apply the Prims algorithm on Graph1 (starting from node A) to draw the MST of Graph1 and determine the cost of the MST. Show your workings. [4 marks]

(c)

Refer to the following undirected graph as Graph2.

(i) (ii)

Draw the adjacency list of Graph2.

[3 marks]

Perform breadth-first search on Graph2 (starting from node A). Show your workings. [4 marks]
/contd next page

Page 5 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 5 (continued) (d) Suppose Dijkstra's algorithm is run on the following directed graph (referred to as Graph3), starting at node A.

Determine the shortest path from A to the other nodes.

[4 marks]

Page 6 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

SECTION B Question 6 [20 marks] (a) Using an example briefly explain the following programming paradigms: (i) (ii) (iii) (iv) (b) Imperative programming Object-oriented programming Functional programming Logic programming [4 x 2 marks]

Briefly explain the following programming language qualities: (i) (ii) Simplicity and Clarity Reliability [2 x 2 marks]

(c)

Consider the syntactic categories Assignment and Expression, which describe the sequences of Tokens that describe arithmetic calculations and assignment of the result to a variable. A simple BNF syntax for these categories is given below: Assignment Identifier = Expression Expression Term | Expression + Term| Expression - Term Term Factor | Term * Factor | Term / Factor Factor Identifier | Literal | (Expression) Identifier Letter | Identifier Letter | Identifier Digit Letter a | b | ... | z | A | B | |Z Digit 0 | 1| 2 | | 9 Draw the parse tree for the expression Z = X + 5*Y. [4 marks]

(d)

Briefly explain the difference between a statically-typed language and a dynamically-typed language. Give an example of each. [4 marks]

Page 7 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 7 [20 marks] (a) Translate the following Java expressions into -calculus notation (using prefix notation): (i) sin(x+3) (ii) length(y)+z (iii) public static int quot(int x, int n) { return (x/n); } [3 marks] Church defined a pure / un-interpreted lambda calculus. Give the definitions for pure lambda calculus. [3 marks] Using an example explain the concept of free and bound variables in lambda calculus. [2 marks] Define a function in Scheme to compute the factorial of N (i.e. N!). [3 marks] Define a function in Scheme that given a list of numbers, it computes the sum of the numbers. [4 marks] The kingdom of Neutronia, where the unit of currency is the tvarp, has the following income tax code: first 5000 tvarps: 0% tax next 10000 tvarps: 10% tax next 20000 tvarps: 15% tax tvarps after 35000: 20% tax Define a function in Scheme, that given an income value, it computes the tax owed. [5 marks]

(b)

(c)

(d)

(e)

(f)

Page 8 of 9

PROGRAMMING LANGUAGES AND ALGORITHMS CSE 2004Y(3)

Question 8 [20 marks] (a)


Study the following Prolog facts:

male(john). male(bill). male(bob). male(jeff). male(ron). female(mary). female(sue). female(nancy). female(jane). mother(mary, sue). mother(mary, bill). mother(sue, nancy). mother(sue, jeff). mother(jane, ron). father(john, sue). father(john, bill). father(bob, nancy). father(bob, jeff). father(bill, ron). (I) Write Prolog clauses/rules to define: (i) The parent relation. (ii) The sister relation. (iii) The aunt relation. (iv) The grandparent relation. Write Prolog queries to find: (i) The grandchildren of mary. (ii) The niece of sue.

[4 x 2 marks]

(II)

[2 marks]

(b)

Write Prolog programs (predicates) to compute: (i) The factorial of N (i.e. N!). (ii) The sum of an array of integers. END OF QUESTION PAPER

[2 x 5 marks]

/kp

Page 9 of 9

You might also like