import java.util.*; public class VectorSched { protected Vector courses; protected Vector times; protected Vector constraintPairs; protected Hashtable mySchedule; public VectorSched() { courses = new Vector(); times = new Vector(); // this should be done with a Hashtable or a HashMap constraintPairs = new Vector(); mySchedule = new Hashtable(); } public void addCourse(String c) { courses.addElement(c); } public void addTimeSlot(String t) { times.addElement(t); } public void addConstraint(String c1, String c2) { constraintPairs.addElement(c1 + "/" + c2); constraintPairs.addElement(c2 + "/" + c1); } protected boolean allowed(String c1, String c2) { return(constraintPairs.indexOf(c1 + "/" + c2) < 0); } protected boolean allowed(String c1, Vector cx) { for(Enumeration e = cx.elements(); e.hasMoreElements(); ) { String c2 = (String)(e.nextElement()); if (!allowed(c1,c2)) return false; } return true; } protected boolean scheduleOneCourse(int cNum, int tNum) { /* DEBUG System.out.println("Trying course " + cNum + " at time " + tNum); */ if (tNum >= times.size()) return false; String t = (String)(times.elementAt(tNum)); if (cNum >= courses.size()) return true; String c = (String)(courses.elementAt(cNum)); Vector cx = (Vector)(mySchedule.get(t)); if (cx == null) { cx = new Vector(); mySchedule.put(t,cx); } if (allowed(c,cx)) { cx.addElement(c); } else return false; int nextc = cNum + 1; int nextt = (tNum + 1) % times.size(); int ix; boolean gotit = false; // this approach will not always yield the most // well spread-out schedule for(ix = 0; ix < times.size(); ix++) { gotit = scheduleOneCourse(nextc, nextt); if (gotit) break; else nextt = (nextt + 1) % times.size(); } if (!gotit) { cx.removeElement(c); return false; } else { return true; } } public boolean schedule() { return scheduleOneCourse(0,0); } public String toString() { StringBuffer buf = new StringBuffer(); for(Enumeration e = times.elements(); e.hasMoreElements(); ) { String t = (String)(e.nextElement()); Vector cx = (Vector)(mySchedule.get(t)); buf.append("Time "); buf.append(t); buf.append(": "); if (cx != null && cx.size() > 0) { for(Enumeration e2 = cx.elements(); e2.hasMoreElements();){ buf.append(e2.nextElement().toString()); buf.append(" "); } } else buf.append("--free--"); buf.append("\n"); } return buf.toString(); } }