CSCI 256
Design and analysis of algorithms
Assignment 9

Due Wednesday, 4/18/2001


Only turn in problems from the second section.

Practice problems:

  1. Show that the sum of the degrees of all vertices of graph G is equal to 2 times the number of edges of the graph.

    Solution: Each edge contributes to the degree of two vertices -- one for each endpoint. Thus the sum of the degrees of the vertices is equal to the total number of endpoint which equals two times the number of edges.

    Could also prove this by induction. Base case - no edges. Sum of degrees is 0.
    Suppose it is true of all graphs with k edges. Let G be a graph with k+1 edges. Erase one edge, e, to get G' with k edges. By induction the sum of degrees is 2*k. If add back e, increase sum of edges by two, giving 2*k+2 = 2*(k+1).

  2. Problem 25.2-2 on page 531 of the text. Show Dijksta's algorithm (and proof of correctness) fail if there are vertices with negative weight edges.

    Solution: Let G have vertices v1, v2, and v3, and let w(v1,v2) = 5, w(v2,v3) = 2, and w(v1,v3) = -10. Then Dijkstra gives d[0] = -20, d[2] = -8, and d[3] = -10, but in fact if keep going back and forth from v1 to v3 then can get cost of path from v1 to v3 to be any value of the form -10 - 20*k for any k.

    The proof fails in that delta(s,y) need no longer be less than or equal to delta(s,u) in the proof (top of page 530 in the text or from lecture notes). Thus the argument that d[y] <= d[u] fails.

  3. An undirected graph is said to be k-colorable if all the vertices of G can be colored usking k different colors such that no two adjacent vertices have the same color. Design a linear-time algorithm to color a graph with two colors or determine that two colors are not sufficient.

    Solution: Use a breadth-first search algorithm. When encounter a new vertex v' in traversing the vertices adjacent to a vertex v, if v' is uncolored, then color it the opposite color of v. If v' is already colored, then make sure that its color is the opposite of v (fail otherwise). The proof of correctness is left as an exercise to the reader.

Problems to be turned in:

  1. Let G = (V,E) be a connected undirected graph. We want to pick a vertex of degree 1 of G, remove it and its incident edge from G, and continue this process (taking and removing) until all edges are removed. If this procedure is possible for a set of graphs, then designing algorithms by induction for these graphs may be easier. Characterize connected undirected graphs that satisfy these conditions. In other words, find necessary and sufficient conditions for a graph G on which the procedure described above is possible. Argue why your characterization is both necessary and sufficient (i.e., why every graph in your set works, and every graph not in will not work).

    Solution: This process works only for acyclic graphs. If G has a cycle then everything in the cycle has degree >= 2. The only way to reduce any of their degrees to anything less than 2 is to remove one of the vertices in the cycle, but can't remove one without first having removed another. Therefore can never remove any of the vertices in the cycle.

    On the other hand, if G is acyclic with n vertices, then it is a tree and the number of edges n-1. By problem 1 above (not to be turned in), the sum of the degrees is is 2n-2. Therefore every acyclic graph G must have at least one node with degree less than 2. Remove it and its incident edge. Clearly it is still acylic. Thus can remove another vertex with its incident edge and continue until all edges are removed.

  2. Problem 23.4-5 on page 488 of the text. Perform a topological sort by repeatedly finding a vertex of in-degree 0, output it, and remove it and all of its outgoing edges from the graph.

    Solution: Create a queue, Q, which can hold vertices. Go through the graph (O(V)) and put all vertices with in-degree 0 on Q.

        while Q non-empty do
           v <- Q.remove()
           for all u in Adj[v] do
               remove (v,u) from G
               if in-degree of u is now 0 then
                  Q.add(u)
           remove v from G and output it
        
    We will use an adjacency list representation in which we save the in-degree with each node. The while loop executes at most O(V) times (each vertex is removed from Q at most once). Removing v from the Q and the graph are both constant time operations (all edges incident to v have already been removed). The for loop is executed at most once for each directed edge (in total), giving cost O(E). Note that because we use the adjacency list implementation, going from one edge in the adjacency list to the next is constant time. Similarly removing that edge from the adjacency list, checking the in-degree, and adding to the Q are all constant time. Thus we get O(V) + O(V) + O(E) = O(V+E).

    If G has a cycle then (by argument in previous problem), the in-degree of any vertex in the cycle will never go to 0 and hence the graph will never be reduced to an empty graph. (Of course topological sort never works in graphs with cycles.)


Back to:

  • CS256 home page
  • Kim Bruce's home page
  • CS Department home page
  • kim@cs.williams.edu