You are on page 1of 11

DATA STRUCTURES AND

ALGORITHMS
ASSIGNMENT

ARASU S [22R202]
SRI GUBERA SAMBATH S
[22R247]
PROBLEM STATEMENT
You are given a set of jobs, each with a specified processing time, release time,
and deadline. Additionally, there is a limited number of resources available, and
each job requires a certain number of resources to execute. Your task is to
schedule the jobs on the available resources in such a way that the total number
of late jobs (jobs completed after their deadline) is minimized.

CONSTRAINTS
1. Each job has a unique ID, processing time, release time, deadline, and
resource requirements.
2. There is a limited number of resources available.
3. Jobs cannot be preempted (once started, a job must finish).
4. Minimize the total number of late jobs.

APPROACH
This problem involves a combination of concepts from job scheduling, resource
allocation, and deadline management. A feasible solution requires considering
the release times, deadlines, and resource constraints. You may need to
implement various algorithms, such as priority scheduling, to optimize the
schedule.
CODE
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <iomanip>

using namespace std;

struct Job {
int id;
int processingTime;
int releaseTime;
int deadline;
vector<int> resourcesRequired;

bool operator<(const Job& other) const {


return deadline < other.deadline;
}
};

class Resource {
public:
vector<int> availableResources;
vector<int> totalResources;
Resource(const vector<int>& resources) : availableResources(resources),
totalResources(resources) {}

bool allocateResources(const vector<int>& resources) {


for (size_t i = 0; i < resources.size(); ++i) {
if (resources[i] > availableResources[i]) {
return false;
}
}

for (size_t i = 0; i < resources.size(); ++i) {


availableResources[i] -= resources[i];
}

return true;
}

void deallocateResources(const vector<int>& resources) {


for (size_t i = 0; i < resources.size(); ++i) {
availableResources[i] += resources[i];
}
}
};

class JobScheduler {
private:
vector<Job> jobs;
Resource resource;
int totalLateJobs;
int totalJobs;
int totalProcessingTime;
vector<vector<char>> ganttChart;

public:
JobScheduler(const vector<Job>& jobs, const vector<int>&
availableResources):
jobs(jobs),
resource(availableResources),
totalLateJobs(0),
totalJobs(0),
totalProcessingTime(0) {}

void scheduleJobs() {
initializeGanttChart();
vector<Job> scheduledJobs;
priority_queue<Job> jobQueue;

int currentTime = 0;

for (const Job& job : jobs) {


totalJobs++;
totalProcessingTime += job.processingTime;

while (!jobQueue.empty() && currentTime < job.releaseTime) {


executeQueuedJob(jobQueue, currentTime, scheduledJobs);
}

if (canScheduleJob(job)) {
scheduleJob(job, currentTime, scheduledJobs);
} else {
enqueueJob(jobQueue, job);
}
}

executeRemainingJobs(jobQueue, currentTime, scheduledJobs);


printGanttChart();
printScheduleDetails(scheduledJobs);
printResourceUsage();
}

private:
void initializeGanttChart() {
ganttChart.resize(jobs.size(), vector<char>(totalProcessingTime, '.')); // '.'
represents idle time
}

void executeQueuedJob(priority_queue<Job>& jobQueue, int& currentTime,


vector<Job>& scheduledJobs) {
Job currentJob = jobQueue.top();
jobQueue.pop();
if (currentTime > currentJob.deadline) {
++totalLateJobs;
}
executeJob(currentJob, currentTime, scheduledJobs);
}

bool canScheduleJob(const Job& job) const {


return resource.allocateResources(job.resourcesRequired);
}

void scheduleJob(const Job& job, int& currentTime, vector<Job>&


scheduledJobs) {
if (currentTime > job.deadline) {
++totalLateJobs;
}

executeJob(job, currentTime, scheduledJobs);


}

void enqueueJob(priority_queue<Job>& jobQueue, const Job& job) {


jobQueue.push(job);
}

void executeJob(const Job& job, int& currentTime, vector<Job>&


scheduledJobs) {
for (int t = 0; t < job.processingTime; ++t) {
ganttChart[job.id - 1][currentTime + t] = 'A' + job.id - 1;
}

currentTime += job.processingTime;
scheduledJobs.push_back(job);
resource.deallocateResources(job.resourcesRequired);
}

void executeRemainingJobs(priority_queue<Job>& jobQueue, int&


currentTime, vector<Job>& scheduledJobs) {
while (!jobQueue.empty()) {
executeQueuedJob(jobQueue, currentTime, scheduledJobs);
}
}

void printGanttChart() const {


cout << "\nGantt Chart:" << endl;
for (size_t i = 0; i < ganttChart.size(); ++i) {
cout << "Job " << i + 1 << ": ";
for (const char& slot : ganttChart[i]) {
cout << slot << ' ';
}
cout << endl;
}
}

void printScheduleDetails(const vector<Job>& scheduledJobs) const {


cout << "\nJob Scheduling Summary:" << endl;
cout << "Total Jobs: " << totalJobs << endl;
cout << "Total Processing Time: " << totalProcessingTime << endl;
cout << "Total Late Jobs: " << totalLateJobs << endl;
}

void printResourceUsage() const {


cout << "\nResource Usage:" << endl;
for (size_t i = 0; i < resource.totalResources.size(); ++i) {
cout << "Resource " << i + 1 << ": ";
cout << "Total: " << resource.totalResources[i] << " ";
cout << "Available: " << resource.availableResources[i] << endl;
}
}
};

int main() {
vector<Job> jobs = {
{1, 5, 0, 8, {2, 1}},
{2, 4, 2, 9, {1, 2}},
{3, 3, 4, 11, {3, 2}}
};
vector<int> availableResources = {3, 3};
JobScheduler scheduler(jobs, availableResources);
scheduler.scheduleJobs();
return 0;
}
PSEUDOCODE:
1. Sort jobs based on their deadlines in ascending order.
2. Initialize a Gantt chart and other variables.
3. Initialize a priority queue for jobs based on release times.
4. Initialize a resource manager to track available resources.
5. Initialize a list to store scheduled jobs.
6. Iterate through each job:
a. While the job queue is not empty and the current time is less than the
release time of the current job:
i. Execute queued jobs.
b. If the job can be scheduled:
i. Schedule the job, update Gantt chart, and deallocate resources.
c. Else, enqueue the job.
7. Execute remaining jobs in the queue.
8. Print Gantt chart, scheduling summary, and resource usage.
OUTPUT:
Gantt Chart:
Job 1: ..... A A A A A A A A
Job 2: .. B B B B B B B B B
Job 3: ......... C C C .....

Job Scheduling Summary:


Total Jobs: 3
Total Processing Time: 12
Total Late Jobs: 1

Resource Usage:
Resource 1: Total: 3 Available: 0
Resource 2: Total: 3 Available: 0

You might also like