2

i am trying to give a solution to my university assignement..given a connected tree T=(V,E). every edge e has a specific positive cost c..d(v,w) is the distance between node v and w..I'm asked to give the pseudocode of an algorithm that finds the center of such a tree(the node that minimizes the maximum distance to every other node)..

My solution consists first of all in finding the first two taller branches of the tree..then the center will be in the taller branch in a distance of H/2 from the root(H is the difference between the heights of the two taller branches)..the pseudocode is:

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

end Algorithm

is this a correct solution??is there an alternative and more efficient one?? Thank you...

bece
  • 175
  • 1
  • 1
  • 10
  • One approach (not efficient) is to first build a 2D matrix of pair-wise distance between all the nodes. The work on the matrix to get the MiniMax sum row or column. – Abhishek Bansal Nov 26 '13 at 16:49
  • @Abhishek Yes,I knew that but I'm looking for a more efficient solution..I need an algorithm that is better than O(n^2) – bece Nov 26 '13 at 16:52
  • and what represents the `n` in *O(n^2)*? – artur grzesiak Nov 26 '13 at 20:38
  • @artur n is the number of vertices – bece Nov 26 '13 at 22:28
  • @albinf thanks. in every tree there is exactly `n-1` edges so my question was kind of stupid :-) – artur grzesiak Nov 26 '13 at 22:35
  • @albinf what do you exactly mean by `two taller branches` and by `the root` of the tree? – artur grzesiak Nov 26 '13 at 22:39
  • 1
    Read this thread: http://stackoverflow.com/questions/5055964/centre-node-in-a-tree In particular the comments on this answer: http://stackoverflow.com/a/5056755/779314 – bdean20 Nov 27 '13 at 03:13
  • @artur the two taller branches are the branches with the highest height..the root instead is the node in wich the tree is rooted – bece Nov 27 '13 at 09:40
  • @bdean20 It's an intersting point of view the one showed in that comment..i'll study it hoping that it can be helpful for me.. – bece Nov 27 '13 at 09:42
  • @albinf tree is just a set vertices and edges -- from mathematical perspective there is no special vertex called 'root'. Do you mean by 'root' the vertex to which you have direct reference in pseudo-code? Do you measure the height of the branches from the 'root'? – artur grzesiak Nov 27 '13 at 09:51
  • @arturgrzesiak exactly..this is what i mean.. – bece Nov 27 '13 at 10:31

1 Answers1

2

Your idea is correct. You can arbitrarily pick any vertex to be a root of the tree, and then traverse the tree in 'post-order'. Since the weights are always positive, you can always pick the two longest 'branches' and update the answer in O(1) for each node. Just remember that you are looking for the 'global' longest path (i.e. diameter of the graph), rather than the 'local' longest paths that go through the roots of the subtrees.

You can find more information if you search for "(weighted) Jordan Center (in a tree)". The optimal algorithm is O(N) for trees, so asymptotically your solution is optimal since you only use a single DFS which is O(|V| + |E|) == O(|V|) for a tree.

ile
  • 339
  • 1
  • 7