Unit II
Unit II
times
• This suggests simply computing by multiplying 1 by a times.
Features
• Brute force is applicable to a very wide variety of
problems.
• For solving some important problems(e.g., sorting,
searching, matrix multiplication, string matching) the
brute-force approach yields reasonable algorithms with no
limitation on instance size.
• A brute-force algorithm can solve problems with
acceptable speed for large class of input compared to that
of more efficient algorithms solving only very few
instances.
• Even though it is an inefficient algorithm still it can be
useful for solving small-size instances of a problem.
Strengths & Weakness
Strengths:
• Wide applicability and Simplicity.
• Yields reasonable algorithm for some problems such as
searching, string matching and matrix multiplication and so
on.
• Provides standard algorithms for simple computational tasks.
Weakness:
• Rarely yields efficient algorithms.
• Unacceptably slow.
• It is neither constructive nor creative.
Brute-Force String Matching
• String matching algorithms are used in text processing.
• Also called as pattern matching algorithm.
=,..., = ,..., = :
of the first matching substring in the text such that
Naïve Algorithm
ALGORITHM BruteForceStringMatch(T[0..n-1], P[0..m-1])
//Implements brute-force string matching
//Input: An array T [0..n − 1] of n characters representing a
text and an array P[0..m − 1] of m characters representing
a pattern
//Output: The index of the first character in the text that
starts a matching substring or −1 if the search is
unsuccessful
for i ← 0 to n − m do
j←0
while j<m and P[j ] = T [i + j ] do
j←j+1
if j = m return i
return −1
Explanation:
•Align the pattern against the first
An Example
m characters of the text and start • The last position in the text
matching the corresponding pairs that can still be a beginning of
of characters from left to right a matching substring. Beyond
until either all the m pairs of the
that position, there are not
characters match or a
mismatching pair is encountered. enough characters to match
•In case of mismatch, shift the the entire pattern; Hence, the
pattern one position to the right algorithm need not make any
and resume the character comparisons there.
comparisons, starting again with
the first character of the pattern
and its counterpart in the text.
Brute Force and Exhaustive
Search
• Exhaustive search is simply a brute-force approach to
combinatorial problems.
• It suggests generating each and every element of the
problem domain, selecting those of them that satisfy all
the constraints, and then finding a desired element.
• Three important problems in exhaustive search : the
traveling salesman problem, the knapsack problem, and
the assignment problem.
Traveling Salesman Problem
• Problem:
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.
• A Hamiltonian circuit is defined as a cycle that passes through all
the vertices of the graph exactly once. It is named after the Irish
mathematician Sir William Rowan Hamilton (1805–1865).
Hamiltonian Circuit
• It is easy to see that a Hamiltonian circuit can also be
defined as a sequence of adjacent vertices , ,..., , where
the first vertex of the sequence is the same as the last one
and all the other vertices are distinct.
• we can get all the tours by generating all the
permutations of n − 1 intermediate cities, compute the
tour lengths, and find the shortest among them.
• There are three pairs of tours
that differ only by their direction. Example
• We could cut the number of
vertex permutations by half.
• For example, choose any two
intermediate vertices, say, b and
c, and then consider only
permutations in which b
precedes c.
• The total number of
permutations needed is still
• This makes exhaustive-search
approach impractical for all but
very small values of n.
• Problem:
Knapsack Problem
Given n items of known
weights , ,..., and values , ,..., and a
knapsack of capacity W, find the most
valuable subset of the items that fit into
• For both the traveling salesman and
knapsack problems, exhaustive search
the knapsack.
leads to algorithms that are extremely
• The exhaustive-search approach to this inefficient on every input.
problem leads to generating all the
subsets of the set of items given.
• These two problems are the best-
known examples of so called NP-hard
• Computing the total weight of each problems. No polynomial-time
subset in order to identify the subset algorithm is known for any NP hard
with the total weight not exceeding the problem.
knapsack capacity, and finding a
subset of the largest value among • More-sophisticated approaches—
them. backtracking and branch-and-bound
enable us to solve some similar
• Since the number of subsets of an n- problems in less than exponential time
element set is , the exhaustive search but not all instances.
leads to a Ω() algorithm.
Example
Assignment Problem
• Problem:
There are n people who need to be assigned to
executejobs, one person per job.
• The cost that would accrue if the person is assigned
to the job is a known quantityfor each pair , , ,...,.
• The problem is to find an assignment with the
minimum total cost.
Example • The exhaustive search
approach to the assignment
problem would require
generating all the
permutations of integers. 1, 2,
• We can describe feasible solutions ...,.
to the assignment problem as n-
tuples <,...,>in which the • computing the total cost of
component, , indicates the column each assignment by summing
of the element selected in the row.
up the corresponding
• For example, <2, 3, 4, 1> indicates elements of the cost matrix,
the assignment of Person 1 to Job 2,
Person 2 to Job 3, Person 3 to Job 4, and finally selecting the one
and Person 4 to Job 1. with the smallest sum.
Hungarian method
• The number of permutations to be considered for the general case of
the assignment problem is
• Exhaustive Search is impractical for all but very small instances of the
problem.
• There is a much more efficient algorithm for this problem called the
Hungarian method after the Hungarian mathematicians Konig and
Egervary.
• The fact that a problem domain grows exponentially or faster does not
necessarily imply that there can be no efficient algorithm for solving it.
• If we want to solve exactly, there are no known polynomial-time
algorithms for problems whose domain grows exponentially with
instance size.