算法设计与分析 4暴力算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

2013-6-3
Knapsack Problem
10
W=7 V=$42 item 1 W=3 V=$12 W=4 V=$40 item 3 W=5 V=$25 item 4
knapsack
item 2
instance of the knapsack problem
2013-6-3
Knapsack Problem
n! (Exhaustive search is impractical for all but every small instances of problems it can be applied to. )
2013-6-3
Summary
Brute force is a straightforward approach to solving a problem. Based on the problem’s statement and definitions of the concepts involved. The principal strengths of the brute-force approach are simplicity; it’s principal weakness is efficiency.
Total value
$0 $42 $12 $40 $25 $36 not feasible not feasible $52 $37 $65 not feasible not feasible not feasible not feasible not feasible
Figure3.8 (b) Its solution by exhaustive search
2013-6-3
Example of brute-force string matching
NOBODY_NOTICED_HIM NOT NOT NOT NOT NOT NOT NOT NOT In the worst case,the algorithm is O(nm).
2013-6-3
6. Exhaustive search
2013-6-3
Assignment Problem
A small instance of this problem follows,with the table’s entries representing the assignment costs C[i,j]: Job1
Person1 Person2 Person3 Person4
Algorithm SequentialSearch(A[0..n],K) A[n] ←K i←0 While A[i] ≠ K do i ← i +1 if i < n return i else return -1
2013-6-3
5.Brute-Force String Matching
Algorithm BruteForceStringMatch (T[0..n-1], p[0..m-1]) for i ← 0 to n-m do j ←0 while j<m and P[j] = T[i+j] do j ← j+1 if j = m return i Return -1
2013-6-3
Three important problem
Traveling Salesman Problem-----finding the shortest hamiltonian circuit of the graph.
Knapsack Problem Assignment Problem
Job2 Job3 Job4 2 4 8 6 7 3 1 9 8 7 8
4
9 6 5 7
2013-6-3
Assignment Problem
9 6 C 5 7 2 4 8 6 7 3 1 9 8 7 8 4
<1,2,3,4> cost=9+4+1+4=18 <1,2,4,3> cost=9+4+8+9=30 <1,3,2,4> cost=9+3+8+4=24 <1,3,4,2> cost=9+3+8+6=26 <1,4,2,3> cost=9+7+8+9=33 <1,4,3,2> cost=9+7+1+6=23 <2,1,3,4>……. …. etc.
2013-6-3
Traveling Salesman Problem
2
a
8 5 7 1
b
3
c
d
a a a a a a
2013-6-3
-> -> -> -> -> ->
b b c c d d
Tour -> c -> -> d -> -> b -> -> d -> -> b -> -> c ->
d c d b c b
2013-6-3
3. Closest Pair of Points
C (n) 2 2 (n i)
i 1 j i 1 i 1 n 1 n n 1
2[(n 1) (n 2) ... 1] (n 1)n (n )
2
2013-6-3
4. Sequential Search
2013-6-3
Traveling Salesman Problem
The traveling salesman problem--- finding the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it started. Modeled by a weighted complete graph. Finding the shortest Hamiltonian circuit of the graph.
ຫໍສະໝຸດ Baidu-> -> -> -> -> ->
a a a a a a
l l l l l l
= = = = = =
Length 2 + 8 + 2 + 3 + 5 + 8 + 5 + 1 + 7 + 3 + 7 + 1 +
1 1 3 3 8 8
+ + + + + +
7 5 7 2 5 2
= = = = = =
18 11 23 11 23 18
Subset
θ {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}
2013-6-3
Total weight
0 7 3 4 5 10 11 12 7 8 9 14 15 16 12 19

Many important problem require finding an element with a special property in a domain that grows exponentially(or faster) with an instance size.
Exhaustive search is simply a brute-force approach to combinatorial problems. It suggests generating each and every element of the problem’s domain,selecting those of them that satisfy the problem’s constraints,and then finding a desired element. (The one that optimizes some objective function).
Exhaustive search is impractical for all. But it’s good for small instances of problems!
2013-6-3
Brute Force
李绍华 广东商学院(信息学院) 计算机科学与技术系
2013-6-3
Brute force
Brute force is a straightforward approach to solving a problem . --Based on the problem’s statement and definitions of the concepts involved.
The “force” implied by the strategy’s definition is that of a computer and not that of one’s intellect. ”Just do it”
2013-6-3
Examples
Example1. bn=b*….*b (n times) (computing bn by multiplying b by b n times) Example2. selection sort Algorithm selectionsort(A[0..n-1]) for i←1 to n-2 do min ← i for j ← i+1 to n-1 do {if A[j]<A[min] min ← j} swap A[i] and A[min]
Knapsack Problem

The number of subsets of an n-element set is
T(n)=Θ(2n)
2n

2013-6-3
Assignment Problem
n people ,n jobs,one person per job.
The cost: the ith person is assigned to the j th job is a known quantity C[i,j] for each pair i,j=1,….n. The problem is to find an assignment with the smallest total cost.
2013-6-3
3. Closest Pair of points
Algorithm BruteforceClosetstPoints (P) dmin ←∞; for i ←1 to n-1 do for j ←i+1 to n do d ←sqrt((xi-xj)2+(yi-yj)2) if d<dmin dmin ←d; index1 ←i; index2 ←j return index1,index2
optimal optimal
Knapsack Problem
Given n items of known weights w1,…,wn and values v1,…,vn and a knapsack of capacity W,find the most valuable subset of the items that fit into the knapsack.
相关文档
最新文档