Skip to content

added kruskal algorithm for matrix based graph #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/visual_graphs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "visual_graphs/version"
require_relative "visual_graphs/graph"
require_relative 'visual_graphs/algorithms/Kruskal'
require_relative 'visual_graphs/adjacency_matrix_graph'

module VisualGraphs
Expand Down
73 changes: 73 additions & 0 deletions lib/visual_graphs/algorithms/Kruskal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'matrix'

class Kruskal_Helper
attr_reader :parent, :result

def initialize(size)
@parent = Array.new(size) {|i| i}
@result = Array.new(size) { |i| Array.new(size) { |j| i == j ? 0 : -1} } # временная замена, т.к. нет нужного класса
end


def find(i)
while i != @parent[i]
i = @parent[i]
end
return i
end

def union(i, j, w)
a = find(i)
b = find(j)
@parent[a] = b
addEdge(i,j,w)
end

def addEdge(first, second, weight)
@result[first][second] = weight
@result[second][first] = weight
end

def result
return @result
end
end

def kruskal(matrix)
exists = lambda do |weight|
weight > 0
end

# следующие три строки честно взяты из интернета, я их не понимаю
n_bytes = [42].pack('i').size
n_bits = n_bytes * 16
max_integer = 2 ** (n_bits - 2) - 1

sz = matrix[0].size
helper = Kruskal_Helper.new sz

(0...matrix.size - 1).each {
min = max_integer
a = -1
b = -1

(0...sz).each do |i|
(0...sz).each do |j|
way = matrix[i][j]
if exists.call(way)
if helper.find(i) != helper.find(j) and way < min
min = way
a = i
b = j
end
end
end
end

helper.union a, b, min
}

#p matrix
#p helper.result
helper.result
end
58 changes: 58 additions & 0 deletions test/algorithms/kruskal_amb_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'minitest/autorun'
require "visual_graphs"
#Test for Kruskal's algorithm for adjacency matrix based graph
class Kruskal_ambTest < Minitest::Test
include VisualGraphs
def setup
# Do nothing
end

def teardown
# Do nothing
end

def test1
matrix =
[
[0, 7, 8, -1, -1,-1],
[7, 0, 11, 2, -1, -1],
[8, 11, 0, 6, 9, -1],
[-1, 2, 6, 0, 11, 9],
[-1, -1, 9, 11, 0, 10],
[-1, -1, -1, 9, 10, 0]
]
expected =
[
[0,7,-1,-1,-1,-1],
[7,0,-1,2,-1,-1],
[-1,-1,0,6,9,-1],
[-1,2,6,0,-1,9],
[-1,-1,9,-1,0,-1],
[-1,-1,-1,9,-1,0]
]
result = kruskal matrix
assert_equal result, expected
end

def test2
matrix =
[
[0,2,-1,6,-1],
[2,0,3,8,5],
[-1,3,0,-1,7],
[6,8,-1,0,9],
[-1,5,7,9,0]
]
expected =
[
[0,2,-1,6,-1],
[2,0,3,-1,5],
[-1,3,0,-1,-1],
[6,-1,-1,0,-1],
[-1,5,-1,-1,0],
]
result = kruskal matrix
assert_equal result, expected
end

end