This preview shows page 1. Sign up to view the full content.
Unformatted text preview: COMP271H COMP271H
Tutorial 3 A “special” vertex on a tree Properties of a tree Properties Notation: T = (V,E) Number of vertices = V Number of edges = V  1 E = O(V) O(V) Running time of DFS on a tree T Question Question How to prove it exists? How
u v Transform v u P > n/2 Q < n/21 P’ = P1 >= n/2 Q’ = Q+1 < n/2 Is the solution unique? Is No. Consider when n=4, there are two No. possible choices…. possible Algorithm’s input Algorithm’s A tree T = (V,E) A tree should has a root, how about the tree root is not specified? root Use DFS on T to find a rooted tree Use BFS on T to find a rooted tree Algorithm’s output Algorithm’s By definition: A vertex u, satisfying the following properties: Meaning of size[u]? Meaning Given a vertex u, size[u] is number of Given nodes (including u) inside this subtree rooted at u Example:
u size[u] = 5 How to compute size[u] Using the idea of DFS Recall inner loop of DFS(v) For each vertex w adj(v) do
• If ( color[w] == white ) then DFS(w) // call it recursively Now we modify the recursive call to help us Now calculate the size of each subtree calculate Algorithm: ComputeTreeSize Algorithm: ComputeTreeSize(u) Count = 1; Color[u] = gray; Count For each v adj(u) do
• tmpCount = ComputeTreeSize(v) • Count = Count + tmpCount Size[u] = Count ; // set count here… Return Count; // for recursive call return value How to find that vertex? How Scan each vertices For each u in vertex set V do
• Count = 0 • If (nsize[u] <= n/2) then For each v adj(u) do • If (size[v] > n/2) then break the inner forloop • Otherwise, all test cases are passed, report u as that Otherwise, “special” vertex “special” Running time analysis Running T = (V,E), where E = V1 Initialization  set all vertice’s color white Takes O(V) time Takes O(V+E) = O(V) Takes Takes sum(O(degree(v)) = O(E) = O(V) Run ComputeTreeSize ~ DFS… Check condition Total: O(V) = O(n) Name of this “special” vertex Name Given a tree T, the vertices that satisify the Given following two properties: following They are called centroid of T Further discussion Further We have proved the existence of We “centroid” vertex “centroid” Given a tree T, there must be at least ONE Given “centroid” vertex “centroid” How about the upper bound? Of course, the How loose bound is V loose Question: Can we formulae the tighter upper Question: bound using some parameters from the tree T (e.g. number of leaves….) (e.g. ...
View
Full
Document
 Spring '10
 may
 English

Click to edit the document details