Skip to content

Dijkstra's algorithm #40

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 18 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
3 changes: 3 additions & 0 deletions lib/visual_graphs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class WrongParamsForWeightedGraphInit < StandardError; end
# error which occurs in case of accessing non-existent files
class FileDoesNotExist < StandardError; end

#error for wrong inner values in algorithm
class WrongParamsForAlgorithm; end

# errors connected with defining of adjacency matrix
class AdjacencyMatrixError < StandardError
def initialize(msg = '', exception_type = '')
Expand Down
79 changes: 79 additions & 0 deletions lib/visual_graphs/algorithms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require_relative 'weighted_graph'

module VisualGraphs
#Algorihtms class for Visual Graph
class Algorithms

INF = 1000000000

def init
#infinity value
end


def check_graph_for_dijkstra(wh_gr, start)

#Check class of input value
unless wh_gr.is_a?(WeightedGraph) or all_weight_positive(wh_gr)
raise WrongParamsForAlgorithm
end

unless wh_gr.adjacency_list.has_key?(start)
raise WrongParamsForAlgorithm
end

end

def all_weight_positive(wh_gr)
wh_gr.edges.each{ |t|
t.each do
if t.is_a? Array and t.length == 2 and t[1] <= 0
return false
end
end
}
true
end

#Dijkstra alg
# return distance from spec vertex for others
def dijkstra(wh_gr, start)

check_graph_for_dijkstra(wh_gr, start)

#Convert to hash
number_of_element = wh_gr.adjacency_list.length
dist = Hash.new(INF)
flags = Hash.new(false)

dist[start] = 0

number_of_element.times do
v = find_uncheck_min(wh_gr, flags, dist)

flags[v] = true

wh_gr.adjacency_list[v].each{ |g|
if dist[v] + g[1] < dist[g[0]]
dist[g[0]] = dist[v] + g[1]
end
}
end
dist

end

def find_uncheck_min(wh_gr, flags, dist)
v = "-1"
wh_gr.vertices.each { |j|
if not flags[j] and (v == -1 or dist[j] < dist[v])
v = j
end
}
v
end

end
end


31 changes: 31 additions & 0 deletions test/algorithms_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'minitest/autorun'
require './lib/visual_graphs/algorithms'
require './lib/visual_graphs/weighted_graph'

module VisualGraphs
class AlgorithmsTest < Minitest::Test
def setup
@filepathToWeighted = 'test/resources/test_weighted_graph.json'
@filepath = 'test/resources/test_data.json'
end


def basic_test_Dijkstra
graph = WeightedGraph.load_from_json(@filepathToWeighted)
dist = Algorithms.dijkstra(graph, "1")
assert_equal [0, 3, 5], dist, 'wrong dist'
end

def wrong_graph_Dijkstra
assert_raises WrongParamsForAlgorithm do
graph = Graph.load_from_json(@filepath)
Algorithms.dijkstra(graph, "1")
end

assert_raises WrongParamsForAlgorithm do
graph = WeightedGraph.load_from_json(@filepathToWeighted)
Algorithms.dijkstra(graph, "120")
end
end
end
end