1

How can I use breath-first-search to find all the possible spanning trees from a starting vertex. Not just one.

  • Obviously you can't do that using breadth-first-search. Imagine a fully connected graph, a possible spanning is a long string , e.g. found by depth depth first search, but BFS is not designed to find it. – Haatschii Jun 15 '20 at 16:12

1 Answers1

2

If you're looking for all possible spanning trees, then you don't actually need to do a BFS. You can just set every edge's weight to 1, then run an algorithm that finds all minimum spanning trees in the graph.

This works because all spanning trees have V-1 edges (where V represents the number of vertices). Since we set all the edges to have weight 1, every spanning tree is a minimum spanning tree!

EDIT: Since you're only looking for spanning trees that start at a certain root, you can solve this problem using depth-first search.

Execute your depth-first search procedure with the starting node as your root target. You can augment the procedure to only connect nodes that are in different components.

Telescope
  • 2,068
  • 1
  • 5
  • 22
  • You are right but in my case i want to find the possible spanning trees from the vertex I start my traversal. – ToniTonagata Jun 15 '20 at 16:15
  • A spanning tree connects every node in the graph, so any spanning tree will automatically include the starting vertex. – Telescope Jun 15 '20 at 16:24
  • I think it has to be the spanning tree's root, from what i understand, that starting vertex. So that would be all spanning trees with that vertex as its root.(something more understandable I hope) – ToniTonagata Jun 15 '20 at 16:31
  • In that case, you should do a DFS. I'll explain more in my answer. – Telescope Jun 15 '20 at 16:33
  • Thank you for your answer but i need to write a program which traverses in width(BFS) from a chosen vertex and then finds the possible spanning trees with that given root. I will mark your answer as accepted anyway. – ToniTonagata Jun 15 '20 at 16:50
  • @ToniTonagata is it an assignment? – JunisvaultCo Jun 15 '20 at 17:03