0

How to find a spanning tree which is not minimum in a graph(if possiblr)

2 Answers2

0

Kruskal algorithm finds the minimum spanning tree ordering all the edges by weight, choosing them from the lightest to the heaviest and adding them to the solution only if they don't form a cycle. When you reach a number of edges equal to the number of vertexes minus one, you have the minimum spanning tree.

To obtain a spanning tree that is not minimum you can just apply the same algorithm without pre-ordering the edges' list, or shuffling randomly the list before starting.

Matteo Di Napoli
  • 567
  • 5
  • 17
0

If your goal is to find any arbitrary spanning tree, regardless of whether it's minimal, you can always just use a plain vanilla DFS or BFS to search the graph, building up the spanning tree by adding in edges to newly-discovered nodes. This runs in time linear in the size of the graph, is fast in practice, and is simple to code up.

If your goal is to find a spanning tree that specifically isn't an MST, you could consider just running a regular MST algorithm, but when comparing weights on edges, always reverse the result of the comparison. This ends up finding a maximum spanning tree, which, unless every spanning tree in the graph happens to be minimum, won't be a minimum spanning tree. This takes the same amount of time as running a regular MST algorithm, so you can take your pick on which one you'd like to use.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065