Interactive · 1 question
A tree is a connected graph without cycles.
Suppose and are vertices of a tree . Which of the following is a possible number of paths from to ?
Suppose there are two different paths and . Since these two paths are different, we know that there exists a point at which they diverge, and since they have the same destination, a point at which the converge again. Let be the vertex at which the two paths go to different edges, and be the next vertex that both paths share. It then follows that from we have a path from to , and from a path from to with no edges in both these paths. Combining the two, we have formed a cycle .
Note here that we were careful in choosing and using precise language, as and could converge and diverge at multiple points before reaching .
Important: this same argument proves that are all wrong answers too. Therefore, all trees have precisely one path between any connected pairs of vertices.
Now let be any graph with this property, that each pair of vertices has either or path from one to the other. If had any cycles, then we could just cycle around many times, which violates this property. Therefore, is a tree. Therefore, a tree can equivalently be defined as any graph with precisely one path between any connected pairs of vertices.
Trees have a very nice structure that allow us to determine if there is a path from one vertex to another.
Depth-First Search
A depth-first search (DFS) starting at vertex and searching for iteratively searches all possible paths by going down one path until it stops, then recursing at the very last possible step. It's called depth-first because it goes down one path until it exhausts everything in that direction. Formally, the algorithm is:
We will use a stack to hold the vertices we need to search, and a list to contain the vertices we have already searched. Therefore, let initially contain the neighbors of and initially contain all of the vertices of the graph except and its neighbors. Now we perform the following step until we can no longer perform it:
Pop an element off of , and delete from . If , we're done. Otherwise, for each neighbor of that is still in , push it to the stack.

Animation of a DFS.
Breadth-First Search
A breadth-first search (BFS) starting at vertex and searching for iteratively expands from by checking the neighbors of all vertices. It's called breadth-first because breadth refers to width. Formally, the algorithm is:
We start with a list for the vertices we've already searched, and another list containing the neighbors of a given level. Initially, contains , and contains the neighbors of . We then perform the following step until we can no longer perform it:
If , we're done. Otherwise, add all elements of to , and let be the list of all vertices not in that are adjacent to some element of of . Finally, replace with .

Animation of a BFS.
Comparing the Algorithms
DFS is better than BFS for memory purposes (it doesn't have to store so many neighbors). However, it is possible for a DFS to look for a very long time in the wrong direction, even if the is adjacent to . For this reason, BFS may be faster because it covers more ground at each iteration.