-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFS_Graphs.rb
97 lines (85 loc) · 1.88 KB
/
BFS_Graphs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require "benchmark/ips"
=begin
Return an array with the given graph in BFS order.
=end
def graph_BFS(hash_graph)
result= []
visited =[0]
queue = [0]
until queue.empty?
current_node = queue.shift
result << current_node
hash_graph[current_node].each do|value|
unless visited.include?(value)
visited << value
queue << value
end
end
end
result
end
def graph_BFS_with_keyword_args(graph: hash_graph)
result= []
visited =[0]
queue = [0]
until queue.empty?
current_node = queue.shift
result << current_node
graph[current_node].each do|value|
queue << value and visited << value unless visited.include? value
end
end
result
end
def model_solution(graph)
queue = [0]
result = []
visited = []
until queue.empty?
head = queue.shift
result << head
visited << head
not_visited = graph[head].reject { |node| visited.include? node }
visited += not_visited
queue += not_visited
end
result
end
hash_graph_1={
0 => [2],
1 => [4],
2 => [5, 0, 3],
3 => [2],
4 => [1, 5],
5 => [4, 2]
}
# => [0, 2, 5, 3, 4, 1]
hash_graph_6 = {
0=>[1, 2],
1=>[0, 2],
2=>[0, 1, 3, 4, 5],
3=>[2, 4],
4=>[3, 2],
5=>[2]
}
#res =[0,1,2,3,4,5]
#vis= [0,1,2,3,4,5]
#q=
# [0, 1, 2, 3, 4, 5]
hash_graph_7 = {
0=>[1, 2],
1=>[0, 3, 4],
2=>[0, 5, 6],
3=>[1],
4=>[1],
5=>[2],
6=>[2]
}
#[0, 1, 2, 3, 4, 5, 6]
# p graph_BFS(hash_graph_1)
Benchmark.ips do |x|
x.report("graph_BFS"){graph_BFS(hash_graph_7) }
x.report("graph_BFS_with_keyword_args"){graph_BFS_with_keyword_args(graph: hash_graph_7) }
x.report("model_solution"){model_solution(hash_graph_7) }
x.compare!
end