Skip to content

Instantly share code, notes, and snippets.

@t0dd
Last active December 21, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0dd/6331095 to your computer and use it in GitHub Desktop.
Save t0dd/6331095 to your computer and use it in GitHub Desktop.
My Ruby implementation of the Quick-Find/ Union algorithm
class QuickFindUnion
def initialize(n)
@id = Array.new(n) {|i| i}
end
def connected?(p, q)
puts @id[p] == @id[q]
end
def union(p, q)
@p_id = @id[p]
@q_id = @id[q]
i = 0
while i < @id.length do
if @id[i] == @p_id
@id[i] = @q_id
end
i += 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment