经典问题-活动选择问题-DP和贪心

活动选择问题:

Given a set S of n activities with and start time, Si and fi, finish time of an ith activity. Find the maximum size set of mutually compatible activities.

 

 

 

 

兼容活动概念:

Activities and j are compatible if the half-open internal [si, fi) and [sj, fj) do not overlap, that is, i and j are compatible ifsi ≥ fj  and s≥  fi

 

算法导论上提供了两种方法,一种是DP方法, 另一种是贪心算法,同时网上还提供了另一种DP的方法。下面对三种方法进行讲解:

1,算法导论上的DP方法:

先对每个活动ai按照活动结束时间fi从小到大排序,之后按照递归方程进行动态规划

 

2. 算法导论上的贪心算法:

先对每个活动ai按照活动结束时间fi从小到大排序,每次选择最早结束的活动,

而这个活动的选择需要与前面所选的活动相容。

原始问题是S= S[0,n+1],设活动am1是S[0,n+1]中具有最早结束时间的活动,

下一个问题是S[m1, n+1],设选择活动am2为S[m1,n+1]中最早结束时间的活动,

则下一个子问题是S[m2,n+1],如此继续:

 


3. 网上提供的DP方法:

 

Before starting the dynamic programming algorithm itself, we do some precomputation, as follows. We first sort the activities according to fi nish time, so that f1 <=f2 <=.. <= fn. We also compute, for every activity i, a number H(i) defi ned as H(i) = max{l<=i-1&& l>=0 && fl<=si} .the maximum value of the empty set is 0. We can compute each value H(i) in time O(log n) using binary search, so all of the precomputation can be done in time O(n log n).

We now perform the four steps of the dynamic programming method.

Step 1:

Describe an array of values we want to compute.

For every integer i, 0 <=i<=n, de fine A(i) = the largest pro fit we can get by (feasibly) scheduling activities from {1; 2,.., i}.

The value we are ultimately interested in is A(n).

Step 2:

Give a recurrence.

A(0) = 0.

This is true because if we are not allowed to schedule any activities at all, then the highest pro fit we can make is 0.

Let 1 <=i <=n. Then A(i) = max{A(i-1); gi + A(H(i))}:

 

.

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值