Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Swift – Disjoint Set (Union-Find)

by fragi
February 21, 2024
in Algorithms
Share on FacebookShare on Twitter

Disjoint Set is an algorithm that can help us solve many graph problems. There are many variations/improvements on the base algorithm, but today we will implement the union find by rank and path compression.

Let’s first initialise the arrays:

Swift
1
2
3
4
5
6
7
8
9
10
11
public class UnionFind {
    var rank: [Int] = []
    var root: [Int] = []
    
    public init(size: Int) {
        for i in 0..<size {
            root[i] = Array(0..<size)
            rank[i] = Array(repeating: 1, count: size)
        }
    }
}

Next we will work on  the find method:

Swift
1
2
3
4
5
6
7
    public func find(_ x: Int) -> Int {
        if x == root[x] {
            return x
        }
        root[x] = find(root[x])
        return root[x]
    }

We have to make sure we update the root array, while we are searching.

And lastly we will implement the union me

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    public func union(_ x: Int, _ y: Int) {
        let rootX = find(root[x])
        let rootY = find(root[y])
        
        if rootX != rootY {
            if rank[rootX] < rank[rootY] {
                root[rootX] = rootY
                rank[rootY] += rank[rootX]
            }
             else {
                root[rootY] = rootX
                rank[rootX] += rank[rootY[
            }
        }
    }

In this method we have to make sure we update correctly the rank array.

https://github.com/Fragki/Algorithms/blob/main/swift-algo/Sources/swift-algo/swift_algo.swift

fragi

fragi

Related Posts

Blake2b Algorithm
Algorithms

Operators precedence

March 2, 2022

Operators precedence matters when we have many operators in one expression. The operator with the highest precedence will be evaluated...

Blake2b Algorithm
Algorithms

Bech32 Algorithm

March 1, 2022

Bech32 is a checksummed base32 format. It is being used in cryptocurrencies development (bitcoin, cardano,...) https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki, https://cips.cardano.org/cips/cip19/. Let's explore the...

Blake2b Algorithm
Algorithms

Blake2b Algorithm

February 28, 2022

Blake2 is a cryptographic hash function. https://www.blake2.net/ Let's now explore how to implement this algorithm in Swift. The first option...

Memoization
Algorithms

Memoization

February 15, 2022

Memoization is a technique to increase the time performance of an algorithm by using some extra memory. According to Wikipedia:...

Performance testing in Xcode
Algorithms

Performance testing in Xcode

February 15, 2022

The performance is most of the times very important. In this post we will explore we can do performance testing...

BIP39
Algorithms

BIP39

February 12, 2022

In this article we will talk about the BIP39 Algorithm that is being used in many crypto. wallets. So what...

Next Post
Heap

Heap

Multiple Equatable methods on different Packages - Bad practise

Unit tests in playground

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.