Question

Using java create the following program and post the answer as well.

The Weighted Interval Scheduling problem is this: Given a set of weighted intervals, choose a set of non-overlapping intervals such that the total weight is maximal. You may think of the “weight” as the profit for doing the work in the given interval

A weighted interval x can be represented by a triple

       x = (s, f, v),

where

         s = start time of x,    f = finish time of x,   v = weight or profit of this job

For example, consider the test case for Weighted Interval Scheduling problem depicted below:

3 2 5 2 3 0 12345678

These weighted intervals can be represented by the triples

         (0,3,3) (1,4,2) (0,5,4) (3,6,1) (4,7,2) (3,9,5) (5,10,2) (8,10,1)

Write a program to compute a solution to the Weighted Interval Scheduling problem.

Your program must read in a set of weighted intervals. Each interval should be entered as 3 integers. The intervals must be given in a textfile and also be entered in increasing order of finish time. In the above case we would have

            0 3 3    1 4 2    0 5 4    3 6 1   ...

The program must print out the value of the total weight or profit of the optimum solution and the indices of the jobs. The output must be in the following format (the output below does NOT have the correct result).

     Optimum profit: 7

     Usng Jobs: 2 5

The program MUST use recursion. An iterative solution will not receive full credit. Use of memoization will receive 20 points extra credit.

You must submit a run using the example above. You must also submit a run using the following sample data set:

         Number of Jobs n = 4

       Job Details {Start Time, Finish Time, Profit}

       Job 1: {1, 2, 50}

       Job 2: {3, 5, 20}

       Job 3: {6, 19, 100}

       Job 4: {2, 100, 200}

In both cases you must submit a printout of the input file used.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

WeightedJobScheduling.java


import java.util.Arrays;
import java.util.Comparator;

/**
* Weighted Job Scheduling
*
* Given N jobs where every job is represented by following three elements of
* it. 1) Start Time 2) Finish Time. 3) Profit or Value Associated. Find the
* maximum profit subset of jobs such that no two jobs in the subset overlap.
*
* Example:
*
* Input: Number of Jobs n = 4. Job Details {Start Time, Finish Time, Profit}
*
* Job 1: {1, 2, 50}
*
* Job 2: {3, 5, 20}
*
* Job 3: {6, 19, 100}
*
* Job 4: {2, 100, 200}
*
* Output: The maximum profit is 250. We can get the maximum profit by
* scheduling jobs 1 and 4.
*
*/


class Job {
    int start;
    int end;
    int profit;

    public Job(int start, int end, int profit) {
   super();
   this.start = start;
   this.end = end;
   this.profit = profit;
    }
}

public class WeightedJobScheduling {
    class Solution {
   int findMaxProfit(Job[] jobs) {
        if (jobs == null || jobs.length == 0) {
       return 0;
        } else if (jobs.length == 1) {
       return jobs[0].profit;
        }

        Arrays.sort(jobs, new Comparator<Job>() {
       @Override
       public int compare(Job j1, Job j2) {
            return j1.end - j2.end;
       }
        });

        int[] dp = new int[jobs.length];
        dp[0] = jobs[0].profit;

        for (int i = 1; i < jobs.length; i++) {
       // Find the latest job (in sorted array) that doesn't
       // conflict with the job[i]
       int j = i - 1;
       while (j >= 0 && jobs[j].end > jobs[i].start) {
            j--;
       }
      
       int include = jobs[i].profit + (j >= 0 ? dp[j] : 0);
       dp[i] = Math.max(include, dp[i-1]);
        }

        return dp[dp.length-1];
   }
    }

    public static void main(String[] args) {
   Job[] jobs = new Job[] { new Job(3, 10, 20), new Job(1, 2, 50),
       new Job(6, 19, 100), new Job(2, 100, 200) };
   System.out.println(new WeightedJobScheduling().new Solution()
       .findMaxProfit(jobs));
    }
}


Problems Javadoc Declaration Conso Coverage <terminated> WeightedJobScheduling Java Application] CProgram Files Javaljdk1.8.0

Add a comment
Know the answer?
Add Answer to:
Using java create the following program and post the answer as well. The Weighted Interval Scheduling...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following greedy strategies for this problem: 1. Select the earliest finishing interval and discard...

    Consider the following greedy strategies for this problem: 1. Select the earliest finishing interval and discard overlapping intervals. Keep doing this until all intervals have been eliminated (either selected or discarded). 2. Select the earliest starting interval and discard overlapping intervals. Keep doing this until all intervals have been eliminated (either selected or discarded). 3. Select the pair of non-overlapping intervals that have the smallest gap between them: find a pair of intervals i # j such that s; -...

  • This assignment requires you to create simulations for different scheduling algorithms commonly employed by operating systems...

    This assignment requires you to create simulations for different scheduling algorithms commonly employed by operating systems to achieve multiprogramming. All problems in this assignment assume the following: The simulations you will be creating are for a uniprocessor system (single CPU). Processes in these simulations will require CPU bursts of one or more time units followed by I/O bursts of one or more time units. For simplicity’s sake, when more than one process is executing its I/O burst at the same...

  • Write a C program that should run on Linux platform using gcc compiler. You are required...

    Write a C program that should run on Linux platform using gcc compiler. You are required to simulate threads creation and termination behavior by using POSIX threads library. Input: In the main program, first take the value for total number of threads and then ask user to provide the arrival time and CPU time (i.e. running time) for each thread. Output: Simulate the behavior of threads arrival, working and termination at a specific time interval (i.e. 500ms). Requirements: i. Name...

  • help!!! ENTERING INTERVAL ANSWERS For intervals of values, enter your answer using interval notation. Here are...

    help!!! ENTERING INTERVAL ANSWERS For intervals of values, enter your answer using interval notation. Here are some examples of how interval notation relates to inequalities: Inequality Interval Notation 3<3<5 (3,5) 3<3<5 (3,5) 2 > 3 (3,00 3<a < 5 or 7 < < <9 (3,5)U (7,9) With inequalities, we use "less than" () or "greater than" (>) to exclude the endpoint of the interval. With interval notation, we use usd round parentheses: (.). With inequalities, we use "less than or...

  • Write a C++/Java program to solve the problem below using Memoization: Problem Definition: Theres a theif...

    Write a C++/Java program to solve the problem below using Memoization: Problem Definition: Theres a theif that enters a store during the night,and upon his entrance,the stores security systems detects the theif and the alarm goes off.The theif knows that theres a hidden door at the corner of this store and in order to escape,he has to get to it.Since the theif has limited time for his burglary,he can only Rob the stuff he comes by on his path towards...

  • , A Total Model for a Training Program In this application, we set up a mathematical model for de...

    please do 1-6 , A Total Model for a Training Program In this application, we set up a mathematical model for determining the t s in setting up a am. Then we use calculus to find the time interval betweeng programs that produces the minimum total cost. The model assumes that the demand for trainees is constant and that the fixed cost of training a batch of trainees is known. Also, it is assumed that people who are trained, but...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • MATLAB Create a function that provides a definite integration using Simpson's Rule Problem Summar This example demo...

    MATLAB Create a function that provides a definite integration using Simpson's Rule Problem Summar This example demonstrates using instructor-provided and randomized inputs to assess a function problem. Custom numerical tolerances are used to assess the output. Simpson's Rule approximates the definite integral of a function f(x) on the interval a,a according to the following formula + f (ati) This approximation is in general more accurate than the trapezoidal rule, which itself is more accurate than the leftright-hand rules. The increased...

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT