Post

# Job Sequencing Problem In C#

This article will discuss the Job sequencing problem in C#. We will achieve it using a greedy approach algorithm.

## Problem statement

The job sequencing problem is a common problem that involves assigning a set of jobs to a set of machines, where each job has a deadline and a profit, and we have to get the maximum profit before the job deadline.

Given an array of jobs where each job has its deadline and profit. And Profit can be obtained only if the job is finished before the deadline. Also, every job takes a single unit of time, so the minimum possible deadline for any job is 1. So we have to get the maximum profit before the job deadline.

For example,

 JobID Deadline Profit 1 3 90 2 1 20 3 2 30 4 1 25 5 2 15

Output

JobID:-> 4,3,1

C# code

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JobsSequencingProblemInCSharp {
public class Jobs {
public int JobsID {
get;
set;
}
get;
set;
}
public int Profit {
get;
set;
}
}
public class JobsSequencingProblem {
public static void JobsSequencing() {
List < Jobs > Jobs = new List < Jobs > () {
new Jobs {
JobsID = 1, Deadline = 3, Profit = 90
},
new Jobs {
JobsID = 2, Deadline = 1, Profit = 20
},
new Jobs {
JobsID = 3, Deadline = 2, Profit = 30
},
new Jobs {
JobsID = 4, Deadline = 1, Profit = 25
},
new Jobs {
JobsID = 5, Deadline = 2, Profit = 15
}
};
// Sort Jobs in the decreasing order of profit
Jobs = Jobs.OrderByDescending(j => j.Profit).ToList();
// Determine the maximum deadline of job
// Initialize the slots array
foreach(Jobs Job in Jobs) {
int slot = -1;
for (int i = Job.Deadline - 1; i >= 0; i--) {
if (slots[i] == 0) {
slot = i;
break;
}
}
// If the slot is available, assign the Jobs to that slot
if (slot != -1) {
slots[slot] = Job.JobsID;
}
}
Console.WriteLine("Selected Jobs:");
for (int i = 0; i < maxDeadline; i++) {
if (slots[i] != 0) {
Console.WriteLine("Jobs " + slots[i]);
}
}
}
}
}
Public static void Main(string[] args) {
JobsSequencingProblem.JobsSequencing();