QB2 - Graph - Solution

# QB2 - Graph - Solution - COMP 271 Design and Analysis of...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 271 Design and Analysis of Algorithms 2004 Fall Semester Solutions to Question Bank Number 2 (Selected Problems) 1 Basic Graph Problems 1. Suppose that G is not connected. Then it is possible to partition the vertex set of G as ( U,V- U ) such that there is no edge from any vertex in U to any vertex in V- U . Then G contains a complete bipartite graph on U and V- U as a spanning subgraph. It is easy to see that any complete bipartite subgraph is connected. If any spanning subgraph of a graph is connected, then the graph is connected. 2. 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 find a centroid by checking the size of each subtree and see whether it satisfies 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 color[u] = gray; // u is discovered for each v in Adj(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 } 1 Check_Centroid_Conditions(u) { for each u in V do { count = 0; if (n - size[u] <= n/2) for each v in Adj(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. 2 Breadth First Search and Depth First Search 1. See figure. d c f b e g a 2. Consider the following directed graph: MT115 MT118 MT117 If we visit the vertices from left to right, each vertex will be its own depth-first tree....
View Full Document

## This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.

### Page1 / 8

QB2 - Graph - Solution - COMP 271 Design and Analysis of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online