In the following picture, T give you the adjacency lists of an undirected graph, plus a suggested layout of the nodes.
Here are two exam questions that I could see myself asking:
Question 1: Suppose I call DFS(J)
. Which node is visited first: K or B? (Hint: It’s not a bad idea to draw the graph here.)
Question 2: Suppose I print out the name of each node, the first time that it is visited after I call DFS(J)
. What will be printed (print a 10-character word, with no spaces, like “TBFNCKMGAD”)? I suggest that you maintain a stack and a “visited” vector, and then instead of doing recursion, you push nodes from the adjacency list in reverse order. You don’t even have to draw the graph for this problem.