ta3sol - color[u] = gray; // u is discovered for each v in...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This question is referring to a problem of tree centroid decomposition. We say that a vertex v c is a centroid of a n -vertex tree T = ( V, E ) if we remove v c generates no subtrees of size more than n/ 2. The algorithm for solving this problem is described as follows. Assume that the input tree is rooted. If not, we can generate such a rooted tree by DFS. Then, we compute and store the size of each subtree rooted at vertex u , in size[ u ]. And a vertex u is a centroid if and only if (a) for each child v i of u , size[ v i ] n/ 2, and (b) n - size [u] n/ 2. Therefore, we can ±nd a centroid by checking the size of each subtree and see whether it satis±es the conditions above. If so, we report that vertex is a tree centroid. Here is the pseudo code of the algorithm. Find_Tree_Centroid(T,root) { for each u in V do // Initialization color[u] = white; Compute_Tree_Size(root); // Compute and store the size of . .. // each subtree rooted at u Check_Centroid_Conditions(root); // Check the conditions } Compute_Tree_Size(u) { count = 1; // Initialize count = 1 for each . .. // subtree rooted at u
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: color[u] = gray; // u is discovered for each v in Children(u) do { if (color[v] == white) { // Visit undiscovered vertex subcount = Compute_Tree_Size(v); count = count + subcount; // Accumulate the count for . .. // each subtree of u } } size[u] = count; // Store size of subtree rooted at u return count; // Return the size of subtree rooted at u } Check_Centroid_Conditions(u) { for each u in V do { count = 0; if (n - size[u] <= n/2) for each v in Children(u) do if (size[v] > n/2) Break out of the inner for loop. output "u is a centroid"; } } } Running time analysis: Initialization takes O ( | V | ) time. The Compute Tree Size () procedure takes O ( | V | ) time to compute the size of each subtree rooted at vertex u . The check in the procedure Check Centroid Conditions takes O ( degree ( u )) time for each vertex u , and sums to O ( | V | ) time for the whole checking process. Thus, the algorithms runs in O ( | V | ) time which is linear. 1...
View Full Document

Ask a homework question - tutors are online