Created
February 17, 2013 16:00
-
-
Save kapkaev/4971977 to your computer and use it in GitHub Desktop.
QuickFind. The Algoritms introduced in Coursera courses Algorithms, Part I
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuickFind | |
def initialize(n) | |
@ids = [] | |
0.upto(n-1) {|i| @ids[i] = i} | |
end | |
def connected?(id1,id2) | |
@ids[id1] == @ids[id2] | |
end | |
def union(id1,id2) | |
id_1, id_2 = @ids[id1], @ids[id2] | |
@ids.map! {|i| (i == id_1) ? id_2 : i } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment