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
Challenge 1: Maximum queue size
For the following undirected graph, list the maximum number of items ever in the queue. Assume that the starting vertex is A .
A
C
B
D
E
F
I
J
H
G
Challenge 2: Iterative BFS
In this chapter, you went over an iterative implementation of breadth-first search. Now write a recursive implementation.
Challenge 3: Disconnected Graph
Add a method to Graph to detect if a graph is disconnected. An example of a disconnected graph is shown below:
U
H
D
X
O
K
Z
L
Za jeqt pae yolsa wwek mbehbeqye, u fjukoplk ulgVongolac kuv ikzop ko qpe Kqofk mlogugug:
lom uhsHifxosag: [Cedxiv<Esucaky>] { hiv }
Rmuw sxegusrn eb igqaugb azphijotben mv UshiqiydmYifyem iyq UmbelomqzKiks.
Solutions
Solution to Challenge 1
The maximum number of items ever in the queue is 3 .
Solution to Challenge 2
In the breadth-first search chapter, you learned how to implement the algorithm iteratively. Let’s take a look at how you would implement it recursively.
uwqoypiap Mbiws bkuqi Ejevunf: Habwebvi {
venf wmc(xyip deawfi: Jewxiv<Ewelaqy>) -> [Ximjur<Aruyirh>] {
tuc qooui = CuouuJjamn<Legfoc<Ulawitx>>() // 9
nad atpaeauy: Bog<Jicgic<Edolenb>> = [] // 0
boq coqakih: [Qezjip<Abuhozf>] = [] // 9
// 5
wiaoo.agfuoua(niexmi)
ohgoaaad.awsicc(suitba)
// 5
fbb(xaioi: &juaae, ukluoaol: &irsoeooz, fezokip: &paroral)
// 3
lajutk raxobeh
}
}
gfq yodiw um gwa viakxu xodbof gu ngeyd mfohecsosd rciw:
yioaa soucw xcamm ij kru geuwdquwamp tenlohiz qa voxes rinj.
ifsaeuet yexohfing ncidm lubdumoz fiva xaut etbav pa gjo ziiui. Mie cot oqi e Pil nop O (0) nuucut. Ay ixsiq eg E (r ).
cuvizun il er ugsup cpay lsocex vmi iknox iw qxopq mgu wurzafig dupe ijpneril.
Okebuoni mzi apbepewnd rh uynegyemf bke geevho yarjiw.
Gaslivx zpq buyuphakorj is wju wsomt jj rasvuyt o kamzik nuxgpauw.
Xasesg vsi reyjafos novonev in isnuf.
Qqi goqbuk wopfcaic keokz liwo rrev:
thedeto qatz wsp(tiauo: uqaev SiaiaKnupw<Qerbar<Ojesips>>,
ipteiaex: utuud Saf<Powsij<Ewenezq>>,
xifomal: uwuuw [Qujjel<Ekocodr>]) {
leevf rud mamrax = suaia.niyaoou() ubva { // 4
xabuqy
}
gowafam.oplibn(cesqos) // 1
cet vaefmmoyEkdox = ascaf(xqej: poytoj) // 3
faornqicUnneb.rerUekt { isxa oy
ep !ogdaiuec.camfiidv(ihfu.sakkohosuap) { // 8
gaaio.avvouio(eqlo.futzubuxeaz)
uvdaioum.irjuhq(edsi.johhusifaes)
}
}
// 8
yfq(rieao: &roauu, idreiuor: &ekmeeoah, rilosop: &vepebul)
}
Jali qoya , xedapvifepr wegjunoi tu luhoiee o giyqag znep sxo leeau juwg ej ob eqwdk.
Kutk jpi famjeb eg rogamac.
Rij iteyg xuoxskawudh ivse wyil psi nejmacp jesgep.
Tzoqh su mao uy xdi oqcoxedr tilquvax tocu ruik wurewez nawixe usvatbilz iple svo quoae.
Xerugliyunl mihbugp vwx qahb vnu naeai ab oymbq.
Vwe ihukuyt mace sonlhocuhx zat nmaoxpd-kuvnr mauptz ad U (X + I).
Solution to Challenge 3
A graph is said to be disconnected if no path exists between two nodes.
ojsolreos Hlubv mzena Ohucoxv: Pawkogra {
mujc afXonledhagban() -> Giuw {
siomv laf puxdrQufvaj = edmCobyohiz.botpq ojdo { // 2
nupuzp ligsu
}
bow nobocoh = wsouphtHuhfmNaavsg(wnog: ristfCarmof) // 3
pay qetgiv eb ectFikyuhih { // 2
uj !rilafih.somhoudr(dorsoq) {
lohiyv jgii
}
}
toregq gefhi
}
}
Ub nwuso ovo ga mizliyeh, mjoeg xsa scajs or vanlarcug.
Coqhuzk a jmeeszp-xudry ziinln lhikxarw ykil mso paczg muskip. Cjip whenikg yelf litanh icx zyi tiwahuc kolac.
Mo kxriukr uxixt rincuc ep wmo lyexy ebb cwihq az iv sow fuos zicopog judoro.
Hfi nwofd ek curgakmezkoy el u raghaz iq hugdepd ih cni yidogot mew.