Perform a depth-first search on the following graph; whenever theres a choice of vertices, pick the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the pre and post number of each vertex.
Read moreTextbook Solutions for Algorithms
Question
Run the DFS-based topological ordering algorithm on the following graph. Whenever you havea choice of vertices to explore, always pick the one that is alphabetically first.(a) Indicate the pre and post numbers of the nodes.(b) What are the sources and sinks of the graph?(c) What topological ordering is found by the algorithm?(d) How many topological orderings does this graph have?
Solution
Problem 3.3
Run the DFS-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first.
(a) Indicate the pre and post numbers of the nodes.
(b) What are the sources and sinks of the graph?
(c) What topological ordering is found by the algorithm?
(d) How many topological orderings does this graph have?
Step by Step Solution
Step 1 of 4
(a)
DFS-based topological ordering:
The idea is to run the DFS algorithm on the graph and print the vertices in the reverse order of post number
Let the given graph be G(V,E)
Let visited[u] = False indicates that the vertex u has not been visited and
visited[u] = True indicates that the vertex u has been visited
At first, for all vertices u in graph G, set visited [u] = False
Start with vertex A.
Exploring vertex A:
Set visited[A] = True
Set Pre number of vertex A to 1.
Vertex reachable from A is C.
As, visited[C] = False
Now Explore C.
Exploring vertex C:
Set visited[C] = True
Set Pre number of vertex C to 2.
Vertices reachable from C are D and E.
As, visited[D] = False
Now Explore D.
Exploring vertex D:
Set visited[D] = True
Set Pre number of vertex D to 3.
Vertex reachable from D is F.
As, visited[F] = False
Now Explore F.
Exploring vertex F:
Set visited[F] = True
Set Pre number of vertex F to 4.
Vertices reachable from F are G and H
As, visited[G] = False
Now Explore G
Exploring vertex G
Set visited[G] = True
Set Pre number of vertex G to 5.
Set Post number of Vertex G to 6.
Exploring vertex H (reachable from F):
Set visited[H] = True
Set Pre number of vertex H to 7.
Set Post number of Vertex H to 8.
All the reachable vertices of F are already visited
Set Post number of Vertex F to 9.
All the reachable vertices of D are already visited
Set Post number of Vertex D to 10.
Exploring vertex E (reachable from C):
Set visited[E] = True
Set Pre number of vertex E to 11.
Set Post number of Vertex H to 12.
All the reachable vertices of C are already visited
Set Post number of Vertex C to 13.
All the reachable vertices of A are already visited
Set Post number of Vertex A to 14.
Explore B:
Set visited[B] = True
Set Pre number of vertex B to 15.
All the reachable vertices of B are already visited
Set Post number of Vertex B to 16.
The following graph shows the pre and post number for each vertex.
full solution