Solutions to problems 3

Algorithms
106
Exercises
Algorithms
106
3.1. Perform a depth-first search on the following graph; whenever there’s a choice of vertices, pick
T-79.4202
Autumn 2015
the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the
Exercises
Principles of Algorithmic
Techniques
pre and post
number of each vertex.
Tutorial 3 3.1. Perform a depth-first search on the following graph; whenever there’s a choice of vertices, pick
B edge orCback edge, and give the
the one that is alphabetically first. Classify eachAedge as a tree
Problems, Hints
and
and
Solutions
pre and
post
number
of each vertex.
Basic problems:
A
BD
C E
D
E
F
F
H
G
I
1. [Dasgupta et al., Ex. 3.2] Perform a depth-first search on each of the following graphs; whenG each
I
3.2. Perform
depth-first
search on
ofHthe
following
whenever there’s
of vertices,
ever there’s
a choice
of vertices,
pick
the
one
that
isgraphs;
alphabetically
first.a choice
Classify
each edge as
pick the one that is alphabetically first. Classify each edge as a tree edge, forward edge, back
a tree3.2.
edge,
forward
edge,
back
edge,
or cross
edge,
andofgive
the
pre
and post number of each
Perform
depth-first
each
of the
graphs;
of vertices,
edge,
or crosssearch
edge,onand
give
thefollowing
pre and
postwhenever
number there’s
eacha choice
vertex.
pick the one that is alphabetically first. Classify each edge as a tree edge, forward edge, back
vertex. edge,
or cross edge, and give the pre and post number of each vertex.
(a)
A
(a)
A
B
B
C
A
(b)
B
C
H
C
C
H
E
E
D
D
G
G
F G
F
B
A
(b)
H
GH
F
D
D
F
E
E
3.3. Run
DFS-based
topological
ordering algorithm
the following
Whenever
you have
(a)
3.3. the
Run
the DFS-based
topological
orderingonalgorithm
on graph.
the (b)
following
graph.
Whenever you have
a choice
of vertices
to explore,
pick
the onepick
thatthe
is alphabetically
first.
a choice
of vertices
to always
explore,
always
one that is alphabetically
first.
A
A
D
F
C
D
G
G
2. [Dasgupta et al., Ex. 3.4] Run the strongly
connected components
algorithm on the following
F
C
H
107
GR : whenever
there
is
a
choice
of vertices to explore,
H
E
B
(a) Indicate
the pre
numbers of the nodes.
always pick
the one
thatandispost
alphabetically
first. In each case answer the following questions.
Band U.V. VaziraniE
S. Dasgupta,
C.H.
Papadimitriou,
directed
graphs
G. When
doing DFS on
A the sources and sinks of the graph?
(i) (b) What are
(ii)
(a) Indicate
the pre and
numbers of the nodes.
S. Dasgupta,
C.H. Papadimitriou,
andpost
U.V. Vazirani
(c) What topological ordering
is found
by the algorithm?
D
C
(b) What are the sources and sinks of the graph?
B
(d) How many topological
orderings does this graph have? A
A
(i)
(c) What
topological
ordering is found by the(ii)algorithm?
B
107
C
D
C
3.4. Run the
connected
components
algorithm
on this
the following
directed graphs G. When
E
(d)strongly
How
many
topological
orderings
does
graph
have?
A
B
C
doing DFS on GRB: whenever there Fis a choice of vertices to explore, always pick the one that is
F
E
alphabetically first.
D
E
3.4. Run the strongly connected components
algorithm on the following directed graphs G. When
F
F
E to explore,
D
doing DFS onHGR : whenever there is a choice of vertices
always pick the one that is
G
alphabetically
first. H J
G
J
I
I
H H
GG
I
I
In each case answer the following questions.
(c)
In each case answer the following questions.
(d)
(a) In what order are the strongly connected components (SCCs) found?
(a) In what order(b)are
theare
strongly
connected
components
Which
source SCCs
and which are
sink SCCs? (SCCs) found?
(c) Draw the “metagraph” (each meta-node is an SCC of G).
(b) Which are source
SCCs and which are sink SCCs?
(d) What
is the
minimum
number connected
of edges you must components
add to this graph to make
it strongly
(a)
In what order
are
the
strongly
(SCCs)
found?
connected?(each meta-node is an SCC of G).
(c) Draw the “metagraph”
The
reverse
of a number
directed
graph
G and
= (V, E)
is another
directed
G graph
= (V, E to
) on
the same
(b)
Which
are
the
source
SCCs
which
SCCs?
(d) What
is 3.5.
the
minimum
of edges
you
mustare
addsink
tograph
this
make
it strongly
R
connected?
R
vertex set, but with all edges reversed; that is, E R = {(v, u) : (u, v) ∈ E}.
a linear-time algorithm for computing the reverse of a graph in adjacency list format.
(c) Draw theGive
“metagraph”
(each meta-vertex is an SCC of G).
R
In an undirected
graph,
degree
of a vertexdirected
u is the number
of neighbors
u has,
3.5. The reverse of3.6.
a directed
graph
G =the(V,
E) d(u)
is another
graph
GR = (V,
Eor
)equivon the same
alently,
the
number
of edges incident
upon
In{(v,
a directed
graph,
weE}.
distinguish between the
vertex
set, butiswith
allminimum
edges
reversed;
that is,
E
u) you
: (u,
v)must
∈
(d) What
the
number
ofRit.=edges
add to this graph to
indegree din (u), which is the number of edges into u, and the outdegree dout (u), the number of
make it strongly
leaving u. for computing the reverse of a graph in adjacency list format.
Give aconnected?
linear-timeedges
algorithm
!
(a) Show that in an undirected graph, u∈V d(u) = 2|E|.
3.6. In an undirected graph,
the degree d(u) of a vertex u is the number of neighbors u has, or equiv(b) of
Use
part (a)incident
to show that
in an undirected
theregraph,
must be an
even
number of vertices
the number
edges
upon
it.water.
In a graph,
directed
we
distinguish
between
the sizes
3. alently,
[Dasgupta
et al.,
Ex.
3.8]
Pouring
We
have
three
containers
whose
whose degree is odd.
indegree din (u), which is the number of edges into u, and the outdegree dout (u), the number of
(c) Does a similar
statement hold for
the number
of vertices
odd indegree
in a directed start out full
7 pints,
and
respectively.
The
7-pint
andwith
4-pint
containers
edges
leaving
u. 4 pints,
graph?
are 10 pints,
of water, but
the 10-pint3.7.container
is initially(V, E)empty.
We are allowed one type of operation: pouring the
bipartite graph is a graph G =!
whose vertices can be partitioned into two sets (V = V ∪V
(a) Show that inA
an
undirected
graph,
d(u) =
2|E|.
u∈V
and Vcontainer
∩ V = ∅) such into
that there
are
no edgesstopping
between vertices
in the when
same set (for
if
contents of one
another,
only
theinstance,
source
container is empty or the
∈ V , then there is no edge between u and v).
(b) Use part (a) u,tov show
that in an undirected graph, there must be an even number of vertices
destination
container
is
full.
We
want
to
know
if
there
is
a
sequence
of pourings that leaves
whose degree(a)is Give
odd.a linear-time algorithm to determine whether an undirected graph is bipartite.
There
many
other
ways
to container.
formulate
Forwith
instance,
undirected graph
exactly
pints(b)in
theare7or
4-pint
(c) Does 2
a similar
statement
hold
for
the
numberthis
of property.
vertices
oddanindegree
in a directed
1
1
2
2
1
graph?
is bipartite if and only if it can be colored with just two colors.
Prove the following formulation: an undirected graph is bipartite if and only if it contains
no cycles of odd length.
3.7. A bipartite graph is(c)a At
graph
G =many
(V, colors
E) whose
vertices
partitioned
intoexactly
two sets
(V = V1 ∪V2
most how
are needed
to colorcan
in anbe
undirected
graph with
one oddand V1 ∩ V2 = ∅) suchlength
thatcycle?
there are no edges between vertices in the same set (for instance, if
u, v ∈ V1 , then there is no edge between u and v).
(a) Give a linear-time algorithm to determine whether an undirected graph is bipartite.
(b) There are many other ways to formulate this property. For instance, an undirected graph
is bipartite if and only if it can be colored with just two colors.
(a) Model this as a graph problem: give a precise definition of the graph involved and state the
specific question about this graph that needs to be answered.
(b) What algorithm should be applied to solve the problem?
Hints: (a) Have vertices of the graph correspond to the different configurations of water in the
has
132 three containers. (b) Any systematic graph-search algorithm will work, but BFS probably
Algorithms
an advantage over DFS. Why?
Exercises
4. [Dasgupta et al., Ex. 4.1] Suppose Dijkstra’s algorithm is run on the following graph, starting
vertex Dijkstra’s
A.
4.1. at
Suppose
algorithm is run on the following graph, starting at node A.
1
A
8
4
E
5
2
B
6
C
2
6
F
G
1
1
D
1
4
H
1
(a) Draw a table showing the intermediate distance values of all the nodes at each iteration of
the algorithm.
(a) Draw a table showing the intermediate distance values for all the vertices at each iteration
(b) Show the final shortest-path tree.
of the algorithm.
4.2. (b)
JustShow
like the
problem, but
this time with the Bellman-Ford algorithm.
theprevious
final shortest-path
tree.
Whenever there is a choice of vertices Ato explore,
always pick the one that is alphabetically
−2
2
first.
4
Advanced problems:
B
D
C
7
S
6
1
3
5. [Dasgupta et al., Ex. 3.7] A bipartite graph
is a graph 5G = (V,FE) whose vertices can be parti−4
−2
/ such
tioned into two sets (V = V1 ∪V2 and V1 ∩V2 = 0)
6 that there are no edges between vertices
in the same set (for instance, if u, v ∈ V11 , then there is no edge−2between u and v).
H
G
3
E
−1 determine
(a) Give a linear-time algorithm to
graph is bipartite.
1 whether an undirected
I
(b) There are many other ways to formulate this property. For instance, an undirected graph is
bipartite if and only if it can be coloured with just two colours.1
4.3. Squares. Design and analyze an algorithm that takes as input an undirected graph G = (V, E)
and Prove
determines
whether formulation:
G contains a simple
cycle (that
is, aiscycle
whichifdoesn’t
intersect
itself)
the following
an undirected
graph
bipartite
and only
if it contains
of length four. Its running time should be at most O(|V |3 ).
no cycles of odd length.
You may assume that the input graph is represented either as an adjacency matrix or with
(c)
At most
how
many colours
are needed
to simpler.
colour an undirected graph with exactly one
adjacency
lists,
whichever
makes your
algorithm
odd-length cycle?
4.4. Here’s a proposal for how to find the length of the shortest cycle in an undirected graph with unit
edge lengths.
Solution:
When a back edge, say (v, w), is encountered during a depth-first search, it forms a
cycle with
the tree edges
from
w tograph,
v. Theassigning
length of one
the cycle
level[v]to−each
level[w]
+ 1, either
(a) Perform
a depth-first
search
of the
of twois colors
vertex,
where
levelIfofwe
a vertex
is its distance
in the DFS
from edge
the root
vertex.two
This
black
orthe
white.
never encounter
a forward,
backtree
or cross
between
vertices
suggests the following algorithm:
with the same color, the graph is bipartite: V1 is the set of all black vertices, and V2 the set of
• Do a depth-first search, keeping track of the level of each vertex.
all white vertices. Else, if we encounter a forward, back or cross edge between two vertices
• Each time a back edge is encountered, compute the cycle length and save it if it is
with the
same color, we claim that the graph is not bipartite: indeed, consider a path P of
smaller than the shortest one previously seen.
tree edges between two vertices a and b of the same color, and e a forward, backward, or
Show that this strategy does not always work by providing a counterexample as well as a brief
(one can
or two
sentence)
graph
be coloured
withexplanation.
k colours, if it is possible to assign one of a given set of k colours to each of the vertices,
so that no two neighbouring vertices get the same colour.
1A
cross edge between a and b. But the definition of bipartite graphs means that there are only
two ways to color the vertices or P, since black and white must alternate on P. Therefore,
there is no way to color a and b with different colors, and the graph is not bipartite.
(b) By the same argument, if G contains a cycle of odd length, there is no way to color this
cycle using just two colors. Then, if G contains no cycle of odd length, we claim that
it can be colored with two colors: indeed, run the previous algorithm. If we ever find a
backwards, forward or cross edge between two vertices with the same color, then this is an
odd cycle.
(c) If there is exactly one cycle of odd length, then remove one of its vertices, and all its
neighboring edges. Formally, let v ∈ V be one of the vertices on the cycle, and let G0 =
(V \ {v}, E ∩ ((V \ {v})2 )).
Then G0 has no cycle of odd length, hence we can color it using at most two colors (pink
and green), by the previous question. Finally, we put v back into G0 , and assign it a different
color, e.g. yellow.
6. [Dasgupta et al., Ex. 4.5] Often there are multiple shortest paths between two vertices of a
graph. Give a linear-time algorithm for the following task.
Input: Undirected graph G = (V, E) with unit edge lengths; vertices u, v ∈ V .
Output: The number of different shortest paths from u to v.
Solution:
Perform a breadth-first search of G, starting from u, labeling each vertex by the number of
shortest paths to reach it. Each time an unexplored vertex is hit in the breadth-first search, by
following edge (a, b) ∈ E, add label(a) to label(b).
When v has been explored, output label(v).