1

There is a graph (directed and unweighted) and a collection of nodes. If I want to find a tree that has all those nodes in it and potentially some other ones as well, would BFS be a good algorithm to use for this case? Can BFS find, let's say as an example, two nodes that are not directly connected but after going to an intermediate node then they are kind of connected?

D.W.
  • 167,959
  • 22
  • 232
  • 500
Caroline
  • 11
  • 2

1 Answers1

1

Assuming you want the following:

Given a graph $G=(V,E)$ and a set of nodes $N \subseteq V$, find a tree $T$ that spans over all nodes in $N$. Here, $T$ is a spanning tree of the induced subgraph of a subset $S$ of vertices where $N \subseteq S \subseteq V$.

Since you have no optimization criterion here, finding a spanning tree of $G$ itself should suffice. If there exists a rooted directed spanning tree in $G$, you can, of course, find that using BFS or DFS by possibly starting it from every vertex. Also see this post for your reference.

codeR
  • 1,983
  • 7
  • 17