Depth-first search

List the DFS visit order starting from A. Visit available neighbors in alphabetical order and use undirected edges.

Examples

Example 1

Input
A-B, A-C, B-D
Expected output
A, B, D, C

Example 2

Input
Vertices A, B, C. Edge B-C
Expected output
A

Hints and solution

Hint 1

Finish the call for B before returning to A and considering C.

Reveal worked answer

For A-B, A-C, and B-D, the first branch goes from A through B to D. It finishes, then exploration returns to A and reaches C. In a disconnected graph the visit stops with A.

Back to the lesson