SlideShare a Scribd company logo
1 of 20
Download to read offline
1
Design and
Analysis of
Algorithm
Week 08
Algorithm Design Techniques
Brute-Force Algorithm
Knapsack Problem
Algorithm
design
Techniques
Brute-Force Algorithm
Introduction to Brute Force
Algorithm
“Data is the new oil” this is the new mantra that is ruling the global economy. We live in the digital world,
and every business revolves around data, which translates into profits and helps the industries stay ahead
of their competition. With the rapid digitization, an exponential increase in the app-based business
model, cyber-crimes is a constant threat. One such common activity that hackers perform is Brute force.
Brute Force is a trial and error approach where attackers use programs to try out various combinations to
break into any websites or systems. They use automated software to repetitively generate the User id
and passwords combinations until it eventually
generates the right combination.
Brute-Force Search
Brute force search is the most common search algorithm as it does not require any domain knowledge; all
that is required is a state description, legal operators, the initial state and the description of a goal state.
It does not improve the performance and completely relies on the computing power to try out possible
combinations.
Brute-Force Search
The brute force algorithm searches all the positions in the text between 0 and n-m, whether the
occurrence of the pattern starts there or not. After each attempt, it shifts the pattern to the right by
exactly 1 position. The time complexity of this algorithm is O(m*n). If we are searching for n characters in
a string of m characters, then it will take n*m tries.
Brute-Force Search
Let’s see a classic example of a travelling salesman to understand the algorithm in an easy manner.
Suppose a salesman needs to travel 10 different cities in a country, and he wants to determine the
shortest possible routes out of all the possible combinations. Here brute force algorithm simply calculates
the distance between all the cities and selects the shortest one.
Another example is to make an attempt to break the 5 digit password; then brute force may take up to
105
attempts to crack the code.
Closest Pair
Problem statement: To find out the two closest points
in a set of n points in the two-dimensional cartesian
plane. There is n number of scenarios where this
problem arises. A real-life example would be in an air
traffic control system where you have to monitor the
planes flying near to each other, and you have to find
out the safest minimum distance these planes should
maintain.
Convex Hull
Problem Statement: A convex hull
is the smallest polygon that
contains all the points. The
convex hull of a set s of the point
is the smallest convex polygon
containing s.
Convex Hull
The convex hull for this set of points is the convex polygon with vertices at P1, P5, P6, P7, P3.
A line segment P1 and Pn of a set of n points is a part of the convex hull if and only if all the other points
of the set lies inside the polygon boundary formed by the line segment.
Let’s relate it with the rubber band,
Point (x1, y1), (x2,y2) make the line ax+by = c
When a = y2-y1, b = x2-x1 and c = x1*y2 – x2*y1 and divides the plane by ax+by-c < 0 and ax+by-c > 0
So we need to check ax+by-c for the other points.
Brute force solve this problem with the time complexity of O(n 3
)
Exhaustive Search
or discrete problems in which there is no known efficient solution, it becomes necessary to test each and
every possible solution sequentially.
Exhaustive search is an activity to find out all the possible solutions to a problem in a systematic manner.
Let’s try to solve the Travelling salesman problem (TSP) using a Brute exhaustive search algorithm.
Exhaustive Search
Problem Statement: There are n cities that salesmen need to travel, he wants to find out the shortest
route covering all the cities.
We are considering Hamilton Circuit to solve this problem. If a circuit exists, then any point can start
vertices and end vertices. Once the start vertices are selected, then we only need the order for the
remaining vertices, i.e. n-1
Then there would be (n-1)! Possible combinations and the total cost for calculating the path would be
O(n). thus the total time complexity would be O(n!).
Knapsack problem
The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight
and a value, determine the number of each item to include in a collection so that the total weight is less
than or equal to a given limit and the total value is as large as possible.
Knapsack problem
Optimal Solution for the 0-1 Knapsack problem, knapsack capacity W = 50.
Values {60, 100, 120}
Weights {10, 20, 30}
Item = 3
Knapsack problem
Recursion by Brute-Force algorithm OR Exhaustive Search.
Approach: A simple solution is to consider all subsets of items and calculate the total weight and value of all
subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the
maximum value subset.
Optimal Sub-structure : To consider all subsets of items, there can be two cases for every item.
Case 1: The item is included in the optimal subset.
Case 2: The item is not included in the optimal set.
Knapsack problem
Therefore, the maximum value that can be obtained from ‘n’ items is the max of the following two values.
Maximum value obtained by n-1 items and W weight (excluding nth item).
Value of nth item plus maximum value obtained by n-1 items and W minus the weight of the nth item
(including nth item).
If the weight of ‘nth’ item is greater than ‘W’, then the nth item cannot be included and Case 1 is the only
possibility.
Knapsack problem
Item Calculated Weights Calculated Values
{} 0 0
{1} 10 60
{2} 20 100
{3} 30 120
{1,2} 30 160
{1,3} 40 180
{2,3} 50 220
{1,2,3} 60 280
Knapsack problem
Item Calculated Weights Calculated Values
{} 0 0
{1} 10 60
{2} 20 100
{3} 30 120
{1,2} 30 160
{1,3} 40 180
{2,3} 50 220
{1,2,3} 60 280
Knapsack problem
Complexity Analysis:
Time Complexity: O(2 n
).
As there are redundant subproblems.
Auxiliary Space :O(1).
As no extra data structure has been used for storing values.

More Related Content

Similar to whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf

Acm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAcm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAhmad Bashar Eter
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 
Discrete structure ch 3 short question's
Discrete structure ch 3 short question'sDiscrete structure ch 3 short question's
Discrete structure ch 3 short question'shammad463061
 
Skiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithmsSkiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithmszukun
 
Quantum inspired evolutionary algorithm for solving multiple travelling sales...
Quantum inspired evolutionary algorithm for solving multiple travelling sales...Quantum inspired evolutionary algorithm for solving multiple travelling sales...
Quantum inspired evolutionary algorithm for solving multiple travelling sales...eSAT Publishing House
 
Popular search algorithms
Popular search algorithmsPopular search algorithms
Popular search algorithmsMinakshi Atre
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptxssuser1fb3df
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptISHANAMRITSRIVASTAVA
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptfdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptKartikGupta711
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithmsNico Ludwig
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"22bcs058
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdfGOWTHAMR721887
 
Dynamic programmng2
Dynamic programmng2Dynamic programmng2
Dynamic programmng2debolina13
 

Similar to whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf (20)

Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
Acm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAcm aleppo cpc training fifth session
Acm aleppo cpc training fifth session
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Discrete structure ch 3 short question's
Discrete structure ch 3 short question'sDiscrete structure ch 3 short question's
Discrete structure ch 3 short question's
 
Unit 2
Unit 2Unit 2
Unit 2
 
Operations Research Project
Operations Research ProjectOperations Research Project
Operations Research Project
 
Skiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithmsSkiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithms
 
Quantum inspired evolutionary algorithm for solving multiple travelling sales...
Quantum inspired evolutionary algorithm for solving multiple travelling sales...Quantum inspired evolutionary algorithm for solving multiple travelling sales...
Quantum inspired evolutionary algorithm for solving multiple travelling sales...
 
Popular search algorithms
Popular search algorithmsPopular search algorithms
Popular search algorithms
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptfdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithms
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
 
Dynamic programmng2
Dynamic programmng2Dynamic programmng2
Dynamic programmng2
 
Ai unit-3
Ai unit-3Ai unit-3
Ai unit-3
 

Recently uploaded

Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Yantram Animation Studio Corporation
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdftbatkhuu1
 
Presentation.pptx about blender what is blender
Presentation.pptx about blender what is blenderPresentation.pptx about blender what is blender
Presentation.pptx about blender what is blenderUbaidurrehman997675
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...ranjana rawat
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...BarusRa
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightDelhi Call girls
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...Amil baba
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 
Cheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk GurgaonCheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk GurgaonDelhi Call girls
 

Recently uploaded (20)

Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
Presentation.pptx about blender what is blender
Presentation.pptx about blender what is blenderPresentation.pptx about blender what is blender
Presentation.pptx about blender what is blender
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
 
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
Cheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk GurgaonCheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Iffco Chowk Gurgaon
 

whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf

  • 1. 1
  • 3. Week 08 Algorithm Design Techniques Brute-Force Algorithm Knapsack Problem
  • 5. Introduction to Brute Force Algorithm “Data is the new oil” this is the new mantra that is ruling the global economy. We live in the digital world, and every business revolves around data, which translates into profits and helps the industries stay ahead of their competition. With the rapid digitization, an exponential increase in the app-based business model, cyber-crimes is a constant threat. One such common activity that hackers perform is Brute force. Brute Force is a trial and error approach where attackers use programs to try out various combinations to break into any websites or systems. They use automated software to repetitively generate the User id and passwords combinations until it eventually generates the right combination.
  • 6. Brute-Force Search Brute force search is the most common search algorithm as it does not require any domain knowledge; all that is required is a state description, legal operators, the initial state and the description of a goal state. It does not improve the performance and completely relies on the computing power to try out possible combinations.
  • 7. Brute-Force Search The brute force algorithm searches all the positions in the text between 0 and n-m, whether the occurrence of the pattern starts there or not. After each attempt, it shifts the pattern to the right by exactly 1 position. The time complexity of this algorithm is O(m*n). If we are searching for n characters in a string of m characters, then it will take n*m tries.
  • 8. Brute-Force Search Let’s see a classic example of a travelling salesman to understand the algorithm in an easy manner. Suppose a salesman needs to travel 10 different cities in a country, and he wants to determine the shortest possible routes out of all the possible combinations. Here brute force algorithm simply calculates the distance between all the cities and selects the shortest one. Another example is to make an attempt to break the 5 digit password; then brute force may take up to 105 attempts to crack the code.
  • 9. Closest Pair Problem statement: To find out the two closest points in a set of n points in the two-dimensional cartesian plane. There is n number of scenarios where this problem arises. A real-life example would be in an air traffic control system where you have to monitor the planes flying near to each other, and you have to find out the safest minimum distance these planes should maintain.
  • 10. Convex Hull Problem Statement: A convex hull is the smallest polygon that contains all the points. The convex hull of a set s of the point is the smallest convex polygon containing s.
  • 11. Convex Hull The convex hull for this set of points is the convex polygon with vertices at P1, P5, P6, P7, P3. A line segment P1 and Pn of a set of n points is a part of the convex hull if and only if all the other points of the set lies inside the polygon boundary formed by the line segment. Let’s relate it with the rubber band, Point (x1, y1), (x2,y2) make the line ax+by = c When a = y2-y1, b = x2-x1 and c = x1*y2 – x2*y1 and divides the plane by ax+by-c < 0 and ax+by-c > 0 So we need to check ax+by-c for the other points. Brute force solve this problem with the time complexity of O(n 3 )
  • 12. Exhaustive Search or discrete problems in which there is no known efficient solution, it becomes necessary to test each and every possible solution sequentially. Exhaustive search is an activity to find out all the possible solutions to a problem in a systematic manner. Let’s try to solve the Travelling salesman problem (TSP) using a Brute exhaustive search algorithm.
  • 13. Exhaustive Search Problem Statement: There are n cities that salesmen need to travel, he wants to find out the shortest route covering all the cities. We are considering Hamilton Circuit to solve this problem. If a circuit exists, then any point can start vertices and end vertices. Once the start vertices are selected, then we only need the order for the remaining vertices, i.e. n-1 Then there would be (n-1)! Possible combinations and the total cost for calculating the path would be O(n). thus the total time complexity would be O(n!).
  • 14. Knapsack problem The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
  • 15. Knapsack problem Optimal Solution for the 0-1 Knapsack problem, knapsack capacity W = 50. Values {60, 100, 120} Weights {10, 20, 30} Item = 3
  • 16. Knapsack problem Recursion by Brute-Force algorithm OR Exhaustive Search. Approach: A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset. Optimal Sub-structure : To consider all subsets of items, there can be two cases for every item. Case 1: The item is included in the optimal subset. Case 2: The item is not included in the optimal set.
  • 17. Knapsack problem Therefore, the maximum value that can be obtained from ‘n’ items is the max of the following two values. Maximum value obtained by n-1 items and W weight (excluding nth item). Value of nth item plus maximum value obtained by n-1 items and W minus the weight of the nth item (including nth item). If the weight of ‘nth’ item is greater than ‘W’, then the nth item cannot be included and Case 1 is the only possibility.
  • 18. Knapsack problem Item Calculated Weights Calculated Values {} 0 0 {1} 10 60 {2} 20 100 {3} 30 120 {1,2} 30 160 {1,3} 40 180 {2,3} 50 220 {1,2,3} 60 280
  • 19. Knapsack problem Item Calculated Weights Calculated Values {} 0 0 {1} 10 60 {2} 20 100 {3} 30 120 {1,2} 30 160 {1,3} 40 180 {2,3} 50 220 {1,2,3} 60 280
  • 20. Knapsack problem Complexity Analysis: Time Complexity: O(2 n ). As there are redundant subproblems. Auxiliary Space :O(1). As no extra data structure has been used for storing values.