Why BFS returns the fewest hops on a graph and DFS returns the first route it meets.
About this comparison
Two copies of one graph, 13 nodes and 15 edges, both searched from A for L: depth-first search in pink with its stack under the graph, breadth-first search in blue with its queue. Nodes show their visit numbers; each counter counts nodes visited.
DFS keeps a stack holding the path it is on. Each beat it steps from the top node to its first unvisited neighbour, in alphabetical order, and pushes it. When the top has nothing new left, DFS pops it and backs up. It stops on reaching L, and the stack is its route.
BFS keeps a queue of nodes seen but not yet visited. Each beat it visits the node at the front and adds that node's unseen neighbours to the back. Here each column is one hop further from A, so BFS clears one column before the next.
Both visit each node at most once: O(V + E) time, O(V) memory. A stack sends DFS after the newest node it found, so it commits to one road; a queue sends BFS after the oldest, so it finishes every route of one length before a longer one, and reaches each node in the fewest hops.
DFS takes the top road to G, then turns down to H, ahead of J in the alphabet, though J leads to L. It wanders through E, I and F to a dead end at C, backs up 2 edges, runs the bottom road out to M, and only then climbs to J and L: all 13 nodes visited, a 10 hop route.
BFS reaches L on its 12th visit, before it ever gets to M, along the 5 hop top road A, B, D, G, J, L. The route DFS returns is twice as long.
Use BFS for the fewest hops when every edge counts the same: degrees of separation, the fewest flights, the fewest moves in a puzzle. When edges carry different costs, use Dijkstra.
Use DFS when any route will do or you must touch everything: is there a path, what is connected, is there a cycle, which task runs first. Its stack holds only the current path, while a BFS queue can hold a whole ring of a wide graph.