Question

In: Computer Science

java code for scheduling problem (start time,end time , profite) using greedy algorithm

java code for scheduling problem (start time,end time , profite) using greedy algorithm

Solutions

Expert Solution

The following code consists of 2 classes one named job and other named Scheduling(contains Main). In order to run the code either save the code with this name (Scheduling) if using any IDE or run the class Scheduling if using CMD.

SOURCE CODE

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Java program for Scheduling in O(nLogn) time
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

// Class to represent a job
class Job
{
   int start, finish, profit;

   // Constructor
   Job(int start, int finish, int profit)
   {
       this.start = start;
       this.finish = finish;
       this.profit = profit;
   }
   Job()
   {}
}

// Used to sort job according to finish times
class JobComparator implements Comparator<Job>
{
   public int compare(Job a, Job b)
   {
       return a.finish < b.finish ? -1 : a.finish == b.finish ? 0 : 1;
   }
}

class Scheduling
{
   /* A Binary Search based function to find the latest job
   (before current job) that doesn't conflict with current
   job. "index" is index of the current job. This function
   returns -1 if all jobs before index conflict with it.
   The array jobs[] is sorted in increasing order of finish
   time. */
   static public int binarySearch(Job jobs[], int index)
   {
       // Initialize 'lo' and 'hi' for Binary Search
       int lo = 0, hi = index - 1;

       // Perform binary Search iteratively
       while (lo <= hi)
       {
           int mid = (lo + hi) / 2;
           if (jobs[mid].finish <= jobs[index].start)
           {
               if (jobs[mid + 1].finish <= jobs[index].start)
               lo = mid + 1;
               else
               return mid;
           }
           else
           hi = mid - 1;
           }
           return -1;
       }

       // The main function that returns the maximum possible
       // profit from given array of jobs
       static public int schedule(Job jobs[])
       {
           // Sort jobs according to finish time
           Arrays.sort(jobs, new JobComparator());
           // Create an array to store solutions of subproblems.
           // table[i] stores the profit for jobs till jobs[i]
           // (including jobs[i])
           int n = jobs.length;
           int table[] = new int[n];
           table[0] = jobs[0].profit;

           // Fill entries in M[] using recursive property
           for (int i=1; i<n; i++)
           {
               // Find profit including the current job
               int inclProf = jobs[i].profit;
               int l = binarySearch(jobs, i);
               if (l != -1)
               inclProf += table[l];
               // Store maximum of including and excluding
               table[i] = Math.max(inclProf, table[i-1]);
           }

           return table[n-1];
       }

       // Driver method to test above
       public static void main(String[] args)
       {
             Scanner s = new Scanner(System.in);
              System.out.print("Enter number of jobs you want: ");
              int n= s.nextInt();
            Job jobs[] = new Job[n];
           for ( int i=0; i<n; i++)
                   {
                      jobs[i]=new Job();
                  System.out.println("\nFor Job "+i);
                   System.out.println("Start Time: ");
                      jobs[i].start = s.nextInt();   // s is a Scanner object
                        System.out.println("End Time: ");
                       jobs[i].finish = s.nextInt();
                   System.out.println("Profit: ");
                        jobs[i].profit = s.nextInt();
                       System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                }    

       System.out.println("Optimal profit is " + schedule(jobs));
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For further queries in this code ask in comment. Please leave a rating/upvote. Cheers!


Related Solutions

In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
Java round robin scheduling algorithm: 10 processes arrive at the same time and the time that...
Java round robin scheduling algorithm: 10 processes arrive at the same time and the time that each requires is random. Show that the output of the original list and list as it changes all the way until nothing is left in the array. Using only main method and not any additional static methods and Only using scanner, arrays, and for looper while/do while loops. No array lists or other methods in java.
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Consider P|rj, prec|Cmax. Show that the greedy algorithm is a 2-approximation. (It pertains to Scheduling Theory,...
Consider P|rj, prec|Cmax. Show that the greedy algorithm is a 2-approximation. (It pertains to Scheduling Theory, Algorithms, and Systems.)
Solve the the process scheduling problem using the round robin FCFS algorithm studied in assignment 7....
Solve the the process scheduling problem using the round robin FCFS algorithm studied in assignment 7. The program will show the order of execution of the processing and will provide the average waiting time for the following scenarios: a) Time quantum =1 b) Time Quantum=3 Use the table below to draw the Gantt Chart (Spread sheet or by hand). Process ID Arrival Time Burst Time 1 0 4 2 1 5 3 2 2 4 3 1 5 4 6...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Greedy algorithm for the minimum coin change problem (the number of required coins is given) function...
Greedy algorithm for the minimum coin change problem (the number of required coins is given) function parameters : - int amount = 20 - int Coins[] = [1, 5, 11, 25] - int requiredCoins = 4 Expected result = 5,5,5,5, ****algorithm should be greedy, and find optimal solution*** You can provide the program or the algorithm
using java Which sorting algorithm is this, justify in detail your answer? image of code in...
using java Which sorting algorithm is this, justify in detail your answer? image of code in this link https://lh3.googleusercontent.com/ie0xsSduZTDJ0PYd3skj43dNApdae1rKAXt1OIdkUE9WAyIrrzLndH2pbsVs_laCJ91H61wxBRopgHPcF8PayLjWHiW-Pn72ti02xxb0aNpx87tFUbT7j8lW0bL4=w655
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT