没有合适的资源?快使用搜索试试~ 我知道了~
首页算法课ppt,贪心算法
算法课ppt,贪心算法

算法课ppt,贪心算法,贪心算法,贪心算法贪心算法贪心算法贪心算法贪心算法贪心算法贪心算法贪心算法贪心算法贪心算法
资源详情
资源评论
资源推荐

1
Greedy Method
•
A greedy algorithm always make the choice that
looks best at every step. That is, it makes local
optimal solution in the hope that this choice will lead
to a globally optimal one.
–
I make the shortest path to the target at each step.
Sometime I win, sometime I lose.

2
Examples of Greedy
Algorithms
•
Scheduling
–
Activity Selection
–
Minimizing time in system
–
Deadline scheduling
•
Graph Algorithms
–
Minimum Spanning Trees
–
Dijkstra’s (shortest path) Algorithm
•
Other Heuristics
–
Huffman coding
–
Coloring a graph
–
Traveling Salesman Problem
–
Set-covering
–
Subset-sum problem

3
例 [ 找零钱 ] 一个小孩买了价值少于 1 元的糖,并将 1 元的钱交给售货员。
售货员希望用数目最少的硬币找给小孩。假设提供数目不限的面值为 25
分、 10 分、 5 分及 1 分的硬币。售货员分步骤组成要找的零钱数,每次
加入一个硬币。
选择硬币时所采用的贪心算法如下:每一次选择应使零钱数尽量增大。为确
保解法的可行性(即:所给的零钱等于要找的零钱数),所选择的硬币不
应使零钱总数超过最终所需的数目。
假设需要找给小孩 67 分,首先入选的是两枚 25 分的硬币,第三枚入选的不
能是 25 分的硬币,否则将不可行(零钱总数超过 67 分),第三枚应选择
10 分的硬币,然后是 5 分的,最后加入两个 1 分的硬币。
贪心算法有种直觉的倾向,在找零钱时,直觉告诉我们应使找出的硬币
数目最少(至少是接近最少的数目)

4
Activity-Selection Problem
•
Problem: Given a set A = {a
1
, a
2
, …, a
n
} of n activities
with start and finish times (s
i
, f
i
), 1 ≤ i ≤ n, select max
imal set S of “non-overlapping” activities.
–
One can think of the problem as corresponding to schedulin
g the maximal number of classes (given their start and finis
h times) in one classroom.
•
Solution:
–
Sort activities by finish times (let a
1
, a
2
, …, a
n
denote sorted
sequence)
–
Pick first activity a
1
–
Remove all activities with start time before finish time of a
1
–
Recursively solve problem on remaining activities.

5
Activity Selection
•
Here is the problem from the book:
–
Activity a
1
starts at s
1
= 1, finishes at f
1
= 4.
–
Activity a
2
starts at s
2
= 3, finishes at f
2
= 5.
–
Activity a
3
starts at s
3
= 0, finishes at f
3
= 6 ...
•
Got the idea?
–
The set S is sorted in monotonically increasing order of finis
h time.The subsets of {a
3
, a
9
, a
11
} and {a
1
, a
4
, a
8
, a
11
}are mutuall
y compatible.
剩余63页未读,继续阅读











安全验证
文档复制为VIP权益,开通VIP直接复制

评论1