The Answer Is Yes, Worst Case
The Answer Is Yes, Worst Case
The Answer Is Yes, Worst Case
) The original brute force algorithm that we used was, Using an outer for loop,
whenever I encounter an A, I go inside another for loop to check if there's a B
present or not. If a B is found, I increment the count. Finally, the value stored in the
count variable yields the required result.
I came across a point while reading about String matching algorithms, when you traverse
right to left rather than left to right, your algorithm is more efficient but here the
substring isn't given as a parameter to the function that you would be using to compute
the required value.
A = "CABAAXBYA"
print total
This works by keeping track in count of the number of B's to the right of the current
point.
A. You drove west to Seattle and then from there South to LA. This is one of the worst way
possible you can go to LA from New York.
B. Lets say you drove south directly to Florida and then went to LA.
Finally best.....
You find the least distance between LA and New York say around 2600 miles and drive on that
road. YOU TAKE EVERY POSSIBLE SHORTCUT TO REACH LA WITH MINIMUM
DISTANCE. This is best case.
12)a)i) Let x1< x2 < . . . < xn be real numbers representing coordinates of n villages located
along a straight road. A post office needs to be built in one of these villages. a) Design an
efficient algorithm to find the post-office location minimizing the average distance between the
villages and the post office.
Let x1 2 n be real numbers representing coordinates of n villages located along a straight road.
A post office needs to be built in one of these villages. a. Design an efficient algorithm to find the
post-office location minimizing the average distance between the villages and the post office.
b. Design an efficient algorithm to find the post-office location minimizing the maximum distance
from a village to the post office.
algorithm PostOffice(P)
m <- (x1+xn) / 2
i <- 1
while xi < m do
i <- i+1
if xi - x1 < xn - xi-1
return xi
else return xi-1
12)a)ii) Explain how exhaustive search can be applied to the sorting problem and
determine the efficiency class of such an algorithm.
13)a)ii) Write the algorithm for optimal binary search tree and solve the following
problem instance to construct the optimal binary search tree. Keys are a, b, c, d and the
probabilities are 0.1, 0.2, 0.4 and 0.3. (8)