Практическая работа 5 по предмету : алгоритмы и структура данных



Download 463,99 Kb.
bet2/6
Sana20.07.2022
Hajmi463,99 Kb.
#827076
TuriПрактическая работа
1   2   3   4   5   6
Bog'liq
algoritm последний

Ford Belman
def bellman_ford(graph, source):
# Step 1: Prepare the distance and predecessor for each node
distance, predecessor = dict(), dict()
for node in graph:
distance[node], predecessor[node] = float('inf'), None
distance[source] = 0

# Step 2: Relax the edges


for _ in range(len(graph) - 1):
for node in graph:
for neighbour in graph[node]:
# If the distance between the node and the neighbour is lower than the current, store it
if distance[neighbour] > distance[node] + graph[node][neighbour]:
distance[neighbour], predecessor[neighbour] = distance[node] + graph[node][neighbour], node

# Step 3: Check for negative weight cycles


for node in graph:
for neighbour in graph[node]:
assert distance[neighbour] <= distance[node] + graph[node][neighbour], "Negative weight cycle."

return distance, predecessor

if __name__ == '__main__':
graph = {
'79': {'77': 43, '35': 27, '92': 69},
'77': {'35': 87, '92': 65, '64': 71},
'64': {'35': 43, '92': 74},
'35': {'92': 49},
'92': {'50': 9},
'50': {'35': 30}
}
graph = {
'79':{'77': 43, '35': 27,'92': 69},
'77':{'35': 87,'92': 65,'64': 71},
'64':{'35': 43,'92': 74},
'35':{'92': 49},
'92':{'50': 9},
'50':{'35': 30}
}

distance, predecessor = bellman_ford(graph, source='S')

print (distance)



Floyd Warshal

def floyd_warshall(nodes,edges):


d={(u,v): float('inf') if u != v else 0 for u in nodes for v in nodes}
for (u,v), w_uv in edges.items():
d[(u,v)]=w_uv

for k in nodes:


for u in nodes:
for v in nodes:
d[(u,v)]=min(d[(u,v)], d[(u,k)] + d[(k,v)])

if any(d[(u,u)]<0 for u in nodes):


print("Grafimiz manfiy vaznli yo'naltirilgan hisoblanadi!")
return d
if __name__ == '__main__':
nodes=[79,77,35,64,92,50]
edges={
(79,77):43,
(79,35):27,
(79,92):69,
(77,35):87,
(77,92):65,
(64,77):71,
(64,35):43,
(35,50):30,
(92,35):49,
(50,92):9,
}
qisqa_yul=floyd_warshall(nodes, edges)
print(qisqa_yul)






Download 463,99 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish