{"id":25116,"date":"2017-10-15T14:34:21","date_gmt":"2017-10-15T09:04:21","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25116"},"modified":"2017-10-15T14:34:21","modified_gmt":"2017-10-15T09:04:21","slug":"job-sequencing-problem","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/job-sequencing-problem\/","title":{"rendered":"Job Sequencing Problem"},"content":{"rendered":"<p>Given an array of jobs where every job has a deadline and associated profit if the job is finished before the deadline. <span id=\"more-132724\"><\/span>It is also given that every job takes single unit of time, so the minimum possible deadline for any job is 1. How to maximize total profit if only one job can be scheduled at a time.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input: Four Jobs with following deadlines and profits\r\n  JobID    Deadline      Profit\r\n    a        4            20   \r\n    b        1            10\r\n    c        1            40  \r\n    d        1            30\r\nOutput: Following is maximum profit sequence of jobs\r\n        c, a   \r\n\r\n\r\nInput:  Five Jobs with following deadlines and profits\r\n   JobID     Deadline     Profit\r\n     a         2           100\r\n     b         1           19\r\n     c         2           27\r\n     d         1           25\r\n     e         3           15\r\nOutput: Following is maximum profit sequence of jobs\r\n        c, a, e\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>We strongly recommend to minimize your browser and try this yourself first.<\/strong><\/p>\n<p>A <strong>Simple Solution<\/strong> is to generate all subsets of given set of jobs and check individual subset for feasibility of jobs in that subset. Keep track of maximum profit among all feasible subsets. The time complexity of this solution is exponential.<\/p>\n<p>This is a standard <a href=\"http:\/\/www.geeksforgeeks.org\/greedy-algorithms-set-1-activity-selection-problem\/\" target=\"_blank\" rel=\"noopener noreferrer\">Greedy Algorithm <\/a>problem. Following is algorithm.<\/p>\n<pre>1) Sort all jobs in decreasing order of profit.\r\n2) Initialize the result sequence as first job in sorted jobs.\r\n3) Do following for remaining n-1 jobs\r\n.......a) If the current job can fit in the current result sequence \r\n          without missing the deadline, add current job to the result.\r\n          Else ignore the current job.<\/pre>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Program to find the maximum profit job sequence from a given array<br\/>\/\/ of jobs with deadlines and profits<br\/>#include&lt;iostream&gt;<br\/>#include&lt;algorithm&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ A structure to represent a job<br\/>struct Job<br\/>{<br\/>   char id;      \/\/ Job Id<br\/>   int dead;    \/\/ Deadline of job<br\/>   int profit;  \/\/ Profit if job is over before or on deadline<br\/>};<br\/> <br\/>\/\/ This function is used for sorting all jobs according to profit<br\/>bool comparison(Job a, Job b)<br\/>{<br\/>     return (a.profit &gt; b.profit);<br\/>}<br\/> <br\/>\/\/ Returns minimum number of platforms reqquired<br\/>void printJobScheduling(Job arr[], int n)<br\/>{<br\/>    \/\/ Sort all jobs according to decreasing order of prfit<br\/>    sort(arr, arr+n, comparison);<br\/> <br\/>    int result[n]; \/\/ To store result (Sequence of jobs)<br\/>    bool slot[n];  \/\/ To keep track of free time slots<br\/> <br\/>    \/\/ Initialize all slots to be free<br\/>    for (int i=0; i&lt;n; i++)<br\/>        slot[i] = false;<br\/> <br\/>    \/\/ Iterate through all given jobs<br\/>    for (int i=0; i&lt;n; i++)<br\/>    {<br\/>       \/\/ Find a free slot for this job (Note that we start<br\/>       \/\/ from the last possible slot)<br\/>       for (int j=min(n, arr[i].dead)-1; j&gt;=0; j--)<br\/>       {<br\/>          \/\/ Free slot found<br\/>          if (slot[j]==false)<br\/>          {<br\/>             result[j] = i;  \/\/ Add this job to result<br\/>             slot[j] = true; \/\/ Make this slot occupied<br\/>             break;<br\/>          }<br\/>       }<br\/>    }<br\/> <br\/>    \/\/ Print the result<br\/>    for (int i=0; i&lt;n; i++)<br\/>       if (slot[i])<br\/>         cout &lt;&lt; arr[result[i]].id &lt;&lt; &quot; &quot;;<br\/>}<br\/> <br\/>\/\/ Driver program to test methods<br\/>int main()<br\/>{<br\/>    Job arr[] = { {&#039;a&#039;, 2, 100}, {&#039;b&#039;, 1, 19}, {&#039;c&#039;, 2, 27},<br\/>                   {&#039;d&#039;, 1, 25}, {&#039;e&#039;, 3, 15}};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    cout &lt;&lt; &quot;Following is maximum profit sequence of jobs\\n&quot;;<br\/>    printJobScheduling(arr, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"output\"><strong>Output:<\/strong><\/h4>\n<pre>Following is maximum profit sequence of jobs\r\nc a e<\/pre>\n<p><strong>Time Complexity<\/strong> of the above solution is O(n<sup>2<\/sup>). It can be optimized using Disjoint Set Data Structure. Please refer below post for details.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Job Sequencing Problem &#8211; Greedy Algorithm &#8211; Given array of jobs where every job has deadline and associated profit if job is finished before the deadline.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,70144],"tags":[71435,71424,71434,71479,71460,71426,71455,71476,71415,71461,71463,71453,71444,71465,71443,71425,71436,71480,71466,71469,71441,71467,71462,71439,71429,71418,71437,71473,71417,71428,71468,71430,71459,71432,71433,71427,71452,71451,71440,71464,71438,71448,71423,71446,71412,71456,71442,71471,71449,71421,71454,71472,71431,71445,71422,71470,71457,71475,71458,71474,71414,71419,71420,71477,71450,71447,71478,71416,71413],"class_list":["post-25116","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-greedy-algorithm","tag-algorithm-for-job-sequencing-with-deadlines","tag-algorithm-jobs","tag-define-job-shop-production","tag-define-sequencing-problem","tag-difference-between-job-scheduling-and-process-scheduling","tag-edd-jobs","tag-example-of-job-shop","tag-example-of-knapsack-problem-using-greedy-method","tag-fcfs-scheduling","tag-how-to-calculate-average-flow-time","tag-job-flow-time","tag-job-m","tag-job-n-shop","tag-job-problems","tag-job-sch","tag-job-scheduling","tag-job-scheduling-algorithm","tag-job-scheduling-algorithm-in-c","tag-job-scheduling-pdf","tag-job-scheduling-problem-example","tag-job-scheduling-problems","tag-job-scheduling-with-deadlines-greedy-algorithm","tag-job-sequence","tag-job-sequence-algorithm","tag-job-sequence-for-n-jobs-in-2-machine","tag-job-sequencing","tag-job-sequencing-algorithm","tag-job-sequencing-in-operation-research","tag-job-sequencing-with-deadlines","tag-job-sequencing-with-deadlines-algorithm","tag-job-sequencing-with-deadlines-example-using-greedy-method","tag-job-sequencing-with-deadlines-program-in-c","tag-job-shop-planning-scheduling-and-control","tag-job-shop-production-ppt","tag-job-shop-scheduling","tag-job-shop-scheduling-excel","tag-job-shop-scheduling-pdf","tag-job-shop-scheduling-ppt","tag-job-shop-scheduling-problem-example","tag-job-shop-scheduling-problem-using-genetic-algorithm","tag-job-shop-scheduling-techniques","tag-job-shopping","tag-jobs-ppt","tag-jssp-jobs","tag-knapsack-problem-example-using-greedy-method","tag-lpt-jobs","tag-n-jobs-2-machines","tag-online-scheduling-algorithms","tag-operations-research-scheduling","tag-operations-scheduling","tag-optimum-solutions-jobs","tag-parallel-job-scheduling","tag-processing-n-jobs-through-2-machines-example","tag-pseudo-code-for-greedy-algorithm","tag-pst-time","tag-scheduling-in-job-shop-type-production","tag-scheduling-optimization-problem","tag-sequence-algorithm","tag-sequence-jobs","tag-sequence-problem-solving","tag-sequencing-model-in-operation-research","tag-sequencing-problem","tag-sequencing-problem-example","tag-single-machine-scheduling","tag-slack-per-operation","tag-time-machine-scheduler","tag-what-is-job-shop-scheduling","tag-what-is-sequence","tag-what-is-sequencing-problem-in-operational-research"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25116"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}