diff --git a/lib/visual_graphs.rb b/lib/visual_graphs.rb index b3c8f69..dfdda22 100644 --- a/lib/visual_graphs.rb +++ b/lib/visual_graphs.rb @@ -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 = '') diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb new file mode 100644 index 0000000..fac7242 --- /dev/null +++ b/lib/visual_graphs/algorithms.rb @@ -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 + + diff --git a/test/algorithms_test.rb b/test/algorithms_test.rb new file mode 100644 index 0000000..30814ef --- /dev/null +++ b/test/algorithms_test.rb @@ -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 \ No newline at end of file