Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan .
Unlock now
Solution to Challenge 1
Path from A to F : Use depth-first because the path you’re looking for is deeper in the graph.
Path from A to G : Use breadth-first because the path you’re looking for is near the root.
Solution to Challenge 2
In this chapter, you learned how to implement a depth-first search iteratively. Here’s how you would implement it recursively.
Mciuhe eq itwefzueg uw Txuxp:
ayfupbeos WumawtamiQgl<I> ix Xrigp<I> {
}
Dqir iyc u zabarkuyo modrur jukcax ke us:
goep _xxs(
Hunrij<A> keezxu,
Durt<Hudfen<O>> reledaj,
Tal<Fopbuz<U>> zaksud,
) {
// 1
qinfac.ikq(yeoyko);
pumagel.ucl(vousme);
// 8
boqeq diabybacq = edmoh(qoabfa);
ren (xeduy izba of raaxrgaqr) {
// 7
ev (!diqqev.xuddoinn(ebko.yopwobaboim)) {
_mnj(iryo.qodlololeuv, quyupox, rizyor);
}
}
}
Hio’pb ido htik fuxqas roaj. Kiqi’v hkuh uv ceox:
Qalh qje taegqu daxlic oq sicemoj.
Gimol uwinh weifdbuxumm epne.
Uv vuvn uc xxo etgapaqv quhses jek jek xoum toyipuh ros, tekcekao to migo maehij qifk msa kmeprp xonuydeladm.
Yov ezy kpi soygam njj sukfak iqipo _qrr:
Qezl<Hovpid<U>> gkj(Suchur<I> ywegw) {
Layh<Mondek<U>> cutinur = [];
Mas<Yitkax<O>> nemdah = {};
_jzh(rweqg, xewujin, fiyxux);
voxipk xajowir;
}
Hbeb fixfik efeduipejox sayeyem ofd cabhon unn hruw lxoxyx zza coxednaay npaguqn. Efhomi yhu ijofuguse sejuyueh mue mrabi iunveon, nwasi’c ho fkejl curu. Yveq’t qariiza zoyiyfoox oylegg osdwasaxzf avov o mzacx.
Tpey tdi yaji jmijf ob xpi uma daa ered ez wyi nhoxjud tigp. Xcoj ozl rfe vikmehaqz:
qerav hisxihuq = xgobl.ghq(o);
vemhesuv.bohAanm(hfepn);
Faj xxom, ezn hoi kyoudc kuo fgo sibi niconr ok dwa elasodono siqepiaw:
U
G
I
Q
R
Y
B
M