# Graphs-TADM2E

# Graph Traversal

**Simulating Graph Algorithms**

5-1.
For the following graphs $ G_1 $ (left) and $ G_2 $ (right):

(see book for figure)

(see book for figure)

- Report the order of the vertices encountered on a breadth-first search starting from vertex $ A $. Break all ties by picking the vertices in alphabetical order (i.e., $ A $ before $ Z $).
- Report the order of the vertices encountered on a depth-first search starting from vertex $ A $. Break all ties by picking the vertices in alphabetical order (i.e., $ A $ before $ Z $).

5-2.
Do a topological sort of the following graph $ G $:

(see book for figure)

**Traversal**

5-3.
Prove by induction that there is a unique path between any pair
of vertices in a tree.

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

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

5-6.
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 $ n $ vertices and a particular starting vertex $ v $
such that $ \Theta(n) $ nodes are simultaneously in the *discovered* state
during a *breadth-first search* starting from $ v $.

(b) Describe a graph on $ n $ vertices and a particular starting vertex $ v $
such that $ \Theta(n) $ nodes are simultaneously in the *discovered* state
during a *depth-first search* starting from $ v $.

(c) Describe a graph on $ n $ vertices and a particular starting vertex $ v $
such that at some point
$ \Theta(n) $ nodes remain *undiscovered*, while $ \Theta(n) $ nodes
have been *processed* during a *depth-first search* starting from $ v $.
(Note, there may also be *discovered* nodes.)

5-7.
Given pre-order and in-order traversals of a binary
tree, 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.

5-8.
Present correct and efficient algorithms to convert an undirected graph $ G $
between the following graph data structures.
You must give the time complexity of each algorithm, assuming $ n $ vertices and $ m $ edges.

- Convert from an adjacency matrix to adjacency lists.
- Convert from an adjacency list to an incidence matrix. An incidence matrix $ M $ has a row for each vertex and a column for each edge, such that $ M[i,j]=1 $ if vertex $ i $ is part of edge $ j $, otherwise $ M[i,j] = 0 $.
- Convert from an incidence matrix to adjacency lists.

5-9.
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
$ (+,-,*,/) $.
For example, the expression $ 2+3*4+(3*4)/5 $ is represented by
the tree in Figure (see book)(a).

(see book for figure)

(see book for figure)
Give an $ O(n) $ algorithm for evaluating such an expression, where there are
$ n $ nodes in the tree.

5-10.
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
$ (+,-,*,/) $.
For example, the expression $ 2+3*4+(3*4)/5 $ is represented by
the DAG in Figure (see book)(b).
Give an $ O(n+m) $ algorithm for evaluating such a DAG, where there are
$ n $ nodes and $ m $ edges in the DAG.
Hint: modify an algorithm for the tree case to achieve the desired efficiency.

5-11.
The war story of **get-graph** 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.}

**Algorithm Design**

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

5-13.
A *vertex cover* of a graph $ G=(V,E) $ is a subset of vertices $ V' $
such that each edge in $ E $ is incident on at least one vertex of $ V' $.

- Give an efficient algorithm to find a minimum-size vertex cover if $ G $ is a tree.
- Let $ 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 $ G $.
- Let $ 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 $ G $.

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

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

5-16.
An *independent set* of an undirected graph $ G=(V,E) $ is a set of vertices $U
$
such that no edge in $ E $ is incident on two vertices of $ U $.

- Give an efficient algorithm to find a maximum-size independent set if $ G $ is a tree.
- Let $ 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 $ G $.
- Let $ G=(V,E) $ be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of $ G $.

5-17.
Consider the problem of determining whether a given undirected graph $ G=(V,E) $
contains a *triangle* or cycle of length 3.

- Give an $ O(|V|^3) $ to find a triangle if one exists.
- Improve your algorithm to run in time $ O(|V| \cdot |E|) $. You may assume $ |V| \leq |E| $.

Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of $ G $.

5-18.
Consider a set of movies $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.

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

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

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

5-22.
Design a linear-time algorithm to
eliminate each vertex $ v $ of degree 2 from a graph
by replacing edges $ (u,v) $ and $ (v,w) $ by an edge $ (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**

5-23.
Your job is to arrange $ n $ ill-behaved children in a straight line,
facing front.
You are given a list of $ m $ statements of the form *$ i $ hates $ j $*.
If $ i $ hates $ j $, then you do not want put $ i $ somewhere behind $ j $,
because then $ i $ is capable of throwing something at $ j $.

- Give an algorithm that orders the line, (or says that it is not possible) in $ O(m+n) $ time.
- Suppose instead you want to arrange the children in rows such that if $ i $ hates $ j $, then $ i $ must be in a lower numbered row than $ j $. Give an efficient algorithm to find the minimum number of rows needed, if it is possible.

5-24.
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?

5-25.
An *arborescence* of a directed graph $ 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
$ G $ contains an arborescence,
and its time complexity.

5-26.
A *mother* vertex in a directed graph $ G=(V,E) $ is a vertex $ v $ such
that all other vertices $ G $ can be reached by a directed path
from $ v $.

- Give an $ O(n+m) $ algorithm to test whether a given vertex $ v $ is a mother of $ G $, where $ n=|V| $ and $ m=|E| $.
- Give an $ O(n+m) $ algorithm to test whether graph $ G $ contains a mother vertex.

5-27.
A *tournament* is a directed graph formed by taking the complete
undirected graph and assigning arbitrary directions on the edges---i.e.,
a graph $ G = (V,E) $ such that for all $ u $,$ v \in V $, exactly one of
$ (u,v) $ or $ (v,u) $ is in $ 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**

5-28.
An articulation vertex of a graph $ G $ is a vertex
whose deletion disconnects $ G $.
Let $ G $ be a graph with $ n $ vertices and $ m $ edges.
Give a simple $ O(n+m) $ algorithm for finding a vertex of $ G $ that is *not*
an articulation vertex---i.e., whose deletion does not disconnect $ G $.

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

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

**Interview Problems**

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

5-32.
Write a function to traverse binary search tree and return the $ i $th node
in sorted order.