# 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.

7.13

7.14

7.15

7.16

7.17

7.18

7.19

7.20

7.21]

7.22

7.23

7.24

7.25

7.26

7.27

7.28

7.29

7.30

7.31

7.32

7.33

7.34

7.35

7.36

7.37

7.38

7.39

7.40

7.41

### Interview Problems

7.42

7.43

Back to Chapter List