#include #include #include /* Sorting criteria */ #define START_TIME 0 #define END_TIME 1 /* Parameters for generation of random intervals */ #define MAX_START 1000 #define MAX_LENGTH 100 typedef struct { unsigned int start; /* Left end */ unsigned int end; /* Right end */ } interval; /* Generate a list of n random intervals */ interval *genIntervals ( unsigned int n ) { interval *A; unsigned i; if (n == 0) return NULL; A = (interval *)malloc(n * sizeof(interval)); for (i=0; i= n1) B[i++] = A2[i2++]; else if (i2 >= n2) B[i++] = A1[i1++]; else if (A1[i1].start <= A2[i2].start) B[i++] = A1[i1++]; else B[i++] = A2[i2++]; } } else if (flag == END_TIME) { while ((i1 < n1) || (i2 < n2)) { if (i1 >= n1) B[i++] = A2[i2++]; else if (i2 >= n2) B[i++] = A1[i1++]; else if (A1[i1].end <= A2[i2].end) B[i++] = A1[i1++]; else B[i++] = A2[i2++]; } } else { fprintf(stderr, "*** Error in merge(): Unknown sorting criterion\n"); free(B); exit(1); } for (i=0; i> 1); n2 = n - n1; mergeSort(A,n1,flag); mergeSort(A+n1,n2,flag); merge(A,n1,A+n1,n2,flag); } /* Print the intervals, six in a line */ void printIntervals ( interval *A, unsigned int n ) { int i; for (i=0; i= n) break; S[j++] = A[i++]; } printf("--- Total number of intervals selected = %u\n", j); printIntervals(S,j); util= 0; for (i=0; i u1, else it stores the index of the next compatible interval */ i, j, L, R, M; /* Used in the binary search */ if (n == 0) return; U = (unsigned int *)malloc(n * sizeof(unsigned int)); V = (int *)malloc(n * sizeof(int)); /* The smallest subproblem of one interval (the last one) */ U[n-1] = A[n-1].end - A[n-1].start; V[n-1] = n; /* Iteratively solve bigger and bigger subproblems */ i = n-2; for (i=n-2; i>=0; --i) { u1 = A[i].end - A[i].start; /* The ith interval is included, so the length of this interval initializes u1 */ /* Now locate the next compatible interval, if any */ x = A[i].end; L = i+1; R = n-1; if (A[R].start < x) { /* No following interval is compatible */ L = n; } else { /* Do binary search among (i+1)st to (n-1)st intervals */ while (L < R) { M = (L + R) / 2; if (A[M].start >= x) R = M; else L = M+1; } u1 += U[L]; } u2 = U[i+1]; /* The ith interval is excluded */ if (u1 >= u2) { /* Better utilization if the i-th interval is included */ U[i] = u1; V[i] = L; /* L stores the index of the next compatible interval */ } else { /* Better utilization if the i-th interval is excluded */ U[i] = u2; V[i] = 0; } } printf("Total utilization = %u (computed by the d-p approach)\n", U[0]); S = (interval *)malloc(n * sizeof(interval)); i = j = 0; while (i < n) { if (V[i]) { /* Include the i-th interval in the schedule */ S[j++] = A[i]; /* Append the i-th interval to the optimal schedule */ i = V[i]; /* Change i to the index of the next compatible interval */ } else { /* Exclude the i-th interval from the schedule */ ++i; /* Search from the next index */ } } printf("--- Total number of intervals selected = %u\n", j); printIntervals(S,j); util= 0; for (i=0; i 1) n = atoi(argv[1]); else scanf("%u",&n); A = genIntervals(n); printIntervals(A,n); printf("\n+++ Schedule 1: Earliest finish time first\n"); mergeSort(A,n,END_TIME); printIntervals(A,n); schedule(A,n); printf("\n+++ Schedule 2: Earliest start time first\n"); mergeSort(A,n,START_TIME); printIntervals(A,n); schedule(A,n); printf("\n+++ Schedule 3: Dynamic programming\n"); printf("The intervals are already sorted with respect to start time\n"); maxutil(A,n,START_TIME); exit(0); }