SlideShare a Scribd company logo
2
Most read
20
Most read
22
Most read
UNIT II
BRUTE FORCE
V. Roseline, M.Sc., M.Phil., B.Ed., SET, NET,(Ph.D).,
Assistant Professor,
Sadakathullah Appa College, Tirunelveli.
BRUTE FORCE
 Brute force is a straightforward approach to solving a problem, usually
directly based on the problem statement and definitions of the
concepts involved.
 Selection Sort, Bubble Sort, Sequential Search, String Matching, Depth-
First Search and Breadth-First Search, Closest-Pair and Convex-Hull
Problems can be solved by Brute Force.
 Examples:
 Computing an : a * a * a * … * a ( n times)
 Computing n! : The n! can be computed as n*(n-1)* … *3*2*1
 Multiplication of two matrices : C=AB
 Searching a key from list of elements (Sequential search)
 Advantages:
 Brute force is applicable to a very wide variety of problems.
 It is very useful for solving small size instances of a
problem, even though it is inefficient.
 The brute-force approach yields reasonable algorithms of at
least some practical value with no limitation on instance size
for sorting, searching, and string matching.
Selection Sort
 First scan the entire given 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, starting with the second element, to find the smallest among
the last n − 1 elements and exchange it with the second element, putting the
second smallest element in its final position in the sorted list.
 Generally, on the ith pass through the list, which we number from 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 passes, the list is sorted.
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]
| 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
Thus, selection sort is a Θ(n2) algorithm on all inputs.
Bubble Sort
 The bubble sorting algorithm is to compare adjacent elements of the list
and exchange them if they are out of order.
 By doing it repeatedly, we end up “bubbling up” the largest element to
the last position on the list. The next pass bubbles up the second largest
element, and so on, until after n − 1 passes the list is sorted.
 Pass i (0 ≤ i ≤ n − 2) of bubble sort can be represented by the ?
following:
A0, . . . , Aj ↔ Aj+1, . . . , An−i−1 | An−i ≤ . . . ≤ An−1
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]
Brute force
COMPARISON OF SORTING
ALGORITHMS
CLOSEST-PAIR AND CONVEX-HULL
PROBLEMS
 We consider a straight forward approach of Brute Force to two well-
known problems dealing with a finite set of points in the plane.
 These problems are very useful in important applied areas like
computational geometry and operations research.
Closest-Pair Problem
 The closest-pair problem finds the two closest points in a set of n
points.
 It is the simplest of a variety of problems in computational
geometry.
 Consider the two-dimensional case of the closest-pair problem. The
points are specified in a standard fashion by their (x, y) coordinates and
the distance between two points pi(xi, yi) and pj(xj, yj ) is the standard
Euclidean distance.
d(pi , pj) = √(xi − xj)2 + (yi − yj)2
ALGORITHM BruteForceClosestPair(P)
//Finds distance between two closest points in the plane by brute force
//Input: A list P of n (n ≥ 2) points p1(x1, y1), . . . , pn(xn, yn)
//Output: The distance between the closest pair of points
for i ←1 to n − 1 do
for j ←i + 1 to n do
d ←min(d, sqrt((xi− xj )2 + (yi− yj )2))
return d
The basic operation of the algorithm will be squaring a
number. The number of times it will be executed can be
computed as follows:
Speeding up the innermost loop of the algorithm could
only decrease the algorithm’s running time by a constant factor,
but it cannot improve its asymptotic efficiency class.
CONVEX-HULL PROBLEM
 Convexity
 A polygon Pis said to be convex if:Pis nonintersecting and for anytwo
points p and q on the boundary of P, segment pq lies entirely inside P.
convex
nonconvex
 Convex Set
 A set of points (finite or infinite) in the plane is called convex if
for any two points p and q
in the set, the entire line segment with the endpoints at p and q
belongs to the set.
 Convex Hull
 The convex hull of a set of points is defined as the smallest convex
polygon, that encloses all of the points in the set. Convex means,
that the polygon has no corner that is bent inwards.
OR
 The convex hull of a set S of points is the smallest convex set
containing S.
 If S is convex, its convex hull is obviously S itself.
 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 not on the same line, its convex hull is the
triangle with the vertices at the three points given; if the three points
do lie on the same line, the convex hull is the line segment with its
endpoints at the two points that are farthest apart.
 Example of the convex hull for a larger set.
Convex Hull Problem
 The convex-hull problem is the problem of constructing the
convex hull for a given set S of n points.
 To solve it, we need to find the points that will serve as the vertices of
the polygon.
 Mathematicians call the vertices of such a polygon “extreme points.”
 An extreme point of a convex set is a point of this set that is not a
middle point of any line segment with endpoints in the set. For
example, the extreme points of a triangle are its three vertices, the
extreme points of a circle are all the points of its circumference, and
the extreme points of the convex hull of the set of eight points in
Figure are p1, p5, p6, p7, and p3.
Algorithm
 We can solve the convex-hull problem by brute-force manner.
 The convex hull problem is one with no obvious algorithmic
solution.
 There is a simple but inefficient algorithm that is based on the
following observation about line segments making up the boundary
of a convex hull: a line segment connecting two points pi and pj of a
set of n points is a part of the convex hull’s boundary if and only if all
the other points of the set lie on the same side of the straight
line through these two points. Repeating this test for every pair of
points yields a list of line segments that make up the convex hull’s
boundary.
 Implementation of algorithm.
 First, the straight line through two points (x1, y1), (x2, y2) in the
coordinate plane can be defined by the equation ax + by = c,where a =
y2 − y1, b = x1 − x2, c = x1y2 − y1x2.
 Second, such a line divides the plane into two half-planes: for all
the points in one of them,
ax + by > c, while for all the points in the other, ax + by < c. (For the
points on the line itself, of course, ax + by = c.) Thus, to check
whether certain points lie on the same side of the line, we can
simply check whether the expression ax + by − c has the same sign for
each of these points.
Time efficiency of this algorithm is in O(n3):

More Related Content

PPTX
Unit 5 internal sorting &amp; files
PDF
Algorithms Lecture 2: Analysis of Algorithms I
PDF
Coin Change Problem
PPTX
Top down parsing
PPT
Chap 5 Tree.ppt
PPTX
PRIM'S ALGORITHM
PPT
Lec 17 heap data structure
PPT
Graph coloring problem
Unit 5 internal sorting &amp; files
Algorithms Lecture 2: Analysis of Algorithms I
Coin Change Problem
Top down parsing
Chap 5 Tree.ppt
PRIM'S ALGORITHM
Lec 17 heap data structure
Graph coloring problem

What's hot (20)

PDF
Recursive algorithms
PPT
Bfs and dfs in data structure
PPT
Recurrences
PPT
Time complexity
PPTX
Stressen's matrix multiplication
PPT
Asymptotic notations
PDF
Design and analysis of algorithms
PPTX
Asymptotic Notation
PDF
All pairs shortest path algorithm
PPTX
Pumping lemma
PPTX
daa-unit-3-greedy method
PDF
Algorithms Lecture 7: Graph Algorithms
PPTX
Data structure - Graph
PDF
Parse Tree
PDF
Shortest path algorithms
PPTX
DAA-Floyd Warshall Algorithm.pptx
PPTX
Performance analysis and randamized agoritham
PPT
Unit 3 graph chapter6
PPTX
Knapsack Problem
Recursive algorithms
Bfs and dfs in data structure
Recurrences
Time complexity
Stressen's matrix multiplication
Asymptotic notations
Design and analysis of algorithms
Asymptotic Notation
All pairs shortest path algorithm
Pumping lemma
daa-unit-3-greedy method
Algorithms Lecture 7: Graph Algorithms
Data structure - Graph
Parse Tree
Shortest path algorithms
DAA-Floyd Warshall Algorithm.pptx
Performance analysis and randamized agoritham
Unit 3 graph chapter6
Knapsack Problem
Ad

Similar to Brute force (20)

PPTX
Brute Force and Divide & Conquer Technique
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
PDF
Daa chapter11
PPTX
Brute force method
PDF
Solution 3.
PPTX
Dmitrii Tihonkih - The Iterative Closest Points Algorithm and Affine Transfo...
PPTX
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
PPTX
Parallel algorithm in linear algebra
PDF
matlab functions
PDF
PPT
5.5 back tracking 02
PDF
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
DOC
algorithm Unit 4
PDF
Sienna 4 divideandconquer
PDF
Nbhm m. a. and m.sc. scholarship test 2007
PPTX
Matrix_PPT.pptx
PPTX
LU-17 Closest pair and convex hull using divide and conquer.pptx
PPTX
Introduction to MATLAB Programming for Engineers
PPTX
Dimension Reduction Introduction & PCA.pptx
PDF
project report(1)
Brute Force and Divide & Conquer Technique
Cs6402 design and analysis of algorithms may june 2016 answer key
Daa chapter11
Brute force method
Solution 3.
Dmitrii Tihonkih - The Iterative Closest Points Algorithm and Affine Transfo...
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Parallel algorithm in linear algebra
matlab functions
5.5 back tracking 02
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
algorithm Unit 4
Sienna 4 divideandconquer
Nbhm m. a. and m.sc. scholarship test 2007
Matrix_PPT.pptx
LU-17 Closest pair and convex hull using divide and conquer.pptx
Introduction to MATLAB Programming for Engineers
Dimension Reduction Introduction & PCA.pptx
project report(1)
Ad

Recently uploaded (20)

PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Basic Mud Logging Guide for educational purpose
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial disease of the cardiovascular and lymphatic systems
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
human mycosis Human fungal infections are called human mycosis..pptx
Classroom Observation Tools for Teachers
Renaissance Architecture: A Journey from Faith to Humanism
Abdominal Access Techniques with Prof. Dr. R K Mishra
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Basic Mud Logging Guide for educational purpose

Brute force

  • 1. UNIT II BRUTE FORCE V. Roseline, M.Sc., M.Phil., B.Ed., SET, NET,(Ph.D)., Assistant Professor, Sadakathullah Appa College, Tirunelveli.
  • 2. BRUTE FORCE  Brute force is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved.  Selection Sort, Bubble Sort, Sequential Search, String Matching, Depth- First Search and Breadth-First Search, Closest-Pair and Convex-Hull Problems can be solved by Brute Force.
  • 3.  Examples:  Computing an : a * a * a * … * a ( n times)  Computing n! : The n! can be computed as n*(n-1)* … *3*2*1  Multiplication of two matrices : C=AB  Searching a key from list of elements (Sequential search)  Advantages:  Brute force is applicable to a very wide variety of problems.  It is very useful for solving small size instances of a problem, even though it is inefficient.  The brute-force approach yields reasonable algorithms of at least some practical value with no limitation on instance size for sorting, searching, and string matching.
  • 4. Selection Sort  First scan the entire given 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, starting with the second element, to find the smallest among the last n − 1 elements and exchange it with the second element, putting the second smallest element in its final position in the sorted list.  Generally, on the ith pass through the list, which we number from 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 passes, the list is sorted.
  • 5. 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]
  • 6. | 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 Thus, selection sort is a Θ(n2) algorithm on all inputs.
  • 7. Bubble Sort  The bubble sorting algorithm is to compare adjacent elements of the list and exchange them if they are out of order.  By doing it repeatedly, we end up “bubbling up” the largest element to the last position on the list. The next pass bubbles up the second largest element, and so on, until after n − 1 passes the list is sorted.  Pass i (0 ≤ i ≤ n − 2) of bubble sort can be represented by the ? following: A0, . . . , Aj ↔ Aj+1, . . . , An−i−1 | An−i ≤ . . . ≤ An−1
  • 8. 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]
  • 11. CLOSEST-PAIR AND CONVEX-HULL PROBLEMS  We consider a straight forward approach of Brute Force to two well- known problems dealing with a finite set of points in the plane.  These problems are very useful in important applied areas like computational geometry and operations research.
  • 12. Closest-Pair Problem  The closest-pair problem finds the two closest points in a set of n points.  It is the simplest of a variety of problems in computational geometry.  Consider the two-dimensional case of the closest-pair problem. The points are specified in a standard fashion by their (x, y) coordinates and the distance between two points pi(xi, yi) and pj(xj, yj ) is the standard Euclidean distance. d(pi , pj) = √(xi − xj)2 + (yi − yj)2
  • 13. ALGORITHM BruteForceClosestPair(P) //Finds distance between two closest points in the plane by brute force //Input: A list P of n (n ≥ 2) points p1(x1, y1), . . . , pn(xn, yn) //Output: The distance between the closest pair of points for i ←1 to n − 1 do for j ←i + 1 to n do d ←min(d, sqrt((xi− xj )2 + (yi− yj )2)) return d
  • 14. The basic operation of the algorithm will be squaring a number. The number of times it will be executed can be computed as follows: Speeding up the innermost loop of the algorithm could only decrease the algorithm’s running time by a constant factor, but it cannot improve its asymptotic efficiency class.
  • 15. CONVEX-HULL PROBLEM  Convexity  A polygon Pis said to be convex if:Pis nonintersecting and for anytwo points p and q on the boundary of P, segment pq lies entirely inside P. convex nonconvex
  • 16.  Convex Set  A set of points (finite or infinite) in the plane is called convex if for any two points p and q in the set, the entire line segment with the endpoints at p and q belongs to the set.
  • 17.  Convex Hull  The convex hull of a set of points is defined as the smallest convex polygon, that encloses all of the points in the set. Convex means, that the polygon has no corner that is bent inwards. OR  The convex hull of a set S of points is the smallest convex set containing S.
  • 18.  If S is convex, its convex hull is obviously S itself.  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 not on the same line, its convex hull is the triangle with the vertices at the three points given; if the three points do lie on the same line, the convex hull is the line segment with its endpoints at the two points that are farthest apart.
  • 19.  Example of the convex hull for a larger set.
  • 20. Convex Hull Problem  The convex-hull problem is the problem of constructing the convex hull for a given set S of n points.  To solve it, we need to find the points that will serve as the vertices of the polygon.  Mathematicians call the vertices of such a polygon “extreme points.”  An extreme point of a convex set is a point of this set that is not a middle point of any line segment with endpoints in the set. For example, the extreme points of a triangle are its three vertices, the extreme points of a circle are all the points of its circumference, and the extreme points of the convex hull of the set of eight points in Figure are p1, p5, p6, p7, and p3.
  • 21. Algorithm  We can solve the convex-hull problem by brute-force manner.  The convex hull problem is one with no obvious algorithmic solution.  There is a simple but inefficient algorithm that is based on the following observation about line segments making up the boundary of a convex hull: a line segment connecting two points pi and pj of a set of n points is a part of the convex hull’s boundary if and only if all the other points of the set lie on the same side of the straight line through these two points. Repeating this test for every pair of points yields a list of line segments that make up the convex hull’s boundary.
  • 22.  Implementation of algorithm.  First, the straight line through two points (x1, y1), (x2, y2) in the coordinate plane can be defined by the equation ax + by = c,where a = y2 − y1, b = x1 − x2, c = x1y2 − y1x2.  Second, such a line divides the plane into two half-planes: for all the points in one of them, ax + by > c, while for all the points in the other, ax + by < c. (For the points on the line itself, of course, ax + by = c.) Thus, to check whether certain points lie on the same side of the line, we can simply check whether the expression ax + by − c has the same sign for each of these points. Time efficiency of this algorithm is in O(n3):