From 2d8171b2b334f9712d3de162269607ce784d419d Mon Sep 17 00:00:00 2001 From: Nimrod Rak Date: Tue, 12 Oct 2021 10:18:41 +0300 Subject: [PATCH 1/2] added disjoint sets data structure --- .../disjoint_sets/disjoint_sets.rb | 37 +++++++++++++++++++ data_structures/disjoint_sets/node.rb | 20 ++++++++++ 2 files changed, 57 insertions(+) create mode 100644 data_structures/disjoint_sets/disjoint_sets.rb create mode 100644 data_structures/disjoint_sets/node.rb diff --git a/data_structures/disjoint_sets/disjoint_sets.rb b/data_structures/disjoint_sets/disjoint_sets.rb new file mode 100644 index 0000000..2d71185 --- /dev/null +++ b/data_structures/disjoint_sets/disjoint_sets.rb @@ -0,0 +1,37 @@ +require "./data_structures/disjoint_sets/node.rb" + +class DisjointSets + def make_set(d) + Node.new(d) + end + + def find_set(x) + raise ArgumentError unless x.class <= Node + x.parent=(find_set(x.parent)) unless x.parent == x + x.parent + end + + def union_set(x, y) + px = find_set(x) + py = find_set(y) + return if px == py + if px.rank > py.rank + py.parent = px + elsif py.rank > px.rank + px.parent = py + else + px.parent = py + py.rank += 1 + end + end +end + +ds = DisjointSets.new +one = ds.make_set(1) +two = ds.make_set(2) +three = ds.make_set(3) +ds.union_set(one, two) +puts ds.find_set(one) == ds.find_set(two) # should be true +ds.union_set(one, three) +puts ds.find_set(two) == ds.find_set(three) # should be true +puts one.rank + two.rank + three.rank == 1 # should be true \ No newline at end of file diff --git a/data_structures/disjoint_sets/node.rb b/data_structures/disjoint_sets/node.rb new file mode 100644 index 0000000..d1cdca5 --- /dev/null +++ b/data_structures/disjoint_sets/node.rb @@ -0,0 +1,20 @@ +class Node + attr_accessor :data, :parent, :rank + def initialize(data) + @data = data + @parent = self + @rank = 0 + end + def parent + @parent + end + def parent=(parent) + @parent = parent; + end + def rank + @rank + end + def rank=(rank) + @rank = rank + end +end \ No newline at end of file From 87db7cf633bd3d155370d5731db452e9fbaf53c1 Mon Sep 17 00:00:00 2001 From: Nimrod Rak Date: Tue, 12 Oct 2021 10:21:38 +0300 Subject: [PATCH 2/2] fixed importing issue --- .../disjoint_sets/disjoint_sets.rb | 21 ++++++++++++++++++- data_structures/disjoint_sets/node.rb | 20 ------------------ 2 files changed, 20 insertions(+), 21 deletions(-) delete mode 100644 data_structures/disjoint_sets/node.rb diff --git a/data_structures/disjoint_sets/disjoint_sets.rb b/data_structures/disjoint_sets/disjoint_sets.rb index 2d71185..d9624fe 100644 --- a/data_structures/disjoint_sets/disjoint_sets.rb +++ b/data_structures/disjoint_sets/disjoint_sets.rb @@ -1,4 +1,23 @@ -require "./data_structures/disjoint_sets/node.rb" +class Node + attr_accessor :data, :parent, :rank + def initialize(data) + @data = data + @parent = self + @rank = 0 + end + def parent + @parent + end + def parent=(parent) + @parent = parent; + end + def rank + @rank + end + def rank=(rank) + @rank = rank + end +end class DisjointSets def make_set(d) diff --git a/data_structures/disjoint_sets/node.rb b/data_structures/disjoint_sets/node.rb deleted file mode 100644 index d1cdca5..0000000 --- a/data_structures/disjoint_sets/node.rb +++ /dev/null @@ -1,20 +0,0 @@ -class Node - attr_accessor :data, :parent, :rank - def initialize(data) - @data = data - @parent = self - @rank = 0 - end - def parent - @parent - end - def parent=(parent) - @parent = parent; - end - def rank - @rank - end - def rank=(rank) - @rank = rank - end -end \ No newline at end of file