教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 教育文库 >

04greedy算法设计与分析 贪心算法

来源:网络收集 时间:2026-08-24
导读: Chapter 4Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 4.1 Interval Scheduling Interval SchedulingInterval scheduling. Job j starts at sj and finishes at fj. Two jobs compatible if the

Chapter 4Greedy Algorithms

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

4.1 Interval Scheduling

Interval SchedulingInterval scheduling. Job j starts at sj and finishes at fj. Two jobs compatible if they don't overlap. Goal: find maximum subset of mutually compatible jobs.

ab c d e

fg h0 1 2 3 4 5 6 7 8 9 10 11

Time3

Interval Scheduling: Greedy AlgorithmsGreedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken.

[Earliest start time] Consider jobs in ascending order of sj. [Earliest finish time] Consider jobs in ascending order of fj. [Shortest interval] Consider jobs in ascending order of fj - sj. [Fewest conflicts] For each job j, count the number of conflicting jobs cj. Schedule in ascending order of cj.

Interval Scheduling: Greedy AlgorithmsGreedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken.

counterexample for earliest start time

counterexample for shortest interval

counterexample for fewest conflicts

Interval Scheduling: Greedy AlgorithmGreedy algorithm. Consider jobs in increasing order of finish time. Take each job provided it's compatible with the ones already taken.

Sort jobs by finish times so that f1 f2 ... fn.set of jobs selected

A for j = 1 to n { if (job j compatible with A) A A {j} } return A

Implementation. O(n log n). Remember job j* that was added last to A. Job j is compatible with A if sj fj*.

Interval Scheduling: AnalysisTheorem. Greedy algorithm is optimal. Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let i1, i2, ... ik denote set of jobs selected by greedy. Let j1, j2, ... jm denote set of jobs in the optimal solution with i1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r.

job ir+1 finishes before jr+1

Greedy:

i1

i2

ir

ir+1

OPT:

j1

j2

jr

jr+1why not replace job jr+1 with job ir+1?

...

Interval Scheduling: AnalysisTheorem. Greedy algorithm is optimal. Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let i1, i2, ... ik denote set of jobs selected by greedy. Let j1, j2, ... jm denote set of jobs in the optimal solution with i1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r.

job ir+1 finishes before jr+1

Greedy:

i1

i2

ir

ir+1

OPT:

j1

j2

jr

ir+1

...

solution still feasible and optimal, but contradicts maximality of r.8

4.1 Interval Partitioning

Interval PartitioningInterval partitioning. Lecture j starts at sj and finishes at fj. Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room.

Ex: This schedule uses 4 classrooms to schedule 10 lectures.

4

e c b a9 9:30 10 10:30 11 11:30 12 12:30 1 1:30

j g h f2 2:3

0 3 3:30

3 2 1

d

i4 4:30

Time10

Interval PartitioningInterval partitioning. Lecture j starts at sj and finishes at fj. Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room.

Ex: This schedule uses only 3.

3 2 1

c b a9 9:30 10 10:30 11

d

f g e h1 1:30 2 2:30 3 3:30

j

i

11:30

12

12:30

4

4:30

Time11

Interval Partitioning: Lower Bound on Optimal SolutionDef. The depth of a set of open intervals is the maximum number that contain any given time. Key observation. Number of classrooms needed depth. Ex: Depth of schedule below = 3 schedule below is optimal.a, b, c all contain 9:30

Q. Does there always exist a schedule equal to depth of intervals?

3 2 1

c b a9 9:30 10 10:30 11

d

f g e h1 1:30 2 2:30 3 3:30

j

i

11:30

12

12:30

4

4:30

Time12

Interval Partitioning: Greedy AlgorithmGreedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom.Sort intervals by starting time so that s1 s2 ... sn. d 0 number of allocated classrooms for j = 1 to n if (lecture schedule else allocate schedule d d + } { j is compatible with some classroom k) lecture j in classroom k a new classroom d + 1 lecture j in classroom d + 1 1

Implementation. O(n log n). For each classroom k, maintain the finish time of the last job added. Keep the classrooms in a priority queue.

Interval Partitioning: Greedy AnalysisObservation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem. Greedy algorithm is optimal. Pf. Let d = number of classrooms that the greedy algorithm allocates. Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms. These d jobs each end after sj. Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than sj. Thus, we have d lectures overlapping at time sj + . Key observation all schedules use d classrooms.

4.2 Scheduling to Minimize Lateness

Scheduling to Minimizing LatenessMinimizing lateness problem. Single resource processes one job at a time. Job j requires tj units of processing time and is due at time dj. If j starts at time sj, it finishes at time fj = sj + tj. Lateness: j = max { 0, fj - dj }. Goal: schedule all jobs to minimize maximum lateness L = max j.

Ex:

1 tj dj 3 6

2 2 8

3 1 9

4 4 9

5 3 14

6 2 15

lateness = 2

lateness = 0

max lateness = 6

d3 = 90 1

d2 = 82 3

d6 = 154 5 6

d1 = …… 此处隐藏:4264字,全部文档内容请下载后查看。喜欢就下载吧 ……

04greedy算法设计与分析 贪心算法.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/116339.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)