Question

You are running a physics experiment with n complicated steps that you must do in order,...

You are running a physics experiment with n complicated steps that you must do in order, and students sign-up for some steps to help. Your experiment requires n steps, and each of the m students gives you a list of which steps they can help out with (steps require special skills). From experience, you know things run most smoothly when you have as little switching of shifts as possible.

For example, if your experiment has <1, 2, 3, 4, 5, 6> steps

  • Student 1: <1, 2, 3, 5>
  • Student 2: <2, 3, 4>
  • Student 3: <1, 4, 5, 6>
    Your optimal solution would be:
    Student 1 <1, 2, 3>, Student 2 does no steps, Student 3 does <4, 5, 6>. Only 1 switch

Another example if your experiment has 8 steps <1, 2, 3, 4, 5, 6, 7, 8>

  • Student 1: <5, 7, 8>
  • Student 2: <2, 3, 4, 5, 6>
  • Student 3: <1, 5, 7, 8>
  • Student 4: <1, 3, 4, 8>
    Your optimal solutions could be any one of these:
    • Student 1 does no steps, Student 2 does <2, 3, 4, 5, 6>, Student 3 does <1, 7, 8>, Student 4 does no steps --> 2 switches (student 3 does step 1, student 2 does up to 6, student 3 picks up again to do 7 and 8)
    • Student 1 does <7, 8>, Student 2 does <2, 3, 4, 5, 6>, Student 3 does <1>, Student 4 does nothing --> 2 switches
    • Student 1 does no steps, Student 2 does <2, 3, 4, 5, 6>, Student 3 does <7, 8>, Student 4 does <1> --> 2 switches

Given: n number of steps, m number of students that give you a list of steps (sorted) they can participate in. Assume there's a lookup table where you can find if student X signed up for step Y in O(1), so no need to factor that into your runtime.

Find: An optimal way to schedule students to steps such that there is the least amount of switching as possible.

- Describe the optimal substructure of this problem

- Describe the greedy algorithm that could find an optimal way to schedule the students

- What is the runtime complexity of your greedy algorithm? Again, you don't need to factor in the setup of the lookup table, just your scheduling algorithm.

public class PhysicsExperiment1 {

/**

   * The actual greedy scheduler you will be implementing!

   * @param numStudents The number of students who can participate, m

   * @param numSteps The number of steps in the experiment, n

   * @param signUpTable An easy lookup tool, signUpTable[x][Y] = student X signed up or did not sign up for step Y.

   *      Example:

          signUpTable[1][3] = 1 if Student 1 signed up for Step 3

          signUpTable[1][3] = 0 if Student 1 didn't sign up for Step 3

   * @return scheduleTable: a table similar to the signUpTable where scheduleTable[X][Y] = 1 means

   *     student X is assigned to step Y in an optimal schedule

   */

public int[][] scheduleExperiments(

    int numStudents,

    int numSteps,

    int[][] signUpTable

) {

    // Your scheduleTable is initialized as all 0's so far. Your code will put 1's

    // in the table in the right places based on the return description

    int[][] scheduleTable = new int[numStudents + 1][numSteps + 1];

    // Your code goes here

    return scheduleTable;

}

/**

   * Makes the convenient lookup table based on the steps each student says they can do

   * @param numSteps the number of steps in the experiment

   * @param studentSignUps student sign ups ex: {{1, 2, 4}, {3, 5}, {6, 7}}

   * @return a lookup table so if we want to know if student x can do step y,

      lookupTable[x][y] = 1 if student x can do step y

      lookupTable[x][y] = 0 if student x cannot do step y

   */

public int[][] makeSignUpLookup(int numSteps, int[][] studentSignUps) {

    int numStudents = studentSignUps.length;

    int[][] lookupTable = new int[numStudents+1][numSteps + 1];

    for (int student = 1; student <= numStudents; student++) {

      int[] signedUpSteps = studentSignUps[student-1];

      for (int i = 0; i < signedUpSteps.length; i++) {

        lookupTable[student][signedUpSteps[i]] = 1;

      }

    }

    return lookupTable;

}

/**

   * Prints the optimal schedule by listing which steps each student will do

   * Example output is Student 1: 1, 3, 4

   * @param schedule The table of 0's and 1's of the optimal schedule, where

   *   schedule[x][y] means whether in the optimal schedule student x is doing step y

   */

public void printResults(int[][] schedule) {

    for (int student = 1; student < schedule.length; student++) {

      int[] curStudentSchedule = schedule[student];

      System.out.print("Student " + student + ": ");

      for (int step = 1; step < curStudentSchedule.length; step++) {

        if (curStudentSchedule[step] == 1) {

          System.out.print(step + " ");

        }

      }

      System.out.println("");

    }

}

/**

   * This validates the input data about the experiment step sign-ups.

   * @param numStudents the number of students

   * @param numSteps the number of steps

   * @param signUps the data given about which steps each student can do

   * @return true or false whether the input sign-ups match the given number of

   *    students and steps, and whether all the steps are guaranteed at least

   *    one student.

   */

public boolean inputsValid(int numStudents, int numSteps, int signUps[][]) {

    int studentSignUps = signUps.length;

    // Check if there are any students or signups

    if (numStudents < 1 || studentSignUps < 1) {

      System.out.println("You either did not put in any student or any signups");

      return false;

    }

    // Check if the number of students and sign-up rows matches

    if (numStudents != studentSignUps) {

      System.out.println("You input " + numStudents + " students but your signup suggests " + signUps.length);

      return false;

    }

    // Check that all steps are guaranteed in the signups

    int[] stepsGuaranteed = new int[numSteps + 1];

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

      for (int j = 0; j < signUps[i].length; j++) {

        stepsGuaranteed[signUps[i][j]] = 1;

      }

    }

    for (int step = 1; step <= numSteps; step++) {

      if (stepsGuaranteed[step] != 1) {

        System.out.println("Your signup is incomplete because not all steps are guaranteed.");

        return false;

      }

    }

    return true;

}

/**

   * This sets up the scheduling test case and calls the scheduling method.

   * @param numStudents the number of students

   * @param numSteps the number of steps

   * @param signUps which steps each student can do, in order of students and steps

   */

public void makeExperimentAndSchedule(int experimentNum, int numStudents, int numSteps, int[][] signUps) {

    System.out.println("----Experiment " + experimentNum + "----");

    if (!inputsValid(numStudents, numSteps, signUps)) {

      System.out.println("Experiment signup info is invalid");

      return;

    }

    int[][] signUpsLookup = makeSignUpLookup(numSteps, signUps);

    int[][] schedule = scheduleExperiments(numStudents, numSteps, signUpsLookup);

    printResults(schedule);

    System.out.println("");

}

/**

   * You can make additional test cases using the same format. In fact the helper functions

   * I've provided will even check your test case is set up correctly. Do not touch any of

   * of the existing lines. Just make sure to comment out or delete any of your own test cases

   * when you submit. The three experiment test cases existing in this main method should be

   * the only output when running this file.

   */

public static void main(String args[]){

    PhysicsExperiment pe = new PhysicsExperiment();

    // Experiment 1: Example 1 from README, 3 students, 6 steps:

    int[][] signUpsExperiment1 = {{1, 2, 3, 5}, {2, 3, 4}, {1, 4, 5, 6}};

    pe.makeExperimentAndSchedule(1, 3, 6, signUpsExperiment1);

    // Experiment 2: Example 2 from README, 4 students, 8 steps

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

    pe.makeExperimentAndSchedule(2, 4, 8, signUpsExperiment2);

    // Experiment 3: Another test case, 5 students, 11 steps

    int[][] signUpsExperiment3 = {{7, 10, 11}, {8, 9, 10}, {2, 3, 4, 5, 7}, {1, 5, 6, 7, 8}, {1, 3, 4, 8}};

    pe.makeExperimentAndSchedule(3, 5, 11, signUpsExperiment3);

}

}

Final Output

----Experiment 1----
Student 1: 1 2 3
Student 2:
Student 3: 4 5 6

----Experiment 2----
Student 1: 7 8
Student 2: 2 3 4 5 6
Student 3: 1
Student 4:

----Experiment 3----
Student 1: 11
Student 2: 9 10
Student 3: 2 3 4 5
Student 4: 1 6 7 8
Student 5:

Thankyou in advance.

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

public class Main {

/**

   * The actual greedy scheduler you will be implementing!

   * @param numStudents The number of students who can participate, m

   * @param numSteps The number of steps in the experiment, n

   * @param signUpTable An easy lookup tool, signUpTable[x][Y] = student X signed up or did not sign up for step Y.

   *      Example:

          signUpTable[1][3] = 1 if Student 1 signed up for Step 3

          signUpTable[1][3] = 0 if Student 1 didn't sign up for Step 3

   * @return scheduleTable: a table similar to the signUpTable where scheduleTable[X][Y] = 1 means

   *     student X is assigned to step Y in an optimal schedule

   */

public int[][] scheduleExperiments(

    int numStudents,

    int numSteps,

    int[][] signUpTable

) {

    // Your scheduleTable is initialized as all 0's so far. Your code will put 1's

    // in the table in the right places based on the return description

    int[][] scheduleTable = new int[numStudents + 1][numSteps + 1];

    // Your code goes here

    return scheduleTable;

}

/**

   * Makes the convenient lookup table based on the steps each student says they can do

   * @param numSteps the number of steps in the experiment

   * @param studentSignUps student sign ups ex: {{1, 2, 4}, {3, 5}, {6, 7}}

   * @return a lookup table so if we want to know if student x can do step y,

      lookupTable[x][y] = 1 if student x can do step y

      lookupTable[x][y] = 0 if student x cannot do step y

   */

public int[][] makeSignUpLookup(int numSteps, int[][] studentSignUps) {

    int numStudents = studentSignUps.length;

    int[][] lookupTable = new int[numStudents+1][numSteps + 1];

    for (int student = 1; student <= numStudents; student++) {

      int[] signedUpSteps = studentSignUps[student-1];

      for (int i = 0; i < signedUpSteps.length; i++) {

        lookupTable[student][signedUpSteps[i]] = 1;

      }

    }

    return lookupTable;

}

/**

   * Prints the optimal schedule by listing which steps each student will do

   * Example output is Student 1: 1, 3, 4

   * @param schedule The table of 0's and 1's of the optimal schedule, where

   *   schedule[x][y] means whether in the optimal schedule student x is doing step y

   */

public void printResults(int[][] schedule) {

    for (int student = 1; student < schedule.length; student++) {

      int[] curStudentSchedule = schedule[student];
      System.out.print(curStudentSchedule);

      System.out.print("Student " + student + ": ");

      for (int step = 1; step < curStudentSchedule.length; step++) {

        if (curStudentSchedule[step] == 1) {

          System.out.print(step + " ");

        }
     

      }

      System.out.println("");

    }

}

/**

   * This validates the input data about the experiment step sign-ups.

   * @param numStudents the number of students

   * @param numSteps the number of steps

   * @param signUps the data given about which steps each student can do

   * @return true or false whether the input sign-ups match the given number of

   *    students and steps, and whether all the steps are guaranteed at least

   *    one student.

   */

public boolean inputsValid(int numStudents, int numSteps, int signUps[][]) {

    int studentSignUps = signUps.length;

    // Check if there are any students or signups

    if (numStudents < 1 || studentSignUps < 1) {

      System.out.println("You either did not put in any student or any signups");

      return false;

    }

    // Check if the number of students and sign-up rows matches

    if (numStudents != studentSignUps) {

      System.out.println("You input " + numStudents + " students but your signup suggests " + signUps.length);

      return false;

    }

    // Check that all steps are guaranteed in the signups

    int[] stepsGuaranteed = new int[numSteps + 1];

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

      for (int j = 0; j < signUps[i].length; j++) {

        stepsGuaranteed[signUps[i][j]] = 1;

      }

    }

    for (int step = 1; step <= numSteps; step++) {

      if (stepsGuaranteed[step] != 1) {

        System.out.println("Your signup is incomplete because not all steps are guaranteed.");

        return false;

      }

    }

    return true;

}

/**

   * This sets up the scheduling test case and calls the scheduling method.

   * @param numStudents the number of students

   * @param numSteps the number of steps

   * @param signUps which steps each student can do, in order of students and steps

   */

public void makeExperimentAndSchedule(int experimentNum, int numStudents, int numSteps, int[][] signUps) {

    System.out.println("----Experiment " + experimentNum + "----");

    if (!inputsValid(numStudents, numSteps, signUps)) {

      System.out.println("Experiment signup info is invalid");

      return;

    }

    int[][] signUpsLookup = makeSignUpLookup(numSteps, signUps);

    int[][] schedule = scheduleExperiments(numStudents, numSteps, signUpsLookup);

    printResults(schedule);

    System.out.println("");

}

/**

   * You can make additional test cases using the same format. In fact the helper functions

   * I've provided will even check your test case is set up correctly. Do not touch any of

   * of the existing lines. Just make sure to comment out or delete any of your own test cases

   * when you submit. The three experiment test cases existing in this main method should be

   * the only output when running this file.

   */

public static void main(String args[]){

    Main pe = new Main();

    // Experiment 1: Example 1 from README, 3 students, 6 steps:

    int[][] signUpsExperiment1 = {{1, 2, 3, 5}, {2, 3, 4}, {1, 4, 5, 6}};

    pe.makeExperimentAndSchedule(1, 3, 6, signUpsExperiment1);

    // Experiment 2: Example 2 from README, 4 students, 8 steps

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

    pe.makeExperimentAndSchedule(2, 4, 8, signUpsExperiment2);

    // Experiment 3: Another test case, 5 students, 11 steps

    int[][] signUpsExperiment3 = {{7, 10, 11}, {8, 9, 10}, {2, 3, 4, 5, 7}, {1, 5, 6, 7, 8}, {1, 3, 4, 8}};

    pe.makeExperimentAndSchedule(3, 5, 11, signUpsExperiment3);

}

}

Add a comment
Know the answer?
Add Answer to:
You are running a physics experiment with n complicated steps that you must do in order,...
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
  • DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT...

    DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT ANSWER MY QUESTIONS package scheduler; import java.util.List; public class Scheduler { /** * Instantiates a new, empty scheduler. */ public Scheduler() { } /** * Adds a course to the scheduler. * * @param course the course to be added */ public void addCourse(Course course) { } /** * Returns the list of courses that this scheduler knows about. * * This returned object...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • Hey, need some help in Java. I'm stuck on one step that prevents me from doing...

    Hey, need some help in Java. I'm stuck on one step that prevents me from doing the rest of the steps and need help on it. I also have a problem when I print databaseCourse and programmingCourse. instead of saying 5 and 7 respectively is shows 51 and 71. Step 8: in CourseGrades, create a method, add, that takes two parameters, studentNum and grade, and changes the grade of student studetNum to the given grade, grade. studentNum represents the student...

  • How to complete these methods: /**    * Returns a reference to the course with title equals to the argument. This    * m...

    How to complete these methods: /**    * Returns a reference to the course with title equals to the argument. This    * method searches in the courses stored in the HashMap {@code courses} to find    * the course whose title equals to the argument {@code title}. If the course is    * not found, {@code null} is returned.    *    * @param title the title of the course    * @return a reference to the course, or {@code null} if the course is not   ...

  • An experiment consists of three steps. In the first step, you need to select one out...

    An experiment consists of three steps. In the first step, you need to select one out of 6 possibilities. In the second step, you need to select 3 out of 8 possibilities without repetition (the order that you select them in doesn't matter). In the third step, you need to select 2 out of 5 possibilities without repetition (the order does matter for this third step!) How many total outcomes are possible?

  • You must use an array to hold the items in the queue. In other words, one...

    You must use an array to hold the items in the queue. In other words, one of your fields should be an array for holding the items, but you may not have any other container fields (no other arrays, lists, stacks, queues, etc.) You will need at least one additional field (an int). package hw5; public class GeneralizedQueue {    /** * Creates an empty queue. */ public GeneralizedQueue() { // TODO }    /** * Checks if the queue...

  • Write a program that, given a month and year, prints a calendar, such as June 2016...

    Write a program that, given a month and year, prints a calendar, such as June 2016 Su Mo Tu We Th Fr Sa 5 6 7 8 910 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 To find out the weekday of the first day of the month, call this function: Computes the weekday of a given date. @param year the year @param month the month (1=January 12=...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • In JAVA Thank You What is the exact output produced by running the method test? /**...

    In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */ public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y); } public void swap (int[] a,...

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