0% found this document useful (0 votes)
12 views11 pages

Lec-6 BruteForceMethod

Chapter 3 focuses on brute force techniques for solving various problems, including sorting algorithms like bubble sort and selection sort, as well as exhaustive search problems like the Traveling Salesman Problem and the Knapsack Problem. It discusses the advantages and disadvantages of brute force methods, highlighting their simplicity and ease of implementation, but also their inefficiency in many cases. The chapter provides detailed algorithmic explanations and time complexity analyses for bubble sort and selection sort.

Uploaded by

zayn96165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Lec-6 BruteForceMethod

Chapter 3 focuses on brute force techniques for solving various problems, including sorting algorithms like bubble sort and selection sort, as well as exhaustive search problems like the Traveling Salesman Problem and the Knapsack Problem. It discusses the advantages and disadvantages of brute force methods, highlighting their simplicity and ease of implementation, but also their inefficiency in many cases. The chapter provides detailed algorithmic explanations and time complexity analyses for bubble sort and selection sort.

Uploaded by

zayn96165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
Chapter 3: Brute Force What are we studying in this chapter? ‘orce technique prute selection sort Bubble sort Seq ential search Brute force string matching To find the mode Exhaustive search + Traveling Salesman Problem « Knapsack problem » Assignment Problem - 6 hours given problem. In a straight forward approach there is no emphasis on the efficiency of the ue is “Just do it”. In this chapter let us is used to design the algorithms?” Along There are various approaches to solve a. and simple technique such as brute force, algorithm. The concept used in this techniq concentrate on “How the Brute Force method With these algorithms, let us obtain the time efficiency of all the algori . Now, let us se “What is brute foree method? What are the various problems that can be solved using this method?” Definition: The strait i given pro on: straight forward method of solving a given L Problems statement and definitions is called Brute Force ‘technique. This met i and, because Asie avert ad ‘© implement than a more sophisticated “times it can be more efficient. i . 7" example, the algorithms 2 us Gp of 2, numbers (C method, Matrix multiplication, addition Selection rap eguential search), alenorae string matching, Traveling sales man problem and ck problem can be solved using brute force technique: and disadvantages of algorithms | d disadvantages of this appros check * Now, + let us gee a lore 1S See “What are the advantages techn), oh y oy. chnique?” ‘The various advantages an z This method is applicable for wide variety of problems such as fin numbers, Computing power, Computing GCD and so on Simple and easy algorithms canbe written. For example, bubble s matrix multiplication etc . aay Can be used to judge more efficient alternative approaches to solve a p Rarely yields efficient algorithms Some brute force algorithms are unacceptably slow: For example, ¥ Not as constructive/creative as other design techniques such as dit This is one of the simplest sorting technique and most straightforwi In this brute-force technique, the adjacent elements in the list exchanged if they are out of order. Now, let us “Design the algorithm n: This is the simplest and easiest sorting technique. In this successive items A[i] and A[i+1] are exchanged whenever Afi] > consider the elements shown below: 40, 50, 30, 20, 10 The elements can be sorted as shown below: | 1" Pass i 2™ Pass a A[0] = 40 40 40 AU] = 30 30 A[2] = 301 20 20 A[3]= 20, 20 rst pass 40 is compared with 50 and the in order, \ 501s compared with 30 and they are exchange 2% ecange has ceed in the same manner, at the end of the fiom pees te ee tn : position. On each successive Pas: the ey Ee item S, the iter it ) the bottom and thus elements are saree es ae largest value \g order. » figure below shows the output of each pass. Out put of each pass st oe Esl 4h 40 30 20 10 = La wa 07 Note: Observe that at the end of each pass smaller values gradually “bubble” 230 ae f 7 a 10“ 30 30 their way upward to the top (like air BY ‘ Th ~ bubbles moving to surface of water) and AB]= ate 10 ) 40 40 (40 hence called bubble sort. 50 50 50 AdJ=107 50 nae ; i i parisons Now we concentrate on the designing aspects of this sorting technique, The cor that are performed in each pass are shown below: Passes >) Pass-1 Pass;2 Pass 3 Pass 4 O-1 Comparing | t t i=0 to 3 i=0 to 2 iO tol 4 = 341) *5-(14+1) 5-(2+1) os i) \ n- G+1) n-G+l) btesipst : re The index i = 0 to n-G+1) ori = Oto mat he index j = 1 10.4 orj = 1 to 5-1. Ing “ %, s the 3 F Patial code can be written as: for j=1ton-l for i=Oton-j-1 if ( Afi] > Ali+1]) ce exchange ( Afi], Ali+1] ) atte end if Sat end for end for So, the complete bubble sort algorithm is shown below: Algorithm BubbleSort(a[], n) //Purpose: Arrange the numbers in ascending order // Inputs : W n-— the number of items present in the table UW a~ the items to be sorted are present in the table //Output: W a: contains the sorted list forj <— 1ton-1do for i — 0 to n-j-Ido if ( afi] > afi+1] ) ~ temp — afi] afi] — afit1) S a{i+1] temp e. end if ae Advantages of bubble sort * or Disadvantages of bubble sort “teen < “eae slowly and hence itis not efficient, Say 4 eR Beni te elements are sorted, nl pases are A, en oa ec time efficiency of bubbl i ib tim y © sort algorithm can be obtained as shown below: arameter to be considered is n, which Tepresent size of the input es ' represent size of the inpu basic operation is the stat “if ali] >= aig 17"; by se tement if ali] >= afi+1) in the innermost for loop number of comparisons depends on the value of. 7 n and the number of times vops are executed. The total number of times the basi ion i ined as shown below: ie is anon ia erecuted rj<1ton-ldo fori — 0 ton-j-ldo if ( a{i] > afi+1] ) // Perform n-1 passes //'To compare items in each pass i // If out of order exchange adjacent elements v a Y nfl om=5 His = Sn-j-1-041 (Note: upper bound — lower bound + 1) =Yn-j (n-l ) + (n-2) +......3 +2 +1 (After substituting for j from 1 to n-1) (n-I)n _ wen : 2° 2 en age (nln Note: Whether the elements are sorted or not, this algorithm requires aa J comparisons. So, the best case and worst case time efficiency remains same. So, let us express using @ notation. : re = i ‘isfied is: Step 4: Letus express f(n) using nee The constraint to be satisfied i: cg) < fn) S$ @* g(n) form2 no Vy ¥y vy : xf Pos won < 1* n° forn2 2 4 2 _ wag iti lis clear from the above relations that c1 = 1/4, ¢2= 1, no = 2, g(n) =n". So, by debi fen) € O(n" fn) € O(gin)) ie. of bubble sort = O(n") Sa, time complexi not sorted and go for the next pass. ts are already sorted?” In gorithm has to perform all (1 “What happens wher Let us see even if the elements are sorted, the al total of n(n-1)/2 comparisons. this function, it is required to. he current pass. In any speci need to go for the next pass. *: To improve the efficiency of are already in ascending order elements are already sorted, there is no by using a flag and setting flag to 0 just before the ;” pass. In exchange is made, set flag to 1. After” pass is over. check for fl 0, it means that items are already sorted and we terminate the loop. fici bble sort algorithi BubbleSort(a{], n) To arrange the numbers in ascending order I n—the number of items present in the table MW a—contains unsorted elements WI a— Contains the sorted elements forj<— 1 ton-1do flag — 0 for i< Oton-j-ldo if ( afi] > afi+1} ) temp < afi] ali] < afi+ 1} a{i+1] < temp flag — 1; // Exchange has taken pla end if end for if (flag = 0) break; // If no exchange, the i Analysis The time efficiency of efficient bubble sort Step 1: The parameter to be considered is n which rhe basic operation in this algorithm is the st the items are already sorted, the basic oj = ‘sic Operatic ji case, sorting requires all the passes ‘Peration is executed only in. pass 1. In the st case time efficiencies, nalysis: The best_case occurs when the elem { one pass is required to check whether the vf times the basic operation is executed can be obtained a ee ie hie JZ fs ; fori — 0 to n-j-Ido Note: Replace j by 1: If all the items are sorted if ( afi] > afi+1]) Vv fin) =)1 <7 =n-2-0+1 =n-1 Thus, f(n) = n-1. In the best case, time efficiency is expressed using Q-notation. The constraint to be satisfied f(n) 2 ¢* g(n) forn2 go yovy v 5 2 ie, nl 21* 1 forn20 |tis clear from the above relations that c = 1, g(n) = n and no = 0. So, by definition f(n) € Q (n) the best case is given by Q(n) Worst case analysis: The total number of times the basic operation is executed is obtained by considering alll the passes as shown below: fi j - 1 passes <1ton-1do 1) Perform n: : items in each pass for i < 0 ton-j-1do 1 To compare items ; e nc ae afi+1}) //1 out of order exchange adjacent element voy On) =§* vi < il te at bound — bound + 1) Dn-j-1-041 (Note: upper ie OS 1 z ma The worst case time efficiency is expressed using O-notation, be satisfied is: ” f(r)

You might also like