# Graph Traversal

### Simulating Graph Algorithms

7.1. For the following graphs $\displaystyle{ G_1 }$ (left) and $\displaystyle{ G_2 }$ (right):
(see book for figures)
(a) Report the order of the vertices encountered on a breadth-first search starting from vertex $\displaystyle{ A }$. Break all ties by picking the vertices in alphabetical order (i.e., $\displaystyle{ A }$ before $\displaystyle{ Z }$).
(b) Report the order of the vertices encountered on a depth-first search starting from vertex $\displaystyle{ A }$. Break all ties by picking the vertices in alphabetical order (i.e., $\displaystyle{ A }$ before $\displaystyle{ Z }$).

7.2. Do a topological sort of the following graph $\displaystyle{ G }$:
(see book for figure)

### Traversal

7.3. Prove that there is a unique path between any pair of vertices in a tree.

7.4. Prove that in a breadth-first search on a undirected graph $\displaystyle{ G }$, every edge is either a tree edge or a cross edge, where $\displaystyle{ x }$ is neither an ancestor nor descendant of $\displaystyle{ y }$ in cross edge $\displaystyle{ (x, y) }$.

7.5. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite?

7.6. You are given a connected, undirected graph $\displaystyle{ G }$ with $\displaystyle{ n }$ vertices and $\displaystyle{ m }$ edges. Give an $\displaystyle{ O(n + m) }$ algorithm to identify an edge you can remove from $\displaystyle{ G }$ while still leaving $\displaystyle{ G }$ connected, if one exists. Can you reduce the running time to $\displaystyle{ O(n) }$?

7.7. In breadth-first and depth-first search, an undiscovered node is marked discovered when it is first encountered, and marked processed when it has been completely searched. At any given moment, several nodes might be simultaneously in the discovered state.
(a) Describe a graph on $\displaystyle{ n }$ vertices and a particular starting vertex $\displaystyle{ v }$ such that $\displaystyle{ \Theta(n) }$ nodes are simultaneously in the discovered state during a breadth-first search starting from $\displaystyle{ v }$.
(b) Describe a graph on $\displaystyle{ n }$ vertices and a particular starting vertex $\displaystyle{ v }$ such that $\displaystyle{ \Theta(n) }$ nodes are simultaneously in the discovered state during a depth-first search starting from $\displaystyle{ v }$.
(c) Describe a graph on $\displaystyle{ n }$ vertices and a particular starting vertex $\displaystyle{ v }$ such that at some point $\displaystyle{ \Theta(n) }$ nodes remain undiscovered, while $\displaystyle{ \Theta(n) }$ nodes have been processed during a depth-first search starting from $\displaystyle{ v }$.(Note, there may also be discovered nodes.)

7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals.

7.9. Present correct and efficient algorithms to convert an undirected graph $\displaystyle{ G }$ between the following graph data structures. You must give the time complexity of each algorithm, assuming $\displaystyle{ n }$ vertices and $\displaystyle{ m }$ edges.
(b)Convert from an adjacency list to an incidence matrix. An incidence matrix $\displaystyle{ M }$ has a row for each vertex and a column for each edge, such that $\displaystyle{ M[i,j]=1 }$ if vertex $\displaystyle{ i }$ is part of edge $\displaystyle{ j }$, otherwise $\displaystyle{ M[i,j] = 0 }$.
(c)Convert from an incidence matrix to adjacency lists.

7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations $\displaystyle{ (+, -, *, /) }$. For example, the expression $\displaystyle{ 2+3*4+(3*4)/5 }$ is represented by the tree in Figure 7.17(a). Give an $\displaystyle{ O(n) }$ algorithm for evaluating such an expression, where there are $\displaystyle{ n }$ nodes in the tree.

7.11. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations $\displaystyle{ (+,-,*,/) }$. For example, the expression $\displaystyle{ 2+3*4+(3*4)/5 }$ is represented by the DAG in Figure (see book)(b). Give an $\displaystyle{ O(n+m) }$ algorithm for evaluating such a DAG, where there are $\displaystyle{ n }$ nodes and $\displaystyle{ m }$ edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency.

7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem.

### Applications

7.13. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell $\displaystyle{ n }$. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win.

7.14. Plum blossom poles are a Kung Fu training technique, consisting of $\displaystyle{ n }$ large posts partially sunk into the ground, with each pole $\displaystyle{ p_i }$ at position $\displaystyle{ (x_i, y_i) }$. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than $\displaystyle{ d }$ meters but less than $\displaystyle{ 2d }$ meters. Give an efficient algorithm to find a safe path from pole $\displaystyle{ p_s }$ to $\displaystyle{ p_t }$ if it exists.

7.15. You are planning the seating arrangement for a wedding given a list of guests, $\displaystyle{ V }$. For each guest $\displaystyle{ g }$ you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if $\displaystyle{ h }$ is on bad terms with $\displaystyle{ g }$, then $\displaystyle{ g }$ is on bad terms with $\displaystyle{ h }$. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists.

### Algorithm Design

7.16. The square of a directed graph $\displaystyle{ G=(V,E) }$ is the graph $\displaystyle{ G^2=(V,E^2) }$ such that $\displaystyle{ (u,w) \in E^2 }$ iff there exists $\displaystyle{ v \in V }$ such that $\displaystyle{ (u,v) \in E }$ and $\displaystyle{ (v,w) \in E }$; i.e., there is a path of exactly two edges from $\displaystyle{ u }$ to $\displaystyle{ w }$.
Give efficient algorithms for both adjacency lists and matrices.

7.17. A vertex cover of a graph $\displaystyle{ G=(V,E) }$ is a subset of vertices $\displaystyle{ V' }$ such that each edge in $\displaystyle{ E }$ is incident on at least one vertex of $\displaystyle{ V' }$.
(a)Give an efficient algorithm to find a minimum-size vertex cover if $\displaystyle{ G }$ is a tree.
(b)Let $\displaystyle{ G=(V,E) }$ be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of $\displaystyle{ G }$.
(c)Let $\displaystyle{ G=(V,E) }$ be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of $\displaystyle{ G }$.

7.18. A vertex cover of a graph $\displaystyle{ G=(V,E) }$ is a subset of vertices $\displaystyle{ V' \in V }$ such that every edge in $\displaystyle{ E }$ contains at least one vertex from $\displaystyle{ V' }$. Delete all the leaves from any depth-first search tree of $\displaystyle{ G }$. Must the remaining vertices form a vertex cover of $\displaystyle{ G }$? Give a proof or a counterexample.

7.19. An independent set of an undirected graph $\displaystyle{ G=(V,E) }$ is a set of vertices $U$ such that no edge in $\displaystyle{ E }$ is incident on two vertices of $\displaystyle{ U }$.
(a)Give an efficient algorithm to find a maximum-size independent set if $\displaystyle{ G }$ is a tree.
(b)Let $\displaystyle{ G=(V,E) }$ be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of $\displaystyle{ G }$.
(c)Let $\displaystyle{ G=(V,E) }$ be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of $\displaystyle{ G }$.

7.20. A vertex cover of a graph $\displaystyle{ G=(V,E) }$ is a subset of vertices $\displaystyle{ V' \in V }$ such that every edge in $\displaystyle{ E }$ contains at least one vertex from $\displaystyle{ V' }$. An independent set of graph $\displaystyle{ G=(V,E) }$ is a subset of vertices $\displaystyle{ V' \in V }$ such that no edge in $\displaystyle{ E }$ contains both vertices from $\displaystyle{ V' }$.
An independent vertex cover is a subset of vertices that is both an independent set and a vertex cover of $\displaystyle{ G }$. Give an efficient algorithm for testing whether $\displaystyle{ G }$ contains an independent vertex cover. What classical graph problem does this reduce to?

7.21. Consider the problem of determining whether a given undirected graph $\displaystyle{ G=(V,E) }$ contains a triangle or cycle of length 3.
(a)Give an $\displaystyle{ O(|V|^3) }$ to find a triangle if one exists.
(b)Improve your algorithm to run in time $\displaystyle{ O(|V| \cdot |E|) }$. You may assume $\displaystyle{ |V| \leq |E| }$.
Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of $\displaystyle{ G }$.

7.22. Consider a set of movies $\displaystyle{ M_1, M_2, \ldots, M_k }$. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time.
You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists.

7.23.The diameter of a tree $\displaystyle{ T=(V,E) }$ is given by
$\displaystyle{ \max_{u,v\in V} \delta(u,v) }$
(where $\displaystyle{ \delta(u,v) }$ is the number of edges on the path from $\displaystyle{ u }$ to $\displaystyle{ v }$). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm.

7.24. Given an undirected graph $\displaystyle{ G }$ with $\displaystyle{ n }$ vertices and $\displaystyle{ m }$ edges, and an integer $\displaystyle{ k }$, give an $\displaystyle{ O(m+n) }$ algorithm that finds the maximum induced subgraph $\displaystyle{ H }$ of $\displaystyle{ G }$ such that each vertex in $\displaystyle{ H }$ has degree $\displaystyle{ \geq k }$, or prove that no such graph exists. An induced subgraph $\displaystyle{ F=(U,R) }$ of a graph $\displaystyle{ G=(V,E) }$ is a subset of $\displaystyle{ U }$ of the vertices $\displaystyle{ V }$ of $\displaystyle{ G }$, and all edges $\displaystyle{ R }$ of $\displaystyle{ G }$ such that both vertices of each edge are in $\displaystyle{ U }$.

7.25. Let $\displaystyle{ v }$ and $\displaystyle{ w }$ be two vertices in a directed graph $\displaystyle{ G=(V,E }$. Design a linear-time algorithm to find the number of different shortest paths (not necessarily vertex disjoint) between $\displaystyle{ v }$ and $\displaystyle{ w }$. Note: the edges in $\displaystyle{ G }$ are unweighted.

7.26. Design a linear-time algorithm to eliminate each vertex $\displaystyle{ v }$ of degree 2 from a graph by replacing edges $\displaystyle{ (u,v) }$ and $\displaystyle{ (v,w) }$ by an edge $\displaystyle{ (u,w) }$. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed.

### Directed Graphs

7.27. The reverse of a directed graph $\displaystyle{ G = (V, E) }$ is another directed graph $\displaystyle{ G^R = (V, E^R) }$ on the same vertex set, but with all edges reversed; that is, $\displaystyle{ E^R = {(v, u) : (u, v) \in E} }$. Give an $\displaystyle{ O(n + m) }$ algorithm for computing the reverse of an $\displaystyle{ n }$-vertex $\displaystyle{ m }$-edge graph in adjacency list format.

7.28. Your job is to arrange $\displaystyle{ n }$ ill-behaved children in a straight line, facing front. You are given a list of $\displaystyle{ m }$ statements of the form “$\displaystyle{ i }$ hates $\displaystyle{ j }$.” If $\displaystyle{ i }$ hates $\displaystyle{ j }$, then you do not want to put $\displaystyle{ i }$ somewhere behind $\displaystyle{ j }$, because then $\displaystyle{ i }$ is capable of throwing something at $\displaystyle{ j }$.
(a) Give an algorithm that orders the line (or says that it is not possible) in $\displaystyle{ O(m + n) }$ time.
(b) Suppose instead you want to arrange the children in rows such that if $\displaystyle{ i }$ hates $\displaystyle{ j }$, then $\displaystyle{ i }$ must be in a lower numbered row than $\displaystyle{ j }$. Give an efficient algorithm to find the minimum number of rows needed, if it is possible.

7.29. A particular academic program has $\displaystyle{ n }$ required courses, certain pairs of which have prerequisite relations so that $\displaystyle{ (x, y) }$ means you must take course $\displaystyle{ x }$ before $\displaystyle{ y }$. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program?

7.30. Gotcha-solitaire is a game on a deck with $\displaystyle{ n }$ distinct cards (all face up) and $\displaystyle{ m }$ gotcha pairs $\displaystyle{ (i, j) }$ such that card $\displaystyle{ i }$ must be played sometime before card $\displaystyle{ j }$. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists.

7.31. You are given a list of $\displaystyle{ n }$ words each of length $\displaystyle{ k }$ in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language.
For example, if the strings are $\displaystyle{ {QQZ, QZZ, XQZ, XQX, XXX} }$, the character order must be $\displaystyle{ Q }$ before $\displaystyle{ Z }$ before $\displaystyle{ X }$.
(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.)
(b) What is its running time, as a function of $\displaystyle{ n, k }$, and $\displaystyle{ \alpha }$?

7.32. A weakly connected component in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components?

7.33. Design a linear-time algorithm that, given an undirected graph $\displaystyle{ G }$ and a particular edge $\displaystyle{ e }$ in it, determines whether $\displaystyle{ G }$ has a cycle containing $\displaystyle{ e }$.

7.34. An arborescence of a directed graph $\displaystyle{ G }$ is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether $\displaystyle{ G }$ contains an arborescence, and its time complexity.

7.35. A mother vertex in a directed graph $\displaystyle{ G = (V, E) }$ is a vertex $\displaystyle{ v }$ such that all other vertices $\displaystyle{ G }$ can be reached by a directed path from $\displaystyle{ v }$.
(a) Give an $\displaystyle{ O(n + m) }$ algorithm to test whether a given vertex $\displaystyle{ v }$ is a mother of $\displaystyle{ G }$, where $\displaystyle{ n = |V| }$ and $\displaystyle{ m = |E| }$.
(b) Give an $\displaystyle{ O(n + m) }$ algorithm to test whether graph $\displaystyle{ G }$ contains a mother vertex.

7.36. Let $\displaystyle{ G }$ be a directed graph. We say that $\displaystyle{ G }$ is k-cyclic if every (not necessarily simple) cycle in $\displaystyle{ G }$ contains at most $\displaystyle{ k }$ distinct nodes. Give a linear-time algorithm to determine if a directed graph $\displaystyle{ G }$ is $\displaystyle{ k }$-cyclic, where $\displaystyle{ G }$ and $\displaystyle{ k }$ are given as inputs. Justify the correctness and running time of your algorithm.

7.37. A tournament is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph $\displaystyle{ G = (V, E) }$ such that for all $\displaystyle{ u,v \in V }$, exactly one of $\displaystyle{ (u, v) }$ or $\displaystyle{ (v, u) }$ is in $\displaystyle{ E }$. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path.

### Articulation Vertices

7.38. An articulation vertex of a connected graph $\displaystyle{ G }$ is a vertex whose deletion disconnects $\displaystyle{ G }$. Let $\displaystyle{ G }$ be a graph with $\displaystyle{ n }$ vertices and $\displaystyle{ m }$ edges. Give a simple $\displaystyle{ O(n+m) }$ algorithm for finding a vertex of $\displaystyle{ G }$ that is not an articulation vertex—that is, whose deletion does not disconnect $\displaystyle{ G }$.

7.39. Following up on the previous problem, give an $\displaystyle{ O(n+m) }$ algorithm that finds a deletion order for the $\displaystyle{ n }$ vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.)

7.40. Suppose $\displaystyle{ G }$ is a connected undirected graph. An edge $\displaystyle{ e }$ whose removal disconnects the graph is called a bridge. Must every bridge $\displaystyle{ e }$ be an edge in a depth-first search tree of $\displaystyle{ G }$? Give a proof or a counterexample.

7.41. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back.
(a) Let $\displaystyle{ G }$ be the original undirected graph. Prove that there is a way to properly orient/direct the edges of $\displaystyle{ G }$ provided $\displaystyle{ G }$ does not contain a bridge.
(b) Give an efficient algorithm to orient the edges of a bridgeless graph $\displaystyle{ G }$ so the result is strongly connected.

### Interview Problems

7.42. Which data structures are used in depth-first and breath-first search?

7.43. Write a function to traverse binary search tree and return the ith node in sorted order.

Back to Chapter List