tut3_ppt

# tut3_ppt - COMP271H COMP271H Tutorial 3 A “special”...

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

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

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/2-1 P’ = P-1 >= 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 sub-tree 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 (n-size[u] <= n/2) then For each v adj(u) do • If (size[v] > n/2) then break the inner for-loop • Otherwise, all test cases are passed, report u as that Otherwise, “special” vertex “special” Running time analysis Running T = (V,E), where |E| = |V|-1 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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern