当前位置:网站首页>回溯法/活动安排 最大兼容活动
回溯法/活动安排 最大兼容活动
2022-06-11 14:26:00 【xcrj】
- 活动安排问题
- n个活动需要使用1个互斥资源,每个活动都有1个开始时间和1个介绍时间因此会产生冲突,求最大兼容活动数量
- 什么是兼容/不兼容活动:
- 活动编号 1 2 3 4
- 开始时间 1 2 4 6
- 结束时间 3 5 8 9
- 活动最终结束时间为latest=0 兼容活动总量compatibleSum=0
- 01,无活动,13,latest=3,活动1;活动2要求从2开始,不兼容,剔除它;34,无活动;48,latest=8,活动3;活动4要求从6开始,不兼容,剔除它
- 活动安排问题本身是1个排列问题,求出1个排列之后需要判断排列树代表的活动是否兼容,求兼容活动最多的排列即兼容活动最多的活动安排
- 使用排列树的递归算法先获取活动的排列
- 回溯法:有回就有去-结点扩展规则,深度优先搜索的解空间树的过程中剪枝,剪枝函数有约束函数,限界函数,解空间树有子集树和排列树两种
- 解空间树/子集数:达到要求的子集,元素子集;分支代表选择或不选择
- 解空间树/排列树:达到要求的排列,所有元素都在;把那个元素放到第1位,把那个元素放到第2位,依次类推
- 剪枝函数/约束函数:决定是否访问下一个结点,子树不满足约束条件,
- 剪枝函数/限界函数:决定是否访问下面的子树,子树得不到需要的解,求最大值使用上界函数,求最小值使用下界函数
package xcrj.kchalgorithm.backtracking;
import java.util.Arrays;
/** * 活动安排问题 * n个活动需要使用1个互斥资源,每个活动都有1个开始时间和1个介绍时间因此会产生冲突,求最大兼容活动数量 * <p> * 什么是兼容/不兼容活动: * 活动编号 1 2 3 4 * 开始时间 1 2 4 6 * 结束时间 3 5 8 9 * 活动最终结束时间为latest=0 兼容活动总量compatibleSum=0 * 0~1,无活动,1~3,latest=3,活动1;活动2要求从2开始,不兼容,剔除它;3~4,无活动;4~8,latest=8,活动3;活动4要求从6开始,不兼容,剔除它 * <p> * <p> * 活动安排问题本身是1个排列问题,求出1个排列之后需要判断排列树代表的活动是否兼容,求兼容活动最多的排列即兼容活动最多的活动安排 * 排列树形成过程:选择第i个元素之后形成的父节点,父节点的孩子结点,通过父结点的第i和第j元素交换,轮流把第j个元素放到第i位,形成子结点 * <p> * 回溯法:有回就有去-结点扩展规则,深度优先搜索的解空间树的过程中剪枝,剪枝函数有约束函数,限界函数,解空间树有子集树和排列树两种 * 解空间树/子集数:达到要求的子集,元素子集;分支代表选择或不选择 * 解空间树/排列树:达到要求的排列,所有元素都在;把那个元素放到第1位,把那个元素放到第2位,依次类推 * 剪枝函数/约束函数:决定是否访问下一个结点,子树不满足约束条件, * 剪枝函数/限界函数:决定是否访问下面的子树,子树得不到需要的解,求最大值使用上界函数,求最小值使用下界函数 */
public class ActivityArrange {
static class Activity {
private int start;
private int end;
public Activity() {
}
public Activity(int start, int end) {
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}
// 不使用下标0,下标是活动的唯一编号
static Activity[] activities = {
new Activity(0, 0), new Activity(1, 3), new Activity(2, 5), new Activity(4, 8), new Activity(6, 9)};
/*求解过程*/
// 活动数量
static int n = 3;
// 活动安排,+1下标0不使用
static int[] xs = {
0, 1, 2, 3};
// 活动最终结束时间
static int latest;
// 兼容活动数量
static int compatibleSum;
/*结果展示*/
// 最优活动安排,+1下标0不使用
static int[] optimalXs = new int[n + 1];
// 最大兼容活动数量
static int maxCompatibleSum;
/** * */
public static void deepFirstSearch(int i) {
// 到达叶子结点,因为根结点为(0,0),所以到达叶子结点是i>n
if (i > n) {
// 结果更好则保留
if (compatibleSum > maxCompatibleSum) {
maxCompatibleSum = compatibleSum;
// 下标0不使用
for (int j = 1; j <= n; j++) {
optimalXs[j] = xs[j];
}
}
}
// 到达非叶子结点,生成i结点的所有孩子结点活动xs[j](j从i开始)
else {
// 下标0不使用
for (int j = i; j <= n; j++) {
// 为了便于回溯,保存
int compatibleSumTemp = compatibleSum;
int latestTemp = latest;
// xs[j]为活动的编号,判断活动是否兼容
if (activities[xs[j]].start >= latest) {
compatibleSum++;
latest = activities[xs[j]].end;
}
// !交换,选择第i个元素之后形成的父节点,父节点的孩子结点,通过父结点的第i和第j元素交换,轮流把第j个元素放到第i位,形成子结点
int temp = xs[i];
xs[i] = xs[j];
xs[j] = temp;
// 第i+1层选择活动xs[j](从i+1开始)作为结点,深度优先遍历操作孩子结点
deepFirstSearch(i + 1);
// 交换
int temp2 = xs[i];
xs[i] = xs[j];
xs[j] = temp2;
// 回溯,取消第i层对活动的选取
compatibleSum = compatibleSumTemp;
latest = latestTemp;
}
}
}
public static void main(String[] args) {
// 1,从活动1开始
deepFirstSearch(1);
System.out.println("最大兼容活动数量:" + maxCompatibleSum);
}
}
边栏推荐
- Sum of two leetcode numbers
- Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?
- Summary of some classic embedded C interview questions
- In depth research and analysis report on global and Chinese one component liquid rubber Market
- HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
- Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
- Nomad application layout scheme 04 of hashicopy (scaling and updating a job)
- B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
- [public class preview]: mxplayer Ott audio and video transcoding practice and optimization
- 02 Tekton Pipeline
猜你喜欢
![[verification of SystemVerilog] ~ test platform, hardware design description, excitation generator, monitor and comparator](/img/3a/0cc26400eeb4b388face09b9a10f27.png)
[verification of SystemVerilog] ~ test platform, hardware design description, excitation generator, monitor and comparator

How to quickly compress the size of video?

Redis configuration and optimization of NoSQL

Question bank and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022

B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡

Ali, tell me about the application scenarios of message oriented middleware?

Precision alignment adjustment platform

非常值得学习的调度开源库推荐

Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?

Top 10 bone conduction earphones in the list, and five easy-to-use bone conduction earphones are recommended
随机推荐
How to quickly compress the size of video?
Recommandation de la Bibliothèque open source de programmation
2022 Hunan Provincial Safety officer-c certificate examination practice questions and online simulation examination
How to manually package your own projects
2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案
Precision alignment adjustment platform
Recommended open source scheduling libraries worth learning
树莓派知识大扫盲
02 Tekton Pipeline
IC fresh Chinese cabbage price of 400000 yuan! Experienced experts who have worked for many years guide you how to choose an offer!
Ali, tell me about the application scenarios of message oriented middleware?
North China pushed Yale hard, MIT won the first place in a row, and the latest 2023qs world university ranking was released
[team learning] task06:for, if, and while
数字化转型项目做了多年,主架构师都绝望了:当初就不应该用外包!
Nomad application layout scheme 04 of hashicopy (scaling and updating a job)
2021 年度 Go 开发者调查
In depth research and analysis report on global and Chinese octamethylcyclotetrasiloxane (D4) market
MySQL advanced statement
Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
What's a good gift for the goddess Festival on March 8? Goddess Day gift sharing