Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

The Answer Is Yes, Worst Case

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

11)a.

) 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"

count = 0 # Number of B's seen


total = 0
for a in reversed(A):
if a=='B':
count += 1
elif a=='A':
total += count

print total
This works by keeping track in count of the number of B's to the right of the current
point.

counting the number of A's to the left instead:


count = 0 # Number of A's seen
total = 0
for a in A:
if a=='A':
count += 1
elif a=='B':
total += count
print total
.
11)a)ii)
The answer is Yes,
Worst Case
So lets start with the worst case here.....
Now there are many possibilities here.... but you will get the notion what they are by example so
stick with me.

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.

Many important problems require finding an element with a special property in


a domain that grows exponentially (or faster) with an instance size. Typically,
such problems arise in situations that involve—explicitly or implicitly—
combinatorial objects such as permutations, combinations, and subsets of a
given set. Many such problems are optimization problems: they ask to find an
element that maximizes or minimizes some desired characteristic such as a path
length or an assignment cost.
Exhaustive search is simply a brute-force approach to combinatorial prob-
lems. It suggests generating each and every element of the problem domain, se-
lecting those of them that satisfy all the constraints, and then finding a desired
element (e.g., the one that optimizes some objective function). Note that
although the idea of exhaustive search is quite straightforward, its
implementation typically requires an algorithm for generating certain
combinatorial objects. We delay a discussion of such algorithms until the next
chapter and assume here that they exist. We illustrate exhaustive search by
applying it to three important problems: the traveling salesman problem, the
knapsack problem, and the assignment problem.
Traveling Salesman Problem
 The traveling salesman problem (TSP) has been intriguing researchers for the
last 150 years by its seemingly simple formulation, important applications, and
interesting connections to other combinatorial problems. In layman’s terms, 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 be stated as the problem of finding the shortest Hamiltonian
circuit of the graph. 
Knapsack Problem 
Here is another well-known problem in algorithmics. 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. If you do
not like the idea of putting yourself in the shoes of a thief who wants to steal the
most
Assignment Problem
 In our third example of a problem that can be solved by exhaustive search,
there are n people who need to be assigned to execute n jobs, one person per
job. (That is, each person is assigned to exactly one job and each job is assigned
to exactly one person.) The cost that would accrue if the ith person is assigned
to the j th job is a known quantity C[i, j ] for each pair i, j = 1, 2, . . . , n. The
problem is to find an assignment with the minimum total cost.

13a)i) compare and contrast dynamic programming and greedy method.

Dynamic Programming Greedy Method

1. Dynamic Programming is used to 1. Greedy Method is also used to get the


obtain the optimal solution. optimal solution.

2. In Dynamic Programming, we choose 2. In a greedy Algorithm, we make whatever


at each step, but the choice may depend choice seems best at the moment and then
on the solution to sub-problems. solve the sub-problems arising after the
choice is made.

3. Less efficient as compared to a greedy 3. More efficient as compared to a greedy


approach approach

4. Example: 0/1 Knapsack 4. Example: Fractional Knapsack

5. It is guaranteed that Dynamic 5. In Greedy Method, there is no such


Programming will generate an optimal guarantee of getting Optimal Solution.
solution using Principle of Optimality.

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)

You might also like