Back to Course

Algorithm

0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
    Stacks
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
Lesson 27 of 43
In Progress

Job Sequencing with Deadlines

In this chapter we will see greedy algorithm examples. In this tutorial we will learn about Job Sequencing Problem with Deadline. This problem consists of n jobs each associated with a deadline and profit and our objective is to earn maximum profit.

We will earn profit only when job is completed on or before deadline. We assume that each job will take unit time to complete.

The setting is that we have n jobs, each of which takes unit time, and a processor on which we would like to schedule them in as profitable  a manner as possible. Each job has a profit associated with it, as well as a deadline; if the job is not scheduled by the deadline, then we don’t get the profit.

Definition of job sequence with deadline

A schedule S is an array: S(1),S(2)…..S(n) where S(t) € { 0, 1, 2……n} for each t € { 1, 2……n}

Job sequence with deadline strategy

  1. Are all tasks completed ? 
  2. What is the maximum profit ? 

Points to remember

  1. In this problem we have n jobs j1, j2, … jn each has an associated deadline d1, d2, … dn and profit p1, p2, … pn.
  2. Profit will only be awarded or earned if the job is completed on or before the deadline.
  3. We assume that each job takes unit time to complete.
  4. The objective is to earn maximum profit when only one job can be scheduled or processed at any given time.

Problem

Consider the following 5 jobs and their associated deadline and profit.

job-sequence-with-deadline

Sort the jobs according to their profit in descending order

Note! If two or more jobs are having the same profit then sort them as per their entry in the job list.

job-sequencing-with-deadline

Find the maximum deadline value

Looking at the jobs we can say the max deadline value is 3.
So, dmax = 3

As dmax = 3 so we will have THREE slots to keep track of free time slots. Set the time slot status to EMPTY

job-sequencing-with-deadline-example

Total number of jobs is 5.
So we can write n = 5

Note!

  1. If we look at job j2, it has a deadline 1. This means we have to complete job j2 in time slot 1 if we want to earn its profit.
  2. Similarly, if we look at job j1 it has a deadline 2. This means we have to complete job j1 on or before time slot 2 in order to earn its profit.
  3. Similarly, if we look at job j3 it has a deadline 3. This means we have to complete job j3 on or before time slot 3 in order to earn its profit.
  4. Our objective is to select jobs that will give us higher profit.

Pseudo code of Jobs Scheduling with Deadlines

for i = 1 to n do
  Set k = min(dmax, DEADLINE(i))  //where DEADLINE(i) denotes deadline of ith job

  while k >= 1 do
    if timeslot[k] is EMPTY then
      timeslot[k] = job(i)
      break
    endif

    Set k = k - 1

  endwhile

endfor

Job Sequencing with Deadlines Algorithms Source Code

See the source code of Job Sequencing with Deadlines problems with c++.

#include <bits/stdc++.h>

     using namespace std;

int main()
{
     int profit[10],dead_line[10];int maximum_profit=0,n,temp;
     cout<<"Enter Number Of Data"<<endl;
     cin>>n;
     bool box[n];
     for(int i=0; i<n; i++){
        cin>>profit[i]>>dead_line[i];
     }

     for(int i=0; i<n-1; i++){
       for(int j=i+1; j<n; j++)

        if(profit[i]<profit[j]){
 
            temp = profit[i];
            profit[i] = profit[j];
            profit[j] = temp;

            temp = dead_line[i];
            dead_line[i] = dead_line[j];
            dead_line[j] = temp;

         }
     }

      cout<<"After Sorting"<<endl;
       for(int i=0; i<n; i++){
          cout<<profit[i]<<" "<<dead_line[i]<<endl;
       }
       cout<<endl<<endl;
       memset(box,false,sizeof(box));
        for(int i=0; i<n; i++){
            for(int j=min(n,dead_line[i])-1; j>=0; j--){
                if(box[j]==false){
                      box[j]=true;
                maximum_profit +=profit[i];
                  break;
                }
            }
        }
        cout<<"Maximum Profit: "<<maximum_profit<<endl;
    return 0;
}

New Report

Close