13.6. Exercises 393 R-13.7 Would you use the adjacency list structure or the adjacency matrix structure in
each of the following cases? Justify your choice.
a. The graph has 10,000 vertices and 20,000 edges, and it is important to use as little space as possible.
b. The graph has 10,000 vertices and 20,000,000 edges, and it is important to use as little space as possible.
c. You need to answer the queryareAdjacentas fast as possible, no matter how much space you use.
R-13.8 Explain why the DFS traversal runs inΘ(n2)time on ann-vertex simple graph that is represented with the adjacency matrix structure.
R-13.9 Draw the transitive closure of the directed graph shown in Figure 13.2.
R-13.10 Compute a topological ordering for the vertices in the directed graph drawn with solid edges in Figure 13.10d.
R-13.11 Can we use a queue instead of a stack as an auxiliary data structure in the topo- logical sorting algorithm shown in Algorithm 13.16?
R-13.12 Give the order in which the edges are labeled by the DFS traversal shown in Figure 13.5.
R-13.13 Give the order in which the edges are labeled by the BFS traversal shown in Figure 13.9.
R-13.14 Give the order in which the edges are labeled by the DFS traversal shown in Figure 13.12.
R-13.15 How many biconnected components would be in the graph shown in Figure 13.5a if we were to remove the edge (B,C) and the edge (N,K)?
Creativity
C-13.1 Justify Theorem 13.11.
C-13.2 Describe the details of anO(n+m)-time algorithm for computing all the con- nected components of an undirected graphGwithnvertices andmedges.
C-13.3 LetT be the spanning tree rooted at the start vertex produced by the depth-first search of a connected, undirected graph,G. Argue why every edge ofG, not in T, goes from a vertex inT to one of its ancestors, that is, it is aback edge.
Hint:Suppose that such a nontree edge is a cross edge, and argue based upon the order the DFS visits the end vertices of this edge how this leads to a contradiction.
C-13.4 SupposeGis a graph withnvertices andmedges. Describe a way to represent GusingO(n+m)space so as to support inO(logn)time an operation that can test, for any two verticesvandw, whethervandware adjacent.
C-13.5 Give a proof of Lemma 13.23.
C-13.6 Show that if a graphGhas at least three vertices, then it has a separation edge only if it has a separation vertex.
C-13.7 Give a proof of Lemma 13.25.
C-13.8 Supply the details of the proof of correctness of theLinkComponentsalgorithm (Algorithm 13.21).
C-13.9 Show how to perform a BFS traversal using, as an auxiliary data structure, a single queue instead of the level containersL0,L1,. . ..
C-13.10 Show that, ifT is a BFS tree produced for a connected graphG, then, for each vertexvat leveli, the path ofT betweensandvhasiedges, and any other path ofGbetweensandvhas at leastiedges.
C-13.11 The directed version of the BFS algorithm classifies nontree edges as being either back edges or cross edges, but it does not distinguish between these two types.
Given a BFS spanning tree,T, for a directed graph,G, and a set of nontree edges, E′, describe an algorithm that can correctly label each edge inE′as being either a back edge or cross edge. Your algorithm should run inO(n+m)time, where nis the number of vertices andmis the number of edges.
Hint:Consider first constructing an Euler tour traversal of the treeT.
C-13.12 Explain why there are no forward nontree edges with respect to a BFS tree con- structed for a directed graph.
C-13.13 In the pseudocode description of the directed DFS traversal algorithm we did not distinguish the labeling of cross edges and forward edges. Describe how to modify the directed DFS algorithm so that it correctly labels each nontree edge as a back edge, forward edge, or cross edge.
C-13.14 Explain why the strong connectivity testing algorithm given in Section 13.4.1 is correct.
C-13.15 LetGbe an undirected graph withnvertices andmedges. Describe anO(n+ m)-time algorithm to determine whetherGcontains at least two cycles.
C-13.16 LetGbe an undirected graph withnvertices andmedges. Describe an algorithm running inO(n+m)time that can determine whetherGcontains exactly two cycles that have no edges in common.
C-13.17 Justify Theorem 13.14.
C-13.18 Show that it is possible to count the total number of paths in a directed acyclic graph,G, with nvertices andmedges usingO(n+m)additions. Also, show that there is a graph,G, where this number is at least 2n/2.
Applications
A-13.1 Aroad networkis a mixed graph defined by the roads in a geographic region.
Vertices in this graph are defined by road intersections and dead ends, and edges are defined by the portions of roads that connect such vertices. An edge is di- rected if its associated road is a one-way street; otherwise, an edge is undirected.
Imagine that you are the manager of a post office delivery system, and that you need to map out the route that a postal worker should drive to deliver mail to all the houses on the roads in a given geographic region. Furthermore, suppose that
13.6. Exercises 395 all the streets in this region are one-way, so the road network for this region is a directed graph,G. An Euler tourof such a directed graph,G, is a cycle that tra- verses each edge ofG exactly once according to its direction. Note that an Euler tour is necessarily the shortest route that starts at the post office (assuming it is one of the vertices inG) and visits every street in the road network represented byG in the appropriate direction (where u-turns are allowed at a vertex with an outgoing edge going back to the origin of an incoming edge). Such a tour always exists ifG is connected and the in-degree equals the out-degree of each vertex inG. Describe an O(n+m)-time algorithm for finding an Euler tour of such a digraph,G, if such a tour exists, starting from some vertex, v, wherenis the number of vertices inG andmis the number of edges inG.
A-13.2 Suppose you are given a connected road network,G, as described in the previous exercise, except that none of the edges inGare directed. Describe an efficient method for designing a tour ofGthat starts at some vertex,v, and traverses each edge ofGexactly once in each direction (with u-turns allowed). What is the running time of your algorithm?
A-13.3 Suppose you work for a company that is giving a smartphone to each of its em- ployees. Unfortunately, the companies that make apps for these smartphones are constantly suing each other over their respective intellectual property. Say that two apps,AandB, arelitigation-conflictingifAcontains some disputed tech- nology that is also contained inB. Your job is to pre-install a set of apps on the company smartphones, but you have been asked by the company lawyers to avoid installing any litigation-conflicting apps. To make your job a little easier, these lawyers have given you a graph,G, whose vertices consist of all the possible apps you might want to install and whose edges consist of all the pairs of litigation- conflicting apps. An independent set of such an undirected graph,G= (V, E), is a subset,I, ofV, such that no two vertices inIare adjacent. That is, ifu, v∈I, then(u, v) ∈E. Amaximal independent setM is an independent set such that, if we were to add any additional vertex toM, then it would not be independent any longer. In the case of the graph,G, of litigation-conflicting apps, a maximal independent set inGcorresponds to a set of nonconflicting apps such that if we were to add any other app to this set, it would conflict with at least one of the apps in the set. Give an efficient algorithm that computes a maximal independent set for a such a graph,G. What is the running time of your algorithm?
A-13.4 Tamarindo University and many other schools worldwide are doing a joint project on multimedia. A computer network is built to connect these schools using com- munication links that form a free tree. The schools decide to install a file server at one of the schools to share data among all the schools. Since the transmission time on a link is dominated by the link setup and synchronization, the cost of a data transfer is proportional to the number of links used. Hence, it is desirable to choose a “central” location for the file server. Given a free treeT and a nodevof T, theeccentricityofvis the length of a longest path fromvto any other node ofT. A node ofT with minimum eccentricity is called acenterofT.
a. Design an efficient algorithm that, given ann-node free treeT, computes a center ofT.
b. Is the center unique? If not, how many distinct centers can a free tree have?