Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet76/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   72   73   74   75   76   77   78   79   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

Chapter 8
 
 
I
 
 
Greedy algorithms
Finally, you can print 
final_stations
, and you should see this:
>>> print final_stations
set([‘ktwo’, ‘kthree’, ‘kone’, ‘kfive’])
Is that what you expected? Instead of stations 1, 2, 3, and 5, you could 
have chosen stations 2, 3, 4, and 5. Let’s compare the run time of the 
greedy algorithm to the exact algorithm.
EXERCISES
For each of these algorithms, say whether it’s a greedy algorithm or not.
8.3
Quicksort
8.4
Breadt
h-irst search
8.5
Dijkstra’s algorithm
NP-complete problems
To solve the set-covering problem, you had to calculate every
possible set.


153
NP-complete problems
Maybe you were reminded of the traveling salesperson problem from 
chapter 1. In this problem, a salesperson has to visi
t ive diferent cities.
And he’s trying to igure out the shortest route that will take him to all 
ive cities. To ind the shortest route, you irst have to calculate every 
possible route.
How many routes do you have to calculate for ive cities?
Traveling salesperson, step by step
Let’s start small. Suppose you only have two cities. here are two routes 
to choose from.


154
Chapter 8
 
 
I
 
 
Greedy algorithms
You may be wondering, “In the traveling salesperson problem, is there 
a spe
ciic city you need to start from?” For example, let’s say I’m the 
traveling salesperson. I live in San Francisco, and I need to go to four 
other cities. San Francisco would be my start city.
But sometimes the start city isn’t set. Suppose you’re FedEx, trying 
to deliver a package to the Bay Area. he package is being lown in 
from Chicago to one of 50 FedEx locations in the Bay Area. hen 
that package will go on a truck that will travel to diferent locations 
delivering packages. Which location should it get lown to? Here the 
start location is unknown. It’s up to you to compute the optimal path 
and start location for the traveling salesperson.
he running time for both versions is the same. But it’s an easier 
example if there’s no deined start city, so I’ll go with that version.
Two cities = two possible routes.
3 cities
Now suppose you add one more city. How many possible routes
are there?
If you start at Berkeley, you have two more cities to visit.

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   72   73   74   75   76   77   78   79   ...   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