UNIT-II
BRUTE FORCE AND DIVIDE-
AND-CONQUER
Brute Force: String Matching-Closest-Pair and
Convex-Hull Problems-Exhaustive Search -
Traveling Salesman Problem - Knapsack
Problem - Assignment problem.
BRUTE FORCE:
Brute Force is 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
Examples:
For a given number 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.
Advantages Of Brute Force:
•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
String Matching
String Matching algorithms 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.
String Matching
Naïve algorithm
Rabin Karp algorithm
Knuth Morris pratt
Boyer Moore
Naïve Algorithm
Simplest method
This algorithm 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.
Homework:
1) Show the comparisons 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
 How many comparisons (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
B) C= 1(Total character compared) *996
C=996
Rabin Karp algorithm
The Rabin–Karp algorithm is an efficient string matching
(pattern searching) algorithm that uses hashing to find
occurrences of a pattern in a given text.
HASH VALUE
CCB-86
CBB-32
BBC-46
BCC-100
CCC-87
CCB-86
CBG-37
BGH-18
GHC-12
HCC-22
CCB-86
CBJ-40
Knuth Morris Pratt Algorithm
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.
Boyer Moore
The Boyer–Moore algorithm 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

Closest-Pair
The Closest pair problem 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.
Closest-Pair
Convex Hull Problem
Given a 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.
The convex hull of 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.
The Points
{1,2,3,4,5,6}
are called
Extreme Points.
Definition Of Extreme Points
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.
Exhaustive Search
Exhaustive Search is 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
Travelling Salesman Problem
Travelling Salesman 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.
Travelling Salesman Problem….
Travelling Salesman 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.
Consider the graph
This is 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
Find all the solution to the travelling salesman
problem by exhaustive search. Give the optimal.
Knapsack Problem
Knapsack Problem which 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
Knapsack Problem
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.
Assignment problem
Consider that there 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.
The cost can be 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
Home Work:
BRUTE FORCE & DIVIDE AND CONQUER-UNIT II
BRUTE FORCE & DIVIDE AND CONQUER-UNIT II

BRUTE FORCE & DIVIDE AND CONQUER-UNIT II

  • 1.
    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.
  • 6.
    String Matching Naïve algorithm RabinKarp algorithm Knuth Morris pratt Boyer Moore
  • 7.
    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
  • 17.
    B) C= 1(Totalcharacter compared) *996 C=996
  • 18.
    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.
  • 20.
  • 21.
    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.
  • 28.
  • 29.
    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.
  • 32.
  • 33.
    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
  • 41.
  • 42.
    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
  • 45.