Discrete MathematicsTrees3 of 5
Trees · 3

Spanning Trees by Depth First and Breadth First Search

When a network has loops of extra roads, we want a way to strip away the extra edges so every vertex stays connected using the fewest possible edges.

1

Spanning Trees and Erasing Cycles

A spanning tree of a graph is a connected subgraph with no cycles that reaches every vertex using only existing edges. Because any tree on nn vertices has n−1\kept{n - 1} edges, every spanning tree of an nn-vertex graph must keep n−1\kept{n - 1} edges, which we can get by erasing one edge from a cycle at a time until no cycles remain.

AA
BB
CC
DD
EE
FF
AA
BB
CC
DD
EE
FF
7 edges7 \text{ edges}
5 edges\htmlClass{lc-green}{5} \text{ edges}
Spanning Trees and Erasing Cycles
AA
BB
CC
DD
EE
FF
Text solution
Count how many edges to eraseA spanning tree on n=6n = 6 vertices must have n−1=5n - 1 = \kept{5} edges, so 2\cut{2} of the 77 edges in the graph must be removed.
Tree edges\displaystyle \text{Tree edges} =6−1\displaystyle = 6 - 1 =5,Edges to erase\displaystyle = \kept{5}, \qquad \text{Edges to erase} =7−5\displaystyle = 7 - \kept{5} =2\displaystyle = \cut{2}
Erase one edge from each cycleRemoving CD\cut{CD} breaks the left cycle A-B-C-D-AA\text{-}B\text{-}C\text{-}D\text{-}A and removing EF\cut{EF} breaks the right cycle A-B-E-F-AA\text{-}B\text{-}E\text{-}F\text{-}A, while the remaining roads around each loop keep all 66 vertices connected.
Spanning tree\displaystyle \text{Spanning tree} ={AB,BC,AD,BE,AF}\displaystyle = \kept{\{AB, BC, AD, BE, AF\}}
2

Counting Spanning Trees

Erasing different edges from a graph's cycles leaves behind different spanning trees. To count the distinct spanning trees without double-counting the order of erasures or accidentally cutting off a vertex, we count the remaining sets of n−1\kept{n - 1} edges by splitting into two cases: trees that keep an edge shared by two cycles, and trees that erase it.

AA
BB
CC
DD
Counting Spanning Trees
How many spanning trees?\text{How many spanning trees?}
AA
BB
CC
AA
BB
CC
DD
Text solution
Keep the shared diagonal BD\shared{BD}To break both the left triangle ABD\leftloop{ABD} and the right triangle BCD\rightloop{BCD} without removing BD\shared{BD}, we erase 11 of the other 2\leftloop{2} edges on the left (ABAB or ADAD) and 11 of the other 2\rightloop{2} edges on the right (BCBC or CDCD).
Trees keeping BD\displaystyle \text{Trees keeping } \shared{BD} =2×2\displaystyle = \leftloop{2} \times \rightloop{2} =4\displaystyle = \kept{4}
Erase the shared diagonal BD\shared{BD}Removing BD\shared{BD} merges the two triangles into a single outer 44-cycle A-B-C-D-AA\text{-}B\text{-}C\text{-}D\text{-}A, which is broken by erasing any 11 of its 44 outer edges.
Trees erasing BD\displaystyle \text{Trees erasing } \shared{BD} =4\displaystyle = \kept{4}
Add the non-overlapping casesEvery spanning tree either includes BD\shared{BD} or does not, so adding the two cases counts each 3\kept{3}-edge spanning tree once.
Total spanning trees\displaystyle \text{Total spanning trees} =4+4\displaystyle = 4 + 4 =8\displaystyle = \kept{8}

In the diamond graph below, erasing the same two edges in either order always leaves the same three edges behind, erasing both edges at A\cut{A} or C\cut{C} cuts off that corner while leaving a triangle cycle, and every valid spanning tree falls into either the 4\kept{4} trees that keep BD\shared{BD} or the 4\kept{4} trees that erase it.

Erasing two edges from the diamond either cuts off a vertex and leaves a triangle cycle, or leaves one of the 8 spanning trees: 4 that keep diagonal BD and 4 that erase it.
3

Spanning Trees by Depth-First Search

On a large graph where cycles are hard to spot by eye, we can build a spanning tree from a single starting vertex by adding n−1\kept{n - 1} edges one at a time, following one rule: only add an edge from a visited vertex to an unvisited vertex. Depth-First Search (DFS) applies this rule by stepping forward to an unvisited neighbor as far as it can go, and backtracking along tree edges only when it hits a dead end where every neighbor is already visited.

Spanning Trees by Depth-First Search
Depth-First Search (DFS)\text{Depth-First Search (DFS)}
AA
BB
CC
DD
EE
FF
A ⁣:A\colon
B,D,FB, D, F
B ⁣:B\colon
A,C,EA, C, E
C ⁣:C\colon
B,DB, D
D ⁣:D\colon
A,CA, C
E ⁣:E\colon
B,FB, F
F ⁣:F\colon
A,EA, E
Text solution
Walk deep from AA until hitting a dead end at DDPicking the first unvisited neighbor alphabetically at each town adds AB\kept{AB}, BC\kept{BC}, and CD\kept{CD}, where DD's remaining neighbor AA is already visited so DA\cut{DA} is skipped to avoid closing the cycle A-B-C-D-A\cut{A\text{-}B\text{-}C\text{-}D\text{-}A}.
Visited: A,B,C,DTree edges: AB,BC,CD(skip DA)\displaystyle \begin{aligned}\text{Visited: } &\kept{A, B, C, D} \\ \text{Tree edges: } &\kept{AB, BC, CD} \\ &(\text{skip } \cut{DA})\end{aligned}
Backtrack along the tree to BB and branch to EERetracing tree edges from DD past CC (which has no unvisited neighbors left) back to BB reaches unvisited neighbor EE, adding BE\kept{BE} and EF\kept{EF} before skipping FA\cut{FA} at FF to finish all 6−1=56 - 1 = \kept{5} edges.
Visited: A,B,C,D,E,FDFS spanning tree: {AB,BC,CD,BE,EF}(skip FA)\displaystyle \begin{aligned}&\text{Visited: } \kept{A, B, C, D, E, F} \\ &\text{DFS spanning tree: } \\ &\quad \kept{\{AB, BC, CD, BE, EF\}} \\ &\quad (\text{skip } \cut{FA})\end{aligned}

At each town on the six-town network, the visited-to-unvisited rule crosses out any edge that would close a cycle, and hitting a dead end retraces as many tree edges as needed to reach the last town that still has an unvisited neighbor.

No matter which unvisited neighbor you pick next, skipping visited vertices blocks every cycle, and backtracking from dead ends always builds a 5-edge spanning tree.
4

Breadth-First Search and Shortest Hop Paths

Instead of walking down a single branch, Breadth-First Search (BFS) builds a spanning tree level by level from the starting vertex, visiting all neighbors 11 hop away before any vertex 22 hops away. It does this by placing each newly visited vertex at the back of a waiting line (queue) and always exploring next from the front of the line.

Breadth-First Search and Shortest Hop Paths
Breadth-First Search (BFS)\text{Breadth-First Search (BFS)}
AA
BB
CC
DD
EE
FF
1 hop\htmlClass{lc-mute}{\text{1 hop}}
2 hops\htmlClass{lc-mute}{\text{2 hops}}
queue\text{queue}
front\htmlClass{lc-mute}{\text{front}}
back\htmlClass{lc-mute}{\text{back}}
Text solution
Explore all 11-hop neighbors from AATaking AA from the front of the waiting line visits all three of its unvisited neighbors B\kept{B}, D\kept{D}, and F\kept{F} right away and places them at the back of the line.
Waiting line: [B,D,F]Tree edges: AB,AD,AF\displaystyle \begin{aligned}\text{Waiting line: } &[B, D, F] \\ \text{Tree edges: } &\kept{AB, AD, AF}\end{aligned}
Explore 22-hop neighbors from BB to reach n−1=5n - 1 = \kept{5} edgesTaking BB from the front skips visited AA and connects BB to unvisited C\kept{C} and E\kept{E}, completing the 5\kept{5}-edge spanning tree while the remaining vertices leave the line skipping DC\cut{DC} and FE\cut{FE}.
Waiting line: [D,F,C,E]BFS spanning tree: {AB,AD,AF,BC,BE}(skip DC,FE)\displaystyle \begin{aligned}&\text{Waiting line: } [D, F, C, E] \\ &\text{BFS spanning tree: } \\ &\quad \kept{\{AB, AD, AF, BC, BE\}} \\ &\quad (\text{skip } \cut{DC, FE})\end{aligned}

Drawing the DFS and BFS spanning trees side by side by hop count from the starting town shows the difference: DFS stretches into a deep chain that can route a direct neighbor the long way around a loop, while BFS stays wide and shallow so every town sits at its fewest-hop distance from the start.

From any starting town, the BFS spanning tree keeps every town at its fewest-hop distance, while the DFS spanning tree stretches into a deep chain around the loops.
5

Connectivity and Spanning Trees

A graph has a spanning tree if and only if the graph is connected. Because DFS and BFS only travel along edges of the graph, we can test whether the whole graph is connected by running either search from a single vertex and seeing if it visits all nn vertices before running out of unvisited neighbors.

Connectivity and Spanning Trees
Text solution
Search from AA until no unvisited neighbors remainOn the 66-vertex graph with BEBE and AFAF removed, starting DFS or BFS at AA visits A,B,C,D\kept{A, B, C, D} and collects 3\kept{3} tree edges before stopping with E\cut{E} and F\cut{F} still unvisited.
Visited from A={A,B,C,D}(4<6 vertices, 3<5 edges)  ⟹  Not connected, no spanning tree\displaystyle \begin{aligned}&\text{Visited from } A = \kept{\{A, B, C, D\}} \\ &(\cut{4} < 6 \text{ vertices}, \ \cut{3} < 5 \text{ edges}) \\ &\implies \text{Not connected,} \\ &\quad \text{ no spanning tree}\end{aligned}

Key concepts

Every spanning tree of an nn-vertex graph connects all nn vertices with n−1\kept{n - 1} existing edges and 00 cycles, and one exists if and only if the graph is connected.

Erasing from cycles (mm edges down to n−1\kept{n - 1})

When a small graph has mm edges, erase m−(n−1)\cut{m - (n - 1)} edges by repeatedly removing one edge from a remaining cycle, which never cuts off any vertex. To count all distinct spanning trees when two cycles share an edge (like BD\shared{BD} in the 44-vertex, 55-edge diamond), split into non-overlapping cases: trees that keep the shared edge (break each cycle using its remaining edges: 2×2=4\leftloop{2} \times \rightloop{2} = \kept{4}) plus trees that erase the shared edge (merging the two cycles into one outer cycle: 4\kept{4}), giving 4+4=84 + 4 = \kept{8}.

Growing from a vertex (00 edges up to n−1\kept{n - 1})

Start at one vertex and only add edges from a visited vertex to an unvisited vertex; skip any edge whose endpoints are both already visited.

  • Use DFS to step forward along one path at a time and backtrack along tree edges only when stuck at a dead end (on the six-town graph from AA, A→B→C→D\route{A \to B \to C \to D} skips DA\cut{DA} and backtracks D→C→B\route{D \to C \to B} before stepping B→E→F\route{B \to E \to F}).
  • Use BFS with a waiting line (front to back) when you need the tree path from the start vertex to every vertex to have the fewest hops in the graph; do not use a DFS tree for fewest-hop distances, because walking deep first can place a 11-hop neighbor like DD at 3\kept{3} hops (A-B-C-D\route{A\text{-}B\text{-}C\text{-}D}).
Testing connectivity

Run DFS or BFS from a single start vertex until the search stops; if fewer than nn vertices were visited (fewer than n−1\kept{n - 1} edges collected), do not restart the search at an unvisited vertex. The graph is disconnected and has no spanning tree.

6

Practice

1

Consider the diamond graph on 44 vertices A,B,C,DA, B, C, D with 55 edges AB,BC,CD,DAAB, BC, CD, DA, and diagonal BDBD. (a) How many edges must every spanning tree of this graph have, and how many edges must be erased to form one? (b) Three students each erase 22 edges and leave behind a set of 33 edges: Set 1 is {AB,BC,CD}\{AB, BC, CD\}, Set 2 is {BC,CD,BD}\{BC, CD, BD\}, and Set 3 is {AB,AD,BD}\{AB, AD, BD\}. Which of these sets is a spanning tree, and why do the others fail?

For (a), use the number of edges in any tree on n=4n = 4 vertices; for (b), check whether each 33-edge set reaches all 44 vertices without keeping a 33-edge triangle cycle.

(a) Count tree edges and erased edgesWith n=4n = 4 vertices, every spanning tree must have n−1=3n - 1 = \kept{3} edges, so 5−3=25 - \kept{3} = \cut{2} edges must be erased. (Watch out: forgetting to subtract 11 from n=4n = 4 gives 44 edges and only 11 erasure, which always leaves a triangle cycle behind.)
Tree edges\displaystyle \text{Tree edges} =4−1\displaystyle = 4 - 1 =3,Edges to erase\displaystyle = \kept{3}, \qquad \text{Edges to erase} =5−3\displaystyle = 5 - \kept{3} =2\displaystyle = \cut{2}
(b) Check each remaining 3\kept{3}-edge setNot every set of n−1=3n - 1 = \kept{3} edges is a spanning tree, because erasing both edges touching a corner cuts off that vertex and leaves the opposite triangle intact. Set 2 ({BC,CD,BD}\{BC, CD, BD\}) erased both AB\cut{AB} and DA\cut{DA}, leaving vertex A\cut{A} disconnected and keeping the cycle B-C-D-B\cut{B\text{-}C\text{-}D\text{-}B}; Set 3 ({AB,AD,BD}\{AB, AD, BD\}) erased both BC\cut{BC} and CD\cut{CD}, leaving vertex C\cut{C} disconnected and keeping the cycle A-B-D-A\cut{A\text{-}B\text{-}D\text{-}A}. Only Set 1 connects all 44 vertices with no cycles.
3 tree edges, 2 erased edges; only Set 1 ({AB,BC,CD}) is a spanning tree\displaystyle \begin{aligned}&\kept{3} \text{ tree edges}, \ \cut{2} \text{ erased edges}; \\ &\text{ only Set 1 } (\kept{\{AB, BC, CD\}}) \\ &\text{ is a spanning tree}\end{aligned}
2

A connected graph on 66 vertices A,B,C,D,E,FA, B, C, D, E, F has 77 edges and the neighbor lists A:B,CA: B, C; B:A,C,D,EB: A, C, D, E; C:A,BC: A, B; D:B,FD: B, F; E:B,FE: B, F; F:D,EF: D, E. Starting at vertex AA and breaking ties alphabetically, trace Depth-First Search (DFS). Give the order in which vertices are marked visited, the 55 spanning tree edges in the order they are added, and the edges skipped because both endpoints are already visited.

From each newly visited vertex, step immediately to its first unvisited neighbor alphabetically; when all neighbors of the current vertex are already visited, retrace your tree edges back to the most recent vertex that still has an unvisited neighbor.

Step forward from AA to dead end CCStart at AA, pick unvisited neighbor BB (add AB\kept{AB}), and from BB pick unvisited neighbor CC (add BC\kept{BC}). At CC, neighbor AA is already visited so edge CA\cut{CA} is skipped to avoid closing cycle A-B-C-A\cut{A\text{-}B\text{-}C\text{-}A}, and DFS backtracks along tree edge CBCB to BB.
Visited: A,B,CTree edges: AB,BC(skip CA, backtrack C→B)\displaystyle \begin{aligned}&\text{Visited: } \kept{A, B, C} \\ &\text{Tree edges: } \kept{AB, BC} \\ &(\text{skip } \cut{CA}, \text{ backtrack } \route{C \to B})\end{aligned}
Branch from BB through D,F,ED, F, EFrom BB, the first unvisited neighbor alphabetically is DD (add BD\kept{BD}). At DD, step forward to DD's unvisited neighbor FF (add DF\kept{DF}), and at FF step to EE (add FE\kept{FE}), where EE's other neighbor BB is already visited so EB\cut{EB} is skipped to avoid closing cycle B-D-F-E-B\cut{B\text{-}D\text{-}F\text{-}E\text{-}B}. (Watch out: after visiting DD, do not jump alphabetically to BB's neighbor EE to get A,B,C,D,E,FA, B, C, D, E, F. DFS must keep stepping forward from the current vertex DD to DD's own unvisited neighbor FF.)
Visited order: A,B,C,D,F,ETree edges: AB,BC,BD,DF,FESkipped edges: CA,EB\displaystyle \begin{aligned}&\text{Visited order: } \\ &\quad \kept{A, B, C, D, F, E} \\ &\text{Tree edges: } \kept{AB, BC, BD, DF, FE} \\ &\text{Skipped edges: } \cut{CA, EB}\end{aligned}
3

On the same graph from Problem 2 (A:B,CA: B, C; B:A,C,D,EB: A, C, D, E; C:A,BC: A, B; D:B,FD: B, F; E:B,FE: B, F; F:D,EF: D, E), trace Breadth-First Search (BFS) starting at vertex AA with alphabetical tie-breaking. (a) Give the order in which vertices enter the waiting line (queue) and the 55 edges of the BFS spanning tree in the order they are added. (b) Compare the number of hops from AA to EE in the DFS spanning tree from Problem 2 versus in this BFS spanning tree.

Place AA in the waiting line first, and each time a vertex leaves the front of the line, add all of its unvisited neighbors to the tree and to the back of the line before moving on to the next vertex in line.

(a) Trace the waiting line level by levelStart with [A][A]. Taking AA from the front adds AB\kept{AB} and AC\kept{AC}, making the line [B,C][B, C]. Taking BB from the front skips visited AA and CC (skipping BC\cut{BC}) and adds BD\kept{BD} and BE\kept{BE}, making the line [C,D,E][C, D, E]. Taking CC skips AA and BB; taking DD adds DF\kept{DF}, placing FF at the back of the line. (Watch out: when EE leaves the line next, do not add EF\cut{EF} for a 6th6\text{th} edge. Vertex FF was already marked visited during DD's turn, so EF\cut{EF} is skipped.)
Queue order: A,B,C,D,E,FBFS tree edges: AB,AC,BD,BE,DF(skip BC,EF)\displaystyle \begin{aligned}&\text{Queue order: } A, B, C, D, E, F \\ &\text{BFS tree edges: } \\ &\quad \kept{AB, AC, BD, BE, DF} \\ &\quad (\text{skip } \cut{BC, EF})\end{aligned}
(b) Compare hop counts from AA to EEIn the DFS spanning tree from Problem 2, the tree path to EE goes around the loop via A-B-D-F-E\route{A\text{-}B\text{-}D\text{-}F\text{-}E}, whereas in the BFS spanning tree it goes directly via A-B-E\route{A\text{-}B\text{-}E}.
DFS tree path A-B-D-F-E=4 hops,BFS tree path A-B-E=2 hops\displaystyle \begin{aligned}\text{DFS tree path } \route{A\text{-}B\text{-}D\text{-}F\text{-}E} &= \kept{4} \text{ hops}, \\ \text{BFS tree path } \route{A\text{-}B\text{-}E} &= \kept{2} \text{ hops}\end{aligned}
4

Find the total number of distinct spanning trees of the six-town graph: 66 vertices A,B,C,D,E,FA, B, C, D, E, F and 77 edges ABAB, BCBC, CDCD, DADA, BEBE, EFEF, FAFA, where the left 44-cycle A-B-C-D-A\leftloop{A\text{-}B\text{-}C\text{-}D\text{-}A} and the right 44-cycle A-B-E-F-A\rightloop{A\text{-}B\text{-}E\text{-}F\text{-}A} share the edge AB\shared{AB}. Show how splitting into cases based on the shared edge AB\shared{AB} counts every spanning tree once.

Every spanning tree keeps 6−1=56 - 1 = \kept{5} edges and erases 7−5=27 - \kept{5} = \cut{2} edges; count the trees that keep the shared road AB\shared{AB} separately from the trees that erase AB\shared{AB}.

Case 1 (keep the shared edge AB\shared{AB})To break both 44-cycles without erasing AB\shared{AB}, we must erase 11 of the other 3\leftloop{3} edges of the left loop (BC,CD,BC, CD, or DADA) and 11 of the other 3\rightloop{3} edges of the right loop (BE,EF,BE, EF, or FAFA). (Watch out: multiplying all 44 edges of the left loop by all 44 edges of the right loop gives 4×4=164 \times 4 = 16, which fails because choosing AB\shared{AB} in both loops only erases 11 edge in total and leaves the outer 66-cycle unbroken.)
Trees keeping AB\displaystyle \text{Trees keeping } \shared{AB} =3×3\displaystyle = \leftloop{3} \times \rightloop{3} =9\displaystyle = \kept{9}
Case 2 (erase the shared edge AB\shared{AB})Removing AB\shared{AB} merges the two 44-cycles into a single outer 66-cycle (BC,CD,DA,BE,EF,FABC, CD, DA, BE, EF, FA), and erasing any 11 of those 66 outer edges breaks the remaining cycle. Because Case 1 trees all contain AB\shared{AB} and Case 2 trees never contain AB\shared{AB}, the two cases do not overlap.
Trees erasing AB\displaystyle \text{Trees erasing } \shared{AB} =6,Total spanning trees\displaystyle = \kept{6}, \qquad \text{Total spanning trees} =9+6\displaystyle = 9 + 6 =15\displaystyle = \kept{15}
5

A graph on 77 vertices A,B,C,D,E,F,GA, B, C, D, E, F, G has the neighbor lists A:C,EA: C, E; B:D,FB: D, F; C:A,E,GC: A, E, G; D:B,FD: B, F; E:A,CE: A, C; F:B,DF: B, D; G:CG: C. Run the connectivity test starting at AA (using DFS or BFS with alphabetical tie-breaking) to determine whether this graph has a spanning tree. List the visited vertices and the tree edges collected before the search stops.

Trace DFS or BFS starting at AA until no visited vertex has an unvisited neighbor, then compare the number of visited vertices to n=7n = 7.

Run the search from AA until it stopsStarting at AA, DFS visits A→C→E\route{A \to C \to E} (skipping EA\cut{EA}), backtracks to CC to visit GG, and backtracks all the way to AA with no unvisited neighbors left (or BFS from AA visits A,C,E,G\kept{A, C, E, G} and empties the waiting line).
Visited: A,C,E,GTree edges: AC,CE,CG (DFS) or AC,AE,CG (BFS)\displaystyle \begin{aligned}\text{Visited: } &\kept{A, C, E, G} \\ \text{Tree edges: } &\kept{AC, CE, CG} \text{ (DFS) or } \\ &\kept{AC, AE, CG} \text{ (BFS)}\end{aligned}
Compare visited count to n=7n = 7The search stops with only 4\cut{4} of the 77 vertices visited and 3<6\cut{3} < 6 edges collected, leaving B,D,F\cut{B, D, F} unreachable in a separate component. (Watch out: do not restart the search at BB to add BDBD and DFDF and claim those 55 edges form a spanning tree. A spanning tree of a 77-vertex graph must be a single connected tree of 7−1=67 - 1 = \kept{6} edges.)
4<7 vertices visited (3 edges): not connected, no spanning tree\displaystyle \begin{aligned}&\cut{4} < 7 \text{ vertices visited } (\cut{3} \text{ edges}): \\ &\text{ not connected, no spanning tree}\end{aligned}
6

On the diamond graph with vertices A,B,C,DA, B, C, D and edges AB,AD,BD,BC,CDAB, AD, BD, BC, CD, suppose you always start your search at vertex AA, where BB and DD are 11 hop from AA and CC is 22 hops from AA, and you may break ties between unvisited neighbors in any order. (a) Which of the 88 spanning trees of the diamond graph can be produced by BFS starting at AA? (b) Why can a spanning tree that contains the diagonal edge BDBD be produced by DFS starting at AA, but never by BFS starting at AA?

Check which vertices are already marked visited by the time AA finishes its turn at the front of the BFS waiting line.

(a) Find all BFS spanning trees from AAWhile exploring AA at the start, BFS must visit all of AA's immediate neighbors (BB and DD in either order), adding both 11-hop edges AB\kept{AB} and AD\kept{AD} and placing BB and DD into the waiting line. Whoever is first in line (BB or DD) then visits the 22-hop vertex CC by adding either BC\kept{BC} or CD\kept{CD}.
BFS spanning trees from A={AB,AD,BC} and {AB,AD,CD}(2 trees)\displaystyle \begin{aligned}&\text{BFS spanning trees from } A \\ &= \kept{\{AB, AD, BC\}} \text{ and } \\ &\quad \kept{\{AB, AD, CD\}} \quad (2 \text{ trees})\end{aligned}
(b) Compare how DFS and BFS handle diagonal BDBDIn BFS from AA, both BB and DD are marked visited during AA's turn before either BB or DD is explored from the waiting line, so when BD\cut{BD} is checked both of its endpoints are already visited and BD\cut{BD} is always skipped. (Watch out: placing BB ahead of DD in the waiting line does not let BFS use BD\cut{BD} during BB's turn, because DD was already marked visited when it entered the line from AA.) In DFS from AA, the search steps to one neighbor first (such as A→B\route{A \to B} via AB\kept{AB}) while DD is still unvisited, allowing DFS to step along BD\kept{BD} next (A→B→D→C\route{A \to B \to D \to C}, giving {AB,BD,CD}\kept{\{AB, BD, CD\}}).
BFS from A marks both B and Dvisited before exploring either,so BD is always skipped;2 BFS trees: {AB,AD,BC},{AB,AD,CD}\displaystyle \begin{aligned}&\text{BFS from } A \text{ marks both } B \text{ and } D \\ &\text{visited before exploring either,} \\ &\text{so } \cut{BD} \text{ is always skipped;} \\ &2 \text{ BFS trees: } \kept{\{AB, AD, BC\}}, \\ &\kept{\{AB, AD, CD\}}\end{aligned}