# JOHNSON算法：流水作业最优调度问题

### 关于流水作业调度问题的 Johnson 算法

1. A B = { i ∣ a i < b i } , B A = { i ∣ a i ≥ b i } {AB = \{ i | a_i < b_i\}, BA = \{ i | a_i ≥ b_i \}}
2. A B AB 中作业依 a i a_i 的非减次序排列；将 B A BA 中作业依 b i b_i 的非增次序排列；
3. A B AB 中作业接 B A BA 中作业即构成满足 J o h n s o n Johnson 法则的最优调度。
#include <stdio.h>
#include <algorithm>
using namespace std;
#define N 8

struct assignment{
int a;
int b;
}asg[N] = {{1,2},{2,4},{3,6},{4,1},{5,7},{6,4},{7,9},{8,3}};

bool cmp1(struct assignment x, struct assignment y){
if (x.a >= y.a){
return true;
}
return false;
}

bool cmp2(struct assignment x, struct assignment y){
if (x.b <= y.b){
return true;
}
return false;
}

void johnson(){
struct assignment A[N], B[N];
int a=0,b=0;
for (int i = 0; i < N; ++i)
{
if (asg[i].a < asg[i].b){
A[a++] = asg[i];
}else{
B[b++] = asg[i];
}
}
sort(A,A+N,cmp1);
sort(B,B+N,cmp2);
for (int i = 0; i < a; ++i){
printf("(%d,%d),",A[i].a, A[i].b);
}
for (int i = 0; i < b; ++i){
printf("(%d,%d),",B[i].a, B[i].b);
}
printf("\n");
}

int main(int argc, char const *argv[]){
johnson();
return 0;
}

@qingdujun
2017-11-30 北京 怀柔

References：
[1] 陈玉福.计算机算法设计与分析，122-123
[2] https://blog.csdn.net/qingdujun/article/details/78674788

08-09
12-21 1087

08-18 598
07-11 3080
11-25
07-31 5918