Find set of nodes over which a tree was mirrored

125 Views Asked by At

I am given an undirected graph, that has been made of 2 trees that are mirrored, example of such below, I know all the edges and vertices, but nothing else (roots etc.)

graph

I need to find the common endings of these two trees - the division nodes, on the example, it is 4, 13, 10, 9, 3.

Any ideas, or at least a push, how to solve it in some way using simple and efficient algorithm for any graph - ? I have tried to do BFS, ended at best finding tree containing given number of nodes, but I kinda feel that is not a good way to continue.

1

There are 1 best solutions below

1
On

For a tree with $n$ nodes, the number of edges is $n{-}1$. For two copies of a tree, you would expect $2n$ nodes and $2n{-}2$ edges, two more nodes than edges. Here you have $16$ edges and $13$ nodes, three fewer nodes than edges, so there must be $5$ nodes amalgamated (at the mirror).

Since it is a tree that is mirrored, any cycle must include at least two of the mirror points.

In your example, the tree has been mirrored along its leaves, and there are no edges that run along the mirror line. Is this true in general for your case? If so, then only nodes of degree $2$ can be on the mirror, and only those that connect to two nodes of the same degree (which further eliminates nodes $1, 5, 7$ and $11$ in the example).