Uploaded by Achilles Joaquin Zubia

CMSC-142-Brute-Force-Notes

advertisement
04/24/2023
Brute Force
↳
Exhaustive search
and
straightforward design
Most
↳
solve
to
3
.
Definition-based algorithm
4
Worst/(word? )
((n)
2
t t (nY
ISAE
:
compare adjacent elements of the list
Lo di nagma-matter and in put
(HOF elements
basic operation
.
Bubble SOrt :
selection sort
:
Stop
,
multiplication
For matrix
Sequential search
input size :
it pass
t0(n)
n(n-1)
(m, n)
in the
are no swaps
n-1
=
=
Average/co
Computing
.
Best/(bestn)
problem
n!
2
.
a
Sort
checks if there
·
algorithm For computing qcd
consecutive checking
.
of algorithm
algorithms
brute Force
Examples OF
1
Approach
improved Bubble
out of order
in list)
.
position
last
to the
By doing
repeatedly
it
,
on the list
and
exchange
up"
.
,
E
-E (n
it they are
element "bubbles
the largest
The next pass bubbles up the second largest element
key comparison
them
pass of the Bubble sort where
0 >i
?
n-2
and
so on
.
The in
is represented as:
=
Ao ,
-x- (i +1)
-n(n-1)
2
(n-is
+x)
GO(nY
(n-1) +
=
+
(n -2)
Pseudocode
Algorithm
1/
(n
-
3) +
+
...
(X- [ry-23-1
Brute Force
·
(list implemented
array)
n-13)
..
Algorithm Sequential Search) (At 0
:
:
Output Array
A
TO n-7]
..
sorted in
nondecreasing
order
1/ implements sequential
ton-2 do
For J5 0 to n-2-i do
11
input An array A of
11
output the index
class Of this
input
basic operation
algorithm
and
the
I
n
2
and a
+
given list with
(successful
searchl
(unsuccessful Search)
a
given
or
the
.
(n)
as a
search
sentinel
key K
A[0 n-17 whose value is equal to
..
[
return
-
1
efficiency
(# of
:
Input size
ni-
k)
1
class
of the
:
Sequential (modified) Algorithm
basic operation
:
((n) depends
(n)
- &
i
What is the
(best)
=nin-i)
,
else return
->
,
search key
/ K do
i
cente
n]
0
it i
(n-2-i-0+)
match
1
-
While Ati
key comparison
:
a
element is Found
(#OF elements in list)
size :
elements
a
..
of the First element in
:
A[n]
Determine the number of times the basic operation is executed
search with
:
iF A[j+1] <Atj] Swap Alj] and Alj +1]
efficiency
key
a
Pseudocode For the modified sequential search:
..
50
until either a match is encountered
Brute-Force Searching
1/ Input An array At O n-17 of orderable elements
FOr :
compares successive elements of
:
:
11
Searching
algorithm
The
search
as
Final positions
.
(- [ y-33-1)
given array bubble sort
sorts a
Anie/Ani ?... Aper
Mist is exhausted without Finding
BubbleSort (A[0
:
...,
,
n-1 passes list is sorted
,
After
+
Algorithms
For bubble Sort
Es A ,
j
in their
-i
Brute-Force Sorting
A
...,
(wort
Ce
:
=
=
elements in list)
Key comparison
on
input
It
n +1
& (1)
t
(SAE
.
& (n)
algorithm?
Kor-1
it
no such
Brute-Force
String Matching
·
·
given
a
(m
n)
=
Brute-Force SMP
String Matching
input size: M
Problem
string
text
of a characters called the
Find
called the pattern ,
substring
a
string
and a
of m characters
of the text that matches the pattern
in other words, Findi the index of the leftmost character of the First matching
,
substring
to
ti
...
=
Po,
...
...,
its
titj
=
Pj
,
...,
tieme
...
........ ,
↓
·
string-matching algorithm
a
it all matches need to
(bestM, )
,
(mn)
n
Key comparison
titmy
...
:
m
(worst n)=
H
text I
=
0
m(n
:
-
m
-
0
+
1)
=M(n - m 1) t0(mn)
Pattern p
+
may continue working until the entire text
be Found
i
=i
Pm-
tny
(m)
t
m
=
,
in the text such that
ti
basic operation
:
is
exhausted
.
Brute-Force Algorithm
Step 1 Align pattern
at
.
Step 2 Moving
.
From
beginning
of text
.
left to right , compare each character of pattern
to
the
corresponding
character in text until
all characters are Found to match
a mismatch
.
Step 3
While pattern is not
pattern
one
is
detected
Found
position
and
to the
(successfull search)
;
or
.
the text is not yet
right
and repeat
exhausted realign
,
Step 2
input size n (degree
:
.
basic operation
M(M
:
p(x))
Multiplication
1
:
,
Di-
:
:
n
(n+1)
2
=
(nY
M(n)
Ein
:
(n -(i +
2
:
+
+)
i 1
=
:
22
2((n
:
:
2
:
(n-1)
-
x)
+
(n 2)
-
+
+
...
(1)
+
0)
il
2(n(n -7)
=
n(n x)
-
+
0(nY
Brute-Force Strategy
Strengths
wide applicability
·
↑
·
simplicity
yields
reasonable algorithms For some important problems (e g matrix multiplication)
.
weakness
~
rarely yields efficient algorithms
some
·
not as
brute-force algorithms
constructive as some
are
unacceptably
other
slow
design techniques
.
Download