# An example of data structure two machine scheduling problem

• 2020-05-27 06:37:04
• OfStack

An example of data structure two - machine scheduling problem

1. Problem description

Two-machine scheduling problem, also known as single-task optimal scheduling: n jobs are handled by two processors, A and B. Let's say the time required for the i job to be processed by machine A is a[i]; if processed by machine B, the time required is b[i]. Each job is now required to be handled by only one machine, and each machine cannot handle two jobs at the same time. A dynamic programming algorithm is designed to minimize the time it takes the two machines to process the n jobs (the total time from the start of any machine to the end of the last machine).

Consider an example: n=6, a = {2, 5, 7, 10, 5, 2}, b = {3, 8, 4, 11, 3, 4}.

Code 2.

``````
#include <iostream>
#include <stdlib.h>
using namespace std;

int max(int a,int b){
return a>b?a:b;
}

int min(int a,int b){
return a<b?a:b;
}

int main(){
int a[6]={2,5,7,10,5,2};
int b[6]={3,8,4,11,3,4};
int sum_a=0,sum_b=0,T=0,n=6;

for (int i = 1; i <=n; i++)
{
T=max(T,min(sum_a+a[i-1],sum_b+b[i-1]));
if(sum_a+a[i-1]>sum_b+b[i-1]){
sum_b+=b[i-1];
cout<<" task "<<i<<" Assigned to B do "<<endl;
}else{
sum_a+=a[i-1];
cout<<" task "<<i<<" Assigned to A do "<<endl;
}
}
cout<<" The total time is: "<<T<<endl;
}

``````

Results 3.

``````
yaopans-MacBook-Pro:algorithm yaopan\$ g++ exercise5-2.cpp
yaopans-MacBook-Pro:algorithm yaopan\$ ./a.out
task 1 Assigned to A do
task 2 Assigned to A do
task 3 Assigned to B do
task 4 Assigned to B do
task 5 Assigned to A do
task 6 Assigned to A do
The total time is: 15

``````

The above is the data structure of the dual machine scheduling example, if you have questions please leave a message or to the site community exchange discussion, thank you for reading, hope to help you, thank you for the support of the site!

Related articles: