SlideShare a Scribd company logo
1 of 22
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]
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

What's hot

Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...
Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...
Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...
Simplilearn
 
Travelling salesman dynamic programming
Travelling salesman dynamic programmingTravelling salesman dynamic programming
Travelling salesman dynamic programming
maharajdey
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 

What's hot (20)

Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Brute force method
Brute force methodBrute force method
Brute force method
 
Maximum sum subarray
Maximum sum subarrayMaximum sum subarray
Maximum sum subarray
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint set
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Regularization in deep learning
Regularization in deep learningRegularization in deep learning
Regularization in deep learning
 
Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...
Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...
Deep Learning Frameworks 2019 | Which Deep Learning Framework To Use | Deep L...
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithm
 
NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
 
Travelling salesman dynamic programming
Travelling salesman dynamic programmingTravelling salesman dynamic programming
Travelling salesman dynamic programming
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Fp growth
Fp growthFp growth
Fp growth
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 

Similar to Brute force

Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeksBeginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
JinTaek Seo
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
LU-17 Closest pair and convex hull using divide and conquer.pptx
LU-17 Closest pair and convex hull using divide and conquer.pptxLU-17 Closest pair and convex hull using divide and conquer.pptx
LU-17 Closest pair and convex hull using divide and conquer.pptx
AKumaraGuru
 
Dimension Reduction Introduction & PCA.pptx
Dimension Reduction Introduction & PCA.pptxDimension Reduction Introduction & PCA.pptx
Dimension Reduction Introduction & PCA.pptx
RohanBorgalli
 

Similar to Brute force (20)

Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Daa chapter11
Daa chapter11Daa chapter11
Daa chapter11
 
Solution 3.
Solution 3.Solution 3.
Solution 3.
 
Dmitrii Tihonkih - The Iterative Closest Points Algorithm and Affine Transfo...
Dmitrii Tihonkih - The Iterative Closest Points Algorithm and  Affine Transfo...Dmitrii Tihonkih - The Iterative Closest Points Algorithm and  Affine Transfo...
Dmitrii Tihonkih - The Iterative Closest Points Algorithm and Affine Transfo...
 
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeksBeginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
 
Parallel algorithm in linear algebra
Parallel algorithm in linear algebraParallel algorithm in linear algebra
Parallel algorithm in linear algebra
 
matlab functions
 matlab functions  matlab functions
matlab functions
 
Ada notes
Ada notesAda notes
Ada notes
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTERSYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Nbhm m. a. and m.sc. scholarship test 2007
Nbhm m. a. and m.sc. scholarship test 2007 Nbhm m. a. and m.sc. scholarship test 2007
Nbhm m. a. and m.sc. scholarship test 2007
 
Matrix_PPT.pptx
Matrix_PPT.pptxMatrix_PPT.pptx
Matrix_PPT.pptx
 
LU-17 Closest pair and convex hull using divide and conquer.pptx
LU-17 Closest pair and convex hull using divide and conquer.pptxLU-17 Closest pair and convex hull using divide and conquer.pptx
LU-17 Closest pair and convex hull using divide and conquer.pptx
 
Dimension Reduction Introduction & PCA.pptx
Dimension Reduction Introduction & PCA.pptxDimension Reduction Introduction & PCA.pptx
Dimension Reduction Introduction & PCA.pptx
 
project report(1)
project report(1)project report(1)
project report(1)
 
MC0082 –Theory of Computer Science
MC0082 –Theory of Computer ScienceMC0082 –Theory of Computer Science
MC0082 –Theory of Computer Science
 
Math vocabulary A-Z
Math vocabulary A-ZMath vocabulary A-Z
Math vocabulary A-Z
 
Relative superior mandelbrot and julia sets for integer and non integer values
Relative superior mandelbrot and julia sets for integer and non integer valuesRelative superior mandelbrot and julia sets for integer and non integer values
Relative superior mandelbrot and julia sets for integer and non integer values
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

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]
  • 9.
  • 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):