---
# System prepended metadata

title: Union Find

---

# Union Find 

We need to implement two functions if a problem can be solved by union find. 

- Find(x) - To find the representative node for x
- Union(x,y) - Join two groups, which is by joining their representatives

```
    def find(self, x):
        if (self.parent[x] == -1):
            return x
        return self.find(self.parent[x])
    
    def union(self, x, y):
        self.parent[self.find(x)] = self.find(y)
```