-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.py
120 lines (70 loc) · 2.08 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
#Now let's open it with pandas
import pandas as pd
from pandas import Series,DataFrame
# Let's import what we'll need for the analysis and visualization
import numpy as np
import matplotlib.pyplot as plt
class node(object):
def __init__(self,x,y):
self.x=x
self.y=y
class grafo (object):
def __init__(self,nodos,conec):
self.nodos=nodos
self.conec=conec
"""
nodos=[]
con=[]
f = open ('random_3.top', 'r+')
for line in f:
words = line.split()
if words[1] == "node":
nodos.append(node( float(words[3]),float(words[4]) ))
f.close()
con =np.ones(len(nodos)*len(nodos)).reshape(len(nodos),len(nodos))*np.inf
f = open ('random_3.top', 'r+')
for line in f:
words = line.split()
if words[1] == "connection" and float(words[4]) < .2:
##print("CONECCCION POSIBLE ENTRE " + words[2] + " Y " + words[3])
con[int(words[2])] [int(words[3])]=float(words[4])
f.close()
graphe = grafo(nodos,con)
"""
A=np.load('A.npy')
nodos=np.load('ccxyth.npy')
con = 1-np.where(A!=0,A,-np.inf)
graphe2=grafo(nodos,con)
def dijsktra(nodoinicial,nodofinal,graphe):
##INIT
numnodos= len(graphe.nodos)
con = graphe.conec
D= np.ones(numnodos)*np.inf
V= np.zeros(numnodos)
ruta=[]
D[nodoinicial]=0
a = nodoinicial
ruta.append(a)
###FIN INIT
jaja=pd.Series(con[a])
cona=jaja[jaja != np.inf]
V[a]=1
for c in cona.index:
if cona [c] < D[c]:
D[c] = cona[c]
Daux = pd.Series(D)
a= Daux[V!=1].argmin()
ruta.append(a)
#####FF
while a != nodofinal:
jaja=pd.Series(con[a])
cona=jaja[jaja != np.inf]
V[a]=1
for c in cona.index:
if cona[c]+D[a] < D[c] :
D[c] = cona[c] + D[a]
Daux= pd.Series(D)
a= Daux[V!=1].argmin()
ruta.append(a)
return (ruta)
print ( dijsktra(13,67,graphe2))