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 (20)

9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
LINEAR BOUNDED AUTOMATA (LBA).pptx
LINEAR BOUNDED AUTOMATA (LBA).pptxLINEAR BOUNDED AUTOMATA (LBA).pptx
LINEAR BOUNDED AUTOMATA (LBA).pptx
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Red black tree
Red black treeRed black tree
Red black tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Alpha-beta pruning (Artificial Intelligence)
Alpha-beta pruning (Artificial Intelligence)Alpha-beta pruning (Artificial Intelligence)
Alpha-beta pruning (Artificial Intelligence)
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Mealy moore machine model
Mealy moore machine modelMealy moore machine model
Mealy moore machine model
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Topological sort
Topological sortTopological sort
Topological sort
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 

Similar to Brute force

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 keyappasami
 
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...AIST
 
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeksBeginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeksJinTaek Seo
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
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 COMPUTERTarun Kumar
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
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 MD Kutubuddin Sardar
 
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.pptxAKumaraGuru
 
Dimension Reduction Introduction & PCA.pptx
Dimension Reduction Introduction & PCA.pptxDimension Reduction Introduction & PCA.pptx
Dimension Reduction Introduction & PCA.pptxRohanBorgalli
 
MC0082 –Theory of Computer Science
MC0082 –Theory of Computer ScienceMC0082 –Theory of Computer Science
MC0082 –Theory of Computer ScienceAravind NC
 
Math vocabulary A-Z
Math vocabulary A-ZMath vocabulary A-Z
Math vocabulary A-Zfgeasland
 

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
 
Brute force method
Brute force methodBrute force method
Brute force method
 
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
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

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):