私は大学の課題に解決策を与えようとしています..接続されたツリー T=(V,E) が与えられます。すべてのエッジ e には、特定の正のコスト c..d(v,w) は、ノード v と w の間の距離です..そのようなツリーの中心を見つけるアルゴリズムの疑似コードを与えるように求められます (そのノード他のすべてのノードへの最大距離を最小化します)。
私の解決策は、まず、木の最初の 2 つの背の高い枝を見つけることです。次に、中心は、ルートから H/2 の距離にある背の高い枝になります (H は、2 つの背の高い枝の高さの差です)。 ..疑似コードは次のとおりです。
Algorithm solution(Node root, int height, List path)
root: the root of the tree
height : the height measured for every branch. Initially height=0
path : the path from the root to a leaf. Initially path={root}
Result : the center of the tree
if root==null than
return "error message"
endif
/*a list that will contain an element <h,path> for every
leaf of the tree. h is the distanze of the leaf from the root
and path is the path*/
List L = empty
if isLeaf(root) than
L = L union {<height,path>}
endif
foreach child c of root do
solution(c,height+d(root,c),path UNION {c})
endfor
/*for every leaf in the tree I have stored in L an element containing
the distance from the root and the relative path. Now I'm going to pick
the two most taller branches of the tree*/
Array array = sort(L)
<h1,path1> = array[0]//corresponding to the tallest branch
<h2,path2> = array[1]//corresponding to the next tallest branch
H = h1 - h2;
/*The center will be the node c in path1 with d(root,c)=H/2. If such a
node does not exist we can choose the node with te distance from the root
closer to H/2 */
int accumulator = 0
for each element a in path1 do
if d(root,a)>H/2 than
return MIN([d(root,a)-H/2],[H/2-d(root,a.parent)])
endif
end for
アルゴリズムを終了
これは正しい解決策ですか??代替のより効率的な解決策はありますか?? ありがとうございました...