0% found this document useful (0 votes)
132 views12 pages

Design and Analysis of Algorithms: Chapter 3 Brute Force

1. Brute force is the simplest algorithm design strategy that involves systematically enumerating all possible candidates for the solution and checking whether each candidate satisfies the problem's constraints. 2. The document describes brute force algorithms for sorting arrays like selection sort and bubble sort. It also covers brute force string matching and discusses solving problems like the knapsack problem and traveling salesman problem using exhaustive enumeration of all possible solutions. 3. Exhaustive search, a form of brute force, works by generating all possible solutions to a problem and checking each one against the problem's constraints to find the desired solution. It is applied in the document to combinatorial problems like the knapsack problem and traveling salesman problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views12 pages

Design and Analysis of Algorithms: Chapter 3 Brute Force

1. Brute force is the simplest algorithm design strategy that involves systematically enumerating all possible candidates for the solution and checking whether each candidate satisfies the problem's constraints. 2. The document describes brute force algorithms for sorting arrays like selection sort and bubble sort. It also covers brute force string matching and discusses solving problems like the knapsack problem and traveling salesman problem using exhaustive enumeration of all possible solutions. 3. Exhaustive search, a form of brute force, works by generating all possible solutions to a problem and checking each one against the problem's constraints to find the desired solution. It is applied in the document to combinatorial problems like the knapsack problem and traveling salesman problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Design and Analysis of Algorithms 201

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

Selection Sort - Algorithm


SelectionSort(A[0.n-1])
// Sorts given array using selection sort.
//Input: An array A[0n-1] orderable //elements.
//Output: An array A[0n-1] sorted in //ascending order.
for i0 to n-2 do
mini
for ji+1 to n-1 do
if A[j]<A[min]

1 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
minj
swap A[i] and A[min]
Selection Sort - Analysis
Input size : n
Basic operation: Comparison - A[j]<A[min]
n 2 n 1

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]

2 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
Bubble Sort - Analysis
Input size : n
Basic operation: Comparison - A[j]<A[min]
n 2 n 2 i

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

The number of key swaps depends on the input.


Worst case
Decreasing arrays
The number of key swaps is the same as the number of key comparisons.
n( n 1)
S worst ( n) C ( n)
( n 2 )
2
Sequential Search
The algorithm simply compares successive elements of a given list with a given
search key until either a match is encountered(successful search) or the list is
exhausted without finding a match(unsuccessful search).
A simple extra trick is often employed in implementing sequential search
If we append the search key to the end of the list, the search for the key will
have to be successful, and therefore we can eliminate a check for the lists end
on each iteration of the algorithm.
Sequential Search Algorithm
Sequential Search(A[0.n-1],k)
// Searches the array using Sequential Search //method.
//Input: An array A[0.n-1] of elements and a //key element k which is to be
//searched.
//Output: if found, returns the position where //the element found else returns -1.
3 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
A[n] k
i=0;
while A[i] k do
i=i+1
if i< n return i
else return -1
Sequential Search Analysis
The algorithm remains linear in both the worst-case and average-case scenario.
This algo provides an excellent illustration of the brute-force approach.
Characteristic strength is its simplicity
Characteristic weakness is its inferior efficiency.
Brute-Force String Matching
Given a string of n characters called the text and a string of m characters(mn)
called pattern, find a substring of the text that matches the pattern.
Brute-Force String Matching Algorithm
BruteForceStringMatching (T[0n-1],p[0m-1])
//Implements String matching
//Input: text array T of n characters, and //pattern array p of m characters.
//Output: Position of first character of //pattern if successful otherwise -1
for i0 to n-m-1 do
j0
while j<m and P[j]=T[i+j]
jj+1
if j=m
return i
return -1

4 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
Brute-Force String Matching Solving
Align the pattern against the first m characters of the text and start matching the
corresponding pairs of characters from left to right until either all the m pairs of
the characters match( then the algorithm can stop) or a mismatching pair is
encountered.
In the next case the pattern is shifted one position to the right and character
comparisons are resumed.
Starting again with the first character of the pattern and its counterpart in the
text.
We do the comparison up to n-m position. Beyond that position there are not

enough

characters that match the entire pattern.

Brute-Force String Matching Tracing


NOBODY

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

5 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
Sixth Attempt
NOBODY

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

This algorithm performs 12 character comparisons on this example.


Brute-Force String Matching Analysis
Every possible index is searched.
The highest possible index i = n m.
In the worst case m comparisons are made for a given value of i.
There are n-m+1 values for i.
The number of comparisons is m(n-m+1) mn

(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) =

6 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
n m 1 m 1

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.

7 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
It is named after the Irish Mathematician Sir William Rowan Hamilton(1805
1865).
The circuit can be defined as a sequence of n+1 adjacent vertices v i0,vi1,,,,,,vin-1.
The first vertex of the sequence is the same as the last one while the others
are distinct.

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

The number of permutations generated will be for n-1 cities .


The number of permutations can be cut by half.
The total number of permutations needed will be (n-1)!/2
The complexity of this problem is O(n-1)!
Knapsack problem

8 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
Given n items of known weights: w1 w2 wn and values: v1 v2 vn and a
knapsack of capacity W find the most valuable subset of the items that fit into
the knapsack.
The exhaustive search approach to this problem leads to
Consider all the subsets of the set of n items given
Compute the total weight of each subset in order to identify the feasible
subsets (i.e. not exceeding the knapsacks capacity)
Fnding a subset of the largest among them.
Knapsack problem Example
Maximum Capacity of the knapsack 10
The number of items 4
Item 1 Weight 4, Profit 42
Item 2 Weight 3, Profit 12
Item 3 Weight 4, Profit 40
Item 4 Weight 4, Profit 25
Subset

Total Weight

Total value

{0}

{1}

42

{2}

12

{3}

40

{4}

25

{1,2}

10

36

{1,3}

82

The exhaustive search approach leads


to generating all the subsets of the set of n items given
Computing the total weight of each subset to identify feasible subsets
Finding a subset of the largest value among them.
9 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0

Knapsack problem Analysis


The number of subsets of an n-element set is 2 n, the exhaustive search leads to
an algorithm that is (2n).
Assignment Problem
Given n jobs j1,j2,j3.jn and n persons p1, p2, p3, pn it is required to assign all n
jobs to all n persons with the constraint that one job is assigned to one person
and no jobs and no people should be left out and the cost involved in completing
all the jobs should be minimum.
Assignment Problem Tracing
The cost involved between ith person and jth job can be represented using a cost
matrix.
Example
J1

J2

J3

J4

P1 8

10

11

P2 9

10

P3 4

11

11

P4 10

The feasible solution to the problem can be represented as a n tuple using a


single dimensional array j1,j2,j3,jn where the index of the array represents the
job number.

10 A. Lekha, Senior Lecturer, Department of MCA, PESIT

Design and Analysis of Algorithms 201


0
1,2,3,4 23

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

Assignment problem Analysis


The total number of feasible solutions for n jobs and persons is n!.
The complexity of the algorithm is O(n!).
Questions
Write the Brute force string matching algorithm and analyze the same for Best,
Average and Worst cases.
Write the pseudo code for selection sort and bubble sort and compare their
orders of growth.
Explain bubble sort with an example.
Explain any two exhaustive search problems with examples.
Determine the number of character comparisons made by the Brute force
algorithm

in

searching

the

pattern

MCA

in

the

$IS$MORE$TO$LIFE$THAN$INCREASING$ITS$SPEED.

11 A. Lekha, Senior Lecturer, Department of MCA, PESIT

text

THERE

Design and Analysis of Algorithms 201


0

12 A. Lekha, Senior Lecturer, Department of MCA, PESIT

You might also like