Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const smallest = new Array(13);
- const makeSet = (i) => {
- smallest[i] = i;
- };
- const find = (i) => {
- return smallest[i];
- };
- const union = (i, j) => {
- const i_id = find(i);
- const j_id = find(j);
- if (i_id === j_id) {
- return
- }
- const minimal = Math.min(i_id, j_id);
- for (let k = 1; k <= smallest.length; k++) {
- if (smallest[k] === i_id || smallest[k] === j_id) {
- smallest[k] = minimal
- }
- }
- };
- for (let i = 1; i <= 12; i++) {
- makeSet(i)
- }
- union(2, 10)
- union(7, 5)
- union(6, 1)
- union(3, 4)
- union(5, 11)
- union(7, 8)
- union(7, 3)
- union(12, 2)
- union(9, 6)
- const print = (i) => console.log(find(i));
- print(6)
- print(3)
- print(11)
- print(9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement