Skip to content

Instantly share code, notes, and snippets.

@kapkaev
Created February 17, 2013 16:00
Show Gist options
  • Save kapkaev/4971977 to your computer and use it in GitHub Desktop.
Save kapkaev/4971977 to your computer and use it in GitHub Desktop.
QuickFind. The Algoritms introduced in Coursera courses Algorithms, Part I
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