Skip to content

Commit d93492b

Browse files
authored
Add Lowest Common Ancestor of two vertices in a tree (TheAlgorithms#2380)
1 parent 792b945 commit d93492b

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

DataStructures/Trees/LCA.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package Trees;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class LCA {
7+
private static Scanner scanner = new Scanner(System.in);
8+
public static void main(String[] args){
9+
10+
//The adjacency list representation of a tree:
11+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
12+
13+
//v is the number of vertices and e is the number of edges
14+
int v = scanner.nextInt(), e = v-1;
15+
16+
for(int i=0;i<v;i++){
17+
adj.add(new ArrayList<Integer>());
18+
}
19+
20+
//Storing the given tree as an adjacency list
21+
int to, from;
22+
for(int i=0;i<e;i++){
23+
24+
to = scanner.nextInt();
25+
from = scanner.nextInt();
26+
27+
adj.get(to).add(from);
28+
adj.get(from).add(to);
29+
}
30+
31+
//parent[v1] gives parent of a vertex v1
32+
int[] parent = new int[v];
33+
34+
//depth[v1] gives depth of vertex v1 with respect to the root
35+
int[] depth = new int[v];
36+
37+
//Assuming the tree to be rooted at 0, hence calculating parent and depth of every vertex
38+
dfs(adj,0,-1,parent,depth);
39+
40+
//Inputting the two vertices whose LCA is to be calculated
41+
int v1 = scanner.nextInt(), v2 = scanner.nextInt();
42+
43+
//Outputting the LCA
44+
System.out.println(getLCA(v1,v2,depth,parent));
45+
46+
}
47+
48+
/**
49+
* Depth first search to calculate parent and depth of every vertex
50+
* @param adj The adjacency list representation of the tree
51+
* @param s The source vertex
52+
* @param p Parent of source
53+
* @param parent An array to store parents of all vertices
54+
* @param depth An array to store depth of all vertices
55+
*/
56+
private static void dfs(ArrayList<ArrayList<Integer>> adj, int s, int p, int[] parent, int[] depth){
57+
for(int adjacent: adj.get(s)){
58+
if(adjacent!=p){
59+
parent[adjacent] = s;
60+
depth[adjacent] = 1 + depth[s];
61+
dfs(adj,adjacent,s,parent,depth);
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Method to calculate Lowest Common Ancestor
68+
* @param v1 The first vertex
69+
* @param v2 The second vertex
70+
* @param depth An array with depths of all vertices
71+
* @param parent An array with parents of all vertices
72+
* @return Returns a vertex that is LCA of v1 and v2
73+
*/
74+
private static int getLCA(int v1, int v2, int[] depth, int[] parent){
75+
if(depth[v1] < depth[v2]){
76+
int temp = v1;
77+
v1 = v2;
78+
v2 = temp;
79+
}
80+
while(depth[v1]!=depth[v2]){
81+
v1 = parent[v1];
82+
}
83+
if(v1==v2) return v1;
84+
while(v1!=v2){
85+
v1 = parent[v1];
86+
v2 = parent[v2];
87+
}
88+
return v1;
89+
}
90+
}
91+
92+
/**
93+
* Input:
94+
* 10
95+
* 0 1
96+
* 0 2
97+
* 1 5
98+
* 5 6
99+
* 2 4
100+
* 2 3
101+
* 3 7
102+
* 7 9
103+
* 7 8
104+
* 9 4
105+
* Output:
106+
* 2
107+
*/

0 commit comments

Comments
 (0)