Lecture 3
Lecture 3
CS3610
03 Brute-force algorithms
03 Brute-force algorithms
F(n):
if n<= 1
return n
else
return F(n-1) + F(n-2)
🡪 Recurrence relation:
A(n) = A(n-1) + A(n-2) + 1 for n>1
A(1) = 1, A(0) = 1
2. Non-homogenous
03 Brute-force algorithms
🡪 For example, if you have a small padlock with digits from 0-9. You forgot your
combination of 4 digits, but you don't want to buy another padlock. Since you
can't remember any of the digits, you have to use a brute force method to open
the lock.
SequentialSearch2(A[0..n], K)
A[n]←K
i ←0
while A[i] = K do
i ←i + 1
if i < n
T(n) = O(𝑛)
return i
T(n) = Ω(1)
else return −1
F2023 Analysis, Design of Algorithms 15
Brute-force algorithms – Bubble sort
0 1 2 3 4
Original list
Pass 1 55 10 26 5 12
Pass 1 10 55 26 5 12
Pass 1 10 26 55 5 12
Pass 1 10 26 5 55 12
Pass 1 10 26 5 55 12
Pass 2 10 26 5 12 55
Pass 2 10 26 5 12 55
Pass 2 10 5 26 12 55
Pass 2 10 5 12 26 55
Pass 3 10 5 12 26 55
Pass 3 5 10 12 26 55
Pass 3 5 10 12 26 55
Pass 4 5 10 12 26 55
No swap in pass 4
🡪 Stop
BubbleSort(A[0..n − 1])
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]
T(n) = O(𝑛2)
T(n) = Ω(n)
F2023 Analysis, Design of Algorithms 30
Brute-force algorithms – Selection sort
right position.
55 10 26 5 12
Original list
🡪 Repeatedly find the minimum element from unsorted part and putting it at the
right position.
🡪 Step 1: Find the minimum element and put it at position 0
3. Brute-force
Minimum = 5 10 26 55 12
position 0
🡪 Repeatedly find the minimum element from unsorted part and putting it at the
right position.
🡪 Step 2: Find the minimum element and put it at position 1
3. Brute-force
Minimum = 5 10 26 55 12
position 1
🡪 Repeatedly find the minimum element from unsorted part and putting it at the
right position.
🡪 Step 1: Find the minimum element and put it at position 2
3. Brute-force
Minimum = 5 10 12 55 26
position 4
🡪 Repeatedly find the minimum element from unsorted part and putting it at the
right position.
🡪 Step 1: Find the minimum element and put it at position 3
3. Brute-force
Minimum = 5 10 12 26 55
position 4
SelectionSort(A[0..n − 1])
for i ←0 to n − 2 do
min ← i
for j ←i+1 to n − 1 do
if A[j + 1]<A[min]
min ← j
swap A[i] and A[min]
T(n) = O(𝑛2)
T(n) = Ω(𝑛2)
F2023 Analysis, Design of Algorithms 36
Brute-force algorithms – String match
🡪 Output: The index of the first character in the text that starts a matching
substring or −1 if the search is unsuccessful
if j = m
return i
return −1
T(n) = O(m𝑛)
T(n) = Ω(𝑛)
F2023 Analysis, Design of Algorithms 38
Brute-force algorithms – Closest pair
3. Brute-force
🡪 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
BruteForceClosestPair(P)
3. Brute-force
d←∞
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
T(n) = O(𝑛2)
T(n) = Ω(𝑛2)
F2023 Analysis, Design of Algorithms 40
Summary