be a connected, undirected graph. Give an O(V + E)

advertisement
22.2-9
Let G =( V,E ) be a connected, undirected graph. Give an O(V + E)-time algorithm
to compute a path in G that traverses each edge in E exactly once in each
direction. Describe how you can find your way out of a maze if you are given a
large supply of pennies.
Solution:
We can use a variant of depth-first search. Every edge is marked the first and
second time it is traversed with unique marks for each traversal. Edges that have
been traversed twice may not be taken again.
Our depth-first search algorithm must ensure that all edges all explored, and
that each edge is taken in both directions. To ensure that all edges are explored,
the algorithm must ensure that unexplored edges are always taken before edges
that are explored once. To ensure that edges are taken in both directions, we
simply backtracking until a new unexplored edge is found. This way, edges are
only explored in the reverse direction during the backtracking.
Complexity time : O( V+E )
1.
Perform a depth-first search of G starting at an arbitrary vertex.( The path
required by the problem can be obtained from the order in which DFS
explores the edges in the graph.)
2.
When exploring an edge (u, v) that goes to an unvisited node the edge (u, v)
is included for the first time in the path.
3.
When DFS backtracks to u again after v is made BLACK, the edge (u, v) is
included for the 2nd time in the path, this time in the opposite direction
(from v to u).
4.
When DFS explores an edge (u, v) that goes to a visited node (GRAY or
BLACK) we add (u, v) (v, u) to the path. In this way each edge is added to the
path exactly twice.
22.3-2
Show how depth-first search works on the graph of Figure 22.6. Assume that the
for loop of lines 5–7 of the DFS procedure considers the vertices in alphabetical
order, and assume that each adjacency list is ordered alphabetically. Show the
discovery and finishing times for each vertex, and show the classification of each
edge.
Solution:
Tree edges: (q, s), (s, v), (v, w), (q, t), (t, x), (x, z), (t, y), (r, u)
Back edges: (w, s), (z, x), (y, q)
Forward edges: (q, w)
Cross edges: (r, y), (u, y)
22.3-3
Show the parenthesis structure of the depth-first search of Figure 22.4.
Solution:
Answer : ( u ( v ( y ( x x ) y ) v ) u ) ( w ( z z ) w )
Download