Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Brute Force *
Dr. Ying Lu
ylu@cse.unl.edu
September 20, 2012
*slides referred to
http://www.aw-bc.com/info/levitin
Design and Analysis of Algorithms Chapter 3
Brute force
Examples:
Selection Sort
Graph Traversal
Simple Computational Tasks
Exhaustive Search
1.
2.
3.
4.
Graph traversal
Traversing graphs
Depth-first search
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(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
w)
Design and Analysis of Algorithms Chapter dfs(
4
8
Question
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)
10
DFS(G)
count :=0
mark each vertex with 0
(unvisited)
for each vertex v V do
if v is marked with 0
dfs(v)
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)
11
DFS(G)
count :=0
mark each vertex with 0
(unvisited)
for each vertex v V do
if v is marked with 0
dfs(v)
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)
12
DFS(G)
count :=0
mark each vertex with 0
(unvisited)
for each vertex v V do
if v is marked with 0
dfs(v)
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)
13
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)
14
DFS(G)
count :=0
mark each vertex with 0
(unvisited)
for each vertex v V do
if v is marked with 0
dfs(v)
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)
15
Breadth-first search
16
Breadth-first traversal:
17
18
BFS(G)
count :=0
mark each vertex with 0
bfs(v)
if w is marked with 0
count := count + 1
mark w with count
add w to the end of the queue
remove a from the front of the queue
19
20
In-class exercise
21
22
23
24
p := 0.0
for i := n down to 0 do
power := 1
for j := 1 to i do
power := power * x
p := p + a[i] * power
return p
Efficiency: (n2)
Can we design aDesign
linear
algorithm for this problem
and Analysis of Algorithms Chapter 3
25
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)
Design and Analysis of Algorithms Chapter 3
26
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 Chapter 3
27
Closest pair
Problem: find the closest pair among n points in k-dimensional
space
28
Closest pair
29
Closest pair
Problem: find the closest pair among n points in k-dimensional
space
Algorithm: Compute distance between each pair of points and
identify the pair resulting in the shortest distance
( xi yi ) 2
i 1
( xi yi ) 2
i 1
30
Closest pair
Problem: find the closest pair among n points in k-dimensional space
Algorithm: Compute distance between each pair of points and
identify the pair resulting in the shortest distance
Basic operation:
( xi yi ) 2
i 1
( xi yi ) 2
i 1
31
32
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
33
Closest pair
Problem: find the closest pair among n points in k-dimensional space
Algorithm: Compute distance between each pair of points and
identify the pair resulting in the shortest distance
Basic operation:
( xi yi ) 2
i 1
( xi yi ) 2
i 1
Efficiency:
(n2)
Design and Analysis of Algorithms Chapter 3
34
In-class exercise
35
In-class exercise
Algorithm:
Step1: Sort the numbers in ascending order, O(nlogn)
Step 2: Compute the differences between adjacent numbers in
the sorted list, (n)
Step 3: Find the smallest such difference, (n)
Running time of the entire algorithm:
O(nlogn) + (n) + (n) = O(nlogn)
Design and Analysis of Algorithms Chapter 3
36
Convex hull
Problem:
Find smallest convex polygon enclosing
n points on the plane
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
37
Output: p2,p9,p11,p4,p5,p6,p8,p2
p11
p4
p1
p2
p3
p7
p8
p10
p6
p5
Path finding
(e.g. embedded AI of Mars mission rovers)
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
Design and Analysis of Algorithms Chapter 3
39
p9
p11
p4
p1
p2
p3
p7
p8
p10
p5
p6
40
p9
p11
p4
p1
p2
p3
p7
p8
p10
p5
p6
41
42
43
Efficiency:
44
Efficiency: (n3)
45
46
47
48
Tour
abcda
abdca
acbda
acdba
adbca
adcba
Efficiency:
Cost
2+3+7+5 = 17
2+4+7+8 = 21
8+3+4+5 = 20
8+7+4+2 = 21
5+4+3+8 = 20
5+7+3+2 = 17
49
Tour
abcda
abdca
acbda
acdba
adbca
adcba
Efficiency: (n-1)!/2
Cost
2+3+7+5 = 17
2+4+7+8 = 21
8+3+4+5 = 20
8+7+4+2 = 21
5+4+3+8 = 20
5+7+3+2 = 17
50
51
Benefit value
wi
vi
2
3
3
4
10
52
max vi subject to
iT
w W
iT
53
54
Find the most valuable subset of the items that fit into the
knapsack
Example:
item
weight
1
2
2
5
3
10
4
5
value
$20
$30
$50
$10
55
0
{1}
2
{2}
5
{3}
10
{4}
5
{1,2}
7
{1,3}
12
{1,4}
7
{2,3}
15
{2,4}
10
{3,4}
15
{1,2,3}
17
{1,2,4}
12
{1,3,4}
17
{2,3,4}
20
{1,2,3,4}
22
Total value
$0
$20
$30
$50
$10
$50
$70
$30
$80
$40
$60
not feasible
$60
not feasible
not feasible
not feasible
56
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)
57
Strengths:
wide applicability
simplicity
yields reasonable algorithms for some important problems
sorting; matrix multiplication; closest-pair; convex-hull
yields standard algorithms for simple computational tasks
and graph traversal problems
58
Weaknesses:
rarely yields efficient algorithms
some brute force algorithms unacceptably slow
e.g., the recursive algorithm for computing Fibonacci numbers
59