What’s the runtime of BFS(u) on a graph with nodes explorable along edges from ?
Try running BFS(2) on the following undirected graph:
Write the order in which nodes are visited, then label each node with the path length to the origin (2). What do you notice about the visit order of BFS?
Consider this: a tree is a special kind of undirected graph with only one path between all pairs of nodes (you could also define it to be a directed graph with edges pointing from parent to child).
DFS(root) on a tree corresponds to which tree traversal?
BFS(root) on a tree corresponds to which tree traversal?
For this problem, let’s stick to thinking about undirected graphs. A connected component is a subgraph that is connected. The number of connected components in a graph is the smallest number of components that, taken together, comprise the whole graph. For example, the following graph has 3 connected components:
Suppose you have an adjacency list representation of a graph. Devise an algorithm to count the number of connected components. Hint: What can you say about the subgraph containing the set of nodes visited by a DFS and all edges that connect pairs of those nodes (this is called the induced subgraph)?