Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
| Algoritmos | C | C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
| ------------------------------- | --------------------------------------- | ----------------------------------------- | ---------------------------------------------- | ---------------------------------------------------- | --------------------------------------------------- | ---------------------------------------------- | ------------------------------------------------- | ------------------------------------------ | -------------------------------------------- | ---------------------------------------------- |
| [Algoritmo Dijkstra][1] | [C](./src/c/AlgoritmoDijkstra.c) | C++ | [Java](./src/java/Dijkstra.java) | [Python](./src/python/dijkstra.py) | [Go](./src/go/dijkstra/dijkstra.go) | Ruby | [JS](src/javascript/AlgoritmoDijkstra.js) | Pascal | Swift | Rust |
| [Algoritmo Floyd Warshall][2] | [C](./src/c/AlgoritmoFloydWarshall.c) | C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
| [Algoritmo Floyd Warshall][2] | [C](./src/c/AlgoritmoFloydWarshall.c) | C++ | Java | [Python](./src/python/floyd-warshall.py) | Go | Ruby | JS | Pascal | Swift | Rust |
| [Busca Binária][5] | C | [C++](./src/cpp/BinarySearch.cpp) | Java | [Python](./src/python/busca_binaria.py) | [Go](./src/go/busca_binaria/busca_binaria.go) | [Ruby](./src/ruby/BuscaBinaria.rb) | [JS](./src/javascript/BinarySearch.js) | [Pascal](./src/pascal/busca-binaria.pas) | Swift | Rust |
| [Busca em Grafos][6] | [C](./src/c/BuscaEmGrafo.c) | C++ | Java | [Python](./src/python/busca_em_grafo.py) | Go | Ruby | [JS](./src/javascript/GraphSearch.js) | Pascal | Swift | Rust |
| [Busca Sequencial][7] | [C](./src/c/BuscaSequencial.c) | C++ | Java | [Python](./src/python/busca_sequencial.py) | Go | [Ruby](./src/ruby/BuscaSequencial.rb) | [JS](./src/javascript/BuscaLinear.js) | Pascal | Swift | Rust |
Expand Down
50 changes: 50 additions & 0 deletions src/python/floyd-warshall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Grafos - Algoritmo de Floyd-Warshall em Python
# Alexandre Lima - 2021
# https://github.com/alelimasilva

from math import inf

def gerar_matriz (n_linhas, n_colunas):
return [[0]*n_colunas for _ in range(n_linhas)]

def imprime(matriz, vertices):
print(' ', end='')
for i in range(vertices):
print(" ",i + 1,end='')
print('')
for i in range(vertices):
print(i + 1, matriz[i])

def floyd_warshall(matriz, vertices):
dist = gerar_matriz(vertices, vertices)
# inicializando a matriz com infinito nas diagonais e as distancias das arestas
for i in range(vertices):
for j in range(vertices):
if(i != j):
if(matriz[i][j] != 0):
dist[i][j] = matriz[i][j]
else:
dist[i][j] = inf
else:
dist[i][i] = inf
# Floyd-Warshal
for k in range(vertices):
for i in range(vertices):
for j in range(vertices):
Dist = inf
if(dist[i][k] != inf or dist[k][j] != inf):
Dist = dist[i][k] + dist[k][j]

if(i != j and Dist != inf and Dist < dist[i][j]):
dist[i][j] = Dist
# printando o resultado
print('Matriz de distâncias')
imprime(dist, vertices)

def main():
grafo = [[0,3,4,0],[0,0,0,5],[0,0,0,3],[8,0,0,0]]
vertices = 4
floyd_warshall(grafo, vertices)

if __name__ == "__main__" :
main()