UNIT-II
BRUTE FORCE ANDDIVIDE-
AND-CONQUER
Brute Force: String Matching-Closest-Pair and
Convex-Hull Problems-Exhaustive Search -
Traveling Salesman Problem - Knapsack
Problem - Assignment problem.
2.
BRUTE FORCE:
Brute Forceis a straightforward approach of solving the
problem.
Works Directly on the problem statement and definitions of
concepts that are directly involved in the problem.
In short the method has “JUST DO IT” approach.
Examples: Selection sort, Bubble Sort, Sequential Search,
Depth first search, Breadth first search
3.
Examples:
For a givennumber a and a non negative integer n,
a^n=a*a*a*……*a for n times
Computing n! =1*2*3*…….n
Performing multiplication of two matrices : C=AB
Searching a key value from given list of elements.
4.
Advantages Of BruteForce:
•Simple and easy to understand
•Easy to implement
•Guarantees correct solution
•Applicable to many problems
•No complex data structures required
•Useful for small input sizes
•Serves as a baseline for optimized algorithms
5.
String Matching
String Matchingalgorithms are used in text processing.
String matching means finding one or more generally all the occurrences
of a string in the text. These occurrences are called as pattern.
Also called as Pattern matching algorithms.
Naïve Algorithm
Simplest method
Thisalgorithm performs a checking at all positions in the text
between 0 to n-m, whether an occurrence of the pattern starts there
or not.
Then after each attempt, it shifts the pattern by exactly one
position to the right.
If match is found then it returns otherwise the matching process
is continued by shifting one character to the right.
If there is no match at all the text for the given pattern even then
we have to do n comparisons.
15.
Homework:
1) Show thecomparisons the Naïve-
String matcher makes for the pattern p={1001} in the text
T={0000100010010}
2)Solve the following using Brute-Force algorithm: Find
whether the given string follows specified pattern and return 0
or 1.
a)Pattern:”abba”input:”redblueredblue” should return 1
b) Pattern:”aaaa”input:”asdasdasdasd” should return 1
c) Pattern:”aabb”input:”xyzabcxyzabc” should return 0
16.
How manycomparisons (both successful and unsuccessful) will be
made by the Brute force string matching algorithm in searching for each
of the following patterns in the binary text of 1000(Thousand) zeros?
A)00001 b)10000
Total unsuccessful searches=n-m+1=1000-5+1=996
Total number of comparisons(C)= Comparisons made by pattern
* Total unsuccessful searches
A) C= 5(Total character compared) *996(1000 zeros-
4matched zeros)
C=4980
Rabin Karp algorithm
TheRabin–Karp algorithm is an efficient string matching
(pattern searching) algorithm that uses hashing to find
occurrences of a pattern in a given text.
Knuth Morris PrattAlgorithm
The Knuth–Morris–Pratt (KMP) algorithm is an efficient
string matching algorithm that avoids unnecessary
comparisons by using information from previous matches.
When a mismatch occurs, KMP does not restart matching
from the beginning of the pattern.
Instead, it uses a preprocessed array called LPS (Longest
Prefix Suffix) to decide how far the pattern can be shifted.
26.
Boyer Moore
The Boyer–Moorealgorithm is a highly efficient string matching (pattern
searching) algorithm that compares the pattern with the text from right to left and
skips sections of the text using smart shift rules.
THIS IS A TEST (TEXT) TEST(PATTERN)
TEST
T=max(1,4-0-1)= 3 = 1
E=max(1,4-1-1)=2
S=max(1,4-2-1)=1
T=max(1,4-3-1)=1 T=1update using recent value
27.
Closest-Pair
The Closest pairproblem is finding the two closest points
from the set of n points.
For two dimensional case the distance between two pints is
denoted by Euclidean distance.
The basic operation is computing Euclidian distance between
two points.
Convex Hull Problem
Givena set {P1,P2,P3,….Pn} of points in the plane, the convex hull H(s)
is the smallest convex polygon in the plane that contains all of the points
of S.The set S is called as convex set.
A polygon is convex if and only if any two points from the set
forming a line segment with end points entirely within the polygon.
30.
The convex hullof a set S points is the smallest convex set
containing S.
If S is a set of two points its convex hull is the line segment
connecting these points. If S is a set of three points then its
convex hull is the triangle
A polygon is convex if and only if any two points from
the set forming a line segment with end points entirely within
the polygon.
Convex hull problem is the problem of constructing the
convex hull for a given set S of n points.
Definition Of ExtremePoints
An extreme point of a convex set is a point which
is not a middle point of any line segment with end
points in the set.
34.
Exhaustive Search
Exhaustive Searchis a method in which solution is
obtained by searching each element of given
problem
Exhaustive Search makes use of straightforward
Brute Force’s approach.
Travelling Salesman Problem
Knapsack Problem
Assignment Problem
35.
Travelling Salesman Problem
TravellingSalesman Problem is a famous problem in the
graph theory.
It can be stated as Consider that there are n cities and
traveling salesman has to visit each city exactly once and
has to return to the city from where he has started.
To model this problem weighted graph can be used.
The vertices of such graph represent cities and the edge
weight specifies the distance between the cities.
36.
Travelling Salesman Problem….
TravellingSalesman Problem can also be stated as
finding shortest Hamiltonian circuit of a graph.
Shortest Hamiltonian circuit is a cycle in the given
graph such that all the vertices of the graph can be
visited only once.
The tour is obtained in such a way has shortest
distance.
37.
Consider the graph
Thisis a weighted graph in
which weights along the
edges represent the
distances among the cities.
To find Hamiltonian circuit (i.e) the path in which
each city is visited exactly once and returning to the
city from which it has started initially
39.
Find all thesolution to the travelling salesman
problem by exhaustive search. Give the optimal.
40.
Knapsack Problem
Knapsack Problemwhich can be solved using exhaustive search.
It can be stated as follows:
Suppose that there are n objects from i=1,2,3,…….n. Each
object i has some weight wi and values associated with each
object is vi.
And capacity of knapsack is W.A thief has to pickup the most
valuable objects to fill the knapsack to its capacity
ITE
M
WEIGHT (
∑ Wi)
VALUES (
∑ Vi )
1 2 $1
2 3 $2
3 4 $8
4 5 $6
1,2 2+3=5 1+2=$3
1,3 2+4=6 1+8=$9
1,4 2+5=7 1+6=$7
2,3 3+4=7 2+8=$10(Not
Feasible as
exceeding W)
1,2,3 2+3+4=9 Not Feasible as
exceeding W
1,2,4 2+3+5=10 Not Feasible as
exceeding W
2,3,4 3+4+5=12 Not Feasible as
exceeding W
Thus by exhaustive search method ,we get
selection of item 2 and item 3 for
knapsack. Each element of the problem
domain has to be searched for obtaining
solution. These problems are also called as
NP hard problems.
43.
Assignment problem
Consider thatthere are n people who need to be assigned to execute n jobs
i.e only one job at a time.
Then problem is to find such assignment that gives smallest total cost.
The cost can be computed as cost C[i,j] i th person assigned to j th job.
44.
The cost canbe obtained by assigning the jobs in various
combinations as n!=4!=4*3*2*1=24
Combinations
(Job 1,2,3,4)
Cost
1,2,3,4 10+5+2+5=22
1,2,4,3 10+5+9+10=34
1,3,4,2 10+4+9+7=30
1,3,2,4 10+4+9+5=28
1,4,2,3 10+8+9+10=37
1,4,3,2 10+8+2+7=27
2,1,3,4 3+7+2+5=17
2,1,4,3 3+1+10+9=23
2,3,1,4 3+9+8+5=25
2,3,4,1 3+9+10+9=31
2,4,1,3 3+7+8+9=27
2,4,3,1 3+7+2+9=21