The problem of arranging the venue greedy algorithm __ algorithm

Source: Internet
Author: User

Problem Description:

Suppose you want to arrange a batch of events in enough venues and want to use as few venues as possible. Design an effective greedy algorithm for scheduling (this problem is in fact the famous graph coloring problem.) If each activity is used as a vertex of the graph, the incompatible activity is connected with the edge. So that the adjacent vertices have a different color of the minimum coloring number, corresponding to the minimum number of venues to be found.

Question Answer:

<1>, using greedy choice strategy to solve the problem of venue arrangement.

The greedy algorithm is an important two properties: the greedy selection property and the optimal substructure property.

1. The greedy choice nature of the problem

Proof: First of all, the venue to arrange the problem of mathematics, with n activities of the set e= {1, 2, ..., n}, each activity I have a request to use the venue of the start when asked Si and an end to ask Fi. That is, K is the minimum number of venues required. Set activity has been sorted, (A1, A2, ..., AK) is required for K a venue that has been arranged for activities. ① when k = 1 o'clock, that is, all activities are compatible in a venue, A1 is the optimal solution to satisfy the greedy choice; ② when k>= 2 o'clock, take B1=a1,bk=ak (i.e. BK is a venue for M activities, (N-M) events are arranged in B1 to bk-1 venues). Is (N-M) the event arrangement needs (K-1) a meeting place. Then (b1,b2,..., bk-1) is a feasible solution of (n-m) activity. On the other hand, it is known by {(A1, A2,..., ak)-ak}= (B1,B2,..., bk-1) that (b1,b2,..., bk-1) is also the optimal solution to satisfy the greedy choice nature, so the problem of arranging the venue is greedy.

2, the optimal substructure property of the problem

Proof: (A1,A2, ..., AK) is n an active set e= {1,2, ..., n} is the optimal solution for the venue required. The A1 is arranged in M-compatible activities, so that is to say (N-M) a full arrangement of the event needs to k-1 a venue. Suppose (n-m) an activity arrangement only needs to k-2 a meeting place or fewer venues. That is to say n an activity arrangement only needs to k-1 a meeting room or less meeting place can arrange to finish, then appear contradictory before and after. When the optimal solution of a problem contains the optimal solution of the sub problem, the problem is called the optimal substructure property.

<2>, algorithm to achieve the idea:

1, first define a time class node, there are two Data,flag attributes, where data is the time value, flag is to determine whether the activity begins or ends.

2, all the time objects are sorted into an array by the. Data, where the merge sort method is selected to sort.

3 The meaning of the greedy selection of the algorithm is to maximize the remaining available time periods so as to arrange as many compatible activities as possible. Take out the data of the array in turn, judge its attribute flag, if a start time, then count++, if the end time count--, the maximum count value is the minimum number of venues required.

<3>, algorithmic Program:

#include <iostream> #include <fstream> using namespace std; Active Time class Node {Public:int data;//time value bool flag;//judge whether to start or end, 0 to start, 1 to end bool operator< (node &sec

ondrational);//Compare the size of two objects by time value};
        The size of the time value in the comparison class bool node::operator< (Node &secondrational) {if (this->data-secondrational.data) <0)
    return true;
else return false; ///copy array function template<typename t> void arraycopy (T source[],int sourcestartindex,t Target[],int,
int length);
Merging array functions template<typename t> void Merge (T list1[],int list1size,t list2[],int list2size,t]); Merge sort function template<typename t> void mergesort (T list[],int arraysize) {if (arraysize>1) {//copy sort first half
        Partial array T *firsthalf=new T[ARRAYSIZE/2];
        Arraycopy (LIST,0,FIRSTHALF,0,ARRAYSIZE/2);

        MergeSort (FIRSTHALF,ARRAYSIZE/2);
        Copy sort the latter half of the array int secondhalflength=arraysize-arraysize/2; T *secondhalf=new T[secondhaLflength];
        Arraycopy (list,arraysize/2,secondhalf,0,secondhalflength);

        MergeSort (secondhalf,secondhalflength);
        Merge and replicate two fractional group T *temp=new T[arraysize];
        Merge (Firsthalf,arraysize/2,secondhalf,secondhalflength,temp);

        Arraycopy (temp,0,list,0,arraysize);
        delete []temp;
        delete []firsthalf;
    delete []secondhalf;
}///merge two arrays into one array in order of size template<typename t> void merge (T list1[],int list1size,t list2[],int list2size,t temp[])
    {int current1=0;
    int current2=0;

    int current3=0; while (current1<list1size&¤t2<list2size) {if (List1[current1]<list2[current2]) TEMP[CU
        Rrent3++]=list1[current1++];
    else temp[current3++]=list2[current2++];
    while (current1<list1size) temp[current3++]=list1[current1++];
while (current2<list2size) temp[current3++]=list2[current2++]; ///Copy an array to another array template<typename t> void arrayCopy (T source[],int sourcestartindex,t target[],int targetstartindex,int length) {for (int i=0;i<length;i++) {
    Target[i+targetstartindex]=source[i+sourcestartindex];
    }//Minimum number of venues function int Greedyplan (Node f[],int n) {int count=0;
    int maxcount=0; /* Traversal activity time, if this time is the start time, then count+1, for the end time, then Count-1 the maximum count value, and assign the count value to Maxcount, this maxcount value is the least number of venues/fo
            R (int i=0;i<n;i++) {if (f[i].flag==0)//If this time is the start time count+1 {count++;
            if (Count>maxcount)//records the maximum Count value {Maxcount=count in the secondary loop;
        } else//If the end time Count-1 {count--;
} return maxcount;
    int main () {//Read the data in the input file fstream fin;
    Fin.open ("Input.txt", ios::in); if (Fin.fail ()) {cout<< "File does not exist!"
        <<endl;
        cout<< "Exit Program" <<endl;
    return 0;
    } int n;

    fin>>n; Establish two NoAn array of de types for storing the start time and end time Node *a=new Node[n];
    Node *b=new Node[n];
        for (int i=0;i<n;i++) {fin>>a[i].data;
    fin>>b[i].data;    
    ///The start time is expressed as 0 for (int j=0;j<n;j++) {a[j].flag=0;    
    ///The end time is expressed as 1 for (int k=0;k<n;k++) {b[k].flag=1;
    //Create a node-type array, copy the data from the first two arrays into this array, node *c=new node[2*n];
    Arraycopy (A,0,c,0,n);

    Arraycopy (B,0,c,n,n);

    The array c is sorted by time value size, which is a stable merge sort mergesort (C,2*n);
    Call the least number of venues function int Mm=greedyplan (C,2*N);

    Console output cout<< "the minimum number of venues is:" <<mm<<endl;
    Writes the result data to the output file FStream fout;
    Fout.open ("Output.txt", ios::out);

    fout<<mm;
    Fin.close ();
    Fout.close ();
    System ("pause");
return 0; }


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.