3
Dat a St ru ct ur es and Al gori thms : CS 210A, ESO2 07A, ES O2 11 Practice-sheet 2 Date:  5 Septemb er, 2012. 1. Recall the problem of nding length of the shortest route to all cells in a  n  × n grid from a given cell  c 0  (specied as a pair ( i, j )). We discussed this problem and designed an ecient algorithm for this problem in the class using the data structure queue. You have to suitably extend/modify this algorithm so that it also computes a data structure of size O (n 2 ) which can be used to eciently report the shortest route from the cell  c 0  to any given cell  c  in the grid. You must also give the corresponding shortest-route-reporting algorithm as well. The time complexity of this algorithm has to be of the order of the number of cells that appear on the shortest route from c 0  to c. 2. Circular linked list is a singly link ed list where the next eld of the last node points to (stor es the address of) the rst node of the list. What are the adv antages/d isadv ant ages of a circular linked list over a singly linked list and a doubly linked list. 3.  The power of Recursion Recursion is a very powerful and eective way to solve various computational prob- lems. Usually the length of the code of a recursive algorithm is very short. However, one has to be very careful in des igning the rec ursive alg ori thm. In par tic ular, one must rst formulate the solution of the problem recursively in terms of smaller in- stanc es of the same proble m and then design a recu rsiv e algorithm based on it. One mus t pay a lot of attent ion while handli ng the base case. Y ou were taught recur sion in ESC101 course. The aim of this exerci se is to refresh your knowledge of recur sion using some old/new proble ms. Write pseudo-c ode for a recursive algorithm for the follo wing problems. Y ou are also advised to implemen t these pseudo-codes in your favorite programming language. (a) Given an array  A  storing  n  distinct elements, and an int eger r, generate all possible permutations formed by picking r  distinct elements from A. (b) Given an array  A  storing  n  distinct elements, and an int eger r, generate all possible combinations formed by picking r  elements from A. (c) Giv en an array  A  storing  n  element s (dupli cates allowed), and an inte ger r, generate all possible combinations formed by picking r  elements from A. (d) Generate all possible strings of length 0’s and 1’s such that there are no consec - utive 1’s in the string. Can you guess how many will they be ? (e) Given a binary se arc h tree T  specied by the address of its root node, enumerate all the values stored in the tree in the decreasing order. (f) Giv en a binary tree  T  specied by the address of its root node, transform this tree into another binary tree which is the mirror image of the original tree T . (g) A stack can be used to generate some (not nece ssaril y all) p ermutatio ns of rst  n numbers. For example, for n = 3, we can generate (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 2, 1) using stacks but we can not generate permutation (3 , 1, 2) using stack. 1

practice-sheet2.pdf

Embed Size (px)

Citation preview

Page 1: practice-sheet2.pdf

8/11/2019 practice-sheet2.pdf

http://slidepdf.com/reader/full/practice-sheet2pdf 1/3

Data Structures and Algorithms : CS210A, ESO207A, ESO211

Practice-sheet 2

Date:   5 September, 2012.

1. Recall the problem of finding length of the shortest route to all cells in a   n  × n

grid from a given cell   c0   (specified as a pair (i, j)). We discussed this problem anddesigned an efficient algorithm for this problem in the class using the data structurequeue. You have to suitably extend/modify this algorithm so that it also computes adata structure of size  O(n2) which can be used to efficiently report the shortest routefrom the cell  c0  to any given cell  c  in the grid. You must also give the correspondingshortest-route-reporting algorithm as well. The time complexity of this algorithm hasto be of the order of the number of cells that appear on the shortest route from  c0  toc.

2. Circular linked list is a singly linked list where the next field of the last node points to(stores the address of) the first node of the list. What are the advantages/disadvantages

of a circular linked list over a singly linked list and a doubly linked list.3.  The power of Recursion

Recursion is a very powerful and effective way to solve various computational prob-lems. Usually the length of the code of a recursive algorithm is very short. However,one has to be very careful in designing the recursive algorithm. In particular, onemust first formulate the solution of the problem recursively in terms of smaller in-stances of the same problem and then design a recursive algorithm based on it. Onemust pay a lot of attention while handling the base case. You were taught recursionin ESC101 course. The aim of this exercise is to refresh your knowledge of recursionusing some old/new problems. Write pseudo-code for a recursive algorithm for thefollowing problems. You are also advised to implement these pseudo-codes in your

favorite programming language.

(a) Given an array   A   storing   n   distinct elements, and an integer   r, generate allpossible permutations formed by picking  r  distinct elements from  A.

(b) Given an array   A   storing   n   distinct elements, and an integer   r, generate allpossible combinations formed by picking  r  elements from  A.

(c) Given an array   A   storing   n   elements (duplicates allowed), and an integer   r,generate all possible combinations formed by picking  r  elements from A.

(d) Generate all possible strings of length 0’s and 1’s such that there are no consec-utive 1’s in the string. Can you guess how many will they be ?

(e) Given a binary search tree T  specified by the address of its root node, enumerate

all the values stored in the tree in the decreasing order.

(f) Given a binary tree  T   specified by the address of its root node, transform thistree into another binary tree which is the mirror image of the original tree  T .

(g) A stack can be used to generate some (not necessarily all) permutations of first nnumbers. For example, for n = 3, we can generate (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 2, 1)using stacks but we can not generate permutation (3, 1, 2) using stack.

1

Page 2: practice-sheet2.pdf

8/11/2019 practice-sheet2.pdf

http://slidepdf.com/reader/full/practice-sheet2pdf 2/3

•  Design and implement an algorithm which prints all permutations of first npositive integers which can be generated by a stack.

•  (optional) Can you give a closed form expression for the number of permu-tations generated using a stack ?

4.  Divide and Conquer paradigm

Design a divide and conquer based algorithm for each of the following problems. Note

that for some of these problems, there may be algorithms which do not use divide andconquer; in fact some of these algorithms may even be more efficient than those basedon divide and conquer. But still, the students must try divide and conquer approachso that they fully understand the technique of divide and conquer.

(a) Suppose you are given an array   A   with   n   entries, with each entry holding adistinct number. You are told that the sequence of values  A[0], A[2],...,A[n − 1]is   unimodular : For some index p  between 0 and  n − 1, the values in the arrayentries increase up to position   p   in   A  and then decrease the remainder of theway until position  n − 1. (So if you have to draw a plot with the array position j  on the  x-axis and the value of the entry  A[ j] on the  y-axis, the plotted pointswould rise until  x-value  p, where they would achieve their maximum, and then

fall from there on.)You would like to find the peak entry  p  without having to read the entire array- in fact, by reading as few entries of  A  as possible. Show how to find the entry p by reading at most  O(log n) entries of  A.

(b) Suppose you are consulting for a bank that is concerned about fraud detection,and they come to you with the following problem. They have a collection of  nbank cards that they have confiscated, suspecting them of being used in fraud.Each bank card is a small plastic object, containing a magnetic strip with someencrypted data, and it corresponds to a unique account number in the bank.Each account can have many bank cards corresponding to it, and we’ll say thattwo cards are equivalent if they correspond to the same account.

It is difficult to read the account number off a bank card directly, but the bank hasa high tech “equivalence tester” that takes two bank cards and, after performingsome computations, determines whether they are equivalent.

The question is the following: among the collection of  n  cards, is there a set of more than   n/2 of them that are equivalent to one another ? Assume that theonly feasible operations you can do with the cards are to pick two of them andplug them into equivalence tester. Design a divide and conquer based algorithmto decide the answer of their question with only   O(n log n) invocations of theequivalence tester.

(c) Recall the problem of finding the number of inversions in an array. This problemwas discussed in the class. Let us call a pair (i, j) a  significant inversion   if  i < jand  A[i]  >  2A[ j]. Design an  O(n log n) time algorithm to count the number of significant inversions in an array.

2

Page 3: practice-sheet2.pdf

8/11/2019 practice-sheet2.pdf

http://slidepdf.com/reader/full/practice-sheet2pdf 3/3

Figure 1: Design of an algorithm sometimes involves fine intricacies which expose a beautiful

structure underlying the problem.

3