Search code examples
javagraphtreeadjacency-matrix

Decompose a tree into forest


How can I decompose a tree into forest such that each tree contains even number of vertices? Does i need to decompose tree randomly and check for every forest? I have already created adjacency matrix but doesn't helping me anyhow!
What should be the correct way to achieve the same?


Solution

  • The problem as stated is trivial. Just return the tree.

    In order for every tree in a forest to contain an even number of vertices, the total vertices in the forest must be even. Which implies the number of vertices in the original tree is even. Since a tree is a forest, simply returning the tree is a valid solution.