Grokking Algorithms



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

EXERCISE
7.1
In each of these graphs, what is the weight of the shortest path from 
start t
o inish?


140
Chapter 7
 
 
I
 
 
Dijkstra’s algorithm
Recap
• Breadt
h-irst search is used to calculate the shortest path for
an unweighted graph.
• Dijkstra’s algorithm is used to calculate the shortest path for
a weighted graph.
• Dijkstra’s algorithm works when all the weights are positive.
• If you have negative weights, use the Bellman-Ford algorithm.


141
greedy 
algorithms
In this chapter
• You 
learn how to tackle the impossible:
problems that have no fast algorithmic solution 
(NP-complete problems).
• 
You learn how to identify such problems when you 
see them, so you don’t waste time trying to ind a 
fast algorithm for them.
• 
You learn about approximation algorithms, which 
you can use to ind an approximate solution to an 
NP-complete problem quickly.
• 
You learn about the greedy strategy, a very simple 
problem-solving strategy.
8


142
Chapter 8
 
 
I
 
 
Greedy algorithms
The classroom scheduling problem
Suppose you have a classroom and want to hold as many classes 
here as possible. You get a list of classes.
You can’t hold
all
of these classes in there, because some of them 
overlap.
You want to hold as many classes as possible in this classroom. How 
do you pick what set of classes to hold, so that you get the biggest set of 
classes possible?
Sounds like a hard problem, right? Actually, the algorithm is so easy, it 
might surprise you. Here’s how it works:
1. Pick the class that ends the soones
t. his is the irst class you’ll hold 
in this classroom.
2. Now, you have to pick a class that starts ater the irst class.
Again, pick the class that ends the soonest. his is the second
class you’ll hold.


143
The classroom scheduling problem
Keep doing this, and you’ll end up with the answer! Let’s try it out. Art 
ends the soonest, at 10:00 a.m., so that’s one of the classes you pick.
Now you need the next class that starts a
ter 10:00 a.m. and ends
the soonest.
English is out because it conlicts with Art, but Math works.
Finally, CS conlicts with Math, but Music works.
So these are the three classes you’ll hold in this classroom.


144

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   66   67   68   69   70   71   72   73   ...   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