Union Find Set

1. Introduction

2. Methods

2.1 find()

The average time complexity of compressed find is O(1).

public int find(int x) {
	
	if (parent[x] == x) return x;
	
	parent[x] = find(x);
	
	return parent[x];
}

2.2 union()

public void union (int x, int y) {
	
	int px = find(x), py = find(y);

	if (px != py) parent[px] = py;
}

   Reprint policy


《Union Find Set》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC