Sie sind auf Seite 1von 7

Brute Force

Brute Force: A straightforward approach usually


based on problem statement and definitions
Examples: Which of the following algorithms are
based on the brute-force approach?
1.
2.
3.
4.
5.
6.
7.

Computing 1 + 2 + + n by adding once number at a time.


Euclids algorithm for GCD
Consecutive integer checking algorithm for GCD.
Sequential search
Binary search
Definition-based matrix multiplication
Selection sort & bubble sort

11/14/2014

Brute force strengths and


weaknesses (1)

Strengths:

applicable to a wide variety of problems


simple
yields standard algorithms for simple computational tasks

sum/product of n numbers
finding max/min in a list

yields reasonable algorithms for some important problems

searching
string matching
matrix multiplication

11/14/2014

Brute force strengths and


weaknesses (2)

Weaknesses:

rarely yields efficient algorithms


some brute force algorithms unacceptably slow
not as creative as some other design techniques

11/14/2014

Selection Sort
Algorithm SelectionSort(A[0..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n 2 do
min i
for j i + 1 to n 1 do
if A[j] < A[min]
C(n)=(n-1)(n)/2
min j
(n2)
swap A[i] and A[min]
11/14/2014

Bubble Sort

(bubble-up from left to right)

Algorithm BubbleSort(A[0..n-1])
//The algorithm sorts array A[0..n-1] by bubble sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n 2 do //bubble up the ith largest element
for j 0 to n-2-i do //j: the index of the left element in each pair
if A[j] > A[j+1]
C(n)=(n-1)(n)/2
swap A[j] and A[j+1]
(n2)

11/14/2014

Bubble Sort

(bubble-up from right to left)

Algorithm BubbleSort(A[0..n-1])
//The algorithm sorts array A[0..n-1] by bubble sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n 2 do //bubble up the ith element
for j n-1 to i+1 do
if A[j] < A[j-1]
swap A[j] and A[j-1]

11/14/2014

Sequential Search

Algorithm SequentialSearch(A[0..n-1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n-1] and a search key K
//Output: Returns the index of the first element of A that matches K
or 1 if there are no matching elements
i 0
Algorithm SequentialSearch2(A[0..n], K)
algorithm implements sequential search with a search
while i < n and A[i] K do //The
key as a sentinel
i i+ 1
A[n] = K
i 0
if i < n
while A[i] K do
return i
C(n)=n
ii+1
else
(n)
if i < n
return -1
return i
else
11/14/2014

return -1

Brute Force Polynomial Evaluation

Problem: Find the value of polynomial


p(x) = anxn + an-1xn-1 + + a1x1 + a0 at a point x = x0
Algorithm BruteForcePoly (a[0..n], x)
//computes the value of polynomial at a given point x by the highest-tolowest
term brute-force algorithm.
//Input: Array a[0..n] of the coefficients of a polynomial of degree n, stored
from the lowest to the highest and a number x

//Output: the value of the polynomial at the point x

P0
for i 1 to n do

power 1 // calculatingx i
for j 1 to i do
power power * x

Efficiency?

p p + a[i] * power

return p

11/14/2014

Polynomial Evaluation: Improvement


We can do better by evaluating from right to left
Algorithm LinearPoly (a[0..n], x)
//computes the value of polynomial at a given point x by the lowestto-highest term linear algorithm.
//Input: Array a[0..n] of the coefficients of a polynomial of degree n,
stored from the lowest to the highest and a number x
//Output: the value of the polynomial at the point x

P a[0]
power 1
for i 1 to n do
power power * x // calculating x i
p p + a[i] * power

Efficiency?

return p
11/14/2014

String Matching (1)

The problem
Given:

A text: a (long) string of n characters to search in


A pattern: a string of m characters (m<=n) to search for

Objective:

An example:

find a substring of the text that matches the pattern

Pattern: name
Text: My name is Tom.

Precisely: we need to find i, the index of the leftmost

character of the first matching substring in the text, such that


T[0] . T[i] . T[i+j] . T[i+m-1] . T[n-1]

11/14/2014

P[0] . P[j] . P[m-1]

10

Brute Force Algorithm


1.

2.

Align pattern against the first m characters of the


text
moving from left to right, compare each character
of pattern to the corresponding character in text
until

3.

all characters are found to match (successful search); or


a mismatch is detected

while the pattern is not found and the text is not


yet exhausted, shift pattern one position to the
right and repeat step 2.

Question: What is the last position that can still be the beginning
of a matching substring ?(assuming the texts positions are
indexed from 0 to n-1)
11/14/2014

1.

2.

11

Brute Force String Matching Examples:


Pattern: name
Text: My name is Tom.
Pattern: 0001
Text: 00000000000000000000 (totally 20 0s)

Number of comparisons:

11/14/2014

12

Brute-Force String Matching Algorithm


Algorithm BruteForceStringMatch(T[0..n-1], P[0..m-1])
//The algorithm implements brute-force string matching
//Input: An array T[0..n-1] of n characters representing a text; an array
P[0..m-1] of m characters representing a pattern.
//Output: The position of the first character in the text that starts the first
matching substring if the search is successful and 1 otherwise
for i 0 to n m do //totally n-m+1 entries
j0
//j: the index for the pattern
while j < m and P[j] = T[ i+j ] do
j=j+1
if j = m return i //the corresponding text for the current entry is exhausted.
return -1
11/14/2014

13

Closest-Pair Problem

Find the two closest points in a set of n points (in the


two-dimensional Cartesian plane).
Points in question can represent such physical objects
as:

airplanes
post offices
statistical samples
DNA sequences, and so on

Brute-force algorithm
Compute the distance between every pair of distinct
points and return the indexes of the points for which the
distance is the smallest.
11/14/2014

14

Closest-Pair Brute-Force Algorithm (cont.)

Efficiency:
How to make it faster?
11/14/2014

15

Exhaustive Search

A combinatorial problem is one that asks to find a combinatorial object


such as a permutation, a combination, or a subset that satisfies certain
constraints and has some desired property (e.g., maximizes a value or
minimizes a cost).

E.g., traveling salesman problem, graph coloring problem (assign the smallest
number of colors to vertices of a graph so that no two adjacent vertices are the
same color.)

Exhaustive search is a brute force solution to combinatorial problems.

Method:

construct a way of listing all potential solutions to the problem in a systematic


manner
all solutions are eventually listed
no solution is repeated
Evaluate solutions one by one, perhaps disqualifying infeasible ones and keeping
track of the best one found so far
When search ends, announce the winner

11/14/2014

16

Example 1: Traveling Salesman Problem


(TSP)

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 shortest Hamiltonian circuit in a weighted
connected graph.
Hamiltonian Circuit: a cycle that passes through all
the vertices of the graph exactly once.

Example:

b
5

3
4

11/14/2014

17

Traveling Salesman by Exhaustive Search


Tour
abcda
abdca
acbda
acdba
adbca
adcba

2+3+7+5
2+4+7+8
8+3+4+5
8+7+4+2
5+4+3+8
5+7+3+2

b
5

3
4

c
11/14/2014

Cost
= 17
= 21
= 20
= 21
= 20
= 17

Efficiency: How many tours do we need to generate for an


exhaustive search?
(n-1)!
The problem is to find a sequence of n+1 adjacent vertices,
where the first equals to the last and the other n-1 are distinct.
Without loss of generality, assume that the circuit starts and
end with a specific node.
We can get the tour by generating all permutations of the n-1
intermediate cities, compute the length, and find the smallest.
18

Example 2: Knapsack Problem


Given n items:
weights: w1 w2 wn
values: v1 v2 vn
a knapsack of capacity W

Find the most valuable subset of the


items that fit into the knapsack

11/14/2014

19

Knapsack by Exhaustive Search


Subset

Total weight

{1}
{2}
{3}
{4}
{1,2}
{1,3}
{1,4}
{2,3}
{2,4}
{3,4}
{1,2,3}
{1,2,4}
{1,3,4}
{2,3,4}
{1,2,3,4}

2
5
10
5
7
12
7
15
10
15
17
12
17
20
22

Total value
$20
$30
$50
$10
$50
$70
$30
$80
$40
$60
not feasible
$60
not feasible
not feasible
not feasible

Example:Knapsack capacity W=16

item

weight

value

1.

$20

2.

$30

3.

10

$50

4.

$10

List all the subsets


compute the total weight and value for each subset.
find the subset with the largest value and W
weight.

Efficiency: how many subsets are


n
generated for an exhaustive search? 2 -1

11/14/2014

The traveling salesman problem and knapsack


problem are the best-known examples of NP-hard
problems.

20

No Polynomial time algorithm is known for any NP hard


problems.
It hasnt been proved that polynomial algorithms to solve
these problems do not exist.

Exhaustive search only works for NP-hard problems


of very small sizes.

11/14/2014

21

Das könnte Ihnen auch gefallen