Skip to content

Commit 473c6d1

Browse files
authored
Merge pull request kelvins#116 from alelimasilva/main
Adicionando Floyd-Warshall
2 parents 6a21ae5 + 55a7b78 commit 473c6d1

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
77
| Algoritmos | C | C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
88
| ------------------------------- | --------------------------------------- | ----------------------------------------- | ---------------------------------------------- | ---------------------------------------------------- | --------------------------------------------------- | ---------------------------------------------- | ------------------------------------------------- | ------------------------------------------ | -------------------------------------------- | ---------------------------------------------- |
99
| [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 |
10-
| [Algoritmo Floyd Warshall][2] | [C](./src/c/AlgoritmoFloydWarshall.c) | C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
10+
| [Algoritmo Floyd Warshall][2] | [C](./src/c/AlgoritmoFloydWarshall.c) | C++ | Java | [Python](./src/python/floyd-warshall.py) | Go | Ruby | JS | Pascal | Swift | Rust |
1111
| [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 |
1212
| [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 |
1313
| [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 |

src/python/floyd-warshall.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Grafos - Algoritmo de Floyd-Warshall em Python
2+
# Alexandre Lima - 2021
3+
# https://github.com/alelimasilva
4+
5+
from math import inf
6+
7+
def gerar_matriz (n_linhas, n_colunas):
8+
return [[0]*n_colunas for _ in range(n_linhas)]
9+
10+
def imprime(matriz, vertices):
11+
print(' ', end='')
12+
for i in range(vertices):
13+
print(" ",i + 1,end='')
14+
print('')
15+
for i in range(vertices):
16+
print(i + 1, matriz[i])
17+
18+
def floyd_warshall(matriz, vertices):
19+
dist = gerar_matriz(vertices, vertices)
20+
# inicializando a matriz com infinito nas diagonais e as distancias das arestas
21+
for i in range(vertices):
22+
for j in range(vertices):
23+
if(i != j):
24+
if(matriz[i][j] != 0):
25+
dist[i][j] = matriz[i][j]
26+
else:
27+
dist[i][j] = inf
28+
else:
29+
dist[i][i] = inf
30+
# Floyd-Warshal
31+
for k in range(vertices):
32+
for i in range(vertices):
33+
for j in range(vertices):
34+
Dist = inf
35+
if(dist[i][k] != inf or dist[k][j] != inf):
36+
Dist = dist[i][k] + dist[k][j]
37+
38+
if(i != j and Dist != inf and Dist < dist[i][j]):
39+
dist[i][j] = Dist
40+
# printando o resultado
41+
print('Matriz de distâncias')
42+
imprime(dist, vertices)
43+
44+
def main():
45+
grafo = [[0,3,4,0],[0,0,0,5],[0,0,0,3],[8,0,0,0]]
46+
vertices = 4
47+
floyd_warshall(grafo, vertices)
48+
49+
if __name__ == "__main__" :
50+
main()

0 commit comments

Comments
 (0)