Created
January 12, 2016 16:57
-
-
Save fransafu/f09efaf3208aaa96077a to your computer and use it in GitHub Desktop.
This is the Dijkstra algorithm in python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dijkstra(matriz_costo, numero_vertices): | |
S = [1] # Vertices visitados, iniciamos con 1 | |
V = list(range(1,numero_vertices+1)) # Vertices disponibles | |
D = [0] * numero_vertices # Arreglo de costo | |
print ('\n[+] Inicializando variables.') | |
print ('Vertice inicial visitado S:' + str(S)) | |
print ('Vertices disponibles en total V:' + str(V)) | |
print ('Arreglo de costo inicializado D:' + str(D)) | |
print ('') | |
# Llena el Arreglo de costo | |
print ('[+] Inicia llenar arreglo de costo.') | |
contador = 0 | |
while(contador < numero_vertices): | |
D[contador] = (find_cost(matriz_costo, 1, contador + 1)) | |
contador += 1 | |
# Finaliza Llena el Arreglo de costo | |
print ('Arreglo de costo D:' + str(D)) | |
print ('') | |
#print ('D:' + str(D)) | |
contador2 = 1 | |
while contador2 <= numero_vertices: | |
print ('[+] Inicia asignar w y agregar a S\n') | |
print ('contador2: \t' + str(contador2)) | |
print ('Arreglo de costo D: \t' + str(D)) | |
print ('S:' + str(S)) | |
print ('v-s: ' + str(v_menos_s(V,S))) | |
print ('minimum: \t' + str(minimum(D, V, S))) | |
# Inicia asignar w | |
for vertice in v_menos_s(V,S): # Recorre los vectores disponibles | |
print('costo vertice: \t' + str(D[vertice - 1])) | |
if(D[vertice - 1] == minimum(D, V, S)): # Evalua si el costo del vertice disponible es el minimo en D | |
w = vertice # Asigna el vertice de costo minimo | |
print ('w = \t' + str(w)) | |
print ('w seleccionado: \t' + str(w)) | |
S.append(w) | |
print ('[+] Finaliza asignar w y agregar a S') | |
print ('') | |
# Finaliza asignar w | |
print ('S:' + str(S)) | |
print ('v-s: ' + str(v_menos_s(V,S))) | |
for vertice in v_menos_s(V,S): | |
print ('Arreglo de costo D:' + str(D)) | |
print ('w:' + str(w)) | |
print ('vertice: \t' + str(vertice)) | |
print ('min('+str(D[vertice - 1]) + ',' + str(D[w - 1]) +'+'+ str(find_cost(matriz_costo, (w), (vertice))) + ')') | |
D[vertice - 1] = min(D[vertice - 1], D[w - 1] + find_cost(matriz_costo, (w), (vertice))) | |
print (D[vertice - 1]) | |
if (v_menos_s(V,S) == []): | |
break | |
contador2 += 1 | |
return D | |
def minimum(D, V, S): ## Retorna costo minimo | |
aux = float('inf') | |
for dato in v_menos_s(V,S): | |
if(aux < D[dato - 1]): | |
aux = aux | |
elif(aux >= D[dato - 1]): | |
aux = D[dato - 1] | |
return aux | |
def v_menos_s(V, S): | |
aux = [] | |
for vertice_v in V: | |
if vertice_v not in S: | |
aux.append(vertice_v) | |
return aux | |
## Funciones | |
def find_cost(matriz_costo, fila_ingresada, columna_ingresada): | |
#Funcion que retorna el valor especificado por los parametros fila y columna ingresados | |
fila = 1 | |
for i in matriz_costo: | |
columna = 1 | |
for j in i: | |
if(fila == fila_ingresada and columna == columna_ingresada): | |
return j | |
columna += 1 | |
fila += 1 | |
def main(): | |
matriz_costo = [] | |
# Llena matriz de costos | |
print ('Llenar matriz de costos.') | |
#numero_filas = int(input('filas: ')) | |
#numero_columnas = int(input('columnas: ')) | |
#for i in range(numero_filas): | |
# matriz_costo.append([]) | |
# for j in range(numero_columnas): | |
# dato = (input('Costo del vertice [' + str(i + 1) + '] al vertice [' + str(j + 1) + ']: ')) | |
# if (dato == 'inf' or dato == 'Inf'): | |
# matriz_costo[i].append(float('inf')) | |
# else: | |
# matriz_costo[i].append(int(dato)) | |
numero_filas = 5 | |
matriz_costo = [[float('inf'), 10, float('inf'), 30, 100], | |
[float('inf'), float('inf'), 50, float('inf'), float('inf')], | |
[float('inf'), float('inf'), float('inf'), float('inf'), 10], | |
[float('inf'), float('inf'), 20, float('inf'), 60], | |
[float('inf'), float('inf'), float('inf'), float('inf'), float('inf')]] | |
# Imprime matriz de costo | |
print ('') | |
print ('Matriz de costo ingresada') | |
for x in matriz_costo: | |
print ('\n', end='') | |
for j in x: | |
print (str(j) + ' ', end='') | |
print ('') | |
print ('') | |
print ('[+] Ejecutando Dijkstra.') | |
print (dijkstra(matriz_costo, numero_filas)) | |
print ("") | |
print ('[+] Finaliza Dijkstra') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment