当前位置:网站首页>Backtracking / activity scheduling maximum compatible activities
Backtracking / activity scheduling maximum compatible activities
2022-06-11 14:42:00 【xcrj】
- The arrangement of activities
- n Activities need to use 1 Two mutually exclusive resources , Every activity has 1 Start time and 1 There will be conflicts between the two introductions , Find the maximum number of compatible activities
- What is compatibility / Incompatible activities :
- Activity number 1 2 3 4
- Starting time 1 2 4 6
- End time 3 5 8 9
- The final end time of the activity is latest=0 Total compatible activities compatibleSum=0
- 01, No activity ,13,latest=3, Activities 1; Activities 2 Demand from 2 Start , Are not compatible , Eliminate it ;34, No activity ;48,latest=8, Activities 3; Activities 4 Demand from 6 Start , Are not compatible , Eliminate it
- The problem of activity arrangement itself is 1 A permutation problem , Find out 1 After permutations, you need to determine whether the activities represented by the permutation tree are compatible , The arrangement with the most compatible activities is the arrangement with the most compatible activities
- Use the recursive algorithm of the permutation tree to get the active permutation first
- backtracking : There will be a return - Node extension rules , Depth first search of the solution space tree in the process of pruning , Pruning function has constraint function , Bounded functions , There are two kinds of solution space trees: subset tree and permutation tree
- Solve the space tree / Number of subsets : Meet the required subset , Subset of elements ; The branch represents a choice or no choice
- Solve the space tree / Permutation tree : Achieve the required arrangement , All the elements are in ; Put that element in the 1 position , Put that element in the 2 position , By analogy
- Pruning function / Constraint function : Decide whether to access the next node , Subtree does not satisfy the constraint ,
- Pruning function / Bounded functions : Decide whether to access the subtree below , Subtree can not get the desired solution , Find the maximum using the upper bound function , Find the minimum using the lower bound function
package xcrj.kchalgorithm.backtracking;
import java.util.Arrays;
/** * The arrangement of activities * n Activities need to use 1 Two mutually exclusive resources , Every activity has 1 Start time and 1 There will be conflicts between the two introductions , Find the maximum number of compatible activities * <p> * What is compatibility / Incompatible activities : * Activity number 1 2 3 4 * Starting time 1 2 4 6 * End time 3 5 8 9 * The final end time of the activity is latest=0 Total compatible activities compatibleSum=0 * 0~1, No activity ,1~3,latest=3, Activities 1; Activities 2 Demand from 2 Start , Are not compatible , Eliminate it ;3~4, No activity ;4~8,latest=8, Activities 3; Activities 4 Demand from 6 Start , Are not compatible , Eliminate it * <p> * <p> * The problem of activity arrangement itself is 1 A permutation problem , Find out 1 After permutations, you need to determine whether the activities represented by the permutation tree are compatible , The arrangement with the most compatible activities is the arrangement with the most compatible activities * The formation process of the permutation tree : Select the first i Parent node formed after elements , The child node of the parent node , Through the parent node i And the j Element exchange , Take turns to j Put the first element in the second i position , Form child nodes * <p> * backtracking : There will be a return - Node extension rules , Depth first search of the solution space tree in the process of pruning , Pruning function has constraint function , Bounded functions , There are two kinds of solution space trees: subset tree and permutation tree * Solve the space tree / Number of subsets : Meet the required subset , Subset of elements ; The branch represents a choice or no choice * Solve the space tree / Permutation tree : Achieve the required arrangement , All the elements are in ; Put that element in the 1 position , Put that element in the 2 position , By analogy * Pruning function / Constraint function : Decide whether to access the next node , Subtree does not satisfy the constraint , * Pruning function / Bounded functions : Decide whether to access the subtree below , Subtree can not get the desired solution , Find the maximum using the upper bound function , Find the minimum using the lower bound function */
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;
}
}
// Do not use subscripts 0, The subscript is the unique number of the activity
static Activity[] activities = {
new Activity(0, 0), new Activity(1, 3), new Activity(2, 5), new Activity(4, 8), new Activity(6, 9)};
/* Solving process */
// Number of activities
static int n = 3;
// Arrangement of activities ,+1 Subscript 0 Don't use
static int[] xs = {
0, 1, 2, 3};
// The final end time of the activity
static int latest;
// Number of compatible activities
static int compatibleSum;
/* Result display */
// Optimal activity arrangement ,+1 Subscript 0 Don't use
static int[] optimalXs = new int[n + 1];
// Maximum number of compatible activities
static int maxCompatibleSum;
/** * */
public static void deepFirstSearch(int i) {
// Reach the leaf node , Because the root node is (0,0), So reaching the leaf node is i>n
if (i > n) {
// If the result is better, keep
if (compatibleSum > maxCompatibleSum) {
maxCompatibleSum = compatibleSum;
// Subscript 0 Don't use
for (int j = 1; j <= n; j++) {
optimalXs[j] = xs[j];
}
}
}
// Reach the non leaf node , Generate i All child node activities of the node xs[j](j from i Start )
else {
// Subscript 0 Don't use
for (int j = i; j <= n; j++) {
// For the convenience of backtracking , preservation
int compatibleSumTemp = compatibleSum;
int latestTemp = latest;
// xs[j] Is the number of the activity , Determine if the activity is compatible
if (activities[xs[j]].start >= latest) {
compatibleSum++;
latest = activities[xs[j]].end;
}
// ! In exchange for , Select the first i Parent node formed after elements , The child node of the parent node , Through the parent node i And the j Element exchange , Take turns to j Put the first element in the second i position , Form child nodes
int temp = xs[i];
xs[i] = xs[j];
xs[j] = temp;
// The first i+1 Layer selection activity xs[j]( from i+1 Start ) As nodes , Depth first traversal operation child node
deepFirstSearch(i + 1);
// In exchange for
int temp2 = xs[i];
xs[i] = xs[j];
xs[j] = temp2;
// to flash back , Cancel the i Layer selection of activities
compatibleSum = compatibleSumTemp;
latest = latestTemp;
}
}
}
public static void main(String[] args) {
// 1, From activities 1 Start
deepFirstSearch(1);
System.out.println(" Maximum number of compatible activities :" + maxCompatibleSum);
}
}
边栏推荐
- China's technology goes to sea, tidb database's overseas exploration road | interview with excellent technical team
- Question bank and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
- Hamad application layout scheme 05 of hashicopy (visit the web page)
- Why do I need the public static void main (string[] args) method?
- How to manually package your own projects
- 2022年湖南省安全员-C证考试练习题及在线模拟考试
- 【SystemVerilog 之 验证】~ 测试平台、硬件设计描述、激励发生器、监测器、比较器
- 为什么需要public static void main(String[ ] args)这个方法?
- 2021 go developer survey
- 02 Tekton Pipeline
猜你喜欢

Summary of some classic embedded C interview questions

Top 10 bone conduction earphones in the list, and five easy-to-use bone conduction earphones are recommended

回溯法/解空间树 排列树

Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)

111. minimum depth of binary tree

Qualcomm WLAN framework learning (29) -- 6GHz overview

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

Webgl programming guide learning (0)

uniapp设置页面跳转效果 - navigateTo切换效果 - 全局animationType动画

基于Qt开发实现的任务管理器
随机推荐
Hamad application layout scheme of hashicopy 01
2022质量员-市政方向-岗位技能(质量员)考试模拟100题及模拟考试
Zhu Ping: in the post epidemic era, medical beauty operations should not only be distracted, but also reverse the routine
2022年湖南省安全员-C证考试练习题及在线模拟考试
高通WLAN框架学习(29)-- 6GHz 概述
Did you break the rules?
Recommended open source scheduling libraries worth learning
大道至简 | 设计 ViT 到底怎么配置Self-Attention才是最合理的?
System.out.println()方法使用需要注意哪些问题
树莓派获得网络安装系统功能,无需借助其他设备
2022 simulated 100 questions and simulated examination of quality officer municipal direction post skills (Quality Officer) examination
2021 年 CNCF 调查:Kubernetes 跨越鸿沟的一年
Global and China dynamic light scattering nano laser particle sizer market depth research and Analysis Report
2022 Hunan Provincial Safety officer-c certificate examination practice questions and online simulation examination
多云安全合规扫描平台之RiskScanner
漫画:有趣的 “切蛋糕“ 问题
2021 go developer survey
CNCF survey in 2021: a year for kubernetes to cross the gap
思科瑞递交科创板注册:拟募资6亿 年营收2.22亿
深度剖析「圈组」关系系统设计 | 「圈组」技术系列文章