Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Perform depth-first search on each of the following graphs; whenever there’s a choice of vertices, pick the one that is alphabetically first. Classify each edge as a tree edge, forward edge, back edge, or cross edge, and give the pre and post number of each vertex.

Short Answer

Expert verified

(a) Order of DFS traversal: ABCDHGFE

Tree edges: AB,BC,CD,DH,GH,FG,FE

Forward edges:AF,BE

Back edge: FG,EG,ED,DB

Pre and Post order are: A(1,16),B(2,15),C(3,14),D(4,13),H(5,12),G(6,11),F(7,10),E(8,9)

,

(b) Order of DFS traversal: ABFCDEHG

Tree edges: AB,AH,HG,BF,FC,DE

Forward edges: FE

Back edge: GA,CB

Cross edge: DC,GF,GB

Pre and Post order are: localid="1657775037924" A(1,16),B(2,11),C(4,5),D(6,9),E(7,8),F(3,10),G(13,14),H(12,15)

Step by step solution

01

Step 1: Depth First Search Graph Traversal

Depth First Search (DFS) is an application of graph traversal. It traverses the node downwards and uses the stack as a data structure through this it traverses all vertices in downward direction one by one.

A graph contains various edges. They are:tree edge, forward edge and back edge. The graph obtained by traversing while using depth first search is called its tree edge. The edge (u,v)where u is descendant and it is not part of depth first search is called forward edge. The edge(u,v) where u is ancestor and it is not part of depth first search is called forward edge.

02

Traverse the graph (a) by depth first search.

(a)

While traversing the graph bydepth-first search, the vertex A is traversed first. From A there are two options B and F and through A traversal is done to B.

From vertex B two options are C and E. Take E as the next vertex then D then using depth first search, go through node H, G and last traverse F. Figure showing graph traversal by depth first search is:

Edge AB in black color is the main edge called tree edge. The edge in blue color for edge AF is called back edge and the edge BE is called as forward edge.

Here the tree edges for the given graph are AB,BC,CD,DH,GH,FG,FE. The forward edges are AF,BE. And the back edge of the graph is FG,EG,ED,DB.The tree edges of the given graph are localid="1657775173067" AB,AH,HG,BF,FC,DE.The forward edge is FE. The back edges are GA,CBand the cross edge of the graph is DC,GF,GB. Hence, after applying depth-first search where the order of traversal is ABCDHGFE .

The pre and post number of each vertex are given as, A1,16 where 1 is pre vertex and 16 is post vertex. First the pre vertex is obtained and then post vertex of the graph is obtained. The final sequence of the pre and post order are A(1,16),B(2,15),C(3,14),D(4,13),H(5,12),G(6,11),F(7,10),E(8,9).

Hence, pre and post numbers of each vertex are localid="1657775094839" A(1,16),B(2,15),C(3,14),D(4,13),H(5,12),G(6,11),F(7,10),E(8,9)and the order of DFS traversal is ABCDHGFE .

03

Traverse the graph (b) by depth first search.

(b)

Figure showing graph traverse by depth first search is

In this graph, according to depth first search start from the node A, there are two vertices that are linked with A they are B or H but as mentioned in the question that give preference of vertices by their alphabetic order. Hence, after this visit B. From B there is a direct connection with F. So, the next node in this path is F then again F is connected with two nodes that are C and D . Here by alphabetical order traversal, first C is traversed then from C there is no other edge for traversing then go to the back edge which is back track F to D . After D , traverse the other nodes in a same way that are E,G,F .

Blue color in the graph shows cross edge. Black color shows tree edge of the traversal and the red edge shows back edge and the edge in the green shows forward edge.

Pre and post number of the vertex A given is A1,16 where 1 is pre vertex and 16 is the post vertex. Similarly, the pre and post number of each vertex of the graph is obtained.

Hence, pre and post numbers of each vertex are A(1,16),B(2,11),C(4,5),D(6,9),E(7,8),F(3,10),G(13,14),H(12,15)and the order of DFS traversal is ABFCDEHG.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

You are given tree T=(V,E) along with a designated root node rV. The parent of any node Vr, denoted p(V), is defined to be the node adjacent to v in the path from r to v . By convention, p(r)=r. For k>1, define pk(v)pk-1(pv)andp1(v)=p(v)(so pk(v)is the k th ancestor of v ). Each vertex v of the tree has an associated non-negative integer label l(v). Given a linear-time algorithm to update the labels of all the vertices T according to the following rule: lnew(v)=l(plvv).

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?

For each node in an undirected graph, let twodegreeube the sum of the degrees of’s neighbors. Show how to compute the entire array of two degree. values in linear time, given a graph in adjacency list format

Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G has a node for each course, and an edge from course v to course w if and only if v is a prerequisite for w. Find an algorithm that works directly with this graph representation, and computes the minimum number of semesters necessary to complete the curriculum (assume that a student can take any number of courses in one semester). The running time of your algorithm should be linear.

Either prove or give a counterexample: if {u,v}is an edge in an undirected graph, and during depth-first search (u)<post (v), then vis an ancestor of uin the DFS tree.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free