





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!






(a) TRUE or FALSE: Representing a graph in adjacency matrix representation takes Θ(|V |^2 ) space. Justification: It’s a |V | × |V | matrix, which has |V |^2 elements.
(b) TRUE or FALSE : The minimum spanning tree in a weighted graph G is always unique, no matter what the graph G or the weights are. Justification: In the following graph, both A − B −C and B −C − A are MSTs of weight 2.
1
1
1
A C
B
Comment: If the edge weights are all different, then the MST is guaranteed to be unique. (c) TRUE or FALSE: Suppose G is a graph with n vertices and n^1.^5 edges, represented in adjacency list representation. Then depth-first search in G runs in O(n^1.^5 ) time. Justification: |V | = n, |E| = n^1.^5. The running time of DFS is O(|V | + |E|) = O(n + n^1.^5 ) = O(n^1.^5 ).
Consider the following code for driving a robot in a spiral-like pattern:
SPIRAL(n):
Let M(n) denote the number of miles that the robot drives (in total) if we call SPIRAL(n).
(a) M( 1 ) = 0.
(b) M( 2 ) = 8.
(c) Write a recurrence relation for M(n):
M(n) = M(n − 2 ) + 4 n.
(d) Solve for M(n), as a function of n. Write your answer using O(·) notation.
M(n) = O(n^2 ).
(a) Write a concise and efficient algorithm to fill in the d′[·, ·] matrix. You should not need more than about 3 lines of pseudocode. Answer:
(b) What is the asymptotic worst-case running time of your algorithm? Express your answer in O(·) nota- tion, as a function of n and |E|. Answer: O(n^2 ).
Consider the following directed graph. Each edge is labelled with the capacity of that edge.
s
c
b d
a t
1
8 4
4
5
2
6
1
(^11)
(a) Find the maximum flow from s to t in this graph. Fill in the graph below with your flow: label each edge with the amount of flow you are sending along that edge.
s
c
b d
a t
1
3
3
2
2
2
1
(^11)
0
(b) What is the value of your flow?
Answer: 5.
(c) Draw the residual graph corresponding to your flow from part (a). I’ve provided the verticies; you fill in the edges of the residual graph. Label each edge in your residual graph with its capacity.
s
c
b d
a t
1
(^5 )
3
2
4
1
(^11)
3 3
1
2
2
(d) Find the minimum-capacity cut (L, R) between s and t in this graph. Show your answer by drawing a circle around the vertices of L, in the picture above. Answer: L = {s, b, c, d}, R = {a,t}, as shown above.
(e) What is the capacity of the cut you identified in part (d)? Answer: 5.
(f) How could you check you didn’t make any mistakes in part (a)? In other words, how could you use your answers to these questions to convince someone else that the flow you drew in part (a) truly is a maximum flow in the graph (without asking them to find the maximum flow themselves from scratch)? Explain, in one or two sentences. Answer: Check that the value of the flow is equal to the capacity of the cut. Given any cut, the value of any flow must be at most the capacity of the cut. The cut shown above has capacity 5, which proves that no flow can have value greater than 5. So the flow shown in part (a) must be optimal.
GRAPH 3-EQUICOLORING is the following search problem:
Given: An undirected graph G with 3n vertices, for some integer n Find: A 3-equicoloring of G, or report that none exists
Recall that a 3-coloring is a way to color each vertex of G with one of 3 colors, say Blue, Gold, and Tan, so that no pair of adjacent vertices share the same color. A 3-equicoloring is a 3-coloring where each color is used on the same number of vertices (so in a graph with 3n vertices there are exactly n Blue vertices, n Gold vertices, and n Tan vertices). You may assume that GRAPH 3-EQUICOLORING is NP-complete.
CLIQUE is the following search problem:
Given: An undirected graph G and a positive integer k Find: A clique of size k in G, or report that none exists
Consider the following two-player game, which is very fun. We start with a n × n matrix M filled with positive integers. When it is her turn, a player can delete the last row or last column of the matrix, but only if the sum of the numbers in that row/column is even. If the sum of numbers in the last row is odd, and the sum of numbers in the last column is odd, then the player has no legal move and loses the game.
Your task: given the n × n matrix M[·, ·], design a polynomial-time algorithm to classify the game as either W (first player wins) or L (first player loses). W means that the first player has a strategy to ensure she will win, no matter how the second player plays the game; L means that no matter what the first player does, the second player has a strategy that will ensure him the ultimate victory. (For instance, the 3 × 3 matrix given above would be classified as W, since there is a strategy Alice can use to ensure she will win.)
In other words, your algorithm should accept M[ 1 ..n, 1 ..n] as input and return either W or L. The running time of your algorithm should be polynomial in n.
(a) In 5 words or less, what would be the best hint you could give to another CS170 student, that would give away how to approach this problem? Answer: dynamic programming.
(b) Show the main idea and pseudocode for your algorithm.
Main idea: We’ll use a table T [ 0 ..n, 1 ..n]. T [i, j] = W if M[ 1 ..i, 1 .. j] is a first-player win; L otherwise. We have the relation
T [i, j] =
W if M[i, 1 ] + M[i, 2 ] + · · · + M[i, j] is even and T [i − 1 , j] = L, W if M[ 1 , j] + M[ 2 , j] + · · · + M[i, j] is even and T [i, j − 1 ] = L, L otherwise.
Pseudocode:
Comment: The pseudocode above runs in O(n^3 ) time. It’s possible to solve this in O(n^2 ) time. We first precompute the sums M[ 1 , j] + · · · + M[i, j] and M[i, 1 ] + · · · + M[i, j] for every i, j, storing them in a n × n matrix; this can be done in O(n^2 ) time. Then steps 4–6 of the code above take O( 1 ) time, so the whole things finishes in O(n^2 ) time. But you didn’t need to do anything nearly so fancy.
Professor Wagner has n topics he’d like to lecture on. As he is preparing his lectures, from time to time he notices a dependency: topic i will need to come before topic j. Let’s build a data structure to record these dependencies. The data structure has two operations:
Note that “comes before” is a transitive relation: if we know that i must come before j, and we know that j must come before k, then it follows that i will have to come before k.
Alice’s Algorithm. Alice proposes the following approach. She will maintain a directed graph G = (V, E) with vertices V = { 1 , 2 ,... , n} corresponding to the n topics, represented in memory using adjacency list representation. At any point in time, the graph G will have an edge (i, j) if and only if Professor Wagner has previously called BEFORE(i, j). Alice suggests to implement QUERY using depth-first search in this graph. In pseudocode:
BEFORE(i, j):
QUERY(i, j):
Answer the following questions.
(a) Suppose Professor Wagner performs m calls to BEFORE, followed by a single call to QUERY, using Alice’s algorithm. What is the worst-case running time of the last call to QUERY? Express your answer as a function of n and m, using O(·) notation. You may assume 0 ≤ m ≤ n^2. Answer: O(m). Comment: There will be only m edges in the graph, and at most m vertices reachable from i, so DFS (without restarts) will take O(m) time.
(b) Suppose Professor Wagner performs a sequence of n^1.^5 calls to BEFORE and n^1.^8 calls to QUERY, interleaved in some order. As in part (a), he uses Alice’s algorithm. What is the total running time of all of these calls, in the worst case? Express your answer as a function of n, using O(·) notation. Answer: O(n^3.^3 ). Comment: The worst case is if Professor Wagner makes all his BEFORE calls before any of the QUERY calls. The BEFORE calls will take O(n^1.^5 ) time. At that point, there will be m = n^1.^5 edges in the graph, so each of the n^1.^8 QUERY calls could take O(n^1.^5 ) time. The total running time is O(n^1.^8 × n^1.^5 ) = O(n^3.^3 ).
Joe’s algorithm. Joe has a different idea. He likes the idea of maintaining a directed graph G = (V, E), but he prefers to store his graph in an adjacency matrix representation. Also, Joe’s graph is a bit different: at any point in time, his graph will have an edge (i, j) if and only if QUERY(i, j) would return true at that point in time. Joe scribbles quickly on the back of an envelope, and you look over his shoulder and see the following pseudocode:
BEFORE(i, j):