Backtracking method to solve flow operation scheduling problem

Problem Description

There are n jobs (numbered 1 to n) that need to be processed on an assembly line composed of two machines M1 and M2. The processing sequence of each job is to process on M1 first, then process on M2. The time required for M1 and M2 to process job i is ai and bi respectively (1≤i≤n).
The flow job scheduling problem requires determining the optimal processing sequence of these n jobs, so that the time required from the first job starting to be processed on machine M1 to the last job being processed on machine M2 is minimized. It can be assumed that once any job starts processing, it is not allowed to be interrupted until the job is completed, that is, non-priority scheduling.

[Input format] The input contains several use cases. The first line of each use case is the number of jobs n (1 ≤ n ≤ 1000). The next n lines have two non-negative integers in each line. The two integers in the i-th line respectively represent the i-th job on the first machine. and processing time on the second machine. End with input n=0.
[Output format] Each use case outputs one line, indicating the total time used for optimal scheduling, that is, the time from the first machine to the end of the second machine.
[Input sample]
4
5 6
12 2
4 14
8 7
0
[Output sample]
33

problem solving

Using the backtracking method to solve, the corresponding solution space is a permutation tree, which is equivalent to finding an arrangement of n jobs to minimize the completion time.
The job numbers are 1 to n.
The array x[] serves as the solution vector, that is, the scheduling plan, that is, x[i] represents the job number executed in the i-th step. Initially, the elements of the array x are 1 to n.
The optimal solution vector is stored with bestx[].
The optimal scheduling time of the optimal solution is represented by bestf.

f1 array: f1[i] represents the total time that the job x[i] executed at the i-th step is executed on M1 (including the execution time of the previous job) f2 array: f2[i] represents the job x[i] executed at the i-th
step i] The total time to complete execution on M2 (including the execution time of the previous job).
Since a job is always executed on M1 first and then on M2, f2[n] is the total time to execute all jobs.

Since each job starts from M1, that is, each job on M1 is executed continuously and does not need to wait, so f1 does not need to be represented by an array, but directly represented by a single variable f1.

f2[i-1]>f1: Demand, etc. f2[i]=f2[i-1]+b[x[i]] f2[i-1]≤f1: Undemand, etc., f2[i]=f1+
b [x[i]]
f1 += a[x[i]];
f2[i]=max(f1,f2[i-1])+b[x[i]];

Permutation tree recursive backtracking framework: Find a solution while finding its f2[n] time!
Pruning: Find f2[i] of the i-th layer, which is the total time after executing job x[i]. If f2[i]≥bestf (bestf is the currently calculated optimal total time to execute all jobs), then There is no need to expand downward from this node, let it become a dead node, that is to say, only expand nodes that satisfy f2[i]<bestf.
Insert image description here

code

int bound(int i)			//求结点的下界值
{
    
      int sum=0;
   for (int j=1;j<=i;j++)		//扫描所有选择的作业
     sum+=b[x[j]];			//累计所有选择作业的b时间
   return f2[i]+tot-sum;		//全部n个作业的b时间和为tot
}
void dfs(int i)			//从第i层开始搜索
{
    
      if (i>n)				//到达叶结点,产生一种调度方案
   {
    
      if (f2[n]<bestf)		//找到更优解
      {
    
      bestf=f2[n];
         for(int j=1; j<=n; j++)	//复制解向量
            bestx[j] = x[j];  
      }
   }
   else
   {
    
      for(int j=i; j<=n; j++)		//没有到达叶结点,考虑可能的作业
      {
    
      swap(x[i],x[j]);
         f1 += a[x[i]];		//选择作业x[i],在M1上执行完的时间
         f2[i]=max(f1,f2[i-1])+b[x[i]];
         if (bound(i)<bestf)		//剪枝
            dfs(i+1);
         f1 -= a[x[i]];		//回溯
         swap(x[i],x[j]);
      }
   }
}

Analysis of Algorithms

The solution space tree of this algorithm is an arrangement tree with a height of n, and the time complexity of the corresponding algorithm is O(n!).

Guess you like

Origin blog.csdn.net/weixin_42729072/article/details/104929949
Recommended