Theory Hw4
Theory Hw4
Point values are assigned for each question. Points earned: ____ / 100, = ____ %
1. Joe Pancake has just invented what he thinks is a great sorting algorithm. Consider the following
code:
/**
* Returns index of the maximum element in array[0..n-1].
*/
int find_max(int array[], const int length) {
int max_index, i;
for (max_index = 0, i = 0; i < length; ++i) {
if (array[i] > array[max_index]) {
max_index = i;
}
}
return max_index;
}
/**
* Reverses array[0..limit].
*/
void flip(int array[], int limit) {
int temp, start = 0;
while (start < limit) {
temp = array[start];
array[start] = array[limit];
array[limit] = temp;
start++;
limit--;
}
}
Show the array [2, 4, 1, 5, 3] after the for loop in pancake_sort executes once.
(5 points, one for each element in the correct position)
What is the best-case complexity of the pancake_sort algorithm above including helper functions?
(1 point for symbol, 1 for function)
What is the worst-case complexity of the pancake_sort algorithm above including helper functions?
(1 point for symbol, 1 for function)
Which of the elementary sorting algorithms discussed in class is closest to (but not exactly the same
as) what Joe has written? Use the complexities to guide your response. Circle your answer. (1 point)
2. You are given a collection of bolts of different widths and corresponding nuts. You are allowed to try
a nut and bolt together, from which you can determine whether the nut is larger than the bolt,
smaller than the bolt, or matches the bolt exactly. However, there is no way to compare two nuts
together or two bolts together. The problem is to match each bolt to its nut.
Note: You cannot get credit for parts b and c if your answer to part a is incorrect.
a) Design an algorithm for this problem with efficient average-case complexity faster than
quadratic run time. (6 points)
b) What is the average-case complexity of your algorithm? (1 point for symbol, 1 for function)
c) What algorithm did we study this semester that is most similar to the algorithm you’ve
designed? (2 points)
3. List the order in which the vertices are visited with a breadth-first search starting with vertex 1. If
there are multiple vertices adjacent to a given vertex, visit the adjacent vertex with the lowest value
first. (10 points)
COMS W3134, Theory Homework 4
4. Using the same graph as in the previous question, list the order in which the vertices are visited with
a depth-first search starting with vertex 1. If there are multiple vertices adjacent to a given vertex,
visit the adjacent vertex with the lowest value first. (10 points)
5. On undirected graphs, does either of the two traversals, DFS or BFS, always find a cycle faster (i.e. in
less operations) than the other? If yes, indicate which of them is better and explain why it is the
case; if not, draw two graphs supporting your answer and explain the graphs. (10 points)
List the order in which the vertices are visited with a topological sort, as shown in class. Always
process the vertex with the lowest number first if several vertices have indegree 0. (10 points)
COMS W3134, Theory Homework 4
Apply Kruskal’s algorithm to find the minimum spanning tree. Edges are sorted first by length, and in
the event of a tie, by name, where the two letters are in alphabetical order. Use makeset(x),
find(x), and union(x, y) to determine if there are cycles.
a) Circle the edges that are part of the minimum spanning tree. (4 points)
AC, AD, AE, BC, BD, BE, DE
c) Draw the tree that results from applying the union-find algorithm for cycle detection. When
drawing the tree, put vertices with lower letters in the left subtrees so that all the vertices in a
level are sorted alphabetically from left to right. (5 points)
8. Using the same graph as in the previous question, use Prim’s algorithm to find the minimum
spanning tree starting with vertex A. Fill in the table below, as shown in lecture. (10 points)
9. In a weighted graph, assume that the shortest path from a source 'S' to a destination 'T' is correctly
calculated using Dijkstra’s shortest path algorithm. If we increase weight of every edge by 1, does
the shortest path always remain the same? If so, prove it. If not, give a counterexample. (10 points)
COMS W3134, Theory Homework 4
10. Suppose you have coins in a row with the following monetary value in cents: 7 1 3 2 1 7 5
You want to take coins such that you maximize the value of collection subject to the constraint that
no two adjacent coins may be taken. Use dynamic programming to fill in the table. (7 points)
Index 0 1 2 3 4 5 6 7
C 0 7 1 3 2 1 7 5
F 0
S 0
What coins did you take? List the indices of the coins taken from lowest to highest, assuming that
first 7-cent coin is in index 1. (2 points)