[ create a new paste ] login | about

Link: http://codepad.org/tJ5qPrgA    [ raw code | output | fork ]

kabhwan - Ruby, pasted on May 11:
=begin
분리 집합(DisjointSet) 을 구현한다

=end

class DisjointSet
  attr_reader :data
  attr_accessor :parent

  def initialize(data)
    @parent = nil

    @data = data
  end

  def union_set(set)
    root = find_set

    set.parent = root
  end

  def find_set
    root = self

    root = root.parent while root.parent

    root
  end
  
end

a = 1;b = 2;c = 3;d = 4
set1 = DisjointSet.new(1)
set2 = DisjointSet.new(2)
set3 = DisjointSet.new(3)
set4 = DisjointSet.new(4)

puts "Set1 == Set2 : #{set1.find_set == set2.find_set}"

set1.union_set(set3)
puts "Set1 == Set3 : #{set1.find_set == set3.find_set}"

set3.union_set(set4)
puts "Set3 == Set4 : #{set3.find_set == set4.find_set}"


Output:
1
2
3
Set1 == Set2 : false
Set1 == Set3 : true
Set3 == Set4 : true


Create a new paste based on this one


Comments: