From 4487614cc8453c39735b2fc9be5b56b84544ef66 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 00:37:33 +0300 Subject: [PATCH 01/16] add Dijkstra alg --- lib/visual_graphs/algorithms.rb | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/visual_graphs/algorithms.rb diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb new file mode 100644 index 0000000..951661f --- /dev/null +++ b/lib/visual_graphs/algorithms.rb @@ -0,0 +1,53 @@ +require_relative 'weighted_graph' + +module VisualGraphs + #Algorihtms class for Visual Graph + class Algorithms + #Dijkstra alg + # return distance from spec vertex for others + def dijkstra(wh_gr, start) + + #infinity value + inf = 1000000000 + + #Check class of input value + wh_gr.is_a?(WeightedGraph) + + n = wh_gr.adjacency_list.length + d = Array.new(n,inf) + u = Array.new(n, false) + + d[start] = 0 + + n.times do + v = -1 + for j in [0..n] do + if not u[j] and (v == -1 or d[j] < d[v]) + v = j + end + end + + if d[v] == inf + break + end + + u[v] = true + + for g in wh_gr[v] do + to = g.first + len = g.last + + if d[v] + len < d[to] + d[to] = d[v] + len + end + end + end + + d + + end + + end +end + + From 5d346c75612b05c8c7eacd4c3a8f07665e978fbe Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 00:54:15 +0300 Subject: [PATCH 02/16] refactor Dijkstra --- lib/visual_graphs/algorithms.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 951661f..3c592c7 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -14,18 +14,18 @@ def dijkstra(wh_gr, start) wh_gr.is_a?(WeightedGraph) n = wh_gr.adjacency_list.length - d = Array.new(n,inf) + dist = Array.new(n,inf) u = Array.new(n, false) - d[start] = 0 + dist[start] = 0 n.times do v = -1 - for j in [0..n] do + [0..n].each { |j| if not u[j] and (v == -1 or d[j] < d[v]) v = j end - end + } if d[v] == inf break @@ -33,17 +33,17 @@ def dijkstra(wh_gr, start) u[v] = true - for g in wh_gr[v] do + wh_gr[v].each { |g| to = g.first len = g.last - if d[v] + len < d[to] - d[to] = d[v] + len + if dist[v] + len < dist[to] + dist[to] = dist[v] + len end - end + } end - d + dist end From 954b10cb0482c2beca9f9a6806551ef453b0d1c2 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:03:07 +0300 Subject: [PATCH 03/16] fix Dijkstra + try to add test --- lib/visual_graphs/algorithms.rb | 1 + test/algorithms_test.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/algorithms_test.rb diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 3c592c7..2826def 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -5,6 +5,7 @@ module VisualGraphs class Algorithms #Dijkstra alg # return distance from spec vertex for others + # start vertex nums from 0 def dijkstra(wh_gr, start) #infinity value diff --git a/test/algorithms_test.rb b/test/algorithms_test.rb new file mode 100644 index 0000000..3db212a --- /dev/null +++ b/test/algorithms_test.rb @@ -0,0 +1,18 @@ +require 'minitest/autorun' +require './lib/visual_graphs/algorithms' +require './lib/visual_graphs/weighted_graph' + +module VisualGraphs + class AlgorithmsTest < Minitest::Test + def setup + @filepath = 'test/resources/test_weighted_graph.json' + end + + + def basic_test + graph = WeightedGraph.load_from_json(@filepath) + dist = Algorithms.dijkstra(graph, 0) + assert_equal [0, 3, 5], dist, 'wrong dist' + end + end +end \ No newline at end of file From 8f2e7e07b65a595ae1c873ea424f38a35740d9f8 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:09:38 +0300 Subject: [PATCH 04/16] another fix Dijkstra --- lib/visual_graphs/algorithms.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 2826def..52a8a0c 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -34,8 +34,8 @@ def dijkstra(wh_gr, start) u[v] = true - wh_gr[v].each { |g| - to = g.first + wh_gr[v].each_with_index { |g, index| + to = index len = g.last if dist[v] + len < dist[to] From 3a14a2f7d61521ca41388e41a07ad40006ff74a2 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:31:51 +0300 Subject: [PATCH 05/16] Try to improve codeclimate --- lib/visual_graphs/algorithms.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 52a8a0c..31c18b4 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -10,28 +10,23 @@ def dijkstra(wh_gr, start) #infinity value inf = 1000000000 - #Check class of input value wh_gr.is_a?(WeightedGraph) - n = wh_gr.adjacency_list.length - dist = Array.new(n,inf) - u = Array.new(n, false) + number_of_element = wh_gr.adjacency_list.length + dist = Array.new(number_of_element,inf) + u = Array.new(number_of_element, false) dist[start] = 0 - n.times do + number_of_element.times do v = -1 - [0..n].each { |j| + [0..number_of_element].each { |j| if not u[j] and (v == -1 or d[j] < d[v]) v = j end } - if d[v] == inf - break - end - u[v] = true wh_gr[v].each_with_index { |g, index| @@ -43,7 +38,6 @@ def dijkstra(wh_gr, start) end } end - dist end From 40947360f8f3eb30ac7098dcc4f04236e4c359e4 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:42:57 +0300 Subject: [PATCH 06/16] Add spec error and check params method --- lib/visual_graphs.rb | 3 +++ lib/visual_graphs/algorithms.rb | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) 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 index 31c18b4..17f28ca 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -6,12 +6,24 @@ class Algorithms #Dijkstra alg # return distance from spec vertex for others # start vertex nums from 0 + # + def check_graph_for_dijkstra(wh_gr, start) + + #Check class of input value + unless wh_gr.is_a?(WeightedGraph) + raise WrongParamsForAlgorithm + end + + if start < 0 or start > wh_gr.length + raise WrongParamsForAlgorithm + end + end + def dijkstra(wh_gr, start) #infinity value inf = 1000000000 - #Check class of input value - wh_gr.is_a?(WeightedGraph) + check_graph_for_dijkstra(wh_gr, start) number_of_element = wh_gr.adjacency_list.length dist = Array.new(number_of_element,inf) From 1813c8cbefc07fc0248126ed000b75a8fc0fafc3 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:45:06 +0300 Subject: [PATCH 07/16] Fix check method for Dijkstra --- lib/visual_graphs/algorithms.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 17f28ca..8ed73ba 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -17,6 +17,12 @@ def check_graph_for_dijkstra(wh_gr, start) if start < 0 or start > wh_gr.length raise WrongParamsForAlgorithm end + wh_gr[v].each do |t| + if t.last <= 0 + raise WrongParamsForAlgorithm + end + end + end def dijkstra(wh_gr, start) From 3adc0d086943474418b3896ce8610d1f32d3fff2 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:49:42 +0300 Subject: [PATCH 08/16] New check method --- lib/visual_graphs/algorithms.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 8ed73ba..3233fa8 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -17,12 +17,20 @@ def check_graph_for_dijkstra(wh_gr, start) if start < 0 or start > wh_gr.length raise WrongParamsForAlgorithm end + + unless all_weight_positive(wh_gr) + raise WrongParamsForAlgorithm + end + + end + + def all_weight_positive(wh_gr) wh_gr[v].each do |t| if t.last <= 0 - raise WrongParamsForAlgorithm + return false end end - + true end def dijkstra(wh_gr, start) From 76ede0b7563e798ed9b65c30e07e0ac991d9a66c Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:51:05 +0300 Subject: [PATCH 09/16] improve cognitive Complexity --- lib/visual_graphs/algorithms.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 3233fa8..b0567a9 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -10,7 +10,7 @@ class Algorithms def check_graph_for_dijkstra(wh_gr, start) #Check class of input value - unless wh_gr.is_a?(WeightedGraph) + unless wh_gr.is_a?(WeightedGraph) or all_weight_positive(wh_gr) raise WrongParamsForAlgorithm end @@ -18,10 +18,6 @@ def check_graph_for_dijkstra(wh_gr, start) raise WrongParamsForAlgorithm end - unless all_weight_positive(wh_gr) - raise WrongParamsForAlgorithm - end - end def all_weight_positive(wh_gr) From eb842c796a75f6af92059bd23afd7454f0816eea Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:52:30 +0300 Subject: [PATCH 10/16] improve cognitive Complexity in Dijkstra --- lib/visual_graphs/algorithms.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index b0567a9..058ccfc 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -52,11 +52,9 @@ def dijkstra(wh_gr, start) u[v] = true wh_gr[v].each_with_index { |g, index| - to = index - len = g.last - if dist[v] + len < dist[to] - dist[to] = dist[v] + len + if dist[v] + g.last < dist[index] + dist[to] = dist[v] + g.last end } end From 5f4bc75868d9b1b07c8b43c0fd907d4181053d93 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 01:57:42 +0300 Subject: [PATCH 11/16] fix dijkstra + codeclimate improve --- lib/visual_graphs/algorithms.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 058ccfc..917526e 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -3,10 +3,13 @@ module VisualGraphs #Algorihtms class for Visual Graph class Algorithms - #Dijkstra alg - # return distance from spec vertex for others - # start vertex nums from 0 - # + + def init + #infinity value + @inf = 1000000000 + end + + def check_graph_for_dijkstra(wh_gr, start) #Check class of input value @@ -29,21 +32,22 @@ def all_weight_positive(wh_gr) true end + #Dijkstra alg + # return distance from spec vertex for others + # start vertex nums from 0 def dijkstra(wh_gr, start) - #infinity value - inf = 1000000000 check_graph_for_dijkstra(wh_gr, start) number_of_element = wh_gr.adjacency_list.length - dist = Array.new(number_of_element,inf) + dist = Array.new(number_of_element, @inf) u = Array.new(number_of_element, false) dist[start] = 0 number_of_element.times do v = -1 - [0..number_of_element].each { |j| + [0..number_of_element-1].each { |j| if not u[j] and (v == -1 or d[j] < d[v]) v = j end From 3845f028fd23444b2d5eaaeffbabfaa081f00b6b Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 02:06:40 +0300 Subject: [PATCH 12/16] find problem --- lib/visual_graphs/algorithms.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 917526e..2d2d09e 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -41,21 +41,23 @@ def dijkstra(wh_gr, start) number_of_element = wh_gr.adjacency_list.length dist = Array.new(number_of_element, @inf) - u = Array.new(number_of_element, false) + flags = Array.new(number_of_element, false) dist[start] = 0 number_of_element.times do v = -1 [0..number_of_element-1].each { |j| - if not u[j] and (v == -1 or d[j] < d[v]) + if not flags[j] and (v == -1 or d[j] < d[v]) v = j end } - u[v] = true + flags[v] = true - wh_gr[v].each_with_index { |g, index| + #problem with no 0,1,2... graph + #!Need to fix logic + wh_gr.to_a[v].each_with_index { |g, index| if dist[v] + g.last < dist[index] dist[to] = dist[v] + g.last From 3f835c4f69576e45a7c78b64b75bf5cd68b1b146 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 02:14:19 +0300 Subject: [PATCH 13/16] improve dijkstra --- lib/visual_graphs/algorithms.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 2d2d09e..e699dcb 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -46,12 +46,7 @@ def dijkstra(wh_gr, start) dist[start] = 0 number_of_element.times do - v = -1 - [0..number_of_element-1].each { |j| - if not flags[j] and (v == -1 or d[j] < d[v]) - v = j - end - } + v = find_uncheck_min(number_of_element, flags, dist) flags[v] = true @@ -68,6 +63,16 @@ def dijkstra(wh_gr, start) end + def find_uncheck_min(number_of_element, flags, dist) + v = -1 + [0..number_of_element-1].each { |j| + if not flags[j] and (v == -1 or dist[j] < dist[v]) + v = j + end + } + v + end + end end From 9dc769e67bc1230b027a2a33d884afa59dc01f96 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 09:52:40 +0300 Subject: [PATCH 14/16] fix dijkstra's alg --- lib/visual_graphs/algorithms.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index e699dcb..3036d3d 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -54,8 +54,8 @@ def dijkstra(wh_gr, start) #!Need to fix logic wh_gr.to_a[v].each_with_index { |g, index| - if dist[v] + g.last < dist[index] - dist[to] = dist[v] + g.last + if dist[v] + g[1][1] < dist[g[1][0]] + dist[g[1][0]] = dist[v] + g[1][1] end } end From a6775412726406e9dcf76b6c015b63e8aabcf1fc Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 11:34:42 +0300 Subject: [PATCH 15/16] fix dijkstra's alg --- lib/visual_graphs/algorithms.rb | 38 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/visual_graphs/algorithms.rb b/lib/visual_graphs/algorithms.rb index 3036d3d..fac7242 100644 --- a/lib/visual_graphs/algorithms.rb +++ b/lib/visual_graphs/algorithms.rb @@ -4,9 +4,10 @@ module VisualGraphs #Algorihtms class for Visual Graph class Algorithms + INF = 1000000000 + def init #infinity value - @inf = 1000000000 end @@ -17,45 +18,44 @@ def check_graph_for_dijkstra(wh_gr, start) raise WrongParamsForAlgorithm end - if start < 0 or start > wh_gr.length + unless wh_gr.adjacency_list.has_key?(start) raise WrongParamsForAlgorithm end end def all_weight_positive(wh_gr) - wh_gr[v].each do |t| - if t.last <= 0 - return false + 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 - end + } true end #Dijkstra alg # return distance from spec vertex for others - # start vertex nums from 0 def dijkstra(wh_gr, start) check_graph_for_dijkstra(wh_gr, start) + #Convert to hash number_of_element = wh_gr.adjacency_list.length - dist = Array.new(number_of_element, @inf) - flags = Array.new(number_of_element, false) + dist = Hash.new(INF) + flags = Hash.new(false) dist[start] = 0 number_of_element.times do - v = find_uncheck_min(number_of_element, flags, dist) + v = find_uncheck_min(wh_gr, flags, dist) flags[v] = true - #problem with no 0,1,2... graph - #!Need to fix logic - wh_gr.to_a[v].each_with_index { |g, index| - - if dist[v] + g[1][1] < dist[g[1][0]] - dist[g[1][0]] = dist[v] + g[1][1] + wh_gr.adjacency_list[v].each{ |g| + if dist[v] + g[1] < dist[g[0]] + dist[g[0]] = dist[v] + g[1] end } end @@ -63,9 +63,9 @@ def dijkstra(wh_gr, start) end - def find_uncheck_min(number_of_element, flags, dist) - v = -1 - [0..number_of_element-1].each { |j| + 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 From fc4eb277768ae4e60092cdfc70875d90547d7945 Mon Sep 17 00:00:00 2001 From: Geluos Date: Tue, 28 Dec 2021 11:44:25 +0300 Subject: [PATCH 16/16] improve Dijkstra tests --- test/algorithms_test.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test/algorithms_test.rb b/test/algorithms_test.rb index 3db212a..30814ef 100644 --- a/test/algorithms_test.rb +++ b/test/algorithms_test.rb @@ -5,14 +5,27 @@ module VisualGraphs class AlgorithmsTest < Minitest::Test def setup - @filepath = 'test/resources/test_weighted_graph.json' + @filepathToWeighted = 'test/resources/test_weighted_graph.json' + @filepath = 'test/resources/test_data.json' end - def basic_test - graph = WeightedGraph.load_from_json(@filepath) - dist = Algorithms.dijkstra(graph, 0) + 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