Design and Analysis of Algorithms: Chapter 3 Brute Force
Design and Analysis of Algorithms: Chapter 3 Brute Force
0
Chapter 3 Brute Force
Introduction
Brute force is the simplest of the design strategies.
It is a straight forward approach to solving a problem, usually directly based on
the problem statement and definitions of the concepts involved.
The force implied is that of a computer and not that of ones intellect.
It is one of the easiest to apply.
Example
Compute an for a given number a an d a nonnegative integer n.
By the definition of exponentiation an = a**a n times.
Selection Sort
Start by scanning the entire list to find the smallest element and exchange with
the first element.
Put the smallest element in the final position of the sorted list .i.e. a[0]
Scan the list starting with the second element, to find the smallest among the n1 elements and exchange it with the second element.
Put the second smallest element in its final position .i.e. a[1].
On the ith pass the algorithm searches for the smallest item among the last n-i
elements and swaps it with ai
C ( n)
i 0 j i 1
n2
C ( n) ( n 1 i 1 1)
i 0
C ( n)
n2
(n 1 i )
i 0
C ( n)
n( n 1)
2
Bubble Sort
In this technique the two successive items a[j] and a[j+1] exchanged whenever
a[j]>a[j+1].
By doing it repeatedly we end up bubbling up the largest element to the last
position on the list.
The next pass bubbles up the second largest element, and so on until, after n-1
passes, the list is sorted.
Bubble Sort - Algorithm
BubbleSort(A[0n-1])
//Sorts the array using bubble sort.
//Input: An array A[0.n-1] of orderable elements.
//Output: An Array A[0.n-1] in ascending //order.
for i0 to n-2 do
for j0 to n-2-i do
if A[j+1]<A[j]
swap A[j+1] and A[j]
C ( n)
i 0
1
j 0
n2
C ( n) (n 2 i 0 1)
i 0
C ( n)
n2
(n 1 i )
i 0
C ( n)
n(n 1)
( n 2 )
2
enough
N O T I C ED H I M
String N O T
First Attempt
NOBODY
N O T I C ED H I M
NOT
Second Attempt
NOBODY
N O T I C ED H I M
NOT
Third Attempt
NOBODY
N O T I C ED H I M
NOT
Fourth Attempt
NOBODY
N O T I C ED H I M
NOT
Fifth Attempt
NOBODY
N O T I C ED H I M
NOT
N O T I C ED H I M
NOT
Seventh Attempt
NOBODY
N O T I C ED H I M
NOT
Eight Attempt
NOBODY
N O T I C ED H I M
NOT
(mn)
Best case
It occurs when the pattern string to be searched is present in the beginning of
the text.
If m is the length of the pattern string, the number of comparisons required is m.
The complexity is given by (m).
Worst case
It happens when the pattern to be searched is present at the end of the text or
if the pattern is not present in the text.
Input size : n and m
Basic operation: j>m and p[j] = t[i+j]
C (n) =
1
i 0
j 0
n m 1
n m 1
m 1 0 1 m
i 0
i 0
n m 1
n m 1
i 0
i 0
m .1 m 1 m(n m 1 0 1)
m ( n m)
mn m 2
mn
Exhaustive Search
It is simply a brute force approach for combinatorial problems.
It suggests
Generate each and every element of the problems domain .
Select from them those that satisfy all constraints
Find the desired element among them.
Travelling Salesman Problem
Given n cities with known distances between each pair, find the shortest tour that
passes through all the cities exactly once before returning to the starting city.
Alternatively : find the shortest Hamiltonian circuit in a weighted connected
graph
TSP Exhaustive Search
The problem can be modeled by a weighted graph.
The graphs vertices represent the cities.
The edge weights specifying the distances.
The problem can then be stated as the problem of finding the shortest
Hamiltonian circuit of the graph.
A Hamiltonian circuit is defined as a cycle that passes through all the graph
exactly once.
Tour
Length
A-B-D-C-A
11
A-B-C-D-A
18
A-C-D-B-A
11
A-C-B-D-A
23
A-D-B-C-A
23
A-D-C-B-A
18
Total Weight
Total value
{0}
{1}
42
{2}
12
{3}
40
{4}
25
{1,2}
10
36
{1,3}
82
J2
J3
J4
P1 8
10
11
P2 9
10
P3 4
11
11
P4 10
2,4,1,3
27
4,2,1,3
29
1,2,4,3 34
2,4,3,1
26
4,2,3,1
29
1,3,2,4 28
3,1,2,4
37
4,3,1,2
26
1,3,4,2 30
3,1,4,2
39
4,3,2,1
34
1,4,2,3 37
3,2,1,4
38
1,4,3,2 28
3,2,4,1
38
2,1,3,4 22
3,4,1,2
33
2,1,4,3 33
3,4,2,1
41
2,3,1,4 18
4,1,2,3
39
2,3,4,1 28
4,1,3,2
30
in
searching
the
pattern
MCA
in
the
$IS$MORE$TO$LIFE$THAN$INCREASING$ITS$SPEED.
text
THERE