20-ECES-122-003 Computer Science II Spring 2004

## Lab Assignment Number 3

Assembly Line Scheduling

Due: April 20, 2004
Submit solution to: ???

Rationale:

This lab exercises your ability to use classes and think recursively. A reasonable solution is ridiculously short and yet the problem is one of the most important we will encounter in Computer Science.

A factory which produces complex machinery on a single assembly line may need to take some care in planning which subassemblies or components are completed at what point on the line1. For, if a component type is scheduled for completion after it is supposed to be installed then a problem will certainly exist. The problem is compounded by the fact that duplicates of the same component type may be used many times in assembling other components which themselves may be used many times and so on.

We wish to find an order of assembly of components on a single production line so that no component requires a subassembly that has not been built earlier in the line. We again turn to graphs to help devise a solution. Use a vertex (a circle on a piece of paper) to represent a component type. If one component type, call it A, requires another, say B, then draw a line between the two circles representing type A and B with an arrowhead pointing in the direction of the B vertex. The arrow tells us that we cannot build A components before we build B components. For example, in the following picture

no component can be built before E components are, A components must wait until D and G components, and so on. If the component assemblies are scheduled in the following order

```     E, G, H, B, F, D, A, C, I, J
```
then all the components can be assembled successfully. By the way, a graph with arrows as above is referred to as a partial ordering of elements (in this case component types A through J) and an ordering such as the one above, where all dependencies are satisfied, is called a topological sort (in this case, of components A through J). If the graph were such that for some vertex you could visit one or more other vertices by moving in the direction of the arrows, and then return to the vertex at which you started, then the graph is not a partial ordering and a topological sort is impossible.

Lab Problem:

Write a program to determine a topological sort of a given partial ordering. This problem and an outline of the code you can write are as follows:

Topological Sort Problem:

Given a collection of n items and dependency lists for each item, determine whether the dependencies support a partial ordering. If not, report this fact. Otherwise, find a topological sort of the vertices with respect to the given dependency lists.

Example:

Given above.

Method:

input: A graph of vertices and lines with arrowheads in one direction
output: "Impossible" or a topological sort of the vertices

```    0. Create objects for each vertex
1. Set the state of each object to START
2. Install all the dependency lists for all vertices
3. For all vertices v execute topo(v) given below
(imagine there is one copy of that code for each vertex)
```

topo:
input: A vertex v - invoked as topo(v)

```    1. Check whether v is in the DONE state
2. If so, return
3. Check whether v is in the ERROR state
4. If so, print "Impossible" and exit
5. Change v's state to ERROR
6. For all the vertices w in the dependency list of v do the following:
6a.   Invoke topo(w)
7. Change v's state to DONE
8. Print "v"
9. Return
```

1. Define a Subassembly class which implements the topo method above. You might consider adding a requires method to the class for inputing data. The method should take a Subassembly object as input and should add that object to the current object's dependency list. Input might look like this:
```     A G D
B H
C A D
D
E
F B G H
G E
H G
I C F
J D H I
```
where each line starts with a Subassembly type which is followed by its list of dependencies. This input would be enough to specify the problem in the example above.

In Case Of Trouble:

If the problem is understanding classes, see the lecture notes. If the problem is inputting, then embed the dependencies in the text of the code and do not worry about the input process right now. Otherwise, identify the problem to the grader or myself.