Presentation is loading. Please wait.

Presentation is loading. Please wait.

LOWEST COMMON ANCESTOR IN TREE CPSC 490 – FEBRUARY 24 TH – MICHAEL HILAND.

Similar presentations


Presentation on theme: "LOWEST COMMON ANCESTOR IN TREE CPSC 490 – FEBRUARY 24 TH – MICHAEL HILAND."— Presentation transcript:

1 LOWEST COMMON ANCESTOR IN TREE CPSC 490 – FEBRUARY 24 TH – MICHAEL HILAND

2 Input 8 9 Output Example

3 Input 8 9 Output 4 Example

4 Input 8 5 Output Example

5 Input 8 5 Output 2 Example

6 Input 8 2 Output Example

7 Input 8 2 Output 2 The lowest common ancestor problem was defined by Alfred Aho, John Hopcroft, and Jeffrey Ullman (1973)Alfred AhoJohn HopcroftJeffrey Ullman1973 Proposed several solutions: O(nlogn), O(n log log n)

8 ALGORITHM f(n) is the preprocessing time g(n) is the query time. Query(a,b) = c

9 ALGORITHM f(n) is the preprocessing time g(n) is the query time. Query(a,b) = c

10 sqrt(H) Sections 0 to sqrt(H) – 1 sqrt(H) to 2 * sqrt(H) – 1 sqrt(H) Sections

11 sqrt(H) parts 0 to sqrt(H) – 1 sqrt(H) to 2 * sqrt(H) – 1 sqrt(H) Sections Pre-Process Array P[1, MAXN]

12 sqrt(H) Sections Pre-Process Array- DFS P[1, MAXN] void dfs(int node, int T[MAXN], int N, int P[MAXN], int L[MAXN], int nr) { int k; if (L[node] < nr) P[node] = 1; else if(!(L[node] % nr)) P[node] = T[node]; else P[node] = P[T[node]]; for each son k of node dfs(k, T, N, P, L, nr); }

13 sqrt(H) Sections Pre-Process Array- DFS P[1, MAXN] int LCA(int T[MAXN], int P[MAXN], int L[MAXN], int x, int y) { while (P[x] != P[y]) if (L[x] > L[y]) x = P[x]; else y = P[y]; while (x != y) if (L[x] > L[y]) x = T[x]; else y = T[y]; return x; }

14

15 APPLICATIONS

16 Bender, Michael A.; Farach-Colton, Martín; Pemmasani, Giridhar; Skiena, Steven; Sumazin, Pavel (2005),Farach-Colton, MartínSkiena, Steven "Lowest common ancestors in trees and directed acyclic graphs" (PDF), Journal of Algorithms 57 (2): 75–94, doi:10.1016/j.jalgor.2005.08.001"Lowest common ancestors in trees and directed acyclic graphs"doi10.1016/j.jalgor.2005.08.001 Range minimum Query and Lowest Common Ancestor, danielp, TopCoder Yoshiya Miyata“Write a neat code to find LCA of two nodes ? and whats the complexity?”, Nov 29, 2012 Lowest common ancestor, Wikipedia, Feb2, 2016 On Finding Lowest Common Ancestors in Trees. V. Aho, J. E. Hopcroft, and J. D. UllmanDOI:10.1137/0205011 References


Download ppt "LOWEST COMMON ANCESTOR IN TREE CPSC 490 – FEBRUARY 24 TH – MICHAEL HILAND."

Similar presentations


Ads by Google