Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet68/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   64   65   66   67   68   69   70   71   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

Chapter 7
 
 
I
 
 
Dijkstra’s algorithm
Here’s the code to make the hash table for the parents:
parents = {}
parents[“a”] = “start”
parents[“b”] = “start”
parents[“fin”] = None
Finally, you need an array to keep track of all the nodes you’ve already 
processed, because you don’t need to process a node more than once:
processed = []
hat’s all the setup. Now let’s look at the algorithm.
I’ll show you the code irst and then walk through it. Here’s the code:
node = find_lowest_cost_node(costs) 
while
node is not None: 
cost = costs[node]
neighbors = graph[node]
for
n in neighbors.keys(): 
new_cost = cost + neighbors[n] 
if
costs[n] > new_cost:
costs[n] = new_cost
parents[n] = node
processed.append(node)
node = find_lowest_cost_node(costs) 
hat’s Dijkstra’s algorithm in Python! I’ll show you the code for the 
function later. First, let’s see this 
find_lowest_cost_node 
algorithm 
code in action.
Find the lowest-cost node
 that you haven’t processed yet.
 If you’ve processed all the nodes, this while loop is done.
Go through all the neighbors of this node.
 
If it’s cheaper to get to this neighbor
by going through this node …
 … update the cost for this node.
 This node becomes the new parent for this neighbor.
 Mark the node as processed.
 Find the next node to process, and loop.


135
Implementation
Find the node with the lowest cost.
Get the cost and neighbors of that node.
Loop through the neighbors.
Each node has a cos
t. he cost is how long it takes to get to that node 
from the start. Here, you’re calculating how long it would take to get to 
node A if you went Start > node B > node A, instead of Start > node A.
Let’s compare those costs.


136

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   64   65   66   67   68   69   70   71   ...   120




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