Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

IS201 Data Structures Kovember 25, 2004 1

FINAL EXAM

empt all questions. Each question carries 8 marks.

h. . Suppose n people are arranged i11 a circle. Number the people from 1 to n. in the
clockwise ord.er. We are given an integer ,m 5 n. Beginiling with the person with
designated ilumber 1,we proceed around the circle (in clockwise order) removiilg every
mth person. After each person is removed, countiilg continues around the circle that
remains. This process coiltiilues until all the n people have been removed. . The .rn --
permutaim is defined as the order in which the people have been removed. As an
-
example, if n = 7, m = 3, then the 3 permutation is 3,6,2,7,5,1,4.
Give an O(m.n) time algorithm which gives ,m outputs the ,m - permutation.
I
Give an O(n log n) time algorithm which given ,m, outputs the ,m- permutnl,ion..
2. Suppose.there are n people living in a city. We number the people from 1 to n. We
:: would like to represent the illformatioil about who kilows who by a two climeilsioi~al
array. So we are give11 an n x n array A. A[i, j ] = 1 if i knows j , otherwise it is 0 (note
.

that it is possible that A[i,j] = 1 but A[j, i] = 0 -. for example, many people may know
a film star, but the reverse may not be true). You call assume that A[i,i] = 1 for all
i, i. We say that a persoil i is a celebrity if everyone kilows this person, but this person
'

does not know anyone other than himself. First show that there call be at most one
$! celebrity. Now give an O(n) time algorithm which given the array A, outputs YES if
: there is a celebrity in the city; otherwise it outputs NO. R~rther,:ifthere is a celebrity,
it should output its number.

1 3. Let G be a coililected undirected graph. Suppose there is a tree which is both a breadth
first search tree and a depth first search tree of G. Is it true that G must be a tree ?
Either prove your answer or give a counterexample;
Suppose you are given a coililected undirected graph G in the adjaceilcy list data-
structure: Each edge e has a length I,. Give an algorithm which given an edge e of G,
outputs YES if e is in a minimum spanning tree of G, otherwise it outputs NO. The
3-
running time of your algorithm should be O(m), where m is the number of edges in G.
2
5. Suppose you are given a coili~ecteduildirected gap11 G = (V, E ) . Each edge 11as a
[
L
length ..I Rlrther, with each edge e, we are also given a quantity t e fe basically
represents the amouilt of tax we shall have to pay if we use edge e. As discussed in the
class, the length of a path is the sum of 1, of all the edges in this path. Similarly, define
the tax paid for using a path as the sum of t, of all the edges in the path. We are
given two vertices u and v in G. We would like to fiilcl a path from u, to v of snlallest
i, length. But there may be several such shortest paths - among these we would like to
prefer the one on which the tax paid is smallest. I11 other words, we would like
the shortest pat11 (i.e., a path of smallest leagth) from u to v on which the ti
is as small as possible. Give ail O(mlog n) time algorit,llm for this problem,
m = ( E J , n= JVI.

You might also like