Unit2 PDF
Unit2 PDF
M
p0 . . . pj . . . pm−1 pattern P
O
C
If matches other than the first one need to be found, a string-matching algorithm
S.
can simply continue working until the entire text is exhausted.
U
ALGORITHM BruteForceStringMatch(T [0..n − 1], P[0..m − 1])
C
FO
//Output: The index of the first character in the text that starts a
D
for i ←0 to n − m do
j ←0
while j <mand P[j ]= T [i + j ] do
j ←j + 1
if j = m return i
return −1
Thus, in the worst case, the algorithm makes m(n − m + 1) character comparisons, which
puts it in theO(nm) class
Write the algorithm to perform Binary Search and compute its time complexity. Or
Explain binary search algorithm with an example
BINARY SEARCH ALGORITHM
Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop [successful search];
otherwise, continue searching by the same
method in A[0..m-1] if K < A[m] and in
A[m+1..n-1] if K > A[m]
// Input: An Array A[0…n-1] sorted in ascending orger and a search key K
//Output: An index of the array‘s element that is equal to K or -1 if there is no such
element.
l = 0; r= n-1
M
while l < r do
O
m =l+r]/2
C
if K = A[m] return m S.
U
else if K < A[m] r =m-1
C
else l = m+1
FO
return -1
TS
Time complexity:
EN
D
CWorst[n]=1,Cavg[n]=log2n,Cbest[n]=log2n+1
U
ST
For Example
The following is our sorted array and let us assume that we need to search the location
of value 31 using binary search.
to change our low to mid + 1 and find the new mid value again. low = mid + 1, mid =
low + [high - low] / 2 Our new mid is 7 now. We compare the value stored at location
7 with our target value 31.
M
O
C
S.
U
C
FO
TS
The value stored at location 7 is not a match, rather it is less than what we are looking
for. So, the value must be in the lower part from this location.
EN
D
U
ST
We compare the value stored at location 5 with our target value. We find that it is a
match.
We conclude that the target value 31 is stored at location 5.
Binary search halves the searchable items and thus reduces the count of comparisons to
be made to very less numbers.
Write down the algorithm to construct a convex hull based on divide and conquer
strategy. Or Explain the convex hull problem and the solution involved behind it
CONVEX HULL OR QUICK HULL PROBLEM
Convex hull: smallest convex set that includes given points. An O[n^3] brute force
time. Assume points are sorted by x-coordinate values
Identify extreme points P1 and P2 [leftmost and rightmost]
Compute upper hull recursively:
1. find point Pmax that is farthest away from line P1P2
2. compute the upper hull of the points to the left of line P1Pmax
M
3. compute the upper hull of the points to the left of line PmaxP2
O
C
Compute lower hull in a similar manner
S.
Finding point farthest away from line P1P2 can be done in linear time
U
Time efficiency: T[n] = T[x] + T[y] + T[z] + T[v] + O[n], where x + y + z +v
C
<= n.
FO
If points are not initially sorted by x-coordinate value, this can be accomplished in
O[n log n] time.
D
CONVEX HULL THEOREM The convex hull of any set S of n>2 points not all
on the same line is a convex polygon with the vertices at some of the points of S.
[If all the points do lie on the same line, the polygon degenerates to a line segment
but still with the endpoints at two points of S.]
The convex-hull problem is the problem of constructing the convex hull for a
given set S of n points.
To solve it, we need to find the points that will serve as the vertices of the polygon
in question.
Mathematicians call the vertices of such a polygon ―extreme points.‖
M
O
C
CLOSEST-PAIR Problem S.
U
Find the two closest points in a set of n points [in the two-dimensional
C
And return the indexes of the points for which the distance
EN
M
half of the elements of A.
O
Recursion Step: Recursively sort array A1 and A2.
C
Conquer Step: Combine the elements back in A by merging the sorted arrays A1
S.
and A2 into a sorted sequence
U
C
FO
M
Cworst[1] = 0.
O
By Master Theorem, Cworst[n] ∈ Θ[n log n] the exact solution to the worst-case
C
recurrence for n = 2 k Cworst[n] = n log2 n − n + 1.
S.
For large n, the number of comparisons made by this algorithm in the average case
U
C
turns out to be
FO
QUICK SORT
Quicksort is the other important sorting algorithm that is based on the divide-
and-conquer approach. quicksort divides input elements according to their
value. A partition is an arrangement of the array‘s elements so that all the
elements to the left of some element A[s] are less than or equal to A[s], and all
the elements to the right of A[s] are greater than or equal to it:
Sort the two subarrays to the left and to the right of A[s] independently.
No work required to combine the solutions to the subproblems.
Here is pseudocode of quicksort: call Quicksort[A[0..n − 1]] where As a
partition algorithm use the HoarePartition
ALGORITHM Quicksort[A[l..r]]
//Sorts a subarray by quicksort
//Input: Subarray of array A[0..n − 1], defined by its
left and right // indices l and r
//Output: Subarray A[l..r] sorted in
nondecreasing order if l < r
s ←Partition[A[l..r]] //s is a split position
Quicksort[A[l..s − 1]]
Quicksort[A[s + 1..r]]
M
O
C
S.
U
C
FO
TS
EN
D
U
ST
M
O
C
S.
U
C
FO
TS
EN
D
U
ST
Explain the method used for performing Multiplication of two large integers. Explain
how divide and conquer method can be used to solve the same.
Some applications like modern cryptography require manipulation of
integers that are over 100 decimal digits long. Since such integers are too long to
fit in a single word of a modern
computer, they require special treatment.In the conventional pen-and-pencil algorithm for
multiplying two n-digit integers, each ofthe n digits of the first number is multiplied by
each of the n digits of the second number for thetotal of n2 digit multiplications.
The divide-and-conquer method does the above multiplication in less than n2
digit multiplications. For any pair of two-digit numbers a = a1a0 and b = b1b0,
their product c can be
M
O
= c210n + c110n/2
C
+ c0, where
S.
c2 = a1 ∗ b1 is the product of their first halves,
U
c0 = a0 ∗ b0 is the product of their second halves,
C
the sum of the a‘s halves and the sum of the b‘s halves
minus the sum of c2 and c0.
TS
Find all the solution to the traveling salesman problem [cities and distance shown
below] by exhaustive search. Give the optimal solutions. Or Explain exhaustive searching
techniques with example. Or Find the optimal solution to the fractional knapsack problem
with example. Or Solve the given knapsack problem
un=3,m=20,[p1,p2,p3]=[25,24,15],[w1,w2,w3]=[18,15,10][M-15][N-14][M-14][N-15]
[M-16]
TRAVELING SALESMAN PROBLEM
The traveling salesman problem [TSP] is one of the combinatorial problems. The
problem asks to find the shortest tour through a given set of n cities that visits
each city exactly once before
returning to the city where it started.The problem can be conveniently modeled by a
weighted graph, with the graph‘s vertices representing the cities and the edge weights
specifying the distances. Then the problem can bestated as the problem of finding the
shortest Hamiltonian circuit of the graph.
[A Hamiltoniancircuit is defined as a cycle that passes through all the vertices of the
graph exactly once]. A Hamiltonian circuit can also be defined as a sequence of n + 1
adjacent vertices
vi0, vi1, . . . , vin−1, vi0, where the first vertex of the sequence is the same as the
last one and all the other n − 1 vertices are distinct. All circuits start and end at one
particular vertex.
M
Figure presents a small instance of the problem and its solution by this method.
O
C
For example, S.
U
C
FO
TS
EN
D
U
ST
FIGURE Solution to a small instance of the traveling salesman problem by
exhaustive search. Time Complexity of TSP:O[n-1!]
KNAPSACK PROBLEM
Given n items of known weights w1, w2, . . . , wn and values v1, v2, . . . , vn
and a knapsack of capacity W, find the most valuable subset of the items that
fit into the knapsack. Real time examples:
A Thief who wants to steal the most valuable loot that fits into his knapsack,
A transport plane that has to deliver the most valuable set of items to a remote
location without
exceeding the plane‘s capacity.
The exhaustive-search approach to this problem leads to generating all the subsets of the
set
of n items given, computing the total weight of each subset in order to identify
M
O
feasible subsets [i.e., the ones with the total weight not exceeding the knapsack
C
capacity], and finding a subset of the largest value among them.
S.
U
C
• weights: w1 w2 … wn
FO
• values: v1 v2 … vn
TS
• a knapsack of capacity W
EN
D
U
ST
ST
U
D
EN
TS
FO
C
U
S.
C
O
M
ASSIGNMENT PROBLEM
There are n people who need to be assigned to n jobs, one person per job. The cost of
assigning person i to job j is C[i,j]. Find an assignment that minimizes the total cost. For
Example,
M
O
C
S.
We can describe feasible solutions to the assignment problem as n-tuples
U
_j1, . . . , jn in which the ith component, i = 1, . . . , n, indicates the column of the
C
element selected in the ith row (i.e., the job number assigned to the ith person).
FO
For example, for the cost matrix above, _2, 3, 4, 1_ indicates the assignment of
Person 1 to Job 2, Person 2 to Job 3, Person 3 to Job 4, and Person 4 to Job 1.
TS
EN
D
U
ST