How to check if a tree has perfect match in linear time?

Give an algorithm with linear time to check if the tree has a perfect match, i.e. a set of edges that touches each vertex of the tree exactly once.

This is from the algorithms of S. Dasgupt, and I just can not cover up this problem. I know that I need to use the greedy approach somehow, but I just can't figure it out. Help?

Pseudo code is fine; as soon as I have an idea, I can implement it in any language trivially.

The algorithm should be linear in everything. O (V + E) is excellent.

+3
source share
5 answers

, . , tree, , . node , .

Ergo, , node, . , .

+7

"", .

, .

.

+1

? , .. (v i, v j) . O (n) .

0

:

For each leaf in the tree: 
  add edge from leaf to its parent to the solution
  delete edge from leaf to its parent
  delete all edges from the parent to any other vertices
  delete leaf and parent from the tree

If the tree is empty then the answer is yes. Otherwise, there no perfect matching.
0

Source: https://habr.com/ru/post/1706975/


All Articles