-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.py
More file actions
50 lines (43 loc) · 1.4 KB
/
Copy pathdijkstra.py
File metadata and controls
50 lines (43 loc) · 1.4 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 12:21:02 2018
@author: abdallah
"""
shortest_path ={}
def dijkstra(graph , Node):
global shortest_path
shortest_path[Node] = 0
growing_node = {Node}
while (len(growing_node) != len(graph) ):
mini = 1000000
mini_edge = (None , None)
for node in growing_node:
for edge in graph[node]:
head_node = edge.split(",")[0]
length = int(edge.split(",")[1])
if head_node not in growing_node:
if shortest_path[node]+ length < mini:
mini_edge = (node ,head_node)
mini = shortest_path[node] + length
if mini_edge != (None , None):
growing_node.add(mini_edge[1])
shortest_path[mini_edge[1]] = mini
else:
for key in graph.keys():
if key not in growing_node:
growing_node.add(key)
shortest_path[key] = mini
graph = {}
with open('dijkstraData.txt') as f:
data = f.readlines()
for line in data:
elements = list(map(str,line.split('\t')[:-1]))
graph[str(elements[0])] = elements[1:]
f.close()
dijkstra(graph , "1")
ans = ''
for i in ['7','37','59','82','99','115','133','165','188','197']:
ans += str(shortest_path[i]) + ","
ans = ans[:-1]
print(ans)