Search code examples
algorithmminimum-spanning-tree

Split an undirected graph by multiple minimum spanning trees


I want to split an undirected graph by multiple minimum spanning trees. There are some special (root) nodes from which I want to start constructing a minimum spanning tree and I know every weight between nodes.

Is there any algorithm to solve this problem? If there are no strict methods, any approximate methods are fine for me.

I attach two output examples. I will be glad if you help me. Thank you.

First sample Second sample


Solution

  • The problem can be solved by creating another special node (lets call red node). Connect red node with every special node (black nodes in initial graph) with zero weight edge. Then search MST from red node. At the end remove red node and all corresponding edges from node, this will split graph into several graphs (same number of special nodes).