dijkstra 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/dijkstra.rb +163 -0
- metadata +46 -0
data/lib/dijkstra.rb
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
class Dijkstra
|
2
|
+
|
3
|
+
#constructor of the class
|
4
|
+
def initialize(startpoint, endpoint, matrix_of_road)
|
5
|
+
|
6
|
+
#start node
|
7
|
+
@start = startpoint
|
8
|
+
|
9
|
+
#end node
|
10
|
+
@end = endpoint
|
11
|
+
|
12
|
+
@path = []
|
13
|
+
|
14
|
+
@PInfinit = 88
|
15
|
+
|
16
|
+
readAndInit( matrix_of_road )
|
17
|
+
|
18
|
+
#Dijkstra's algorithm in action and good luck
|
19
|
+
dijkstra()
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
# This method determines the minimum cost of the shortest path
|
24
|
+
def getCost()
|
25
|
+
@R[@end]
|
26
|
+
end
|
27
|
+
|
28
|
+
# get the shortest path
|
29
|
+
def getShortestPath()
|
30
|
+
ROAD(@end)
|
31
|
+
@path
|
32
|
+
end
|
33
|
+
|
34
|
+
def ROAD(node)
|
35
|
+
if @F[node] != 0
|
36
|
+
ROAD(@F[node])
|
37
|
+
end
|
38
|
+
|
39
|
+
@path.push(node)
|
40
|
+
end
|
41
|
+
|
42
|
+
def dijkstra()
|
43
|
+
|
44
|
+
start = @start
|
45
|
+
min = @PInfinit
|
46
|
+
posMin = @PInfinit
|
47
|
+
|
48
|
+
(1..@nodes-1).each do |i|
|
49
|
+
@R[i] = @road[start][i]
|
50
|
+
if i != start
|
51
|
+
if @R[i] < @PInfinit
|
52
|
+
@F[i] = start
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#for debug
|
58
|
+
#print @R
|
59
|
+
|
60
|
+
@S[start] = 1
|
61
|
+
|
62
|
+
#for debug
|
63
|
+
#print @S
|
64
|
+
|
65
|
+
(1..@nodes-2).each do |d|
|
66
|
+
|
67
|
+
min = @PInfinit
|
68
|
+
|
69
|
+
(1..@nodes-1).each do |i|
|
70
|
+
if @S[i] == 0
|
71
|
+
if @R[i] < min
|
72
|
+
min = @R[i]
|
73
|
+
posMin = i
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
@S[posMin] = 1
|
79
|
+
|
80
|
+
(1..@nodes-1).each do|j|
|
81
|
+
if @S[j] == 0
|
82
|
+
if @R[j] > @R[posMin] + @road[posMin][j]
|
83
|
+
@R[j] = @R[posMin] + @road[posMin][j]
|
84
|
+
@F[j] = posMin
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
def readAndInit( arr )
|
94
|
+
|
95
|
+
@nodes = arr[0][0]+1
|
96
|
+
|
97
|
+
n = arr.size()-1
|
98
|
+
|
99
|
+
@road = Array.new(@nodes) { Array.new(@nodes) }
|
100
|
+
@R = Array.new(@nodes)
|
101
|
+
@S = Array.new(@nodes)
|
102
|
+
@F = Array.new(@nodes)
|
103
|
+
|
104
|
+
(0..@nodes-1).each do |i|
|
105
|
+
@R[i] = 0
|
106
|
+
end
|
107
|
+
|
108
|
+
(0..@nodes-1).each do |i|
|
109
|
+
@S[i] = 0
|
110
|
+
end
|
111
|
+
|
112
|
+
(0..@nodes-1).each do |i|
|
113
|
+
@F[i] = 0
|
114
|
+
end
|
115
|
+
|
116
|
+
(0..@nodes-1).each do |i|
|
117
|
+
(0..@nodes-1).each do |j|
|
118
|
+
if i == j
|
119
|
+
@road[i][j] = 0
|
120
|
+
else
|
121
|
+
@road[i][j] = @PInfinit
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
(1..n).each do |i|
|
127
|
+
x = arr[i][0]
|
128
|
+
y = arr[i][1]
|
129
|
+
c = arr[i][2]
|
130
|
+
@road[x][y] = c
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def writeToFile(filename)
|
135
|
+
|
136
|
+
f = File.open(filename,"w")
|
137
|
+
out = "Cost -> " + @R[@end].to_s + "\nShortest Path -> " + @path.to_s
|
138
|
+
f.write(out)
|
139
|
+
|
140
|
+
f.close()
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
r = [[5],
|
145
|
+
[1,2,1],
|
146
|
+
[1,3,9],
|
147
|
+
[1,5,3],
|
148
|
+
[2,4,3],
|
149
|
+
[2,3,7],
|
150
|
+
[4,3,2],
|
151
|
+
[4,1,1],
|
152
|
+
[5,2,4],
|
153
|
+
[5,4,2]]
|
154
|
+
|
155
|
+
start_point = 1
|
156
|
+
end_point = 3
|
157
|
+
|
158
|
+
ob = Dijkstra.new(start_point, end_point, r)
|
159
|
+
|
160
|
+
print "Cost = ", ob.getCost(), "\n"
|
161
|
+
print "Shortest Path = ", ob.getShortestPath()
|
162
|
+
|
163
|
+
ob.writeToFile("shortestpath.out")
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dijkstra
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adrian Statescu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Dijkstra's Algorithm For Shortest Path Directed Graph
|
15
|
+
email: mergesortv@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/dijkstra.rb
|
21
|
+
homepage: http://github.com/thinkphp/dijkstra.gem
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.24
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Dijkstra!
|
46
|
+
test_files: []
|