0% found this document useful (0 votes)
60 views43 pages

Design and Analysis of Algorithms: Dr. Sobia Arshad

This document discusses brute force algorithms and their analysis. It provides examples of problems that can be solved using brute force, including computing factorials, matrix multiplication, linear search, and sorting algorithms like bubble sort and selection sort. String matching and the traveling salesperson problem are also presented as problems addressed through exhaustive search brute force algorithms. However, exhaustive search leads to extremely inefficient algorithms for problems like the traveling salesperson and knapsack problems. More efficient algorithms need to be designed to improve performance over brute force approaches.

Uploaded by

Mohsin Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views43 pages

Design and Analysis of Algorithms: Dr. Sobia Arshad

This document discusses brute force algorithms and their analysis. It provides examples of problems that can be solved using brute force, including computing factorials, matrix multiplication, linear search, and sorting algorithms like bubble sort and selection sort. String matching and the traveling salesperson problem are also presented as problems addressed through exhaustive search brute force algorithms. However, exhaustive search leads to extremely inefficient algorithms for problems like the traveling salesperson and knapsack problems. More efficient algorithms need to be designed to improve performance over brute force approaches.

Uploaded by

Mohsin Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

DESIGN AND ANALYSIS OF ALGORITHMS

Dr. Sobia Arshad

Lecture 8
Brute Force

“A straightforward approach based on problem


statement and definitions of concepts involved”

+ “Just do it”
+ Simplest and easiest to apply
+ Widely applicable
- Lower efficiency
Examples

 Computing an (a > 0, n a positive integer)


 Computing n!
 Multiplying two n by n Matrices
 Linear search
 Bubble sort
 Selection sort
Bubble Sort
 Each pair of adjacent element is compared and
elements are swapped if they are not in order.
Bubble Sort Analysis
String Matching

Finding pattern in a given text of strings.


Pattern Recognition
String Matching

Given:
Text: a (long) string of n characters to search in
Pattern: a (short) string of m characters to search for

Examples:
Text: It is never too late to have a happy life.
Pattern: happy

Text: 10010101101001100101111010
Pattern: 001011
String Matching Example

Text: NOBODY NOTICED HIM

Pattern: NOT
NOT
NOT
NOT
NOT
NOT
NOT
NOT
String Matching
String Matching
Brute force solution:
1. Align pattern at beginning of text
2. moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
3. while pattern is not found and the text is not yet exhausted,
realign pattern one position to the right and repeat step 2.
String Matching Algorithm
Algorithm StringMatch(T, P)

Input:
An array T[0..n-1] of n characters, representing text
An array P[0..m-1] of m characters, representing pattern

Output:
The position of the first character in the text that starts the first
matching substring if the search is successful and -1 otherwise
public static int search( String text, String pattern)
{

int lengthOfText=text.length();
int lengthOfPattern=pattern.length();

for(int i=0;i<=lengthOfText-lengthOfPattern;i++)
{
int j;

for(j=0;j<lengthOfPattern;j++)
{
if(text.charAt(i+j)!=pattern.charAt(j))
{
break;
}
}
if(j==lengthOfPattern)
return i;
}
return 0;
}
Brute Force Substring Search

TEXT

PATTER
N

Given:
Text: a (long) string of n characters to search in
Pattern: a (short) string of m characters to search for
First iteration
Shift right by one upon mismatch

Second iteration
3rd and 4th Iteration
5th to 7th Iterations
8th to 10th iterations
Final iteration
Problem with Brute Force
Problem with Brute Force
Finally..
String Matching Algorithm
Finding Closest-pair
Brute Force Solution
Closest Pair Algorithm
Efficiency Analysis
Exhaustive Search
 A Brute Force technique that tries all possibilities
until a solution is found

Such an algorithm can be:


 Optimizing: Find the best solution. This may

require finding all solutions


 Satisfying: Stop as soon as a solution is found that

is good enough
Exhaustive Search Method

 Construct a way of listing all potential solutions to


the problem in a systematic manner
 Evaluate solutions one by one, disqualifying
infeasible ones and keeping track of the best one
found so far
 When search ends, announce the winner

Exhaustive search is simply a brute-force approach


to combinatorial problems
The Traveling Sales Person Problem
The Traveling Sales Person Problem

Problem Description:
 Given n cities with known distances between each

pair, find the shortest tour that passes through all


the cities exactly once before returning to the
starting city.

The problem can be stated as the problem of


finding the shortest Hamiltonian circuit of
the graph.
Application of TSP
 The TSP has several applications in planning,
logistics, and the manufacture of microchips.

 In these applications, the concept city represents,


for example, customers, soldering points, and the
concept distance represents travelling times or cost.
Exhaustive Search- TSP
Brute Force Approach
 Find all possible tours

 Calculate cost of each tour

 Return lowest cost tour


Exhaustive Search- TSP
The Knapsack Problem

•Thief
•Transport plane
The Knapsack Problem
Application of Knapsack Problem
 This problem often arises in resource
allocation where there are financial
constraints.
The Knapsack Problem
Knapsack by Exhaustive Search
Knapsack Algorithm
Concluding Remarks
 Thus, for both the traveling salesman and knapsack
problems considered above, exhaustive search
leads to algorithms that are extremely inefficient on
every input. 
Conclusion- Brute Force Approach

 Brute force algorithms are usually inefficient


(slow)
 Exhaustive search algorithms mostly belong to
exponential or factorial complexity class (very poor
performance)
 There is a need to design efficient Algorithms

You might also like