CS136 Program #8

Due Sunday, 5/10/98


Intro

You are to write a program which will help the registrar schedule final exams so that no student has two exams at the same time. You are to use a "greedy" algorithm to determine an assignment of classes to exam slots so that:

  1. No student is enrolled in two courses assigned to the same exam slot.

  2. Any attempt to combine two slots into one would violate rule 1.
Thus we wish to get by without gratuitously wasting exam slots (students would like to get out of here as soon as possible, after all).

What

Input to the program will be a file generated by my program "Register", which is generated by the program in file "Register.java". Information on each student is written on the file using the writeUTF method of DataOutputStream. For each student, 5 strings are written. The first for the name, and the next 4 for courses selected. The program insists that each student takes exactly 4 courses. A possible file would include The output of the program should be a list of time slots with the courses whose final will be given at that slot.


How

The key to doing this assignment is to build a graph as you read in the file of students and their schedules. (An end-of-file condition will be signalled by a read throwing an EOFException which can be handled in a catch clause.)

Each node of the graph will be a course taken by at least one student in the college. An edge will be drawn between two nodes if there is at least one student taking both courses. The label of an edge could be the number of students with both classes (though we don't really need the weights for this program). Thus if there are only the three students listed above, the graph would be as given below (edges without a weight label have weight 1).

A "greedy" algorithm to find an exam schedule satisfying our two constraints would work as follows. Choose a course (say, PHIL 101) and stick it in the first time slot. Search for a course to which it is not connected. If you find one (e.g., HIST 301), add it to the time slot. Now try to find another which is not connected to any of those already in the time slot. If you find one (e.g. SOCI 201), add it to the time slot. Continue until all nodes in the graph are connected to at least one element in the time slot. When this happens, no more courses can be added to the time slot (why?). (By the way, the final set of elements in the time slot is said to be a maximal independent set in the graph.)

If there are remaining nodes in the graph, pick one and enter it in a new time slot and then try adding other courses to the same slot as before. Continue adding time slots for remaining courses until all courses are taken care of. Print the exam schedule. For the graph shown, a possible exam schedule is:

   Time 1: PHIL 101, HIST 301, SOCI 201
   Time 2: MATH 151
   Time 3: CSCI 136
   Time 4: ENGL 201
   Time 5: PSYC 212
Notice that no pair of time slots can be combined without creating a time conflict with a student. Unfortunately, this is not the minimal schedule as one can be formed with only 4 time slots. (See if you can find one!) Thus a greedy algorithm of this sort will give you a schedule with n slots, no two of which can be combined, but a different selection of courses in slots may result in fewer than n slots. Any schedule which satisfies are our constraints will be acceptable (though see below for extra credit).


Hints for building the final exam schedule:

You are to represent graphs as adjacency lists. (Why does that make the most sense for this application?) Vertex labels should be the course names.

Here is one possible way to find a collection of maximal independent sets from the graph. Represent each slot by some sort of a list (or, better yet, a binary search tree). To find a maximal independent set for a slot, pick any vertex of the graph and add it to the list. Cycle through all other vertices of the graph. If a vertex is not connected to any of the vertices already in the slot, throw it in. Continue until you've tried all vertices. Now delete all vertices in the slot from the graph. Fill successive slots in the same way until there are no vertices left in the graph.


Extra Credit:

A wide variety of extra credit is possible. Here are some options:
  1. Always generate the best possible exam schedule (that is, the one with the fewest number of slots).

  2. Allow students to take more or fewer than four courses.

  3. Print out a final exam schedule for each student.

  4. Print out a final exam schedule ordered by course number.

  5. Arrange the time slots in an order which tries to minimize the number of students who must take exams in three consecutive time slots. Warning: This part is hard!
Feel free to add other useful bells and whistles. As usual, be sure sure to indicate in the heading of your program what extras you have included.


Implementation Hints:

I suggest that you look very carefully at my program for generating the files in order to see how I wrote the files. You will use similar file operations (except reading instead of writing). You should print the final exam schedule in a text area in a frame. An important operation for this is "append(String newText)" which adds newText to the end of whatever has been written so far in the TextArea. Sending the message setEditable(false) to the TextArea will ensure that the user cannot accidentally override the information printed there (only the program can write information there).

Please see the associated instructions on how to pop us file dialog boxes for loading and saving files.


Final Instructions:

You will be responsible for building this program entirely yourself as I am not providing you with any starter code or project. We will be enforcing the honor code rather strictly for this program. You may ask others to help you find mistakes in your code, but no one else may assist you in writing correct code.