0.8em 0.6em .4em .4em
When faced with a difficult engineering problem or homework assignment, you may find that you don't know how to get started, or you get stuck trying to get your program to work. In these situations, it is helpful to have systematic methods to fall back on. This handout describes a method developed at Rice University and tested at over 20 universities around the world. I have adapted the method for use in COMP 40.
A wise man once said that we don't need to use systematic methods on every problem, just on problems that are hard. An even wiser man said that it is sometimes useful to practice systematic methods on easy problems, so that when confronted with a hard problem, you can focus on the problem, not the method.
If you get stuck on a problem, please complete the following checklist. If completing the checklist does not get you unstuck, take your completed checklist to a member of the course staff and ask for help. Similarly, if you find yourself unable to complete the checklist, ask for help. The course staff will not answer substantive questions for students without checklists.
Let's suppose you're given the following problem:
Simulate the real-time behavior of an analog circuit. We'll assume that the circuit is a set of elements, and each element provides an event stream. An event stream is one of the following:
You are given a set of event streams; your job is to run a simulation that prints out the time of each event and the string associated with that event.
- A pointer to a structure containing a time, a string, and a function which, when applied to the pointer, returns an event stream.
- The NULL pointer
Here's the checklist:
We have these data:
A wide variety of algorithms go with these data. I'll focus on finding the event stream with the smallest time.
We'll need an abstraction that maintains a set of events and allows us quick access to the next event, which is the one with the smallest time. Let's call this abstraction an event queue.
A set of event streams, each of which has a time.
We'll want these functions:
Here are some examples:
eventq q = new_queue(); add_event(q, e1); // e1 has time 3.0 add_event(q, e2); // e2 has time 9.0 add_event(q, e3); // e3 has time 2.0 isempty(q); // returns false get_event(q); // returns e3 get_event(q); // returns e1 isempty(q); // returns false get_event(q); // returns e2 isempty(q); // returns true
I'll use a binary heap, which is either
Test cases are left as an exercise for the reader. [This phrase sounds academically respectable, but unless the exercise is carefully crafted, it's really just a way of dodging work. I'm~dodging work.]