Professional Documents
Culture Documents
Data Structures and Algorithm
Data Structures and Algorithm
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>
struct Job {
int id;
int processingTime;
int releaseTime;
int deadline;
vector<int> resourcesRequired;
class Resource {
public:
vector<int> availableResources;
vector<int> totalResources;
Resource(const vector<int>& resources) : availableResources(resources),
totalResources(resources) {}
return true;
}
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;
if (canScheduleJob(job)) {
scheduleJob(job, currentTime, scheduledJobs);
} else {
enqueueJob(jobQueue, job);
}
}
private:
void initializeGanttChart() {
ganttChart.resize(jobs.size(), vector<char>(totalProcessingTime, '.')); // '.'
represents idle time
}
currentTime += job.processingTime;
scheduledJobs.push_back(job);
resource.deallocateResources(job.resourcesRequired);
}
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 .....
Resource Usage:
Resource 1: Total: 3 Available: 0
Resource 2: Total: 3 Available: 0