I have a weighted tree which looks like (weights are in brackets)
A1
/ \
B1(3) B2(2)
/ \ / \
C1(1) C2(3) C3(4)
/ \ / \ / \
D1(8) D2(7) D3(2) D4(5)
......
So, each node has two children. And each node shares a child with a neighbour node. A depth of the tree can be very high.
3 + 1 + 8 = 12
3 + 1 + 7 = 11
3 + 3 + 7 = 13 ... and so on
What is the best way to find the shortest path? As a result I need not a sum of weights but a full path (lets say A1-B2-C3-D3).
I will be more than happy if you could reference me to the right algorithm.. Or provide java/pseudo code solution.
Thank you!
I am looking for a full path from top to bottom
This may be a natural Dynamic Programming (DP) problem due to the child sharing property. I suggest using a bottom-up DP algorithm to solve this problem.
The state transformation equation is listed as below:
SP(n) = min {for every c of n's children | SP(c) + weight(c)}
As for implementation, we scan bottom-up from leaves to compute the SP(n) until we reach the root. And the time cost is O(n) since we compute it in one run.
You may want to look at Alpha-beta-pruning . This algorithm basically removes part of the search tree as soon as they are known to be obsolete, ie a shorter path to the same position is already known.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.