SlideShare a Scribd company logo
1 of 46
Brute Force and
Exhaustive Search
definition
• Brute force is a straightforward approach to
solving a problem, usually directly based on
the problem statement and definitions of the
concepts involved.
• In simple just do it.
Brute force approach is not an important algorithm
design strategy for the following reasons:
• First, unlike some of the other strategies, brute force is
applicable to a very wide variety of problems. Its used for
many elementary but algorithmic tasks such as
computing the sum of n numbers, finding the largest
element in a list and so on.
• Second, for some important problems—e.g., sorting,
searching, matrix multiplication, string matching—the
brute-force approach yields reasonable algorithms of at
least some practical value with no limitation on instance
size.
• Third, the expense of designing a more
efficient algorithm may be unjustifiable if only
a few instances of a problem need to be
solved and a brute-force algorithm can solve
those instances with acceptable speed.
• Fourth, even if too inefficient in general, a
brute-force algorithm can still be useful for
solving small-size instances of a problem.
• Finally, a brute-force algorithm can serve an
important theoretical or educational purpose
as a yardstick with which to judge more
efficient alternatives for solving a problem.
Selection sort and Bubble sort:
• The application of Brute-force for the problem
of sorting: given a list of n orderable items(eg.,
numbers characters, strings etc.) , rearrange
them in increasing order. The straightforward
sorting is based on two algorithms selection
and bubble sort. Selection sort is better than
the bubble sorting, but, both are better
algorithms because of its clarity.
Selection sort:
• By scanning the entire list to find its smallest element
and exchange it with the first element, putting the
smallest element in its final position in the sorted
list.
• Then scan the list with the second element, to find
the smallest among the last n-1 elements and
exchange it with second element.
• Continue this process till the n-2 elements. Generally,
in the ith pass through the list, numbered 0 to n-2,
the algorithm searches for the smallest item among
the last n-i elements and swaps it with Ai.
• A0 ≤ A1 ≤ ……….. ≤ Ai-1 Ai, ………… Amin , ………. An-1
in their final positions the last n-i elements After n-1
• ALGORITHM SelectionSort(A[0..n − 1])
• //Sorts a given array by selection sort
• //Input: An array A[0..n − 1] of orderable
elements
• //Output: Array A[0..n − 1] sorted in
nondecreasing order
• for i ←0 to n − 2 do
• min←i
• for j ←i + 1 to n − 1 do
• if A[j ]<A[min] min←j
• swap A[i] and A[min]
example
• The e.g. for the list 89,45,68,90,29,34,17 is
89 45 68 90 29 34 17
17 |45 68 90 29 34 89
17 29 | 68 90 45 34 89
17 29 34| 90 45 68 89
17 29 34 45 |90 68 89
17 29 34 45 68 |90 89
17 29 34 45 68 89 |90
17
29
34
45
68
89
analysis
• The analysis of selection sort is straight
forward. The input size is given by the number
of elements n;
• the basic operation is the key comparison
A[j ]<A[min].
• The number of times it is executed depends
only on the array size and is given by the
following sum:
n-2 n-1 n-2 n-2
c(n) = ∑ ∑ 1 = ∑ [(n-1)-(i+1)+1]= ∑ n-1-i
i-0 j=i+1 i=0 i=0
• Whether you compute this sum by distributing
the summation symbol or by immediately
getting the sum of decreasing integers, the
answer, of course, must be the same:
n-2 n-1 n-2 n-2
c(n) = ∑ ∑ 1 = ∑ [(n-1)-(i+1)+1]= ∑ n-1-i
i-0 j=i+1 i=0 i=0
Thus selection sort is a θ(n^2 ) algorithm on all
inputs. Note, The number of key swaps is only
θ(n), or n-1.
Bubble Sort
It is based on the comparisons of adjacent
elements and exchanging it. This procedure is
done repeatedly and ends up with placing the
largest element to the last position. The
second pass bubbles up the second largest
element and so on after n-1 passes, the list is
sorted. Pass i(0≤ i ≤ n-2) of bubble sort can be
represented as:
• A0, . . . , Aj?↔Aj+1, . . . , An−i−1 | An−i≤ . . . ≤ An−1in their
final positions
algorithm
• ALGORITHM BubbleSort(A[0..n − 1])
//Sorts a given array by bubble sort
//Input: An array A[0..n − 1] of orderable elements
//Output: Array A[0..n − 1] sorted in nondecreasing
order
for i ←0 to n − 2 do
for j ←0 to n − 2 − i do
if A[j + 1]<A[j ] swap A[j ] and A[j + 1]
Analysis:
• The no of key comparisons is the same for all
arrays of size n, it is obtained by a sum which
is similar to selection sort.
• The no of key swaps depends on the input.
The worst case of decreasing arrays, is same
as the no of key comparisons:
3.2 Sequential search and Brute Force
string matching:
• The two applications of searching based on for
a given value in a given list. The first one
searches for a given value in a given list. The
second deals with the stringmatching problem
with a given text and a pattern to be searched.
Sequential search:
• Here, the algorithm compares successive
elements of a given list with a given search key
until either a match is encountered (successful
search) or the list is exhausted without finding
a match(unsuccessful search). Here is an
algorithm with enhanced version: where, we
append the search key to the end of the list,
the search for the key have to be successful,
so eliminate a check for the list’s end on each
iteration.
• Another , method is to search in a sorted list.
So that the searching can be stopped as soon
as the element greater than or equal to the
search key is encountered.
Analysis:
• The efficiency is determined based on the key
comparison
• when the algorithm runs the longest among
all possible inputs i.e., when the search
element is the last element in the list.
• when the search key is the first element on
list.
• when the search key is almost in the middle
position i.e., hall the list will be searched.
Note: its almost similar to the straightforward
method with slight variations. It remains to be
linear in both average and worst cases.
Brute-force string matching:
• The problem is: given a string of n characters called the text
and a string of m (m≤n) characters called the pattern, find a
substring of the text that matches the pattern. The Brute-
force algorithm is to align the pattern against the first m
characters of the text and start matching the corresponding
pairs of characters from left to right until either all the m pairs
of the characters match or a mismatching pair is encountered.
The pattern is shifted one position to the right and character
comparisons are resumed, starting again with the first
character of the pattern and its counterpart in the text. The
last position in the text that can still be a beginning of a
matching sub string is n-m(provided that text’s positions are
from 0 to n-1). Beyond that there is no enough characters to
match, the algorithm can be stopped.
Analysis:
• The worst case is to make all m comparisons
before shifting the pattern, occurs for nm+1
tries. There in the worst case, the algorithm is
in θ (nm). The average case is when there can
be most shift after a very few comparisons
and it is to be a linear, ie., θ (n+m) = θ (n).
Closest-Pair and Convex-Hull
Problems by Brute Force
• The closest-pair problem calls for finding the
two closest points in a set of n points. It is the
simplest of a variety of problems in
computational geometry that deals with
proximity of points in the plane or higher-
dimensional spaces
• Points in question can represent such physical
objects as airplanes or post offices as well as
database records, statistical samples, DNA
sequences, and so on.
• One of the important applications of the
closest-pair problem is cluster analysis in
statistics.
• For simplicity, we consider the two
dimensional case of the closest-pair problem.
We assume that the points in question are
specified in a standard fashion by their (x, y)
Cartesian coordinates and that the distance
between two points pi(xi,yi) and pj(xj, yj ) is
the standard Euclidean distance.
• The basic operation of the algorithm is
computing the square root.
• But square roots in the loop can be avoided!
• The trick is to
• realize that we can simply ignore the square-
root function and compare the values
(xi− xj )^2 + (yi− yj )^2 themselves.
• Then the basic operation of the algorithm will
be squaring a number.
DEFINITION The convex hull of a set S of points is the smallest
convex set containing S. (The “smallest” requirement means that
the convex hull of S must be a subset of any convex set
containing S.)
Exhaustive search :
• It is a straightforward method used to solve
problems of combinatorial problems. It
generates each and every element of the
problem’s domain, selecting based on
satisfying the problem’s constraints and then
finding a desired element(eg., maximization or
minimization of desired characteristics). The 3
important problems are TSP, knapsack
problem and assignment problem
Traveling salesman problem
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 stated as
the problem of finding the shortest
Hamiltonian circuit of the graph-which is a
weighted graph, with the graph’s vertices
representing the cities and the edge weights
specifying the distance. Hamiltonian circiut is
defined as a cycle that passes thru all the
vertices of the graph exactly once.
• The Hamiltonian circuit can also be defined as
a sequence of n+1 adjacent vertices vi0, vi1, ….
Vin-1, vi0, where the first vertex of the sequence
is the same as the last one while all other n-1
vertices are distinct. Obtain the tours by
generating all the permutations of n-1
intermediate cities, compute the tour lengths,
and find the shortest among them.
• Consider the condition that the vertex B
precedes C then,The total no of permutations
will be (n-1)! /2, which is impractical except
for small values of n. On the other hand, if the
starting vertex is not considered for a single
vertex, the number of permutations will be
even large for n values.
* Knapsack problem:
• The problem states that: 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. Eg consider a transport plane
that has to deliver the most valuable set of
items to a remote location without exceeding
its capacity.
Example: W=10, w1,w2, w3, w4 = { 7,3,4,5 } and
v1,v2, v3, v4 = { 42,12,40,25 }
s
• This problem considers all the subsets of the
set of n items given, computing the total
weight of each subset in order to identify
feasible subsets(i.e., the one with the total
weight not exceeding the knapsack capacity)
and finding the largest among the values,
which is an optimal solution. The number of
subsets of an n-element set is 2n the search
leads to a Ω(2n) algorithm, which is not based
on the generation of individual subsets.
• Thus, for both TSP and knapsack, exhaustive
search leads to algorithms that are inefficient
on every input. These two problems are the
best known examples of NP-hard problems.
No polynomial-time algorithm is known for
any NP-hard problem. The two methods
Backtracking and Branch & bound enable us to
solve this problem in less than exponential
time.
• Assignment problem:
The problem is: given n people who need to be
assigned to execute n jobs, one person per
job. The cost if the ith person is assigned to the
jth 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 smallest total cost.
Example:

More Related Content

What's hot

Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Bharat Bhushan
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 

What's hot (20)

Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Hashing
HashingHashing
Hashing
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
chapter 1
chapter 1chapter 1
chapter 1
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Minmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slidesMinmax Algorithm In Artificial Intelligence slides
Minmax Algorithm In Artificial Intelligence slides
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 

Similar to Brute force method

Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
chidabdu
 
Selection sort
Selection sortSelection sort
Selection sort
asra khan
 
AOT3 Multivariable Optimization Algorithms.pdf
AOT3 Multivariable Optimization Algorithms.pdfAOT3 Multivariable Optimization Algorithms.pdf
AOT3 Multivariable Optimization Algorithms.pdf
SandipBarik8
 

Similar to Brute force method (20)

Ada notes
Ada notesAda notes
Ada notes
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
 
Brute force
Brute forceBrute force
Brute force
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
Perform brute force
Perform brute forcePerform brute force
Perform brute force
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
brown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algobrown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algo
 
Selection sort
Selection sortSelection sort
Selection sort
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
L1803016468
L1803016468L1803016468
L1803016468
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
sorting
sortingsorting
sorting
 
Sorting
SortingSorting
Sorting
 
AOT3 Multivariable Optimization Algorithms.pdf
AOT3 Multivariable Optimization Algorithms.pdfAOT3 Multivariable Optimization Algorithms.pdf
AOT3 Multivariable Optimization Algorithms.pdf
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 

Brute force method

  • 2. definition • Brute force is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved. • In simple just do it.
  • 3. Brute force approach is not an important algorithm design strategy for the following reasons: • First, unlike some of the other strategies, brute force is applicable to a very wide variety of problems. Its used for many elementary but algorithmic tasks such as computing the sum of n numbers, finding the largest element in a list and so on. • Second, for some important problems—e.g., sorting, searching, matrix multiplication, string matching—the brute-force approach yields reasonable algorithms of at least some practical value with no limitation on instance size.
  • 4. • Third, the expense of designing a more efficient algorithm may be unjustifiable if only a few instances of a problem need to be solved and a brute-force algorithm can solve those instances with acceptable speed. • Fourth, even if too inefficient in general, a brute-force algorithm can still be useful for solving small-size instances of a problem. • Finally, a brute-force algorithm can serve an important theoretical or educational purpose as a yardstick with which to judge more efficient alternatives for solving a problem.
  • 5. Selection sort and Bubble sort: • The application of Brute-force for the problem of sorting: given a list of n orderable items(eg., numbers characters, strings etc.) , rearrange them in increasing order. The straightforward sorting is based on two algorithms selection and bubble sort. Selection sort is better than the bubble sorting, but, both are better algorithms because of its clarity.
  • 6. Selection sort: • By scanning the entire list to find its smallest element and exchange it with the first element, putting the smallest element in its final position in the sorted list. • Then scan the list with the second element, to find the smallest among the last n-1 elements and exchange it with second element. • Continue this process till the n-2 elements. Generally, in the ith pass through the list, numbered 0 to n-2, the algorithm searches for the smallest item among the last n-i elements and swaps it with Ai. • A0 ≤ A1 ≤ ……….. ≤ Ai-1 Ai, ………… Amin , ………. An-1 in their final positions the last n-i elements After n-1
  • 7. • ALGORITHM SelectionSort(A[0..n − 1]) • //Sorts a given array by selection sort • //Input: An array A[0..n − 1] of orderable elements • //Output: Array A[0..n − 1] sorted in nondecreasing order • for i ←0 to n − 2 do • min←i • for j ←i + 1 to n − 1 do • if A[j ]<A[min] min←j • swap A[i] and A[min]
  • 8. example • The e.g. for the list 89,45,68,90,29,34,17 is 89 45 68 90 29 34 17 17 |45 68 90 29 34 89 17 29 | 68 90 45 34 89 17 29 34| 90 45 68 89 17 29 34 45 |90 68 89 17 29 34 45 68 |90 89 17 29 34 45 68 89 |90 17 29 34 45 68 89
  • 9. analysis • The analysis of selection sort is straight forward. The input size is given by the number of elements n; • the basic operation is the key comparison A[j ]<A[min].
  • 10. • The number of times it is executed depends only on the array size and is given by the following sum: n-2 n-1 n-2 n-2 c(n) = ∑ ∑ 1 = ∑ [(n-1)-(i+1)+1]= ∑ n-1-i i-0 j=i+1 i=0 i=0 • Whether you compute this sum by distributing the summation symbol or by immediately getting the sum of decreasing integers, the answer, of course, must be the same:
  • 11. n-2 n-1 n-2 n-2 c(n) = ∑ ∑ 1 = ∑ [(n-1)-(i+1)+1]= ∑ n-1-i i-0 j=i+1 i=0 i=0 Thus selection sort is a θ(n^2 ) algorithm on all inputs. Note, The number of key swaps is only θ(n), or n-1.
  • 12. Bubble Sort It is based on the comparisons of adjacent elements and exchanging it. This procedure is done repeatedly and ends up with placing the largest element to the last position. The second pass bubbles up the second largest element and so on after n-1 passes, the list is sorted. Pass i(0≤ i ≤ n-2) of bubble sort can be represented as: • A0, . . . , Aj?↔Aj+1, . . . , An−i−1 | An−i≤ . . . ≤ An−1in their final positions
  • 13. algorithm • ALGORITHM BubbleSort(A[0..n − 1]) //Sorts a given array by bubble sort //Input: An array A[0..n − 1] of orderable elements //Output: Array A[0..n − 1] sorted in nondecreasing order for i ←0 to n − 2 do for j ←0 to n − 2 − i do if A[j + 1]<A[j ] swap A[j ] and A[j + 1]
  • 14.
  • 15. Analysis: • The no of key comparisons is the same for all arrays of size n, it is obtained by a sum which is similar to selection sort.
  • 16. • The no of key swaps depends on the input. The worst case of decreasing arrays, is same as the no of key comparisons:
  • 17. 3.2 Sequential search and Brute Force string matching: • The two applications of searching based on for a given value in a given list. The first one searches for a given value in a given list. The second deals with the stringmatching problem with a given text and a pattern to be searched.
  • 18. Sequential search: • Here, the algorithm compares successive elements of a given list with a given search key until either a match is encountered (successful search) or the list is exhausted without finding a match(unsuccessful search). Here is an algorithm with enhanced version: where, we append the search key to the end of the list, the search for the key have to be successful, so eliminate a check for the list’s end on each iteration.
  • 19.
  • 20. • Another , method is to search in a sorted list. So that the searching can be stopped as soon as the element greater than or equal to the search key is encountered.
  • 21. Analysis: • The efficiency is determined based on the key comparison • when the algorithm runs the longest among all possible inputs i.e., when the search element is the last element in the list.
  • 22. • when the search key is the first element on list. • when the search key is almost in the middle position i.e., hall the list will be searched. Note: its almost similar to the straightforward method with slight variations. It remains to be linear in both average and worst cases.
  • 23. Brute-force string matching: • The problem is: given a string of n characters called the text and a string of m (m≤n) characters called the pattern, find a substring of the text that matches the pattern. The Brute- force algorithm is to align the pattern against the first m characters of the text and start matching the corresponding pairs of characters from left to right until either all the m pairs of the characters match or a mismatching pair is encountered. The pattern is shifted one position to the right and character comparisons are resumed, starting again with the first character of the pattern and its counterpart in the text. The last position in the text that can still be a beginning of a matching sub string is n-m(provided that text’s positions are from 0 to n-1). Beyond that there is no enough characters to match, the algorithm can be stopped.
  • 24.
  • 25.
  • 26. Analysis: • The worst case is to make all m comparisons before shifting the pattern, occurs for nm+1 tries. There in the worst case, the algorithm is in θ (nm). The average case is when there can be most shift after a very few comparisons and it is to be a linear, ie., θ (n+m) = θ (n).
  • 27. Closest-Pair and Convex-Hull Problems by Brute Force • The closest-pair problem calls for finding the two closest points in a set of n points. It is the simplest of a variety of problems in computational geometry that deals with proximity of points in the plane or higher- dimensional spaces
  • 28. • Points in question can represent such physical objects as airplanes or post offices as well as database records, statistical samples, DNA sequences, and so on. • One of the important applications of the closest-pair problem is cluster analysis in statistics.
  • 29. • For simplicity, we consider the two dimensional case of the closest-pair problem. We assume that the points in question are specified in a standard fashion by their (x, y) Cartesian coordinates and that the distance between two points pi(xi,yi) and pj(xj, yj ) is the standard Euclidean distance.
  • 30.
  • 31. • The basic operation of the algorithm is computing the square root. • But square roots in the loop can be avoided! • The trick is to • realize that we can simply ignore the square- root function and compare the values (xi− xj )^2 + (yi− yj )^2 themselves. • Then the basic operation of the algorithm will be squaring a number.
  • 32.
  • 33. DEFINITION The convex hull of a set S of points is the smallest convex set containing S. (The “smallest” requirement means that the convex hull of S must be a subset of any convex set containing S.)
  • 34.
  • 35. Exhaustive search : • It is a straightforward method used to solve problems of combinatorial problems. It generates each and every element of the problem’s domain, selecting based on satisfying the problem’s constraints and then finding a desired element(eg., maximization or minimization of desired characteristics). The 3 important problems are TSP, knapsack problem and assignment problem
  • 36. Traveling salesman problem 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 stated as the problem of finding the shortest Hamiltonian circuit of the graph-which is a weighted graph, with the graph’s vertices representing the cities and the edge weights specifying the distance. Hamiltonian circiut is defined as a cycle that passes thru all the vertices of the graph exactly once.
  • 37. • The Hamiltonian circuit can also be defined as a sequence of n+1 adjacent vertices vi0, vi1, …. Vin-1, vi0, where the first vertex of the sequence is the same as the last one while all other n-1 vertices are distinct. Obtain the tours by generating all the permutations of n-1 intermediate cities, compute the tour lengths, and find the shortest among them.
  • 38.
  • 39. • Consider the condition that the vertex B precedes C then,The total no of permutations will be (n-1)! /2, which is impractical except for small values of n. On the other hand, if the starting vertex is not considered for a single vertex, the number of permutations will be even large for n values.
  • 40. * Knapsack problem: • The problem states that: 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. Eg consider a transport plane that has to deliver the most valuable set of items to a remote location without exceeding its capacity.
  • 41. Example: W=10, w1,w2, w3, w4 = { 7,3,4,5 } and v1,v2, v3, v4 = { 42,12,40,25 }
  • 42. s
  • 43. • This problem considers all the subsets of the set of n items given, computing the total weight of each subset in order to identify feasible subsets(i.e., the one with the total weight not exceeding the knapsack capacity) and finding the largest among the values, which is an optimal solution. The number of subsets of an n-element set is 2n the search leads to a Ω(2n) algorithm, which is not based on the generation of individual subsets.
  • 44. • Thus, for both TSP and knapsack, exhaustive search leads to algorithms that are inefficient on every input. These two problems are the best known examples of NP-hard problems. No polynomial-time algorithm is known for any NP-hard problem. The two methods Backtracking and Branch & bound enable us to solve this problem in less than exponential time.
  • 45. • Assignment problem: The problem is: given n people who need to be assigned to execute n jobs, one person per job. The cost if the ith person is assigned to the jth 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 smallest total cost.