Skip to content

Commit 3bbd1ae

Browse files
committed
Algorithm for Job Scheduling algorithm
1 parent 018c47b commit 3bbd1ae

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"etc": {
7171
"name": "Uncategorized",
7272
"list": {
73-
"flood_fill": "Flood Fill"
73+
"flood_fill": "Flood Fill",
74+
"job_scheduling": "Job Scheduling Problem"
7475
}
7576
}
7677
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Job Scheduling Algorithm": "An array of jobs along with their deadline and profit (if job completes within deadline) where every job takes single unit of time. Maximize total profit if only one job can be scheduled at a time.",
3+
"Applications": [
4+
5+
],
6+
"Complexity": {
7+
"time": " O(N<sup>2</sup>)",
8+
"space": "O(N)"
9+
},
10+
"References": [
11+
"<a href='http://ocw.mit.edu/courses/civil-and-environmental-engineering/1-204-computer-algorithms-in-systems-engineering-spring-2010/lecture-notes/MIT1_204S10_lec10.pdf'>mit.edu</a>"
12+
],
13+
"files": {
14+
"job_scheduling": "Job Scheduling Algorithm"
15+
}
16+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// sort according to decreasing order of profit
2+
// Bubble sort implemented ... Implement a better algorithm for better performance
3+
for ( var i = 0; i < N - 1; i++ ) {
4+
for ( var j = 0; j < N - i - 1; j++ ) {
5+
if ( profit[j] < profit[ j + 1 ] ) {
6+
var temp = profit[j];
7+
profit[j] = profit[j+1];
8+
profit[j+1] = temp;
9+
temp = deadline[j];
10+
deadline[j] = deadline[j+1];
11+
deadline[j+1] = temp;
12+
var t = jobId[j];
13+
jobId[j] = jobId[j+1];
14+
jobId[j+1] = t;
15+
}
16+
}
17+
}
18+
19+
var slot = new Array (N);
20+
var result = new Array (N);
21+
for (var i = N - 1; i >= 0; i--) {
22+
result[i] = '-';
23+
}
24+
tracer._setData(jobId);
25+
tracer1._setData(deadline);
26+
tracer2._setData(profit);
27+
tracer3._setData(result);
28+
29+
// Initialise all slots to free
30+
for (var i = 0; i < N; i++ ) {
31+
slot[i] = 0;
32+
}
33+
34+
// Iterate through all the given jobs
35+
for ( var i = 0; i < N; i++ ) {
36+
/*
37+
Start from the last possible slot.
38+
Find a slot for the job
39+
*/
40+
tracer._select( i )._wait();
41+
tracer1._select( i )._wait();
42+
for ( var j = Math.min ( N, deadline[i]) - 1; j >= 0; j-- ) {
43+
if ( slot[j] === 0 ) {
44+
tracer3._notify( j, jobId[i] )._wait();
45+
result[j] = jobId[i];
46+
slot[j] = 1;
47+
tracer3._denotify(j);
48+
break;
49+
}
50+
}
51+
tracer._deselect( i );
52+
tracer1._deselect( i );
53+
}
54+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var jobId = ['a','b','c','d','e'];
2+
var deadline = [2,1,2,1,3];
3+
var profit = [100,19,27,25,15];
4+
var N = deadline.length;
5+
6+
var tracer3 = new Array1DTracer('Schedule');
7+
var tracer = new Array1DTracer('Job Ids');
8+
var tracer1 = new Array1DTracer('Deadlines');
9+
var tracer2 = new Array1DTracer('Profit');

0 commit comments

Comments
 (0)