# Practice Exam for First Midterm

### Directions:

• Answer the problems on the exam pages.
• There are six problems for 100 total points. Probable scale is A=93, C=69.
• No books, notes, calculators, or collaboration.
• The actual exam will have a time limit of 120 minutes, though it is not intended that you will need all the time.
• Questions 1 and 2 are "true/false with explanation" -- you get five points for a correct boolean answer, and up to five additional points for a convincing justification.

```  Q1: 10 points
Q2: 10 points
Q3: 15 points
Q4: 15 points
Q5: 20 points
Q6: 30 points
Total: 100 points
```

• Question 1 (10): True or false with justification: Suppose that in an instance of the original Stable Marriage problem with n couples (so that every man ranks every woman and vice versa), there is a man M who is last on each woman's list and a woman W who is last on every man's list. If the Gale-Shapley algorithm is run on this instance, than M and W will be paired with each other.

• Question 2 (10): True or false with justification: Suppose we have an instance of the original Stable Marriage problem with M and W as above. In any stable solution to the instance, M and W will be paired with each other.

• Question 3 (15): Suppose we are to receive a set of n jobs, each with a deadline, and we need to schedule them on a single machine. Whenever a job finishes on the machine, we must begin the available job with the earliest deadline. But we may receive the jobs in any order and at any time, and we may not begin a job before we receive it. Indicate an algorithm to schedule the jobs (providing the correct available job whenever a new job is needed) using O(n log n) total computation time for the scheduling. Argue carefully that your algorithm meets this time bound.

• Question 4 (15): Suppose that we have n jobs to do, and that for each number i, the time needed to do job i is Θ(i2). Prove that the time needed to do all n jobs (assuming that there is no idle time) is Θ(n3).

(Recall the definition of the Θ symbol. If f and g are functions, "f = Θ(g)" means that both f = O(g) and f = Ω(g) are true. Thus "T(i) = Θ(i)" means that there are two constants c and d, with c > d > 0, such that for sufficiently large i (i greater than some i0), T(i) satisfies di2 ≤ T(i) ≤ ci2. When you show that the total time is Θ(n3), you may assume that n is sufficiently large relative to i0.)

• Question 5 (20): The graph Kn is defined to be an undirected graph with n vertices and all possible edges. That is, the vertices are named {1,...,n} and for any numbers i and j with i ≠ j, there is an edge between vertex i and vertex j. (Remember that an undirected graph may not have loops or parallel edges.)

Carefully describe the result of a breadth-first search and a depth-first search of Kn. For each search, describe the tree and indicate where there are non-tree edges. (There are choices made during the course of each search, but it turns out that these choices do not affect the shape of either tree.)

It may help to draw the result of BFS and DFS on the graph K4 or K5 before attempting to describe the general case.

• Question 6 (30):

• (a,10) Suppose I am given a labeled undirected graph, in the form of an adjacency list. This graph has a vertex for each of the n towns in Massachusetts, and there is an edge from vertex A to vertex B if you can drive from town A to town B directly, without passing through another town. Each edge is labeled by its length in miles.

We need to place a sign in each town indicating the distance from it to Boston by the shortest route along the edges. Describe an algorithm to find this distance for each town, and give a big-O bound on this algorithm's running time in terms of n.

• (b,20) Suppose I have the results of part (a) in a table, and each edge of my graph contains a pointer into this table. Thus, given town A, for every town B such that there is an edge between A and B, I can find the distance from A to B and the distance from B to Boston, each in O(1) time. Describe a greedy algorithm to determine a route from A to Boston that is optimal -- it is no longer than any other route in the graph. (To describe the route means to list all the towns to be visited, in the correct order.)

Prove that your algorithm takes no longer than any other algorithm. You should probably use mathematical induction.