Pgcs
Pgcs
This question paper has 5 printed sides. Part A has 10 questions of 3 marks each. Part B has
7 questions of 10 marks each. The total marks are 100. Answers to Part A must be filled in
the answer sheet provided.
Part A
1. The regular expression (a∗ + b)∗ is equivalent to which of the following regular expres-
sions:
(a) a∗ b∗ (b) (a∗ b + b)∗ (c) (a + b∗ )∗ (d) (a∗ b)∗
Answer: (c) (a + b∗ )∗
(a∗ + b)∗ allows any string over {a, b} and is the same as (a + b)∗ . Option (a) only
allows strings of the form ai bj . Options (b) and (d) do not allow strings of the form
ai with no b’s. a
2. An FM radio channel has a repository of 10 songs. Each day, the channel plays 3
distinct songs that are chosen randomly from the repository.
Mary decides to tune in to the radio channel on the weekend after her exams. What
is the probability that no song gets repeated during these 2 days?
10 2 10 −1
(a) 3
· 6
10 10 −2
(b) 6
· 3
10 7 10 −2
(c) 3
· 3
· 3
10 7 10 −1
(d) 3
· 3
· 6
10 7 10 −2
Answer: (c ) 3
· 3
· 3
10 7
3
is the number of ways of choosing 3 songs on day 1. 3
is the number of ways
10 7
of choosing 3 different songs on day 2, so 3 3
is the number of combinations that
10 10
meet Mary’s requirement. 3 3 is the total number of ways of choosing 3 songs on
each of the two days, without any constraints. The ratio is the given probability. a
3. Four siblings go shopping with their father. If Abhay gets shoes, then Asha does not
get a necklace. If Arun gets a T-shirt, then Aditi gets bangles. If Abhay does not get
shoes or Aditi gets bangles, the mother will be happy. Which of the following is true?
1
(b) If Aditi got bangles, then Abhay got shoes.
(c) If the mother is not happy, then Asha did not get a necklace and Arun did not
get a T-shirt.
(d) None of the above.
Answer: (c ) If the mother is not happy, then Asha did not get a necklace and
Arun did not get a T-shirt.
Let p denote that Asha get a necklace, q denote that Abhay gets shoes, r denote that
Arun gets a T-shirt, s denote that Aditi gets bangles and w denote that mother is
happy. The question is equivalent to the formula φ : q → ¬p ∧ r → s ∧ (¬q ∨ s) → w.
A formula ψ is a valid implication iff every truth assignment to p, q, r, s, w that makes
φ true also makes ψ true. Option (a) is the formula ψa : w → s, option (b) is the
formula ψb : s → q and option (c) is the formula ψc : ¬w → (¬p ∧ ¬r). Neither ψa nor
ψb are valid implications but ψc is.
a
4. City authorities are concerned about traffic accidents on major roads. They would like
to have ambulances stationed at road intersections to quickly reach the scene of any
accident along these roads. To minimize response time, ambulances are to be located
at intersections with traffic lights so that any segment of road can be reached by at
least one ambulance that does not have to pass through a traffic light to reach the
scene of the accident. If we model the road network as a graph, where intersections
with traffic lights are vertices and edges represent road segments between traffic lights,
the graph theoretic question to be answered is:
Each ambulance “covers” the adjacent roads, and all roads are covered in this way. a
2
Answer: (a) I only
6. What does the following function compute in terms of n and d, for integer values of
d? Note that the operation / denotes floating point division, even if the arguments
are both integers.
function foo(n,d){
if (d == 0){
return 1;
}else{
if (d < 0){
return foo(n,d+1)/n;
}else{
return n*foo(n,d-1);
}}}
For d > 0, this computes nd by repeated multiplication. For d negative, the answer is
1
nd
by repeated division. a
3
f(){ g(){
w = 5; z = w+1;
w = 2*z + 2; z = 3*z - w;
} print(z);
}
We start with w and z set to 0 and execute f() and g() in parallel—that is, at each
step we either execute one statement from f() or one statement from g(). Which of
the following is not a possible value printed by g()?
(a) -2 (b) -1 (c) 2 (d) 4
Answer: (c) 2 a
8. A stable sort preserves the order of values that are equal with respect to the comparison
function. We have a list of three dimensional points
[(7, 1, 8), (3, 5, 7), (6, 1, 4), (6, 5, 9), (0, 2, 5), (9, 0, 9)].
We sort these in ascending order by the second coordinate. Which of the folllowing
corresponds to a stable sort of this input?
(a) [(9, 0, 9), (7, 1, 8), (6, 1, 4), (0, 2, 5), (6, 5, 9), (3, 5, 7)]
(b) [(0, 2, 5), (3, 5, 7), (6, 1, 4), (6, 5, 9), (7, 1, 8), (9, 0, 9)]
(c) [(9, 0, 9), (7, 1, 8), (6, 1, 4), (0, 2, 5), (3, 5, 7), (6, 5, 9)]
(d) [(9, 0, 9), (6, 1, 4), (7, 1, 8), (0, 2, 5), (3, 5, 7), (6, 5, 9)]
Answer: (c) [(9, 0, 9), (7, 1, 8), (6, 1, 4), (0, 2, 5), (3, 5, 7), (6, 5, 9)]
In a stable sort, the original order of the pairs (7,1,8),(6,1,4) and (3,5,7),(6,5,7) with
equal second coordinates must be preserved in the sorted output.
a
4
9.
15
Suppose we constructed the binary
search tree shown at the right by start-
ing with an empty tree and inserting 7 28
one element at a time from an input se-
quence, without any rotations or other
manipulations. Which of the following 3 12 19 37
assertions about the order of elements
in the input sequence cannot be true?
1 6 8 14 16 23 29 42
28 is an ancestor of 16, so 16 must have come after 28. In the tree only ancestor-
descendant relationships matter in determining the order in which elements arrive.
An ancestor must always come before any of its descendants. Incomparable elements
could come in any order. a
(a) If the best algorithm for B takes exponential time, there is no polynomial time
algorithm for A
(b) If the best algorithm for A takes exponential time, there is no polynomial time
algorithm for B.
(c) If we have a polynomial time algorithm for A, we must also have a polynomial
time algorithm for B.
(d) If we don’t know whether there is a polynomial time algorithm for B, there cannot
be a polynomial time algorithm for A.
Answer: (b) If the best algorithm for A takes exponential time, there is no
polynomial time algorithm for B.
If we have a polynomial time solution for B, the reduction gives us a polynomial time
solution for A.
a
5
Part B
1. Let Σ = {a, b, c}. Let Leven be the set of all even length strings in Σ∗ .
(a) Construct a deterministic finite state automaton for Leven .
(b) We consider an operation Eraseab that takes as input a string w ∈ Σ∗ and erases
all occurrences of the pattern ab from w. Formally, it can be defined as follows:
(
w if w does not contain the pattern ab
Eraseab (w) :=
Eraseab (w1 ) Eraseab (w2 ) if w = w1 ab w2 for some w1 , w2 ∈ Σ∗
For instance, Eraseab (cacb) = cacb, Eraseab (cabcbab) = ccb and Eraseab (ab) = .
For a language L, we define Eraseab (L) to be the set of strings obtained by applying
the Eraseab operation to each string in L:
Eraseab (L) := {Eraseab (w) | w ∈ L}
Answer:
(a) Leven can be recognized by an automaton with two states {q0 , q1 }, where q0 is both
an initial and final state. On input letters a and b, the automaton switches from
q0 and q1 and vice versa. An odd length input will take the automaton to q1 and
an even length input will take the automaton to q0 .
(b) Eraseab (Leven ) is the set of all even length strings which do not contain ab.
It is easy to construct a nondeterministic automaton with three states {q0 , q1 , q2 }
for the language Lab consisting of all strings containing ab. Here, q0 is the initial
state and q2 is the final state. There is a self loop on {a, b} at both q0 and q2 and
a b
there are transitions q0 →− q1 and q1 → − q2 .
Since Lab is regular, so is its complement Lab , the language of all strings without
ab.
Eraseab (Leven ) is the intersection of Lab with Leven .
a
2. There are a number of tourist spots in a city and a company GoMad runs shuttle
services between them. Each shuttle plies between a designated origin and destination,
and has a name. Due to lack of coordination, the same name may be allotted to
multiple routes.
To make matters worse, another company GoCrazy introduces its shuttle services
using the same set of shuttle names. A GoMad shuttle and a GoCrazy shuttle with
the same name may start at different origins and/or end at different destinations.
A pass from a company allows unlimited travel in all the company’s shuttles. For each
company, we have a list that specifies all routes allotted to each shuttle name.
Design an algorithm to find out if there is a source s, a target t, and a sequence of
shuttle names σ such that, irrespective of whether you are carrying a GoMad pass or
a GoCrazy pass, you can start at s and arrive at t using the sequence σ.
6
Answer: Create a graph where the set of vertices are pairs (Spot 1, Spot 2) of tourist
spots. There is an edge labeled “Name 1” from (Spot 1, Spot 2) to (Spot 3, Spot 4)
iff there is a GoMad shuttle “Name 1” from Spot 1 to Spot 3 and a GoCrazy shuttle
“Name 1” from Spot 2 to Spot 4. The answer to the question is yes iff there is a
directed path from a vertex (Spot 1, Spot 1) to a vertex (Spot 2, Spot 2). a
3. Let Σ = {a, b}. Given words u, v ∈ Σ∗ , we say that v extends u if v is of the form xuy
for some x, y ∈ Σ∗ . Given a fixed word u, we are interested in identifying whether a
finite state automaton accepts some word that extends u.
Describe an algorithm that takes as input a finite state automaton (DFA or NFA) A
over Σ = {a, b} and a word u ∈ Σ∗ and reports “Yes” if some word in the language of
A extends u and “No” if no word in the language of A extends u.
Answer: Let R be the set of states that can be reached by a path from the initial
state in A and let S be the set of states from which there is a path to one of the final
states in A.
For each pair of states (r, s) ∈ R × S, Ar,s is obtained from A by keeping the set of
states and set of transitions unchanged, but making r the unique intial state and s
the unique final state.
If u ∈ L(Ar,s ), then there must be a word xuy ∈ L(A), where x labels the path from
the initial state to r (r ∈ R is reachable from the initial state), u labels the path from
r to s, and y labels the path from s to some final state of A (s ∈ S, so such a path
must exist.)
Hence, output “Yes” if u ∈ L(Ar,s ) for some (r, s) ∈ R × S, and “No” otherwise. a
7
5. An undirected graph is connected if, for any two vertices {u, v} of the graph, there is
a path in the graph starting at u and ending at v. A tree is a connected, undirected
graph that contains no cycle.
(a) A leaf in a tree is a vertex that has degree 1. Prove that if G is a tree with at
least two vertices then G contains at least two leaves.
(b) A bipartite graph is one in which the vertex set V can be partitioned into two
disjoint sets V1 and V2 so that for every edge {u, v}, u and v lie in different
partitions—that is, u ∈ V1 and v ∈ V2 or vice versa. Prove that if G is a tree
with at least two vertices, then G is bipartite.
Answer:
(a) Consider a maximal path ρ = v0 v1 . . . vk in the tree. If v0 is not a leaf, then it has
a neighbour v 6= v1 , and the path ρ can be extended to a longer one vρ since there
are no cycles. This contradicts the fact that ρ is maximal, thereby showing that
v0 is a leaf. A similar argument shows that vk is also a leaf.
(b) If we root the tree at any node, we can colour the tree level by level by alternating
two colours. This 2-colouring gives us the partition of the vertex set.
6. We are given a sequence of pairs of integers (a1 , b1 ), (a2 , b2 ), . . . (an , bn ). We would like
to compute the largest k such that there is a sequence of numbers ci1 ≤ ci2 ≤ . . . ≤ cik
with 1 ≤ i1 < i2 < ... < ik ≤ n and for each j ≤ k, cij = aij or cij = bij . Describe an
algorithm to solve this problem and explain its complexity.
Answer: Compute two arrays A and B with A[i] and B[i] giving the length of the
longest such sequence in (a1 , b1 ), . . . (ai , bi ) with ai and bi as the last elements, respec-
tively.
Note that A[1] = 1 and B[1] = 1. To compute A[i] and B[i] for i ≥ 2, we first compute
the following quantities:
(
0 if aj > ai for all j < i
αi1 =
max{ A[j] | j < i and aj ≤ ai } otherwise
(
0 if bj > ai for all j < i
αi2 =
max{ B[j] | j < i and bj ≤ ai } otherwise
(
0 if aj > bi for all j < i
βi1 =
max{ A[j] | j < i and aj ≤ bi } otherwise
(
0 if bj > bi for all j < i
βi2 =
max{ B[j] | j < i and bj ≤ bi } otherwise
8
7. Consider the following function that takes as input a sequence A of integers with
n elements, A[1], A[2], . . . , A[n] and an integer k and returns an integer value. The
function length(S) returns the length of sequence S. Comments start with //.
v = A[1];
AL = [ A[j] : 1 <= j <= n, A[j] < v ]; // AL has elements < v in A
Av = [ A[j] : 1 <= j <= n, A[j] == v ]; // Av has elements = v in A
AR = [ A[j] : 1 <= j <= n, A[j] > v ]; // AR has elements > v in A
Answer: