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0, 1, 0, 1, 0], #23
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], #24
|
|
[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
|
|
|
|
def printSolution(dist):
|
|
print("Odleglosc punktow od wyznaczonego")
|
|
for node in range(len(graph)):
|
|
print("Punkt: {} odleglosc: {}".format(node, dist[node]))
|
|
|
|
|
|
def minDistance(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):
|
|
dist = [sys.maxsize] * len(graph)
|
|
dist[src] = 0
|
|
sptSet = [False] * len(graph)
|
|
|
|
for cout in range(len(graph)):
|
|
u = minDistance(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)
|
|
return dist
|
|
|
|
|
|
#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) |