dijkstra_1
This commit is contained in:
parent
70e5f5c16c
commit
b55566c193
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,3 +3,6 @@
|
|||||||
|
|
||||||
#pycache
|
#pycache
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
||||||
|
#macOS
|
||||||
|
.DS_Store
|
76
dijkstra.py
Normal file
76
dijkstra.py
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
def printSolution(dist, graph):
|
||||||
|
print("Odleglosc punktow od wyznaczonego")
|
||||||
|
for node in range(len(graph)):
|
||||||
|
print("Punkt: {} odleglosc: {}".format(node, dist[node]))
|
||||||
|
|
||||||
|
|
||||||
|
def minDistance(graph, dist, sptSet):
|
||||||
|
mini = sys.maxsize
|
||||||
|
for v in range(len(graph)):
|
||||||
|
if dist[v] < mini and sptSet[v] == False:
|
||||||
|
mini = dist[v]
|
||||||
|
min_index = v
|
||||||
|
return min_index
|
||||||
|
|
||||||
|
|
||||||
|
def dijkstra(src, graph):
|
||||||
|
dist = [sys.maxsize] * len(graph)
|
||||||
|
dist[src] = 0
|
||||||
|
sptSet = [False] * len(graph)
|
||||||
|
|
||||||
|
for cout in range(len(graph)):
|
||||||
|
u = minDistance(graph, dist, sptSet)
|
||||||
|
sptSet[u] = True
|
||||||
|
for v in range(len(graph)):
|
||||||
|
if graph[u][v] > 0 and sptSet[v] == False and dist[v] > dist[u] + graph[u][v]:
|
||||||
|
dist[v] = dist[u] + graph[u][v]
|
||||||
|
|
||||||
|
printSolution(dist, graph)
|
||||||
|
|
||||||
|
|
||||||
|
# Graf, koszt przejscia 1
|
||||||
|
# 1- 2- 3- 4- 5
|
||||||
|
# |
|
||||||
|
# 10- 9- 8- 7- 6
|
||||||
|
# |
|
||||||
|
# 11-12-13-14-15
|
||||||
|
# |
|
||||||
|
# 20-19-18-17-16
|
||||||
|
# |
|
||||||
|
# 21-22-23-24-25
|
||||||
|
|
||||||
|
graph = [[0, 1, 0, 0, 0, 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[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0] ] #25
|
||||||
|
|
||||||
|
|
||||||
|
#wypisuje tylko odpowiednio koszt przejscia do danego punktu
|
||||||
|
#struktura grafu jest na tyle uproszczona ze nie trzeba wypisywac po kolei ktore punkty odwiedzamy
|
||||||
|
#bo idziemy albo o punkt nizej albo wyzej
|
||||||
|
#oczywiscie da to sie wszystko jakos bardziej rozwinac
|
||||||
|
|
||||||
|
dijkstra(2, graph)
|
Loading…
Reference in New Issue
Block a user