


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



HW 1 Solution Set Dr. Charles Martel, Susan Margulies Due Monday, April 14 ECS 122A, Spring 2008
We claimed in lecture that the stable marriage algorithm could be implemented to run in O(n^2 ) time. Describe data structures which will allow this run time. In particular, you should describe how to store the preference lists, the current marriage, and the men who are unpaired. Justify that each proposal now takes O(1) time.
We can store the unpaired men in a queue or a stack. Then, checking to see if there are any unpaired men and getting the next unpaired man can be done in constant time. We can store the current spouse of each women in a length n array (assuming we have numbered the women and men 1 to n). Clearly, setting and retreiving this can be done in constant time. This leaves the problem of the preference lists. For the men, it is easy enough to store the list in an array in the “normal” order, keeping around the last index looked at. For the women, however, storing the lists this way presents a problem. Whenever we look up the ranking of the current proposing groom, we must take Θ(n) to do a linear search to find his position. To overcome this, we can store the preference list “inside-out.” Instead of listing the grooms in rank order, we can list the ranks in groom order. Thus, when we want to look up a prospective bride’s rank of groom x, we can merely look in cell x of her preference table. To be thorough, we need to show that we can make this conversion in no more than Θ(n^2 ) time, the overall running time of the algorithm. This is easy. We have n brides, each with a size n array to convert. Each step of this conversion takes constant time (reading groom x at position i in the old array, causes us to set postion x to i in the new array). Thus the pre-processing takes Θ(n^2 ). Given that each part of the proposal takes constant time, we claim that the proposal takes constant time overall.
n^3 ≤ cn^2 n ≤ c
Therefore, for any constant c that we pick, we can always find an n such that n is greater than c. Therefore, since we can never find a c and an n 0 such that ∀n > n 0 , n^3 ≤ cn^2 , this implies that O(n^3 ) 6 = O(n^2 ).
log n ≤ n log (n≤) ≤ n≤ ≤ log n ≤ n≤
log n ≤
n≤
Therefore, let c = 1/≤, and take n 0 to be any n > 1.
ln n ≤ n ≤ ln n ≤ n
The second inequality holds because we have fixed an ≤. Thus we can simply choose an n > n 0 that makes that inequality true. Because the difference between n and ln n increases as n increases, we can always pick an n 0 that allows us to satisfy the second inequality.
≤ ln n ≤ n ln n≤^ ≤ n eln^ n ≤ ≤ en n≤^ ≤ en
Therefore, let c = 1, and for all n greater than the n 0 we determined to satisfy the second inequality, n≤^ ≤ cen^ and n≤^ = O(en). Notice that we can generalize this proof to use any base logorithm greater than 1, and that gives us n≤^ = O(dn) for any ≤ and any d > 1.
The subroutine CommonNeighbors runs in O(|V |) time, since each vertex could be adjacent to n − 1 other vertices. Since there are O(|V |^2 ) pairs of vertices, and each pair is processed in O(|V |) time, the algorithm runs in O(|V |^3 ) time.
One of the following statements is true. Say which one and prove it.
a. If a directed graph G has k strongly connected components, by adding one more edge to G, the number of strongly connected components can drop at most by 1 (i.e the new graph obtained from G by adding one edge has at least k − 1 strongly connected components).
b. For every k, there exists a graph G that has k strongly connected components and such that if we add one particular edge to G, we can make it be strongly connected (i.e. the new graph has only 1 strongly connected component).
Solution: The correct statement is b. Consider a directed almost-cycle in k vertices. This graph consists of directed edges (1, 2), (2, 3),... , (k − 1 , k). Since there are no cycles in this graph, each vertex is its own strongly connected component. Thus, there are k strongly connected components. However, if the directed edge (k, 1) is added, every vertex can reach every other vertex. Thus, the graph consists of one strongly connected component.
In an undirected graph with 2 connected components it is always possible to make the graph con- nected by adding only one edge. Give an example of a directed graph with two strongly connected components such that no addition of one edge can make the graph be strongly connected. Solution: Consider the graph with two isolated nodes and no edges. The addition of any edge creates a path from one vertex or the other, but not both.