-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdijkstra.py
167 lines (100 loc) · 3.86 KB
/
dijkstra.py
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
# Dijkstra's algorithm conceived by computer scientist Edsger Dijkstra in 1956 and published in 1959
# is a graph search algorithm that solves the single-source shortest path problem for a graph with
# non-negative edge path costs, producing a shortest path tree.
# O(N^3) Running Time complexity
class Dij:
#matrix of roads
road = [[]]
#infinity
infinit = 9999
#array of costs
D = []
#array of selected nodes
S = []
#array of fathers
T = []
#number of nodes
nodes = -1
#output
output = []
def __init__(self, start):
#read the graph from FILE.in
self.readGraph()
#start node
self.r = start
#assign again
r = self.r
#step 1 you must to mark the selected start node in the utility vector called S
self.S[ r ] = 1
#step 2
for j in range(1, self.nodes + 1):
self.D[ j ] = self.road[r][j]
#step 3
for j in range(1, self.nodes+1):
if self.D[ j ]:
self.T[ j ] = r
for i in range(1, self.nodes):
#find the nodes that is the close to started node
min = 9999
#at nest step you must walk through array and find out which one is the minimum cost from start node.
for j in range(1, self.nodes + 1):
#if not selected
if self.S[ j ] == 0:
if self.D[j] < min:
min = self.D[j]
pos = j
self.S[ pos ] = 1
#next step
for j in range(1, self.nodes + 1):
if self.S[ j ] == 0:
if self.D[ j ] > self.D[ pos ] + self.road[ pos ][ j ]:
self.D[ j ] = self.D[ pos ] + self.road[ pos ][ j ]
self.T[ j ] = pos
f = open('road.out','w')
for k in range(1, self.nodes + 1):
if k is not r:
if self.T[ k ]:
print "Road from ", r, " to ", k
self.draw(k)
print self.output
f.write("Road from {} to {} => {}".format(r,k,str(self.output)))
f.write("\n")
self.output = []
else:
print "There is not exists road"
f.close()
def draw(self, node):
if self.T [ node ]:
self.draw( self.T[ node ] )
print node
self.output.append(node)
def readGraph(self):
counter = 0
input = []
with open('graph.in', 'r') as file:
for a_line in file:
counter += 1
if counter == 1:
number_of_nodes = int(a_line.rstrip())
else:
input.append(a_line.rstrip())
size = len( input )
self.nodes = number_of_nodes
self.road = [[0 for i in range(0, number_of_nodes + 1)] for j in range(0, number_of_nodes + 1)]
for i in range(0, self.nodes + 1):
for j in range(0, self.nodes + 1):
if i == j:
self.road[i][j] = 0
else:
self.road[i][j] = self.infinit
for i in range(0, size):
component = input[i]
node1 = int(component[0])
node2 = int(component[2])
cost = int(component[4])
self.road[node1][node2] = cost
#init
self.D = [ 0 ]* (number_of_nodes + 1)
self.S = [ 0 ]* (number_of_nodes + 1)
self.T = [ 0 ]* (number_of_nodes + 1)
ob = Dij(2)