Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet61/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   57   58   59   60   61   62   63   64   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

7
Dijkstra’s 
algorithm


116
Chapter 7
 
 
I
 
 
Dijkstra’s algorithm
In the last chapter, yo
u igured out a way to get from point A to point B.
It’s not necessarily the fastest path. It’s the shortest path, because it has 
the least number of segments (three segments). But suppose you add 
travel times to those segments. Now you see that there’s a faster path.
You used breadth-irst search in the last chapter. Breadth-irst search 
will ind you the path with the fewest segments (the irst graph shown 
here). What if you want the fastest path instead (the second graph)? You 
can do that
fastest
with a diferent algorithm called
Dijkstra’s algorithm. 
Working with Dijkstra’s algorithm
Let’s see how it works with this graph.
Each segment has a travel time in minutes. You’ll use Dijkstra’s 
algorithm to go from start to inish in the shortest possible time.


117
Working with Dijkstra’s algorithm
If you ran breadt
h-irst search on this graph, you’d get this
shortest path.
But that path takes 7 minutes. Let’s see if you can ind a path that takes 
less time! here are four steps to Dijkstra’s algorithm:
1. Find the “cheapest” node. his is the node you can get to in the least 
amount of time.
2. Update the costs of the neighbors of this node. I’ll explain what I 
mean by this shortly.
3. Repeat until you’ve done this for every node in the graph.
4. Calculate the inal path.
Step 1: 
Find the cheapest node. You’re standing at the start, wondering 
if you should go to node A or node B. How long does it take to get to 
each node?
It takes 6 minutes to get to node A and 2 minutes to get to node B.
he rest of the nodes, you don’t know yet.
Because you don’t know how long it takes to get
to the inish yet, you put down ininity (you’ll see 
why soon). Node B is the closest node … it’s 2 
minutes away.


118

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   57   58   59   60   61   62   63   64   ...   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