0% found this document useful (0 votes)
10 views

QuestionBank2024

Uploaded by

dia.batra0704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

QuestionBank2024

Uploaded by

dia.batra0704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

564 Chapter 12.

Strings and Dynamic Programming

12.3 Pattern Matching Algorithms


In the classic
Question pattern
Bank: matching problem
Algorithms on strings, we Data
and Advanced are given a text string T
Structures of
(2024)
length n and a pattern string P of length m, and want to find whether P is a substring
of T . The notion of a “match” is that there is a substring of T starting at some
index i that matches P, character by character, so that T [i] = P[0], T [i + 1] = P[1],
. . ., T [i + m − 1] = P[m − 1]. That is, P = T [i..i + m − 1]. Thus, the output from
a pattern matching algorithm could either be some indication that the pattern P
Unit 3: Strings (String Matching: Brute Force, KMP algorithm; Tries: Standard Tries,
does not exist in T or an integer indicating the starting index in T of a substring
Compressed Tries, Su x Tries, Search Engines):
matching P. This is exactly the computation performed by the find function of the
STL string interface. Alternatively, one may want to find all the indices where a
1. Compute the failure function used by the Knuth-Morris-Pratt(KMP) string matching
substring
algorithm for theoffollowing
T matching P begins.
pattern: AABAACAABABA
In this section, we present three pattern matching algorithms (with increasing
levels trie
2. Construct of difficulty).
for the following words: ABOUT, ADAGE, BARD, BARN, BED, BET,
CAN,CAT, DIKDIK, DIKTAT

3. Create
12.3.1 a trieBrute
consisting
Force of the following words: bear, bell, bid, bull, buy, belly, buyer,
sell, stock, stop. Delete the word bell and show the resultant trie.
The brute-force algorithmic design pattern is a powerful technique for algorithm
4. Whatdesign
is the when
cost of wesearching
have something wewwish
a string in a to search
trie? for or when we wish to opti-
Explain.
mize some function. In applying this technique in a general situation we typically
5. Drawenumerate
the suffixall tree for theconfigurations
possible string abcabaca$.
of the inputs involved and pick the best of all
these enumerated configurations.
6. Given a text string Tthis
In applying of length n and
technique a pattern
to design the string P of length
brute-force patternm, we are algo-
matching required to
find therithm,
startingwe derive what is probably the first algorithm that we might think ofPfor
index of the rst occurrence of P in T or an indication that the is not in
T. Consider
solvingthethefollowing algorithm
pattern matching to solve thesimply
problem—we problem:
test all the possible placements
of P relative to T . This algorithm, shown in Code Fragment 12.3, is quite simple.

Algorithm BruteForceMatch(T, P):


Input: Strings T (text) with n characters and P (pattern) with m characters
Output: Starting index of the first substring of T matching P, or an indication
that P is not a substring of T
for i ← 0 to n − m {for each candidate index in T } do
j←0
while ( j < m and T [i + j] = P[ j]) do
j ← j+1
if j = m then
return i
return “There is no substring of T matching P.”
Code Fragment 12.3: Brute-force pattern matching.

What is the running time of the algorithm? Specify your answer in terms of m and n.

7. Suppose we're looking for pattern P = "nano" in text T = “banananobano". Show complete
working of the BruteForceMatch algorithm algorithm on the the given problem instance. Mention
! the number of comparisons done by in the process. !
ffi
fi
8. Suppose we're looking for pattern P = "nano" in text T = “banananobano". Show
complete working of the Knuth-Morris-Pratt algorithm algorithm on the the given problem
instance. Mention the number of comparisons done by in the process.

9. Draw standard trie for the following set of words: arid, area, eerie, err, red, read, road,
roast, logged, loggerhead, loggia, logging

10. Draw compressed trie for the following set of words: arid, area, eerie, err, red, read,
road, roast, logged, loggerhead, loggia, logging,

11. Draw su x trie for the following


i) aaaa
ii) aabb
iii) abba
iv) abcd

Unit 4: More on Trees ( B Trees, 2-4 Trees):

1. Insert the following keys one by one in the following B-tree of order ve:
58, 78, 40, 42, 99, 64
Show the status of the tree after each insertion.

2. A memory block can store maximum 4 values and minimum 2 values in ascending
order and give way to 5 more such blocks for creating hierarchical data storage. However,
the top block of this hierarchical data storage is allowed to have minimum 1 value.
Considering these restrictions, store the following elements in a way such that the search
time is logarithmic. Show the data structure after each insertion.

<50,17,76,9,23,54,14,19,72,12,67,100,11,25,7,8,52,53,51>

Then, delete values “8” and “23” one by one. Show the data structure after each
deletion.

3. Insert the following keys in an intially empty B-tree of order:

6, 4, 22, 10, 2, 14, 3, 8, 11, 13, 5, 9, 15, 18, 21, 1

Show the contents of the tree after each insertion. Mention the total number of times that
a split happens during these insertions
ffi
fi
4. Insert the following keys in an intially empty B-tree of order:

6, 4, 22, 10, 2, 14, 3, 8, 11, 13, 5, 9, 15, 18, 21, 1

Show the contents of the tree after each insertion. Mention the total number of times that
a split happens during these insertions

Unit 5: More on Graphs ( Bellman Ford Algorithm, Union Find Data Structures
application Kruskal’s algorithm)

1. Consider the following recurrence relation:


f(n) = 2, if n <= 1
f(n) = f(n-2) + n, if n > 1

Write a dynamic programming algorithm to compute f(n).

2. What would be the time complexity if one memoized the recursive computation of nth
Fibonacci number.

3. An undirected graph having 5 nodes (A,B,C, D and E) is given below in the form of a weighted
matrix :
A B C D E
A 0 12 9 3 7
B. 12 0 2 0 5
C. 9 2 0 4 0
D. 3 0 4 0 6
E. 7 5 0 6 0

(weight 0 means no edge otherwise a positive value represents


cost of moving from one vertex to other)
(i). Draw the corresponding simple, weighted, undirected graph.
(ii) Find the minimum spanning tree using Kruskal’s algorithm.

4. Given an undirected graph G = (V,E), where V = {a, b, c, d, e, f, g, h, i, j, k} and the set of edges
E = {(d,i), (f,k), (g,i), (b,g), (a,h), (i,j), (d,k), (b,j), (d,f), (g,j), (a,e)}. Find connected components in
the given graph. List the vertices in each connected component. (Hint: use Union-Find data
structure)

5. Would you use BFS to find the shortest path between two nodes in a weighted graph with
arbitrary edge weights? If yes, justify your answer. If no, then give a counterexample.

6. Can we use DFS algorithm to find the shortest-path distance from the source to a reachable
vertex in an unweighted graph? If yes, justify, if no give a counter example.
7. Both Dijkstra's and BFS algorithm are used to nd shortest path in a graph, can they be used
interchangeably? Explain brie y.

8. Can a graph in which the edge weights are not necessarily distinct have more than one MSTs? If
yes, give an example. If no, justify.

9. Given a graph G with weights, weights are allowed to be negative, we are required to find
shortest path from a vertex s to a vertex t in the graph. Write a dynamic programming algorithm to
solve the problem. Give an example to show that the Dijkstra’s algorithm fails to find shortest path
for such a graph.

10. The number of sub problems in the DP solution provided by the Bellman-Ford algorithm of the
shortest path problem is ———-?

11. Use the Kruskal’s algorithm to find an MST in the following graph.

12. Using the Bellman Ford algorithm, determine the shortest paths from A to all other nodes in
the following graph. What is the length of the shortest path from A to C?

13. Using the Bellman Ford algorithm, determine the shortest paths from all other nodes to H in
the following graph. What is the length of the shortest path from A to H?
fl
fi
2 Handout 14: Lecture Notes on Skip Lists

– Cost:

– Minimized when

– Resulting 2-level structure:


14. Using the linked-list representation of Union-Find and the weighted-union heuristic, a sequence
of m make-set, union,sqrt nand find operations, n of which are make-set, are performed.
Prove that the total time taken by these operations is O(m+nlgn).

sqrt n sqrt n sqrt n sqrt n

3 linked lists:
Unit 6: Randomization ( Randomized Quicksort, Randomized Select, Skip lists ):
linked lists:
1. What is the bene t of randomization in quicksort algorithm ?
linked
2. Consider the lists: skip-list:
following

– Becomes like a binary tree:

14 79

14 50 79 110

14 34 50 66 79 96 110 125

14 23 34 42 50 59 66 72 79 86 96 103 110 116 125

– Example: Search for 72


Delete 50 from the list. Show the resultant skip list. How many comparisons are done in the
Level 1: 14 too small, 79 too big; go down 14
process?
a) Search 77 in the Level
new2:skip
14 too
list. small, 50 toocomparisons
How many small, 79 too are
big; done
go down 50 process?
in the
Level 3: 50 too small, 66 too small, 79 too big; go down 66
3. Consider the following list of elements: 10, 20, 14, 5, 6, 8, 11, 19, 25, 42, 18, 96, 35, 44, 3
Level 4: 66 too small, 72 spot on
Find 6th smallest element in the given list. Use Randomised-Select algorithm. Consider the
following indexes for pivot element in the subsequent lists, resulting from the recursive steps:

First step: 5
Second step: 2
Third step: 2
Forth step: 3
Fifth step: 2
fi
without boxed numbers—specifically, the four edges of capacity 3—have 2
no flow being sent on them.)
s
(a)
What is the value of this flow? Is this a maximum (s,t) flow in this
graph? 4
In which of the recursive steps the partition was nearly balanced? v
(b) Find a minimum s-t cut in the flow network pictured in Figure 7.26,
and also say what its capacity is.
Figure 7.25 Wh
Unit 7: Network Flows ( Ford Fulkerson algorithm for the max ow problem ) imum capacity
3. Figure 7.27 shows a flow network on which an s-t flow has been computed. this flow netwo
1. Following gure
The shows
capacitya ofow network
each on which
edge appears asan s-t ow
a label nexthas
to been computed.
the edge, and theThe
capacity of each edgein
numbers appears as a the
boxes give labelamount
next toofthe edge,
flow sentand
on the numbers
each in boxes
edge. (Edges
give the amount of ow
without sent
boxed on eachhave
numbers edge.
no (Edges without
flow being sent boxed numbers have no
on them.)
ow being sent on them.)
(a) What is the value of this flow? Is this a maximum (s,t) flow in this
graph?
(a) What is the value of this ow? Is this a maximum (s,t) ow in this graph?

5 5
10 5
3 3

8 8 10 8 8 8
s t

3 3
5
5
5 10
d

Figure 7.26 What is the value of the depicted flow? Is it a maximum flow? What is the
minimum cut?
2. Following gure shows a ow network on which an s-t ow has been computed. The
capacity of each edge appears as a label next to the edge, and the numbers in boxes
give the amount of ow sent on each edge. (Edges without boxed numbers have no ow
being sent on them.)

(a) What is the value of this ow? Is this a maximum (s,t) ow in this graph?

416 Chapter 7 Network Flow

a
6 5
10 2 5
1
1
3 6 5
s b c t
3 5 5

1 3 3
1 10
1
d

Figure 7.27 What is the value of the depicted flow? Is it a maximum flow? What is the
minimum cut?

(b) Find a minimum s-t cut in the flow network pictured in Figure 7.27,
and also say what its capacity is.

4. Decide whether you think the following statement is true or false. If it is


fl
fi
fi
fl
fl
fl
fl
fl
fl
fl
fl
fl
fl
fl
fl
(b) Find a minimum s-t cut in the flow network pictured in Figure 7.27,
and also say what its capacity is.
3. Decide whether you think the following statement is true or false. If it is true, give a
4. Decide whether you think the following statement is true or false. If it is
short explanation. If it is false, give a counterexample.
true, give a short explanation. If it is false, give a counterexample.

Let G be an arbitrary flow network, with a source s, a sink t, and a positive


integer capacity ce on every edge e. If f is a maximum s-t flow in G, then f
saturates every edge out of s with flow (i.e., for all edges e out of s, we have
f (e) = ce ).
Network Flow Example (from CLRS)
5. Decide whether you think the following statement is true or false. If it is
4. Following
true, give gure shows
a short a ow network.
explanation. If it is Compute
false, givemaximum (s,t) ow for it.
a counterexample.

! Capacities
Let G be an arbitrary flow network, with a source s, a sink t, and a positive
integer capacity ce on every edge e; and let (A, B) be a mimimum s-t cut with
respect to these capacities {ce : e ∈ E}. Now suppose we add 1 to every capacity;
then (A, B) is still a minimum s-t cut with respect to these new capacities
{1 + ce : e ∈ E}.

6. Suppose you’re a consultant for the Ergonomic Architecture Commission,


and they come to you with the following problem.
They’re really concerned about designing houses that are “user-
friendly,” and they’ve been having a lot of trouble with the setup of light
! Maximum flowswitches
fixtures and (of 23in total units) houses. Consider, for example,
newly designed
a one-floor house with n light fixtures and n locations for light switches
mounted in the wall. You’d like to be able to wire up one switch to control
each light fixture,
5. Following in such
gure shows a way
a ow that aonperson
network which at
an the
s-t switch
ow hascan seecomputed.
been the The
capacity of each edge appears as
light fixture being controlled. a denominator of the label next to the edge, and the
numbers in the numerator give the amount of ow sent on each edge.

(a) What is the value of this ow?


(b) Draw residual graph for the given graph. Find an augmenting path in the residual
graph on which more ow can be sent from s to t. What is the value of ow on the
augmenting path, computed by you?
Network Flow Problems 5
fi
fi
fl
fl
fl
fl
fl
fl
fl
fl
Unit 2: Hash Tables, Dictionaries ( Hash Functions, Collision resolution schemes )

Unit 1: List and Iterator ADTs ( Vectors, Lists, Sequences )

You might also like