fix: converted single to triple backticks (#36228)
This commit is contained in:
@@ -14,31 +14,32 @@ localeTitle: Floyd Warshall Algorithm
|
||||
|
||||
### تنفيذ بايثون
|
||||
|
||||
`# A large value as infinity
|
||||
inf = 1e10
|
||||
|
||||
def floyd_warshall(weights):
|
||||
V = len(weights)
|
||||
distance_matrix = weights
|
||||
for k in range(V):
|
||||
next_distance_matrix = [list(row) for row in distance_matrix] # make a copy of distance matrix
|
||||
for i in range(V):
|
||||
for j in range(V):
|
||||
# Choose if the k vertex can work as a path with shorter distance
|
||||
next_distance_matrix[i][j] = min(distance_matrix[i][j], distance_matrix[i][k] + distance_matrix[k][j])
|
||||
distance_matrix = next_distance_matrix # update
|
||||
return distance_matrix
|
||||
|
||||
# A graph represented as Adjacency matrix
|
||||
graph = [
|
||||
[0, inf, inf, -3],
|
||||
[inf, 0, inf, 8],
|
||||
[inf, 4, 0, -2],
|
||||
[5, inf, 3, 0]
|
||||
]
|
||||
|
||||
print(floyd_warshall(graph))
|
||||
`
|
||||
```python
|
||||
# A large value as infinity
|
||||
inf = 1e10
|
||||
|
||||
def floyd_warshall(weights):
|
||||
V = len(weights)
|
||||
distance_matrix = weights
|
||||
for k in range(V):
|
||||
next_distance_matrix = [list(row) for row in distance_matrix] # make a copy of distance matrix
|
||||
for i in range(V):
|
||||
for j in range(V):
|
||||
# Choose if the k vertex can work as a path with shorter distance
|
||||
next_distance_matrix[i][j] = min(distance_matrix[i][j], distance_matrix[i][k] + distance_matrix[k][j])
|
||||
distance_matrix = next_distance_matrix # update
|
||||
return distance_matrix
|
||||
|
||||
# A graph represented as Adjacency matrix
|
||||
graph = [
|
||||
[0, inf, inf, -3],
|
||||
[inf, 0, inf, 8],
|
||||
[inf, 4, 0, -2],
|
||||
[5, inf, 3, 0]
|
||||
]
|
||||
|
||||
print(floyd_warshall(graph))
|
||||
```
|
||||
|
||||
#### معلومات اكثر:
|
||||
|
||||
|
Reference in New Issue
Block a user