Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ASSIGNMENT-2

Name : V. Rohith Kumar

Roll No : B20ME040

Sub : POM

1 Ans:

import java.util.*;

public class Main


{
public static void main(String[] args)
{
String[] jobs = {"A", "B", "C", "D", "E"};
int[] processTime = {25, 10, 22, 34, 12};
int[] dueDate = {0, 0, 0, 0, 0};

String[] sptSequence = calculateSPTSequence(jobs, processTime);


String[] eddSequence = calculateEDDSequence(jobs, dueDate);

System.out.println("i) Optimal Job Sequence based on SPT: " + Arrays.toString(sptSequence));


System.out.println(" Optimal Job Sequence based on EDD: " +
Arrays.toString(eddSequence));

double meanFlowTimeSPT = calculateMeanFlowTime(sptSequence, processTime);


double meanFlowTimeEDD = calculateMeanFlowTime(eddSequence, processTime);

System.out.printf("ii) Mean Flow Time (SPT): %.2f days\n", meanFlowTimeSPT);


System.out.printf(" Mean Flow Time (EDD): %.2f days\n", meanFlowTimeEDD);
}
private static String[] calculateSPTSequence(String[] jobs, int[] processTime)
{
int n = jobs.length;
String[] sequence = new String[n];

int[] remainingProcessTime = Arrays.copyOf(processTime, n);

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


int minIndex = findMinIndex(remainingProcessTime);
sequence[i] = jobs[minIndex];
remainingProcessTime[minIndex] = Integer.MAX_VALUE;
}

return sequence;
}

private static String[] calculateEDDSequence(String[] jobs, int[] dueDate)


{
int n = jobs.length;
String[] sequence = new String[n];

int[] remainingDueDate = Arrays.copyOf(dueDate, n);

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


int minIndex = findMinIndex(remainingDueDate);
sequence[i] = jobs[minIndex];
remainingDueDate[minIndex] = Integer.MAX_VALUE;
}

return sequence;

}
private static int findMinIndex(int[] array)
{
int minIndex = 0; for (int i
= 1; i < array.length; i++)
{
if (array[i] < array[minIndex])
{
minIndex = i;
}
}
return minIndex;
}

private static double calculateMeanFlowTime(String[] sequence, int[] processTime)


{
int n = sequence.length; int[]
completionTime = new int[n];

completionTime[0] = processTime[0];
for (int i = 1; i < n; i++)
{
completionTime[i] = completionTime[i - 1] + processTime[i];
}

double meanFlowTime = 0;
for (int i = 0; i < n; i++)
{
meanFlowTime += completionTime[i];
}

return meanFlowTime / n;
}
}

2 Ans :
#include <stdio.h>

#define N 9

void calculateCriticalPath(int to[N], int tm[N], int tp[N], int dependencies[N][2], int
earliestStart[N], int latestStart[N])
{ int
i, j;

for (i = 0; i < N; i++)


{
int activity = i + 1;
earliestStart[activity] = to[i];

for (j = 0; j < N; j++)


{
if (dependencies[j][1] == activity)
{
if (earliestStart[activity] < earliestStart[dependencies[j][0]])
earliestStart[activity] = earliestStart[dependencies[j][0]];
}
}
}

for (i = N - 1; i >= 0; i--)


{
int activity = i + 1;
latestStart[activity] = earliestStart[N];
for (j = 0; j < N; j++) { if (dependencies[j][0] ==
activity) { if (latestStart[activity] >
latestStart[dependencies[j][1]]) latestStart[activity]
= latestStart[dependencies[j][1]];
}
}
}
}

void printProjectNetwork(int dependencies[N][2])


{
int i;

printf("Project Network:\n");
for (i = 0; i < N; i++) {
printf("%d-%d\n", dependencies[i][0], dependencies[i][1]);
}
printf("\n");
}

int main() {
int to[N] = {3, 2, 6, 2, 5, 3, 3, 1, 4}; int
tm[N] = {6, 5, 12, 5, 11, 6, 9, 4, 19}; int
tp[N] = {15, 14, 30, 8, 17, 15, 27, 7, 28};

int dependencies[N][2] = {
{1, 2}, {1, 6}, {2, 3}, {2, 4},
{3, 5}, {4, 5}, {6, 7}, {5, 8}, {7, 8}
};

int earliestStart[N + 1];


int latestStart[N + 1];
printProjectNetwork(dependencies);

calculateCriticalPath(to, tm, tp, dependencies, earliestStart, latestStart);

int projectCompletionTime = earliestStart[N];


printf("Critical Path: ");
for (int i = 0; i < N; i++)
{
if (earliestStart[i + 1] == latestStart[i + 1])
{
printf("%d ", i + 1);
}
}
printf("\nProject Completion Time: %d days\n", projectCompletionTime);

int standardDeviation = (tp[0] - to[0] + 4 * (tm[0] - to[0] + 2 * (tp[0] - tm[0]))) / 6;


int variance = standardDeviation * standardDeviation;

printf("Standard Deviation: %d days\n", standardDeviation);


printf("Variance: %d days^2\n", variance);

return 0;
}

You might also like