let there be an undirected tree T, and let there be: T.leaves – all the leaves (each v such that d(v) = 1). we know: |T.leaves| and the distance between u and v for each u,v in T.leaves.
in other words: we have an undirected tree, and we know how many leaves it has, and the distance between every 2 leaves.
we need to find how many inside vertices (d(v)>1) are in the tree.
note: building the complete tree is impossible because if we have only 2 leaves but the distance between them is 2^30, it will take too long…
I tried to start from the shortest distance and count how many vertices are between them, and then adding the vertex closest to them, but for this I need some formula f(leaves_counted,next_leaf) but I could not manage to find that f…
any ideas?
Continued from discussion in comments. This is how to check a particular (compressed) edge to see if you can attach the new vertex
nsomewhere in the middle of it, without iterating over the distances.Ok, so you need to find three numbers:
l(the distance of the attach point from the left node of the edge in question),x(the distance of the new node from the attach point) andr(symmetrical tol.)Obviously, for every node
yin setL(the left part of the tree), its distance toAmust differ from its distance tonby the same number (lets call itdlwhich must be equall+x). If this is not the case, there is no solution for this particular edge. Same goes for nodes inR, withdrandr+xrespectively.If the above holds, then you have three equations:
l + x = dlr + x = drr+l = dist(A,B)Three equations, three numbers. If this has a solution then you have found the right edge.
At worst you need to iterate the above for every edge, but I think it can be optimized – the distance check on
LandRmight exclude one of the parts of the tree from further search. It might also be possible to somehow get the number of nodes without even constructing the tree.