Depth First Search and Breadth First Search Question

362 Views Asked by At

I was going through my assignments and got stuck in one question. Though I have solved the same, just want to confirm if my answers are right as I am new to the topic.

Ques: Using Depth First Search (DFS) traverse the following graph by using A as the starting node: Image for the question

Thanks for the help and please also elaborate the answer so that I can get good knowledge about the same.

Ans.) 1. DFS - A, B, C, F, E, D, G 
2. BFS - A, B, D, C, E, F, G 
1

There are 1 best solutions below

0
On BEST ANSWER

Concerning your solution to DFS: This really depends on the specification of your DFS. Should you always choose the alphabetically least node whenever there are multiple possibilities? Because then the sequence of nodes you traverse would be unique: A, B, C, F, D, G, E

For the BFS: You start at node A. Now you first look at all direct neighbours of A, so the sequence should start with A, B, D, E ... (assuming that we visit those nodes in alphabetic order). Now that we visited all directed neighbours, we visit all nodes with distance 2 from A. We start with the direct neighbours of B, since we first visited B in the previous step. We get A, B, D, E, C, F, G.