有关树的算法问题
给定一棵树
(0,1)
(0,7)
(0,12)
(0,13)
(0,15)
(1,2)
(1,4)
(1,6)
(1,7)
(2,3)
(2,5)
(2,6)
(3,4)
(3,5)
(7,8)
(8,9)
(8,10)
(8,11)
(9,10)
(9,11)
(12,13)
(13,14)
(13,15)
0代表根节点,每一数组代表两节点间有无向边。请问,如何判断某节点是否是另一节点的祖先?谢谢
------解决方案--------------------考虑一下这些问题吧:
1, How do you represent a single Node in the Graph?
2, How do you represent a single edge in the Graph?
3, How do you build the Graph from your input?
If there is a path from Node A to Node B, then A is a ancestor of B.
------解决方案--------------------1。深度搜索
2。最小传递闭包算法
3。求出两点间的最短路径,若不为无穷大则成立