Multi-Machine scheduling problem (not preemptive dispatch)

Source: Internet
Author: User
Tags int size

I. Description of the problem

A project has an identical m machine, we have n tasks to deal with, task I processing time for TI. scheduling refers to the task of assigning tasks to the machine at run time, so that:
(1) A machine can only handle one task at a time;
(2) A task cannot be handled on both machines at the same time;
(3) The processing time of a task I is TI time unit.
and find out the minimum time required to complete all tasks.

Second, solve the idea

Greedy strategy with the longest processing time task priority:
When the n≤m, as long as the machine I of [0, TI] time interval assigned to task I can;
When N>m, the N tasks are sorted from large to small according to their required processing time, and then the tasks are assigned to the idle machine in turn.

Third, the specific code

(1) MinHeap.h


Template<class T> class Minheap {private://element Array, No. 0 number position also stores element T *heap;
    The current number of elements int currentsize;

    The maximum number of elements that can be accommodated int maxSize;
    From the top down, make the keyword small byte on the upper void Filterdown (const int start, const int end);

adjust void Filterup (int start) from bottom to top;
    public:minheap (int n = 1000);

    ~minheap ();
    Insert element bool Insert (const T &x);
    Get the minimum element T getmin ();

    Delete the minimum element T removemin ();
    BOOL IsEmpty () const;
    BOOL Isfull () const;
void Clear ();

};
    Template<class t> minheap<t>::minheap (int n) {maxSize = n;
    heap = new T[maxsize];
currentsize = 0;

} template<class t> Minheap<t>::~minheap () {delete [] heap;} Adjust the template<class t> void Minheap<t>::filterup (int start) {/I to the parent node of j = j = Start, I =
    (j-1)/2;

    T temp = heap[j];
        while (J > 0) {if (Heap[i] <= temp) break; else {Heap[j] =Heap[i];
            j = i;
        i = (i-1)/2;
} Heap[j] = temp; }//Top-down adjustment, so that the key small node on the upper Template<class t> void minheap<t>::filterdown (const int start, const int end) {in
    t i = start, j = 2*i + 1;

    T temp = heap[i];
        while (j <= end) {if ((J < End) && (Heap[j] > heap[j + 1])) J + +;
        if (temp <= heap[j]) break;
            else {Heap[i] = heap[j];
            i = j;
        j = 2*j + 1;
} Heap[i] = temp; } template<class t> bool Minheap<t>::insert (const T &x) {if (currentsize = = MaxSize) return f

    Alse;
    Heap[currentsize] = x;

    Filterup (currentsize);
    currentsize++;
return true;

} template<class t> T minheap<t>::getmin () {return heap[0];}
    Template<class t> T Minheap<t>::removemin () {T x = heap[0];

    Heap[0] = heap[currentsize-1];
    currentsize--; Adjust the newRoot node Filterdown (0, currentSize-1);
return x;

} template<class t> bool Minheap<t>::isempty () const {return currentsize = = 0;}

Template<class t> BOOL Minheap<t>::isfull () const {return currentsize = = maxSize;} Template<class t> void Minheap<t>::clear () {currentsize = 0;}

(2) main.cpp

/* Greedy algorithm, multi-machine scheduling problem (not preemptive scheduling)/#include <iostream> #include "MinHeap.h" using namespace std;
    Class Jobnode {Public:operator int () const {return time;
    }//task ID int id;
The task takes an int time;

};
    Class Machinenode {Public:operator int () const {return avail;
    }//machine ID int id;
Machine working time int avail;

};

Greedy algorithm template<class t> void greedy (T a[], int n, int m);

Select Sort, from large to small sort template<class t> void Selectsort (T a[], int n);
    int main () {//task quantity int N;

    Number of machines int M;
    cout<< "Please enter the number of tasks:";
    cin>>n;
    cout<<endl;
    cout<< "Please enter the number of machines:";
    cin>>m;

    cout<<endl;
    The processing time required by each task Jobnode *a = new Jobnode[n];
    /*a[0].id = 0;
    A[0].time = 0;*/cout<< "Please enter the processing time required for each task:" <<endl;
    cout<<endl;
        for (int i = 0; i < N; i++) {a[i].id = i + 1; The processing time required for the cout<< "<<i+1<<" task is: ";
        cin>>a[i].time;
    cout<<endl;

    } greedy (A, N, M);
    Delete[] A;
return 0; } template<class t> void greedy (T a[], int n, int m) {//Machine number more than the number of tasks, directly assign if (n <= m) {cout& lt;< "Assign a machine directly to each task.
        "<<endl;
    Return
    //select sort, from large to small selectsort (A, n);
    Minheap<machinenode> H (m);

    Machinenode x;
        for (int i = 1; I <= m; i++) {x.avail = 0;
        X.id = i;
    H.insert (x);
        for (int i = 0; i < n; i++) {x = H.removemin (); cout<< "Machine" <<x.id<< "from" <<x.avail<< "to" << (X.avail + a[i].time) << "
        Time period assigned to the task <<a[i].id<<endl;
        X.avail + = A[i].time;
    Inserts x into the appropriate position in the heap according to the new avail value h.insert (x);
    } cout<<endl;
    The completion time or the schedule length int *mac_time = new Int[m];
        for (int i = 1; I <= m; i++) {x = H.removemin ();
        Mac_time[i-1] = X.avail; Cout<< "Machine" <<x.id<< "Completion time is:" <<x.avail<<endl;
    int finshtime = mac_time[0];
    for (int i = 1; i < m i++) {if (Mac_time[i] > Finshtime) finshtime = Mac_time[i];
    } cout<<endl;

    cout<< "Completion time or schedule length is:" <<finshTime<<endl;
delete [] mac_time;
    } template<class t> void Selectsort (T a[], int n) {bool sorted = false;
        for (int size = n;!sorted && size > 1; size--) {int indexofmin = 0;
        sorted = true; Find the smallest element for (int i = 1; i < size; i++) {if (A[indexofmin] >= a[i)) Inde
            Xofmin = i;
        else sorted = false;
            ///Exchange if (indexofmin!= size-1) {T temp = a[indexofmin];
            A[indexofmin] = a[size-1];
        A[SIZE-1] = temp; }
    }
}

(3) Simple code to solve the minimum finish time

#include <iostream> #include <algorithm> #include <string.h> using namespace std;  
int speed[10010];  
int mintime[101];  
BOOL CMP (const int &AMP;X, const int &y) {return x > y;
    int main () {//task number int N;

    Number of machines int M;
    cout<< "Please enter the number of tasks:";
    cin>>n;
    cout<<endl;
    cout<< "Please enter the number of machines:";
    cin>>m; 

    cout<<endl;  
    memset (speed, 0, sizeof (speed));  

    memset (mintime, 0, sizeof (mintime));
        for (int i = 0; i < N; ++i) {cout<<] The processing time required for the "<<i+1<<" task is: ";  
        cin>>speed[i];
    cout<<endl;  
    Sort (speed, speed + N, CMP);   
        for (int i = 0; i < n; ++i)//At the same time satisfy n <= m and n > M {//When Mac >= task is replicated, the minimum element additive (greedy pour)  
    *min_element (mintime, mintime + M) = Speed[i]; //Maximum total time cout<< "completion time or schedule length:" <<*max_element (mintime, Mintime + M) <<endl;  
return 0;   }

Four, the example solves

The processing time for 7 separate tasks is {2,14,4,16,6,5,3} processed on 3 machines, and the scheduling process is:
(1) input

(2) Output

Therefore, the minimum time to complete all tasks is 17.

V. Complexity analysis

When n>m, selecting the sort time consuming O (NLOGN), initializing the heap takes an O (m), the heap removemin and inserts are time-consuming O (NLOGM), so the algorithm's complexity is O (NLOGN).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.