0% found this document useful (0 votes)
38 views

Chapter 3

The document discusses brute force algorithms and exhaustive search techniques. It provides examples of selection sort, bubble sort, string matching, polynomial evaluation, and the closest pair problem solved using brute force approaches. Brute force methods are simple to implement but often inefficient, evaluating all possible solutions via a systematic search. The traveling salesman problem is presented as a classic problem addressed through exhaustive search of all possible tours.

Uploaded by

Amanuel mergia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Chapter 3

The document discusses brute force algorithms and exhaustive search techniques. It provides examples of selection sort, bubble sort, string matching, polynomial evaluation, and the closest pair problem solved using brute force approaches. Brute force methods are simple to implement but often inefficient, evaluating all possible solutions via a systematic search. The traveling salesman problem is presented as a classic problem addressed through exhaustive search of all possible tours.

Uploaded by

Amanuel mergia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 3

Brute Force

Gedamu A.
[email protected]
Image processing and Computer vision SIG

Some of the sides are exported from different sources to clarify the topic

Computer Science and Engineering Program:DAA


Gedamu Alemu
2
Content
 Selection and Bubble sorting
 Sequential Searching and Brute-Force String Matching
 Closest-Pair and Convex –Hull problems by Brute Force
 Exhaustive Searching

Computer Science and Engineering Program:DAA


Gedamu Alemu
3

Introduction
Brute Force
 A straightforward approach:
 usually based directly on the problem’s statement
 definitions of the concepts involved
Examples:
1. Computing an (a > 0, n a nonnegative integer)= a*a*….*a

2. Computing n!

3. Multiplying two matrices

4. Searching for a key of a given value in a list

Computer Science and Engineering Program:DAA


Gedamu Alemu
5
Brute-Force Sorting Algorithm
 Selection Sort:
 Sort Scan the array to find its smallest element and swap it
with the first element.
 Then, starting with the second element,
 scan the elements to the right of it to find the smallest
among them and swap it with the second elements.
 Generally, on pass i (0  i  n-2), find the smallest element in
A[i..n-1] and swap it with A[i]:

A[0]  . . .  A[i-1] | A[i], . . . , A[min], . . ., A[n-1]


in their final positions
Example: 7 3 2 5

Computer Science and Engineering Program:DAA


Gedamu Alemu
Analysis of Selection Sort

Time efficiency: Θ(n^2)


Space efficiency: Θ(1), so in place
Stability:
yes

Computer Science and Engineering Program:DAA


Gedamu Alemu
7
Brute Force - Bubble Sort

 Another brute force application to the sorting problem


is to:
• compare adjacent elements of the list.
• and exchange them if they are out of order.

 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.

Computer Science and Engineering Program:DAA


Gedamu Alemu
8
Brute Force - Bubble Sort

Algorithm BubbleSort (A[0…n-1])


for i ← 0 to n-2 do
for j ← 0 to n-2-i do
if A[j]<A[j+1]
swap A[j] and A[j+1]

The number of times it is executed T(n) depends only on the array’s


size and is given by the following sum:

Computer Science and Engineering Program:DAA


Gedamu Alemu
Brute-Force String Matching
 pattern: a string of m characters to search for
 text: a (longer) string of n characters to search in
 problem: find a substring in the text that matches the pattern

Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
─ all characters are found to match (successful search); or
─ a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2

Computer Science and Engineering Program:DAA


Gedamu Alemu
Examples of Brute-Force String Matching

1. Pattern: 001011
Text: 10010101101001100101111010

2. Pattern: happy
Text: It is never too late to have a happy childhood.

Computer Science and Engineering Program:DAA


Gedamu Alemu
Pseudocode and Efficiency

Θ(mn) comparisons (in the worst case)


Time efficiency:
Why?

Computer Science and Engineering Program:DAA


Gedamu Alemu
Brute-Force Polynomial Evaluation

Problem: Find the value of polynomial


p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Brute-force algorithm
p  0.0

for i  n downto 0 do
power  1
for j  1 to i do //compute xi
power  power  x
p  p + a[i]  power

return p

Efficiency: Θ(n^2) multiplications

Computer Science and Engineering Program:DAA


Gedamu Alemu
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:

Better brute-force algorithm


p  a[0]
power  1
for i  1 to n do
power  power  x
p  p + a[i]  power
return p

Efficiency: Θ(n) multiplications

Horner’s Rule is another linear time method.

Computer Science and Engineering Program:DAA


Gedamu Alemu
Closest-Pair Problem

Find the two closest points in a set of n points (in the two-
dimensional Cartesian plane).

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.

Computer Science and Engineering Program:DAA


Gedamu Alemu
Closest-Pair Brute-Force Algorithm (cont.)

Efficiency: Θ(n^2) multiplications


How to make it faster? Using divide-and-conquer!

Computer Science and Engineering Program:DAA


Gedamu Alemu
Brute-Force Strengths and Weaknesses
 Strengths
 wide applicability
 simplicity
 yields reasonable algorithms for some important problems
(e.g., matrix multiplication, sorting, searching, string matching)

 Weaknesses
 rarely yields efficient algorithms
 some brute-force algorithms are unacceptably slow
 not as constructive as some other design techniques

Computer Science and Engineering Program:DAA


Gedamu Alemu
17
Exhaustive Search
 A brute force solution to a problem involving search for an
element with a special property
 usually among combinatorial objects such as permutations,
combinations, or subsets of a set.
Method:
 generate a list of all potential solutions to the problem in a systematic
manner

 evaluate potential solutions one by one, disqualifying infeasible ones


and, for an optimization problem, keeping track of the best one found
so far

 when search ends, announce the solution(s) found

Computer Science and Engineering Program:DAA


Gedamu Alemu
Example 1: Traveling 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 shortest Hamiltonian circuit in a weighted connected
graph
 Example:
2
a b
5 3
8 4

c 7 d
How do we represent a solution (Hamiltonian circuit)?
Computer Science and Engineering Program:DAA
Gedamu Alemu
TSP by Exhaustive Search

Tour Cost
a→b→c→d→a 2+3+7+5 = 17
a→b→d→c→a 2+4+7+8 = 21
a→c→b→d→a 8+3+4+5 = 20 2
a→c→d→b→a 8+7+4+2 = 21
a b
5 3
a→d→b→c→a 5+4+3+8 = 20 8 4
a→d→c→b→a 5+7+3+2 = 17

Efficiency: c 7 d
Θ((n-1)!)

Computer Science and Engineering Program:DAA


Gedamu Alemu
Example 2: Knapsack Problem

Given n items:
 weights: w1 w2 … wn
 values: v1 v2 … vn
 a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack

Example: Knapsack capacity W=16


item weight value
1 2 $20
2 5 $30
3 10 $50
4 5 $10

Computer Science and Engineering Program:DAA


Gedamu Alemu
Knapsack Problem by Exhaustive Search
Subset Total weight Total value
{1} 2 $20
{2} 5 $30
{3} 10 $50
{4} 5 $10
{1,2} 7 $50
{1,3} 12 $70
{1,4} 7 $30 Efficiency:
{2,3} 15 $80 Θ(2^n)
{2,4} 10 $40
{3,4} 15 $60
{1,2,3} 17 not feasible
{1,2,4} 12 $60
{1,3,4} 17 not feasible
{2,3,4} 20 not feasible
{1,2,3,4} 22 not feasible

 Each subset can be represented by a binary string (bit vector, Ch 5).

Computer Science and Engineering Program:DAA


Gedamu Alemu
Example 3: The Assignment Problem
 There are n people who need to be assigned to n jobs:
 one person per job.
 The cost of assigning person i to job j is C[i,j].
 Find an assignment that minimizes the total cost.

Job 1 Job 2 Job 3 Job 4


Person 1 9 2 7 8
Person 2 6 4 3 7
Person 3 5 8 1 8
Person 4 7 6 9 4

Algorithmic Plan: Generate all legitimate assignments, compute


their costs, and select the cheapest one.
How many assignments are there? n!
Computer Science and Engineering Program:DAA
Gedamu Alemu
Assignment Problem by Exhaustive Search
9 2 7 8
6 4 3 7
C=
5 8 1 8
7 6 9 4
Assignment (col.#s) Total Cost
1, 2, 3, 4 9+4+1+4=18
1, 2, 4, 3 9+4+8+9=30
1, 3, 2, 4 9+3+8+4=24
1, 3, 4, 2 9+3+8+6=26
1, 4, 2, 3 9+7+8+9=33
1, 4, 3, 2 9+7+1+6=23
etc.
(For this particular instance, the optimal assignment can be found by
exploiting the specific features of the number given. It is: )

2,1,3,
Computer Science and Engineering Program:DAA
4 Gedamu Alemu
Final Comments on Exhaustive Search
 Exhaustive-search algorithms run in a realistic amount of time only on
very small instances

 In some cases, there are much better alternatives!


 Euler circuits
 shortest paths
 assignment problem

 In many cases, exhaustive search or its variation is the only known way to
get exact solution

The Hungarian method runs in O(n^3) time.

Computer Science and Engineering Program:DAA


Gedamu Alemu
Graph Traversal

Many problems require processing all graph vertices (and edges)


in systematic fashion

Graph traversal algorithms:

 Depth-first search (DFS)

 Breadth-first search (BFS)

Computer Science and Engineering Program:DAA


Gedamu Alemu
Depth-First Search: (Brave Traversal)
Visits graph’s vertices by always moving away from last visited vertex
to an unvisited one,
backtracks if no adjacent unvisited vertex is available.

 Recursive or it uses a stack

Using Stack
 a vertex is pushed onto the stack when it’s reached for the first time
 a vertex is popped off the stack when it becomes a dead end, i.e., when
there is no adjacent unvisited vertex

Computer Science and Engineering Program:DAA


Gedamu Alemu
Algorithm

Computer Science and Engineering Program:DAA


Gedamu Alemu
Example: DFS traversal of undirected graph

a b c d

e f g h

DFS tree:

DFS traversal stack: 1 2 6 7

a b c d
abgcdh
abgcd
abgcd
abgc
abfe

e f g  h
abg
abf
abf
ab

ab

4 3 5 8

a

Red edges are tree edges and other edges are back
edges.

Computer Science and Engineering Program:DAA


Gedamu Alemu
Notes on DFS
 DFS can be implemented with graphs represented as:
 adjacency matrices: Θ(|V|2). Why?
 adjacency lists: Θ(|V|+|E|). Why?

 Yields two distinct ordering of vertices:


 order in which vertices are first encountered (pushed onto stack)
 order in which vertices become dead-ends (popped off stack)

 Applications:
 checking connectivity, finding connected components
 checking a cyclicity (if no back edges)

Computer Science and Engineering Program:DAA


Gedamu Alemu
The Problem

Finding paths from a vertex


to all other vertices with the
smallest number of edges

Computer Science and Engineering Program:DAA


Gedamu Alemu
Breadth First Search
 Visits graph vertices by moving across to all the neighbors of
the last visited vertex

 Instead of a stack, BFS uses a queue

 Similar to level-by-level tree traversal

 “Redraws” graph in tree-like fashion.

Computer Science and Engineering Program:DAA


Gedamu Alemu
Example of BFS traversal of undirected graph

a b c d

e f g h

BFS tree:

1 2 6 8
BFS traversal queue: a b c d

e f g h

3 4 5 7
bef
efg

hd
ch
fg

Red edges are tree edges


d
a

and white edges are cross


edges.
Computer Science and Engineering Program:DAA
Gedamu Alemu
Write an Algorithm for BFS Using a queue?

Computer Science and Engineering Program:DAA


Gedamu Alemu

You might also like