05 Brute Force
05 Brute Force
05 Brute Force
Examples:
1. Selection Sort
2. Graph Traversal
3. Simple Computational Tasks
4. Exhaustive Search
Design and Analysis of Algorithms –
1
Chapter 3
Brute force strengths and weaknesses
• Strengths:
– wide applicability
– simplicity
DFS(G) dfs(v)
count :=0 count := count + 1
mark each vertex with 0 mark v with count
(unvisited) for each vertex w adjacent to v
for each vertex v V do do
if v is marked with 0 if w is marked with 0
dfs(v) dfs(w)
e f g h
dfs(v)
count := count + 1
mark v with count
Depth-first traversal: for each vertex w adjacent to v
do
if w is marked with 0
Design and Analysis of Algorithms –
dfs(w)
9
Chapter 4
Question
How to rewrite the procedure dfs(v), using a
stack to eliminate recursion
dfs(v)
count := count + 1
mark v with count
for each vertex w adjacent to v do
if w is marked with 0
dfs(w)
DFS(G) dfs(v)
count :=0 count := count + 1
mark each vertex with 0 mark v with count
(unvisited) for each vertex w adjacent to v
for each vertex v V do do
if v is marked with 0 if w is marked with 0
dfs(v) dfs(w)
Design and Analysis of Algorithms –
12
Chapter 4
Time efficiency analysis
DFS can be implemented with graphs
represented as:
– Adjacency linked lists:
DFS(G) dfs(v)
count :=0 count := count + 1
mark each vertex with 0 mark v with count
(unvisited) for each vertex w adjacent to v
for each vertex v V do do
if v is marked with 0 if w is marked with 0
dfs(v) dfs(w)
Design and Analysis of Algorithms –
13
Chapter 4
Time efficiency analysis
DFS can be implemented with graphs represented
as:
– Adjacency matrices: Θ(V2)
– Adjacency linked lists: Θ(V+E)
DFS(G) dfs(v)
count :=0 count := count + 1
mark each vertex with 0 mark v with count
(unvisited) for each vertex w adjacent to v
for each vertex v V do do
if v is marked with 0 if w is marked with 0
dfs(v) dfs(w)
Design and Analysis of Algorithms –
14
Chapter 4
Application: checking graph connectivity and
finding connected components
DFS(G) dfs(v)
count :=0 count := count + 1
mark each vertex with 0 mark v with count
(unvisited) for each vertex w adjacent to v
for each vertex v V do do
if v is marked with 0 if w is marked with 0
dfs(v) dfs(w)
DFS(G) dfs(v)
count :=0 count := count + 1
mark each vertex with 0 mark v with count
(unvisited) for each vertex w adjacent to v
for each vertex v V do do
if v is marked with 0 if w is marked with 0
dfs(v) dfs(w)
a b c d
e f g h
Breadth-first traversal:
a b c d
e f g h
x := x0
p := a[0]
power := 1
for i := 1 to n do
power := power * x
p := p + a[i] * power
return p
Efficiency: (n)
x := x0
p := a[0]
power := 1
for i := 1 to n do
power := power * x
p := p + a[i] * power
return p
Efficiency: (n)
Can we design a better than linear algorithm for this problem
Design and Analysis of Algorithms –
27
Chapter 3
Brute force closest-pair algorithm
Closest pair
• Problem: find the closest pair among n points in k-dimensional
space
(x y ) i i
2
i 1
• (squaring): k
d2 (x y )
i 1
i i
2
k
d2 (x y )
i 1
i i
2
Calculation
– Applying the product rule, there are n*(n-1) pairs among n
points
– Considering (a, b) and (b, a) as the same, we divide the
above number by 2 and thus, there are n*(n-1)/2 different
pairs of points
k
d2 (x y )
i 1
i i
2
• Efficiency: (n2)
– Convex:
• A geometric figure with no
convex indentations.
• Formally, a geometric figure is convex
if every line segment connecting
interior points is entirely contained
within the figure's interior.
Non-convex
Design and Analysis of Algorithms –
36
Chapter 3
Example: Convex Hull
Input: p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11
Output: p2,p9,p11,p4,p5,p6,p8,p2 p9
p11
p4
p2 p1
p7 p3
p10 p5
p8
p6
Convex hull: application domain*
• Computer visualization, ray tracing
– (e.g. video games, replacement of bounding boxes)
• Path finding
– (e.g. embedded AI of Mars mission rovers)
• Geographical Information Systems (GIS)
– (e.g. computing accessibility maps)
• Visual pattern matching
– (e.g. detecting car license plates)
• Verification methods
– (e.g. bounding of Number Decision Diagrams)
• Geometry
– (e.g. diameter computation)
*slide refer to http://www.montefiore.ulg.ac.be/~briquet/algo3-chull-20070206.pdf
Efficiency:
• Efficiency: (n3)
c 7
d
Design and Analysis of Algorithms –
47
Chapter 3
Traveling salesman 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:
• 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: (n-1)!/2
2 3
This is a knapsack 3 4
Max weight: W = 20 4 5
5 8
W = 20
9 10
Design and Analysis of Algorithms –
51
Chapter 3
0-1 Knapsack problem
• Problem, in other words, is to find
max vi subject to w W i
iT iT
Example:
item weight value Knapsack capacity W=16
1 2 $20
2 5 $30
3 10 $50
4 5 $10
Design and Analysis of Algorithms –
54
Chapter 3
Knapsack by exhaustive search
Subset Total weight Total value
0 $0
{1} 2 $20 Most valuable subset?
{2} 5 $30
{3} 10 $50
{4} 5 $10
{1,2} 7 $50
{1,3} 12 $70 Efficiency:
{1,4} 7 $30
{2,3} 15 $80
{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
• Algorithm:
– We go through all combinations and find the one with
maximum value and with total weight less or equal to
W
Efficiency:
• Since there are n items, there are 2n possible
combinations of items.
• Thus, the running time will be O(2n)