-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
1/20
Design and Analysis of Algorithm
Br u t e Fo r ce A l go r i t hm
D esign Tech n iq u e
c Definition of Brute Force algorithm designtechnique
c Importance of Brute Force algorithm designtechnique
c Application of Brute Force algorithm designtechnique in the following:
Brute Force Algorithm Design Technique * Property of STI
Page 1 of 20
q Bubble sort
q Sequential search
q String matching
q Exhaustive search
c Sample problem using the application of
Exhaustive search
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
2/20
Design and Analysis of Algorithm
Wh at i s B r u t e Fo r ce
A l go r i t h m D esi gnTechn ique?
c a straight forward approach to solving a problem,usually directly based on the problem statement
anddefinition of theconcepts involved. [LEV07]
c a simple algorithm design technique which worksthrough every possible answer until the correctone is found
Brute Force Algorithm Design Technique * Property of STI
Page 2 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
3/20
Design and Analysis of Algorithm
Wh at i s B r u t e Fo r ce
A l go r i t h m D esi gnTechn ique?
c Sample Application Problem:
q Finding all of the factors for number 625
c Possible Solut ion to the Problem:
q Divide every number up to 624 into 625
q For each number without remainders, the
Brute Force Algorithm Design Technique * Property of STI
Page 3 of 20
.
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
4/20
Design and Analysis of Algorithm
Wh at i s B r u t e Fo r ce
A l go r i t h m D esi gnTechn ique?
c Importance of using Brute Force algorithmdesign technique
q Always return the correct result
q Useful for testing the accuracy of fasteralgorithms
q Sometimes a particular problem can be solvedso quickly with a brute force method that it
Brute Force Algorithm Design Technique * Property of STI
Page 4 of 20
oesn ma e sense o was e me evs ng amore elegant solution.
q Applicable to a very wide variety of problems
• sorting, searching, string matching.
q It yields reasonable algorithms of at leastsome practical value with no limitation on
instance size.
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
5/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Sel ect i o n So r t
c Sorting
q data are arranged in according to their values
c Selection sort:
q the smallest value in the unsorted portion of alist is selected and placed at the end of thesorted portion of the list
Brute Force Algorithm Design Technique * Property of STI
Page 5 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
6/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Sel ect i o n So r t
c Pseudo code for Selection sort algorithm
Brute Force Algorithm Design Technique * Property of STI
Page 6 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
7/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Sel ect i o n So r t
c Example of Selection sort algorithm
q Given the original unsorted list:
q Solution:
24 79 46 9 33 57
Brute Force Algorithm Design Technique * Property of STI
Page 7 of 20
After Pass 1After Pass 1
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
8/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Sel ect i o n So r t
c Analysis of algorithm of Selection sort using theBrute Force approach
q the algorithm’s basic operation is the keycomparison List [ j] < List [min ]
q the number of times it is executed dependsonly on the array’s size
2
Brute Force Algorithm Design Technique * Property of STI
Page 8 of 20
q the number of key swaps is only O(n) or, moreprecisely,n -1
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
9/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Bu b b l e So r t
c Bubble Sort
q compares the adjacent elements of the list
and swap if elements are out of order
Brute Force Algorithm Design Technique * Property of STI
Page 9 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
10/20
Design and Analysis of Algorithm
c Pseudo code for Bubble sort algorithm
B r u t e Fo r ce A l go r i t h m
i n Bu b b l e So r t
Brute Force Algorithm Design Technique * Property of STI
Page 10 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
11/20
Design and Analysis of Algorithm
c Example of Bubble sort algorithm
q Given the original unsorted list:
q Solution:
B r u t e Fo r ce A l go r i t h m
i n Bu b b l e So r t
Brute Force Algorithm Design Technique * Property of STI
Page 11 of 20
After Pass 1
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
12/20
Design and Analysis of Algorithm
c Analysis of algorithm of Bubble sort using theBrute Force approach
q the outer loop runs n times and the onlycomplexity in this analysis in the inner loop
q the number of key comparisons for the bubblesort version given above is the same for allarra s of size n
B r u t e Fo r ce A l go r i t h m
i n Bu b b l e So r t
Brute Force Algorithm Design Technique * Property of STI
Page 12 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
13/20
Design and Analysis of Algorithm
c Analysis of algorithm of Bubble sort using theBrute Force approach
q The number of key swaps depends on theinput.
B r u t e Fo r ce A l go r i t h m
i n Bu b b l e So r t
Brute Force Algorithm Design Technique * Property of STI
Page 13 of 20
q For the worst case of decreasing arrays, it isthe same as the number of key comparisons.
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
14/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Seq u en t i a l Sear ch
c Sequential search
q compares successive elements of a given list
with a given search key until either a match isencountered or the list is exhausted withoutfinding a match
c Pseudo code for Sequential search algorithm
Brute Force Algorithm Design Technique * Property of STI
Page 14 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
15/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Seq u en t i a l Sear ch
c Example of Sequential search algorithm
q Given the original unsorted list:
q Solution:
Brute Force Algorithm Design Technique * Property of STI
Page 15 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
16/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n St r i n g M at ch i n g
c String matching
q process of finding the exact copy of pattern(s)
in the given text
c Pseudo code for Pattern matching algorithm
Brute Force Algorithm Design Technique * Property of STI
Page 16 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
17/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n St r i n g M at ch i n g
c Example of Pattern matching algorithm
q Given a string of n characters, find a substring
of the text that matches the pattern, that is tofind i—the index of the leftmost character ofthe first matching substring in the text—suchthat:
Brute Force Algorithm Design Technique * Property of STI
Page 17 of 20
Solution:
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
18/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n St r i n g M at ch i n g
c Analysis of algorithm of Pattern matching usingthe Brute Force approach
q the algorithm shifts the pattern almost alwaysafter a single character comparison
q the algorithm may have to make all mcomparisons before shifting the pattern, andthis can ha en for each of the n -m + 1 tries
Brute Force Algorithm Design Technique * Property of STI
Page 18 of 20
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
19/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h m
i n Exh au st i ve sear ch
c Exhaustive search:
q is a brute-force approach to combinatorial
problem
c Example of Exhaustive search
q Traveling Salesman Problem
• find the shortest tour through a given set of n
Brute Force Algorithm Design Technique * Property of STI
Page 19 of 20
returning to the city where it started
• in what order should he visit them to minimizethe distance travelled?
-
8/21/2019 MELJUN CORTES ALGORITHM Brute Force Algorithm Design Technique_II
20/20
Design and Analysis of Algorithm
B r u t e Fo r ce A l go r i t h mi n Exh au st i ve Sear ch
c Determining the tour and the total cost asillustrated in the table below:
Brute Force Algorithm Design Technique * Property of STI