04greedy算法设计与分析 贪心算法
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
相关推荐:
- [教育文库]夜场KTV服务员的岗位职责及工作流程[1]
- [教育文库]企划、网络、市场绩效考核方案
- [教育文库]学党史、知党情、强党性--“党的基本理
- [教育文库]2016年高考物理大一轮总复习(江苏专版
- [教育文库]干部廉洁自律自查自纠的报告
- [教育文库]2010年北京大学心理学系拟录取硕士研究
- [教育文库]资金时间价值练习题及答案
- [教育文库]保护环境的心得体会
- [教育文库]英语角内容:英语趣味小知识
- [教育文库]档案收集与管理工作通知
- [教育文库]劳动规章制度范本范本
- [教育文库]高考物理一轮复习课后限时作业1运动的
- [教育文库]机械工艺夹具毕业设计195推动架设计说
- [教育文库]通用技术教学比赛说课稿2
- [教育文库]2018年四年级英语下册 Module 7 Unit 2
- [教育文库]第2章 宽带IP网络的体系结构
- [教育文库]九年级化学第五单元课题3《根据化学方
- [教育文库]小学英语六年级情态动词用法归纳
- [教育文库]甲级单位编制窑井盖项目可行性报告(立
- [教育文库]2016-2021年中国城市规划行业全景调研
- 高考英语听力十大场景词汇总结
- 全省领导班子思想政治建设座谈会会议精
- 人教版新课标高一英语提优竞赛试题 下
- 江西省2014年生物中考试题
- 长沙镇食品药品安全事故应急预案
- 《金刚石、石墨和C60》片段教学设计
- 福州教育学院(王旭东)
- 基于EDA音乐播放器的设计
- 9、古诗两首《夜书所见》《九月九日忆
- 小学语文课外阅读有效策略探讨
- 贵州文化产业发展成支柱产业的问卷调查
- 膀胱类癌的诊治体会(附3例报告)
- 发动机积碳产生的原因
- Configuring Code Composer Studio for
- 学生良好的心理素质如何培养点滴谈
- 46 电沉积法制备锂离子电池用硅-锂薄膜
- 美舍雅阁公司管理中各部门职责
- 去壳剥皮的小妙招
- 六自由度运动平台的仿真研究
- Pride and Prejudice(傲慢与偏见)




