Lecture Notes 4 Brute Force Algorithms
Lecture Notes 4 Brute Force Algorithms
Lecture Notes 4 Brute Force Algorithms
Analysis of Algorithms
Lecture Notes 4
Brute Force Algorithms
1
Brute Force
Definition: A straightforward approach, usually based directly
on the problem’s statement and definitions of the
concepts involved
2
Brute Force
• Easiest to apply
• ‘Force’ is used to emphasize the power of a
computer, not one’s intellect.
• Just do it!
3
Brute Force
• Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
4
Brute-Force Sorting Algorithm
Selection 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 element.
• Generally, on pass i (0 i n-2), find the smallest element in
A[i..n-1] and swap it with A[i]:
Example: 7 3 2 5
5
Analysis of Selection Sort
Time efficiency:
Space efficiency:
6
Bubble Sort
Time efficiency:
Space efficiency:
9
Examples of Brute-Force String
Matching
Pattern: 001011
Text: 10010101101001100101111010
10
Pseudocode and Efficiency
12
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 down to 0 do
power 1
for j 1 to i do //compute xi
power power x
p p + a[i] power
return p
Efficiency:
13
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:
Efficiency:
14
Closest-Pair Problem
Brute-force algorithm
- Compute the distance between every pair of
distinct points
- return the indexes of the points for which the
distance is the smallest.
15
Closest-Pair Brute-Force Algorithm (cont.)
Efficiency:
16
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
17
Example 1: Traveling Salesman Problem
• 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
Example: 2
a b
5 3
8 4
c d
7
18
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
a→c→d→b→a 8+7+4+2 = 21
a→d→b→c→a 5+4+3+8 = 20
a→d→c→b→a 5+7+3+2 = 17
Efficiency:
19
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
21
Example 3: Assignment Problem
• Problem: There are n people who need to be
assigned to execute n jobs, one person per job.
• C[i, j]: The cost that would accrue if the ith
person is assigned to the jth job for each pair i,
j = 1, 2, . . . , n.
• The problem is to find an assignment with the
minimum total cost.
22
Assignment Problem by Exhaustive Search
9 2 7 8
C= 6 4 3 7
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. etc.
Efficiency ?
23
Final Comments on Exhaustive Search
• Exhaustive-search algorithms run in a realistic amount
of time only on very small instances
24
Graph Traversal Algorithms
25
Depth-First Search (DFS)
• Visits graph’s vertices by always moving away from last
visited vertex to unvisited one, backtracks if no adjacent
unvisited vertex is available.
• Uses a 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
• “Redraws” graph in tree-like fashion (with tree edges and
back edges for undirected graph)
26
Depth-First Search (DFS)
Traversal’s stack:
acd
fbe
ghij
a, c, d, f, b, e, g, h, i, j
27
Pseudocode of DFS
28
Notes on DFS
• DFS can be implemented with graphs represented as:
– adjacency matrices: Θ(V2) (if adjacency matrix is used as data
structure to store the graph)
– adjacency lists: Θ(|V|+|E|) (if adjacency list is used as data
structure to store the graph)
• Applications:
– checking connectivity, finding connected components
– checking acyclicity
– finding articulation points and biconnected components
– searching state-space of problems for solution (AI)
29
Notes on DFS
• Recurrence Relation?
• Complexity?
30
Breadth-first search (BFS)
• If depth-first search is a traversal for the brave
(the algorithm goes as far from “home” as it can),
breadth-first search is a traversal for the cautious.
• It proceeds in a concentric manner by visiting first
all the vertices that are adjacent to a starting
vertex, then all unvisited vertices two edges apart
from it, and so on, until all the vertices in the
same connected component as the starting
vertex are visited.
31
Breadth-first search (BFS)
• Visits graph vertices by moving across to all
the neighbors of last visited vertex
32
Breadth-first search (BFS)
a, c, d, e, f, b, g, h, j, i
33
Pseudocode of BFS
34
Example of BFS traversal of undirected graph
a b c d
e f g h
35
Notes on BFS
• BFS has same efficiency as DFS and can be
implemented with graphs represented as:
– adjacency matrices: Θ(V2)
– adjacency lists: Θ(|V|+|E|)
36