-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.rb
More file actions
171 lines (133 loc) · 3.27 KB
/
Copy pathnode.rb
File metadata and controls
171 lines (133 loc) · 3.27 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Class : Node
# Each node object contains detail of each node
class Node
@@obj_refs = Array.new
@@weight = 0
@@count = 0
def initialize(value)
@name = value
@adjacent_nodes = []
@weight = []
@@obj_refs << self
end
def read
self
end
# add edges
def add_edges(adjacent_node , weight)
@adjacent_nodes << adjacent_node
@weight << weight.to_i
end
# check if node exists or not
def self.exists(node_obj)
@@obj_refs.each do |obj|
name1 = obj.get_value
name2 = node_obj.get_value
temp = obj
if obj != node_obj
if name2 == name1
node_obj.delete()
return temp
end
end
end
return false
end
def get_value
@name
end
def self.all
@@obj_refs.each do |obj|
p obj
end
end
def self.count
@@obj_refs
end
def delete
@@obj_refs.delete_if{ |obj| obj == self }
end
# calculate distance between any given nodes
def self.distance(*nodes)
if !nodes.include? nil
#p nodes
node_stack = []
nodes = nodes.reverse
# p nodes
nodes.each do |source|
node_stack << Node.get_obeject(source)
end
current = node_stack.pop
terminal = node_stack.last
@@weight += self.calculate_weight(current,terminal)
#p "------self calling------"
nodes.pop
var1 , var2 = nodes.reverse
self.distance(var1 ,var2)
end
@@weight
end
# calculate weight between two nodes only
# and return weight
def self.calculate_weight(current,terminal)
current_adjacent_nodes = current.instance_variable_get("@adjacent_nodes")
current_nodes_weight = current.instance_variable_get("@weight")
if current_adjacent_nodes.include? terminal.get_value
# find index of terminal adjacent node
index = current_adjacent_nodes.index(terminal.get_value)
weight = current_nodes_weight.at(index)
return weight
else
puts "XXX NO ROAD current to terminal node XXX"
end
end
# return childrens of a object
def children
@adjacent_nodes
end
# get object of named value
def self.get_obeject(name)
#p "evoked...get object -> #{name}"
@@obj_refs.each do |node|
#p node.get_value
if name == node.get_value
return node
end
end
return nil
end
# find count of how many paths there exists
# for any given point X and Y
def self.find_paths(source,terminal)
p "----------#{source}---#{@@count}---------------"
root = Node.get_obeject(source)
p root
p "obect catched.."
#p root
if root == nil or source == terminal
p " path terinated..."
has_child = false
else
p "has child..."
child_stack = root.children
p child_stack
has_child = true
end
if source == terminal
p "source equals to terminal , incrementing..."
@@count += 1
end
if has_child
loop do
child = child_stack.pop
#p "loop for child #{child}----"
if child != nil
Node.find_paths(child , terminal)
else
break
end
end
end
"no of paths b/w #{source} to #{terminal} = #{@@count}"
end
end