Data Structures - Efficient Algorithms and Intractable Problems - Solved Exams, Exams of Algorithms and Programming

Main points of this past exam are: Data Structures, Information Requested, Statements Initialize, Structure Descibed, Data Structures, Resulting Data, Structure, Including Rank, Prim'S Algorithm, Implementation

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shameem_99
shameem_99 🇮🇳

4.4

(12)

58 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 170 Spring 2000
Solutions and grading standards for exam 1
Clancy
Exam information
179 students took the exam. Scores ranged from 5 to 30, with a median of 16 and an average of 16.3. There
were 19 scores between 23 and 30,82 between 16 and 22, 73 between 8 and 15, and 5 between 1 and 7. (I
estimate, based on past experience in other classes, that three-quarters of the points on exams plus good
grades on homework and projects would earn you an A-; similarly, a test grade of 16 may be projected to a
B-.)
There were two versions of the exam, A and B. (The version indicator appears at the bottom of the first page.)
If you think we made a mistake in grading your exam, describe the mistake in writing and hand the
description with the exam to your discussion t.a. or to Mike Clancy. We will regrade the entire exam.
Solutions and grading standards
Problem 0 (1 point)
If you earned some credit on a problem and did not put your name on the page, you lost 1 point. If you did not
indicate your discussion section and t.a., you lost 1 point. (The reason for this apparent harshness is that
exams can get misplaced or come unstapled, and we would like to make sure that every page is identifiable.
We also need to know where you will expect to get your exam returned.) If you did not identify where you
were sitting, you lost 1 point.
Problem 1 (4 points)
The two versions of this problem involved the following sequences of operations:
Version A
Union (0, 1);
Union (2, 3);
Union (0, 2);
Union (4, 5);
Union (0, 4);
Version B
Union (4, 5);
Union (2, 3);
Union (2, 4);
Union (0, 1);
Union (2, 0);
Both sequences lead to similar trees; thus we present only the solution to version A.
CS 170, Midterm #1 Solutions, Spring 2000
CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy 1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Data Structures - Efficient Algorithms and Intractable Problems - Solved Exams and more Exams Algorithms and Programming in PDF only on Docsity!

CS 170 Spring 2000

Solutions and grading standards for exam 1

Clancy

Exam information

179 students took the exam. Scores ranged from 5 to 30, with a median of 16 and an average of 16.3. There were 19 scores between 23 and 30,82 between 16 and 22, 73 between 8 and 15, and 5 between 1 and 7. (I estimate, based on past experience in other classes, that three-quarters of the points on exams plus good grades on homework and projects would earn you an A-; similarly, a test grade of 16 may be projected to a B-.)

There were two versions of the exam, A and B. (The version indicator appears at the bottom of the first page.)

If you think we made a mistake in grading your exam, describe the mistake in writing and hand the description with the exam to your discussion t.a. or to Mike Clancy. We will regrade the entire exam.

Solutions and grading standards

Problem 0 (1 point)

If you earned some credit on a problem and did not put your name on the page, you lost 1 point. If you did not indicate your discussion section and t.a., you lost 1 point. (The reason for this apparent harshness is that exams can get misplaced or come unstapled, and we would like to make sure that every page is identifiable. We also need to know where you will expect to get your exam returned.) If you did not identify where you were sitting, you lost 1 point.

Problem 1 (4 points)

The two versions of this problem involved the following sequences of operations:

Version A Union (0, 1); Union (2, 3); Union (0, 2); Union (4, 5); Union (0, 4);

Version B Union (4, 5); Union (2, 3); Union (2, 4); Union (0, 1); Union (2, 0);

Both sequences lead to similar trees; thus we present only the solution to version A.

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy 1

Prim’s algorithm, if coded inappropriately, contributes e * 0(n) operations, which dominates the total running time.

Part a was worth 4 points. Grading awarded 1 point to repeating the information from previous analyses about the number of iterations for each loop, 2 points for noting that ExtractMin was 0(n) and updating an element was 0(1), and 1 point to providing some rationale for these values. The most common error here was to overlook the updating operation, which lost 1 point. A number of you provided no rationale for the estimates, also a 1-point deduction. Finally, a few of you seemed not to understand that no sorting was taking place.

Part b, which contributed the other 2 points, was to produce a worst-case graph and explain how it produces the worst case. No points were awarded for an answer without an explanation. Interestingly, ExtractMin exhibits worst-case behavior with any assignment of weights to edges, since every element in the queue must be examined to find the minimum. We gave full credit to an answer that noted this. A more detailed analysis, however, reveals that the number of updates will depend on the assignment of edge weights; in the worst case, a neighbor’s key value (in Prim’s algorithm) or d value (in Dijkstra’s) will be updated every time the neighbor is examined. A pattern that produces the worst case and an example of a corresponding assignment of edge weights are shown below.

Many students produced a graph in which the last vertex in the queue was always the vertex with minimum value. As noted previously, the position of the minimum-valued vertex in the queue does not affect the running time, so this answer lost 1 point.

Problem 3 (3 points)

This problem involved getting a theta value for the solution to a recurrence. Version A’s recurrence was

T(n) = 9 * T(n / 3) + n^2 + n * log(n)

while version B’s was

T(n) = 4 * T(n / 2) + sqrt(n) + n * log(n)

Both recurrences could be solved with the Master Theorem. In version A, the variables a, b, and f have values a = 9, b = 3, and f(n) = n^2 + n log(n), which is Theta (n^2). The value of log_base_b(a) is 2 since 9 = 3^2. Case 2 of the Master Theorem applies to give an estimate of Theta (n^2 * log(n)). Version B’s recurrence

required slightly more work. There, a = 4, b = 2, and f(n) = n^(1/2)+ log(n), which is O(n^(1/2)). The value of log_base_b(a) is 2 since 4 = 2^2. Case 1 of the Master Theorem applies, with the desired epsilon being 1.5, giving an estimate of Theta (n^2).

An alternate form of the Master Theorem, given on page 9 of the Readings, uses a d variable, which is 2 in version A’s recurrence and 1/2 in the version B recurrence. Using this version of the Master Theorem was fine.

Points were awarded as follows: 1 for identifying the variable values necessary to apply the Master Theorem, 1 more for correctly specifying the order of f, and 1 more for successfully applying the Master Theorem. You lost 1 point for not simplifying log_base_3(9) or log_base_2(4) to 2. Another common error that lost 1 point on version B was to note that f(n) = O(n^2). This does not provide enough information to determine which case of the Master Theorem applies.

With difficulty, one could produce a solution using the iteration method instead of the Master Theorem. Only one person succeeded with this approach. You received 1 point if you made any forward progress in the analysis.

Problem 4 (8 points)

This problem was the same on both versions of the exam. You were to prove that a directed graph G = (V, E) with no isolated vertices is strongly connected if and only if there is a cycle in G that includes every edge at least once (and possibiy more than once). As with any “ if and only if” proposition, there were two things to prove:

  1. If G is strongly connected, then there is a cycle that contains every edge at least once.
  2. If there is a cycle that contains every edge at least once, then G is strongly connected.

The second part is the easier of the two. Since the assumed cycle contains every edge and there are no isolated vertices, the cycle also contains every vertex. Consider any two vertices u and v. A path from u to v is found by starting at u on the cycle and following it around to v; a path from v to u is found in the same way. These two paths, found for all pairs of vertices in the graph, satisfy the conditions for strong connectivity.

There were two successful approaches to the first part:

  • a proof by contradiction that involved assuming that no cycle could include every edge;
  • a proof by construction, involving the repeated augmenting of a cycle to include more edges.

Here are sample solutions for each approach.

Suppose that G is strongly connected, yet no cycle includes every edge in the graph. Let C be a cycle that includes the maximum number of edges of G, and let (u, v) be an edge not included. Let w be some vertex on the cycle. Since G is strongly connected, there is a path P1 from w to u and a path P2 from v to w. These two paths, along with (u, v) and C, comprise a longer cycle. It starts somewhere on C, reaches w, detours on P through (u,v), returns to w on F2, and continues along C to the starting point. It also contains all the edges in C plus an edge not in C, namely (u,v), which contradicts the assumption that C included the maximum number of edges possible. Thus there is no cycle that includes as many edges as possible without including them all, which is what we wanted to prove.

Unfortunately, some of you overlooked the “ if and only if’ nature of the proposition, losing either 2 or 6 points depending on which part you overlooked. You could earn 1 point on this problem merely by specifying the two things to prove.

Problem 5 (8 points)

This problem was the same on both versions. You were to find an efficient algorithm that, given a directed acyclic graph G = (V, E) and a vertex a in V, counts the number of paths from a to each other vertex (The original problem said “ all other vertices”, but this was corrected at the exam. Another clarification made at the exam was that G contains no multiple edges; that is, for any u and v, the number of edges connecting u to v is at most 1, and the number of edges connecting V to u is at most 1.)

There were a number of solution approaches. The best one took advantage of the fact that G is a DAG and can therefore be topologically sorted. This leads to an algorithm that runs in time O(n + e):

Set the path counts of all vertices to 0, and set a’s path count to 1. Topologically sort the vertices. For each vertex u in topological order, do the following:

For each neighbor v of u, add u’s path count to v’s path count.

To prove this (which you didn’t need to do), one would note that a vertex u isn’t reached until all the path counts of vertices between it and a have been updated; this is because of the topological ordering. Thus, when U is processed in the outer loop, the number of paths to each of its predecessors is known and tallied in u’s own count.

Not taking account of multiple ways to get to a vertex from a was the flaw in another common solution, namely applying CLR's DFS or BFS algorithm directly and merely increment a vertex’s path count each time it is visited. The problem with this is demonstrated in the graph below. The first time vertex c is encountered, it’s marked:

when c is next encountered on the second path out of a , there will be no way to go past it to add the second path into b’s count.

A third approach attempted to solve the problem of missing paths by continuing the search through marked vertices. This search—again, either depth first or breadth first—traverses every path from a to every other vertex. Unfortunately, the tradeoff is speed; this algorithm run in time exponential in the number of vertices. An example on which it performs badly is shown below. If k is approximately equal to the

square root of n, the number of paths from one end of the graph to the other is approximately n^(sqrt(n)/2).

One last approach arose from noting that the (u,v)-th entry in the kth power of the adjacency matrix of G is the number of paths of length k from u to v. If the first n-1 powers of G’s adjacency matrix are added, the resulting matrix contains the desired path counts. Matrix multiplication (at least the standard algorithm) is O(n^3), and there are n-1 multiplications, yielding a run time estimate of O(n^4).

This problem was worth 8 points. You could lose points both for inefficiency and for incorrectness. For example, a solution that computed powers of the adjacency matrix correctly earned 6 out of 8, and a correct DFS-baaed solution earned 4 out of 8. Error deductions varied; an off-by-one initialization or update lost 1, while the DFS-based solution that marked vertices already encountered lost 4. Some 2-point errors were a more serious error in computing the path count, the use only of the n-1st power of the adjacency matrix rather than the sum of the powers, or a generally correct solution that topologically sorted the vertices but then accidentally gave a positive path count to a vertex not reachable from a. You also lost 1 point if you didn’t maintain separate path counts for the individual vertices, instead doing a separate search for all paths to each destination vertex. Solutions that started by topologically sorting the vertices but then made no subsequent use of the topological ordering got no credit for the good start.

A few students mentioned memoization as a way to reduce the inefficiency. You had to be explicit about how it would be applied to get any credit for it, though. If your algorithm was incorrect, providing a DAG on which it would work incorrectly would earn you an extra point.

On the low end, if you invoked the term “ DFS” or “ BFS” or “ topological sort” and then followed up with some kind of use of the technique, you earned at least 2 points.

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact [email protected]

Posted by HKN (Electrical Engineering and Computer Science Honor Society)University of California at BerkeleyIf yo 8