Download as pdf
Download as pdf
You are on page 1of 39
——— Module 2 - ee — AVA Trees eee —s AVA Tree is a Atight balanced Binawy Search Trees _ in which the di ference bin the fuuight the faye ee ght @ubbree is ether -L-os—ow 1, Oy OF +1 _ FAVA Tree ave also called a sel balanatig binary Garth bree se AVE Trees Search fime is — OLeqn) a 9 $l is-mamed after ib inventos Cave) Addon Veleky Landes_ Why AVA Trees? to - Ty _undersftand dhe eed for AVL trees, Get us fook ab — Some disadvantages 6} Simple Binary Search Hees - 9 Consider the 4ol- b, ss inguled “in the gown orderin tre BST _ 7 ten XO The Feit of the tree _¢ ows Lineaaly, fn_sige tohen we inser! the keys in _intheasing orda Sq thecs Value. Tho —_the_éearth_opetotion at worst case, Fakes O(n) ___ 3 On _She the othe hand i, the ther taf the Ate 49 _ —_ Folanud) we t eller geaacheng bime hel “Us mow look ot the dame. Keys hut tnserted_ ino dim. osdes key _40, 20, 30) 60, 50 10 Here, the keys are dhe Same, bul Sines ne _tosated in a diy order, they lake dt : a ; the Aeight of, the baie. _nemaint. .balanced+- rence xc will not Hate “more than OClogn) for ony a te dn AvL trees, we keep a check On The Shergy the bee aluring- ingeytion Operaton - -3'Modiveations ate mad. fo maintain heb sithowt violaing the fymdarnental Prope of Bs. Balance Factor tn Avi trees - Balance factor CBF) if a fundamental attribulr bl_every mode_tn_Ave_trees thay helps fo monitor P tye_thee's Arecght = = > BF ww known as the diferente blo_the herght of shy felt gubtvee & the sughh Subtree « ==. —F BE (node) 2 hatgh} of [ell subtree — trerght q ri Subhas Allowed Values BF Ove —1,0 and +1. —._AvL Rotations _ i : To _ mak ane | _tnse tin 7 ican a ae ee : 4 ele. Gy Anode Srons dhe hee, the fel: _. Botations age exptme so ~ ~> LL Rotahon Cleft UL t Rotation >) A i a 3 4 Rotation. CRight - ight Rotatvers,) bs 2 OR Rotation. C Ley ~ Right Rotation) > RL Rotahon Re i CRM - Left Ratahon) bl Relation Chet — Lee “Retadron) Ths rotation is fa Palanan Pertormed tohen a eur Mode is _insested: “ the ele Che td * the, tet sublaee — - Sire €_tmpalanced - ~ BE o) anode = os eva 3 he eataloneg Bo ll (aT an “Tncs_totation tte pexjormed then _a_mew) made_te inserted at the aight child. of, the ‘igh Sublece - : z y o LR Rotahion-> BF oF mode = -2 BE. pts sig he chile sol rt Right I ft Relation ___ Tus notation ormed ushen Ga mew mode ts inseaked at the td the Jeft Subbtace ef tne aegint cholo] the —plvel meds 2 R-L Rotation —> ) PF ef a node =-2 | BF of its 2u ght cha td Peat AS “aolaroney _ a —— mode. (30) wrth Brey uble ——_- Rotation * Right Bebe - Rotation hed 21g tht Rotakion po es SN Ts Atation_t | -£ Perse new node._t>_cnsold th = the tri teed _ Balance factor 9 oa nodes 49. {-—_—_—— —<——— — Bala jeer % Mele childe-) __|mseetion Oper ation_in Avi trees a epi toppliow jor iserory Ked_the newly tmserted node bow 1) Per form Bsr instal forts — _ J). Starting from w travel up & fend the fpast unbalana_nede. kes | hel _< be the feast unbalanced node, y be the Child of 2 thad comes on the path 470m wo 2% % be_the_qrand ehild_of x that _tomes_on_the path from — — us fo x — 3) Rebalarce the tree | —Pejprmeng a pprepscate- _2otato ns 6m_4ne Sublace_Avoted with X- There COM ba 4 possible — eases (notations) - ©) _y ts a Rett child fr At Belt child of Y— 11 Rolabon 1 ee = kR Rotate, del = 7 Ce Vis a sigok child $1 _ts_a. aught Righ'- Righ! Retain (RR soba») (4) pea oO, Zot xis Met ebild of Y - = Rig felt Case_- RR—co |t-case- Right Rlate(z} Ri LR case — Left aotaieCy) then Right Robate(z) RR tate = belt soba (ED, oR caste Right 70: tale (y) then Lal} Rolle (2) ; ©. _Ganstructe- AVk Haeas toe dhe Ql. Numbers. | 4,8, 12, 46 ~ 23, 5, 44) 86, 20 - \msert 4 ; Insert tis 2 2 Fi R votation ° Snsext 20 Q.| Conslaict AVL trees _for. the _}ol. numbers « Seek 1, 2,3p 4 78, 7) bp Sy Ul, 10p12 inser . - Snsert 2 Inset 3 sin)? ‘ Os Cy? RR = = o 2} > ’ yi — oO : | Snsert + Snseat &- (a) a #25 pe I @) @- 1)o -2 o o ° = ce Snsert 6 © Inset 10, 20, Is) 25 30, 16/18, 19 inks an__ AVL... tree, —___rsetttieg @ Snot 03 WL? : ~ — o> ~~ __ Sasert is. Snsert do Is) “2° ra ° Snsert It \¢ } ] Deletion in an AVL Tree. - a node prom an AVL tree follows dhe came ry) > Deleting. Balance pacer ofan AVL Lig BST delebor - > Drehon may dislan dhe and therefore the [xz needs to be, aebalanced. Inorg, bo marntaim ib as an Avi tree Rules for Binaay Search Tree Deletion _ - 3. Node to be elie i& the feof: — Strnply, Yemove from de, eg.- 7 ; CF) - deleteCad), Ge) Ga) . Ge @9 - 8 Nodeto_be deleted has only-one. child _ Copy the Child 4, the node & delete the child. _ a delekeCaoQ mamas — \ = e = | GG) SS. ©) Node 4o be deleled Pas Jwo duldaen, i oe > find the inotder sucesso the modes —2 fopy the contents of the inorder Successev fo “the J Mode B dolele the. inorder suctessor i — 2 Note that mmorder Predecessor on also fazed. ___._Snorder predecessor of 0 node ana PST. in the teh sublyee et Inavder Sutvesir a Synallesl: mode in dhe aght bbe _—Sleps_ ro _ AVL tvee_delehior. _ BE Search the node cohuch te do ba deleted. _ ae tobe. deleted is rato. lea, Node then Simply delete \hat_ modes a cy KY Ke leaf, delete %_| a case: Gy x hes-one child, use tt te asplace ¥ ____— eased if x thas two_children , Aeplace K-wotth ile ingader. Geicees on _tnotden _pasdecestor Than delole R2+ Traverse ack Up the Path_taxsards dpe root mode Checking Balance 4actor_4 ever _nodt+ along dhe_path . 3. St the BF of a node is mot = ¢by hen _balana ——}_—tie_Ints_using OppeapeialtZingt ble _natalicns Delete, 71M, 4, 14 Jrara_tine dol. AVL {we ¥ # Slo. Deleleg - ql | Date tap I SS eo : vr Node thas 2; Take inovdey _ SuceeSgor of Take inordey Succe stor oh ear le. —> ahd gon, as Delete > §), woe lake inorder predecessor a ————— = Take inorder_predecess 44 — ™. 2 — CBF AVL Tree Deletion = Rotations ae A becomes crihical 2 On deletven 4 node % if, nea c ghether X ts in the 9 Tape oS Setahon depends ° subtree - let eublaze oy A ov in QT OT em aa by Sp Xe in Tyl subbse a, A ig * by SPX fe in Night tublét pf’) ” #obanion peers id Renolahens ase, 4, 3 3 types RORAatin Ri Rolekion _ R-1 Rotation - L- Relahons ane o| 3 \ypes Ss ho Kotahon L} Rotahon L-l Rotation - Cost 4: Deletin from ugh sublace - Gpp _« Ro_ Ratatrons i) BF (node) = +2 and rey =0 > aol fe LL Rotalon xR Retahon :- Sy Br(node) = 42 8 Br (nede-3 lett chal =~ => Gimilay to LL Rotation . xR Rotation :- 54 Br (node) 242 8 BF (node fark Ould i > Birvilax 40 LR Rotakion Ro Rotation : —_ Fhet © be moot ol left sublece Oo} A Cocheal't i 2 Py Rolation te d Koko! Oppel Br %, Beso. (to) | 20) ¥ o Delete 36 0) G9 G) °° i Ri Rotation TE B be_root of left subtree of _ 2 Ri_aotatton i's appked_ 4 palance factor ¢ yNedea A( cautheal node) of Bad - _R-|-potahon Let 8 be moot of lef t ox_ug ht_aublree o A Contes pe v Hy alan gaco 4 B ts -) cepode® ce feo 2 CR Rotali Rotate 4 R Rotalion oe i ay R-|-Rotaton Ae) ay oY Case 2.:- trom aia - apply L Rolahons » ee el pesnetianeagel tkielaade > vgbshidleD = Sirnilax ee ha stat tah‘on x A) Rotahen.- SY Be(node) =-2 8 BE (node => right th child) a+ op imma 4o RL Rotation - 4 4. Rotahon !: 44, Belnode)= a & BE (node 2x} tdld) = a 2 Srnlar_t Re Rotahen - tt LO Rotahon x Let B be the root of mai éubbvee aa A { exe mode) —_— Ns Cogitica mode) [Shek B be sect acght Sublaee FO B _— Ow eres: ahi rotation s_ appled. 'f Pea A uy — Ie cigs fp Node® RL _ KA Relator > het B toe oot of ~subliie o, A_Curitical node) | -7-L-l_yotahon_is_ apple / t),-BLo} Bs =I — ae NodeA Analysis} AVLTrees t >The insertion Of 0. Node te going to take -O(logn) time 1 hecause the Lee is balanced reed tt 3 \so, dhe uobalarces_in —the_inserlén process get_gixed_ - et aera acd Tumbtn_o} rotations - Sothe_enlie_proces — is 0 Uogn) time ~ 2? We can delete _ om a eat un YMede in constant Abas & oCha ; 1 . = —ftred | Taher 9000 Lime hut since the untae ae ao lake bove 4 . Ghatak prop t ~}Abeve the bee, the enlixe_dlelehion Propog i! i 0 Cogn) time gel es Relen process fakes a “Cénsheutt AVI he. rear ae igh: 1 sequent 4. ube . eae or @, $532, #6, N48 _ __ __ Snsertso Snsert 20 Snsert 6O__ _ Spsext 10_ = E of Ae ! 7 HD ° 2 : [BB 1 OO Snsext 8 ER Retake AL Rotahen(69 _ AR Rolahon Q.. Delete the gpl modes iy oe quentes in the tpl AVL tree. 2,3, lo, 1B, 4,4) My = 4S a Delel@ te ban i There are modes AVL bee, Monimuna eight “gules tHe dag 2). 2 aiid hd be, ramen nig cht excc0ced I Aly & do n : 24 Anergat_oy_Avl bre sh, maanemuns nos of nodes Can de oMtl a 2 a neimober + of, nodes tra _dree_with Lreigts! 1 tepyesented a8 Be Tv nn 4Nth-2) for n7o whee Nlo)cd 8 NU)-9 3 > The complerilf “h segs insevling & deletion 2 é D 04, . 7 a —O. at atte — height a an — ties wilh Frei! - Resume thar the Arwight Of 4 bee with a single nok iso Sela, For finding maximum heughls the nodes Ahould be @ minut al cach level a Assuming height as, minimus Yo. mes equi Noch) O NCh- + NCh-2) +4 : NO) = NOD + NCol+4 2 Q4el+-4 _ dt means, heeght 2 '¢ Aeheved ustn minimurs 4 ned. Assummg —hetghi as_3, mintmum nuns Nh) = NCh-) +NCh-2De db N(3) = N(QVENQD t] 2 4424) 23. ———, Sb means, shea ee 0) Nodes Ae. 3 ts _acheeved wen mini mum tno, Using 4 modes, We can achive maximum haighba: @. Bind ne wetrimume Ae(ghh of the Avi Ina wi Pra a - : OF dhe AVL ade with : Sen Ne Arave, Sh then AVL [ade with J nodes ht of Avi ‘ . —ne: of modes = aha : A bee_is hy maximum ght yp oy —— = _ghtl 3 - aa. 8 4 = a 4 h-2 Topological Soriing —Ai* at Pa petogral Ling fora Grrecled Acy dee. aph (DAs? isa Inear_ovdering “of vertites auch that for st deed edgeCu,v)_, Vertex u Comes before Yi dhe Ordering . 2Aopetogical sorting fora graph ts not possible if the gtaph _ts_not_q DAG: a4 ; q at - £ , seg @ A oe WR ee Topolegre al sorting — ayb- — - —— a There _can_ bg_emtore_Inan_ons._sjalegecal_20.xting. fx-a Graph ——.— 2.9. _ — — gs 3 Ey Topelegiesl sesiing 7" OF b ach @ @ ABCDE oa ACBDE. x x “ Algontiro Jor Leal ST cal Chan's Aborto) Topelegical_sork. C po 4 store each verter's tndegree in an_anaay~ — Sittalize a queue with vertices shaving thdeguee 201° __ophile there are_verhies nemaraing tthe g neue © Dequene _and__ofp a vevteq - —————______ Decaemen|__ Indegs ce of. all_vestives —cadyacer US ib by Enq uene atl they-any of these verkites eohose (ndegaee became zero 3 Assay ie Queue Dequene @ 7 -. Decrement Indegsre 4 bac by 4 adjacent ha) Enquene bec —— ir feled el! ofp? a,b 1¢ “— k_andhen¢.- a Assay polpoe Queue Dequeue & Pecnement_' degree el ale | f= a ale | enqueea td Enquewe_b- an . ; polp» ad Dequene Gd & decrement Indegree of veslices cosh y 4 alo | Enqueue te, re] ide) - aoe 44.3¢——— ts q ae oat ts Dequeue b, Dequeue ¢, | ae 7 Time _Gomplezity_of —Sopohgtcal_Sor4 = —______ ____ “Tire Gaopeity of epeheal_ sorting is OLY), where _ Nc Verhiees, E2 Edges - : {ofind oul dhe dine complexili a algorithm» IeLe — try to_ffnd te _tirae_eompleifiol_ead shes» —____ 2 —~{o_determine the _tndegree_of eath_veatex, we have to_itevale. gh te ete Te 0 Aina complexily OL > Next, to _lbok_4pr_vestices_with zero _indeqaee.. This_woill_aequere a mcd apeal he ate that eee the _indeg see_of — each node The sige of the ts equal to ¥: So the Mime complexity of thes_sters 2 eho. OW) 5 Next, for cach bE" with an_ind ef 1D, Ue Fermsve_ ee tot detterserst_the _indegtee of those Weshices _Adyarent_ts d_by 4. Im the antive gun a ths algpnithe fhe mo- ol time, woe have to_pesform docs ement _____epiration ts equal 40th, me.of edges. Sot will Jake OLE) tine - > WMrenever_we decsement___4he ind, anode, we check _ Ane_Indegree °F the node ieee oat or_mot- Sy _ zere we ada_it te the queue- |, the enbire sun pee ih aan oteur af noax (yal) Hoes + ale ae > S addinggp. all the thdivedud Aun “hime 1 the lime Complex topological cort ve OCy+e) has ithe Hesh i case oh best wont cace _ tire _com plumilj ot iq7—Since_t0e ermg__the Bore _steps, aagandless af uw athe. tee are erganized- ree | ares eee fo Store the —_ le Mndegnes _o! ices__Thes usill_nequcre OLY) spay hices “oil “indegaee ° dove the New —- > a ewe or clack) » (nthe me a Hf na oly.) SPs | n ney wirin will lone all Ver “ilove all Te in Sorted Ord, > Finally we need to st tn an ansay requiring O(Y) space + Adding all three, we. arra ve. ts OY) Apbleation s of, _Tepelogscal Sorting _ > Sehealing obs Siem the qiven dependencies Str i the order of Coropilahen tasks na Comphin at the Space Comp leat 9 Deler min 3 Insruchon seheiling pe ab i 3 Data Serialization PY _ a a Grays - ah A —giaph_is a “dala aati “that comussh of Alewina 2 tomponenk -_ bates = A 4inilé eek 4 saves breealls nodes) _ ps4 cme seb of P onder ed ara of 2th jem (uyDaulled a as 7 Re Ettabhors ma a — D ddyaeny Matwx 2 x -— 2) Adjocency Lt - ra dia ceric i —Adyseanig wratrt i A 2p array Of size YxY, Usher ~ Vis the now of vertices in aquaph . Phe the 20 Oanag be adCILI, a slot ads CeI Ce. | eae that there an tdge. Gore Vertex ¢ bj: Ned malar o undivecled aya ph xs alesages me rad malwi2 is alco used te sain a Ht y - oh, adj ted 2 US, then there is an edge {rom eget __—-verler j__with weight Ws — genta = ng AaBter Ty NS gs gn ————£4 + —Space._complerity — _olv?) myer tas = 7 Time Compleat —— Com pa ten 4 -|]_meighbou ve of verter takes lv) time .! Time tom plenity 2 Dly2)* Adjaceney list Represenbabion 3 An_array of ( £ set > The_sig. the array it equal _todhe_no- of veshues : _ het the _amay be ALI. An_enby ALE] acpresenti_the list erties adyacent to" verter. > This Datesin' also_be ured te gepresent a_toeighted — i GAaph : 2 the weights of edger can be represented asi gf pairs LT Tali lt ° : THE Dzau 2 tte 3 4 7 Ee aul) she mul) _ —____34 mis the no. of Yerlices ign jet dee Abe ne: of edges ane te dime com plexi 9} bea ding ssudh_a_ [isk ——Bpace_Corapleritn O(mpm) ~ Quaph travels ——— Des 1 Depth. firs! Sarch)._____——— _ __ Brs_( cadlth_ peas eseanch) - | be FS Breadth Depth feast search (_D a algorthro se do search all the verte, > $f is a_recursive a _Liee ova giaph~ > A DFS implemen! Hon puts a one_o| the two Categories : 4 Visited fet ath Vertex % the gap lnk Ye Nok Watked ss pt yet! the pes algoattne gorks 08 | ollows + = _ 4. Staali oy J any one of dhe graphs Verhices on __ topoka aback and Make A visited. Onda iti neirbors lich 2. gale be top iters of the stack ob add it te the visited it 8 Createalist of that Verkex's adjacent nodes. Add the _ ~————ones_which_asen't in dhe visited Lis! Jo the top % the | Shack - = Aw Ketp_nepeating ship 283 until _ Shack is emply a ‘We ctasl fom velox 0. The algorinn. sles by pala —— Wi the wicked leh 8 pulle Nl ite adi p Tin ee pu 7% al “ adjacen!_-verhees on “Visiked ie I Ty | be ee Page we EP - Next we viet! the element the tap of, clack 4g i & to its_adyacet modes. Since_O tras Alneady Wesited, we vid? felt 11) wited > ; fer stack a Vester 2 Sas an _unyicited verter (adjacent) 4, 0 we ____044 that to the top eo} the stack. & v's (babs PELL waited yor 4. | 4\3 l Stack - Vey Next we wet the elewent at the dob stack 4. A_go to ie dpa cent modes + No_adfacen) modes jor 4. Visited Po |r 2|4l Stack [s_| Neat we yicit- 3, Ct doernct have any Unvisited adjacent mode, _$0-122 haus tonapliel Te fh asl faaveric { of Ne. soph: Stack sempty Algorithms ; Given an_undirected graph (divectec G2 We) with _ om Nerhies and an array visited (1_inthally. set 400 Foy ary. node to wvietled (]_=4 if, ( has already. been visited.” the graph G1 _&_amay wisted CS are global —____hel_$ be a _shaék_of —unvisite d~verhives « initially ene pty - § Algorithro BFT (e1,n) i gore. to _n__do. ~ nisited Ci] 20 for tz4 ton do tL Cvieiked Cc12 ©) then Drs Ci); 4s pFsCv2 | Algorithon : - —_.._ | — 1 a _ Ts? uy : _ ee \ ——} visited CvI 243 NY ~ I __repeal a Fh fer all Vevhiteswad jacerd Som uw de a —e - $ ~ ~ I : ip (Visited Lud: ©) then _ push_uws to S, Ss — - Wetec 3 — ti ew ts Pe — — _ I, § ts empty th then Tere - | us Pop Ses 1 [ud 237 Sunt] Gols ae a ai atoctab ty. Com Jexily, Anal Gime. Gommplexite — 1? Lek _V be the noo) verhiees._ e “bed : ee 0 belle ms re hedge ? 6h the “Raph & nepresented as ad ‘acer: st + * Here each node ‘maintains a st ob “all it. ade racerd < ed —— 1 ¥fov each node, we discover all it snergbbouss _ - traversing its ads Odvacney Uist Jast Once. im linear S Goce: x fora directed oe feud) sum of, cera “oh. the ad jacenuyy liste of al) ye nodes ts E- 3 the Aime covoplenity- fe Ws case care ( OCW) +00) - oles), ¥ On _undivecled spaph, each edge appears” fue —The lime cons lexityy tow thes Case 1S Oly) + 0 (a6) Visited fr) thas t» be ab sek nm. id Palde ts OL Ve e) - _ mo —t ss Sh he Gro pare & Aepretonted os an - adouenegy ratte — Ca Vx amay) + - x Poy each mods, we wall fave to travesse an sie Row ee Mee Vit the mala to déeover all ii oul “ wat com plexity (¢ Oyxv) = ov) es —_——|_ Space Comperily —. - 2 Sinte_rwe_ave_mainkaining 4 Stack do Keep track of the _ fast sristed mode, fn toons case, the stack tould take upto _ the sge the Mode in the graph > Hence the space complexile arr: en en See aes Dept Rabel SABE ECD Brs_- AsepeEr- "Recun¥ive ‘Version 24 Algoacihrn pes(v) Algorithm pest) —_ a ____atiéted Ev ed a a 4) 4 tree A 4 oe iH at ee ee ae cas ______, Applieations_o) DFS. — ectia as — D_Detechn cyele tn a Haph - Path fending es 3) Topological Sorting bipoatt ee A) Jo tes! th a@ graph ts bipartite » —— aieh i o ng Been Gnnected_ Components rh agape : 6) Solv _puczl’s worthonly aneSoluttova -— ——-___ | Bread ves Sails (B69). : —— > do Bes we etast al verler_¥and mark it as visite, D> The vestex__y_ts_at this time Said 40 be unexplored. — 2A _vertex_ic gard 4 rave been explored. by-an alg. »urben i alg hos visited all verhtes Adeer oe 2 dllunvesited verties acljacent Tron ave visited next. lp ‘hese dxe_mew_unexplored vertices a —_? Yertex_v tras mow been explored = a ___2 the nevsly Vesited tev — aven't been__explored_and =i ———,—a7e_put onto the endl q_ lect of unexplored vyesdices, 3 The Sinsk vertex on this_lést__is the next to be explored. — 2 Raplration continues untel no —Unexplored vertex ic let ——>.hist_o unexplored vertices Sperakes At a queue - - ——-BF8example tel _ Saitially ~ TUTTE vereg on gy pT 1 et i We stark_foom mesh 0, the BF ala, chap a \ 4 7—"e BR ala. gt, ing —tlin the visited bist ang Oddy 6 atic te paling aut Uatcent vertices |g - === oe vealed SS ee GT Vesit a fyxst “neighbour of 0, 4 anil add ib adjacent ___—“hesghhons to _ueus —Nelghbo no}, 4 O44, — Ovistted _— felt] LL I vévcted nk — _—___—|_ gill Rls LI Gueue - 7 Visit Vertex 2 Gnd put ile neighbor 4 onls queue. a}ilz Visite Tele) J Guene Yoil_3 4:2, far mo “Unvinited ne tafabors plz [1 vated fal LI | ] Bf Quene - Niet 4 o] 7 jz ls | Visited OH TLE TT ae ee treadth — 0} Gr te Carried out begin = mode _¢ visited (e724, ming at vertex for_any—tnvt i tyos —alneady bes | vited. The graph Gr-and_anraste haa bal, ated £1 an imaihalcged A 4072 Os a Queue ob unexplored. verhias | Dilgorithom ara (4) tea tom —Ge— ‘ o vieled LEJz0- or €74 ton do iy ( vinted (éj]} 20) then BrsCé): a Algorithms Brs(v) i us v; Visited WwI= 45 — yepeak ¢ Aor ll Verbices ua adyacer’ foro ude : iy (vetted [u5\ 20) then : i Add us_[.4 viu'ted Tus J ai; S ___& it. 4 1s erply then relay * Delete_the next element, u ORD . 3 until alse.> pe"4- t . Comply Anal iso Bes ame as DRS: > Shudy PFS andlysis - Aa jaceney ag s a - Time. Complerity..€ 0 Cvse) Aa jaceney matrix Fime Complexity > ov?) > Space Com plexity t- Using. q queue, Worst cases size fi of He Queue might be VY .! Space complenity 2 0.1). rf = __ Mpplications 6] BES ——— a oe Shentesd ae ee D Finding the Chortest Pally c@ Minimum Sanning, Tree. — e Unweigpted graph - _.. DFeer to Veer Networkes 47 Peer do Ter nlws like Bil Terter) & BFS 1s Used to Jind all neighbors 3) Crawlers m_seanth oni rie Be Crauders build ides Using Bis 4) Social Networking Websites :. In social Mlse , vse Can tind — pes ple within iven_disshana“K’ Srom a person warns pre nk eves > vO ____ 5) GPs Navigahion sing Brs_b used to find all netghbaxing | _focahons gd 9 Broad caching fy hos + Cle pts afte bo — Pree a | PRS - BFS_traverces according t6_|_4 DFS traverses according f _____ Mite fevel ____——piy, | tree depth ae a. BFS uses Quewe_h find harks | 2-__Dee user stack |s find s-P- 3. Here Siblings are_viei ked_ —“}g. Mere chddien are visited odors the childeren _ __ before seblings ——___— ____ 4:_BPS is belle» usher targel te 4 DFS tc _bether when fargel is Jar Sram _cource - __ B: PFS is more suitable Su game or puck problems: Wk make a dearion , tren explore all palhs dhrough dhs decision -sdnd thee deenton leads ls Worn stualen 7 we stop . ee eve rs no back halig 6 Se PRs user backliacking - {_7- Des is pate = __elesey to courte - 5. As BFC eonsides all neighows | $0 ib ig net cuttable fox deation trees used tn puzel ———.— Games ___— 4 _ ds tng Gpaph an SCO 9 | Bhonaly Connected Components of a _Diaecked —Craaph » te rard fo be @ sliongly conned, olivected . grap) aeachable. qyom evey oh, cy evter 1S _| gor { pond exe bin everyaill par of verhices (50 enters egs- by n > 8 onaly connected gear, 2 eg: ge -» Not strongly connethed Gren, >A chong Connected component (¢ the pottien | a_dirvected “graph '9 shed there {so bath row _2ath verter “to another verter - (6 cc) > 2 te apleableengortenneltd stp lohat are 8G This Graph t& mokan St because ue there te Mo path exiclS bln 382, 3 80,384 also__blyn 484, 4&5, 486, 743, $2 745d 4 AO: There aye 4 &CCs - SCs — 0,12 seta > 8 SUZ 94,56 Sly >t ee - _ __ | __——Note'-_pll_the nodes _im_a componen!. ane Always —sbsngly ____tomnected ty On _Undirecked gf 4p ___________ GA th can serolo ts (0) eat = at _Note:- On reversing _all the _ edges 4 the graph, the type — + the_gsaph_won't change gt Sone i “it tate, then _Aaversing edges leo_reakes the gsapb_tuans tobe SCG. S}_the graph ts_mnotan_cor,_then Aeversed 91% ph alfo__wond bean StG- “The SCCs in the gsaph ob the _8¢Cs inthe _ Sevesed ararh algo_aumains dayne - eg. . Reverse, (a reed 3 dt ts an SCH Nolan SCOr : Mt we dake. the tsans se 4 a Graph» ccuatll — _ptmam as SCC Direct ion__bla_Comaponents Gre aeversed + This proper: “aan be _tuced _ln_detect Secs» ee GB — ES) ! kosangi's _ Alpert "0 Paferm prs Uavasel of before reluan! 9) Find the leans) 5 Top modes th prs on the tes One at Example () ne Ko be _to Sind _StCs__ (raph: Tash nodes fo Hack ng” ore graph fy aeversing the edges « by one Par Slack Gnd again do medifred graph: fath succesdul Des ron gly. connected Component - O—® daitially stack ‘s emply . Visited Ut fe get fe falee Stack__ v ] LT ] Fie = aceon o 12 s 6 Fo "sited Flee) F wWe start From node _O- a Nisited 2 [T Gi F FlelelF & Node o will call mode a. -.Misited = [rit] elelr Node 4 wilh calledynede 2) _ waited = COOTT Te TTT) - Node 2 will ¢a\ 3 Oy wlth is visited, then call” voita> Ir[rrir le FT elF ola 2 64 Nede 3 will Nisiled = hike thes FA wt!ll y# wil) call ay @) , 4 alasady Vis led call APs (4 Call abs Cs) S will call “ds (e) 3 ee $< o 1.2 et Wisited = ered 7) — From __4_thert_are— no__oulgoing edges 4¥_Sa_the 0 palin oq omits 6. When we are Aeturniing —b — is pushed on_te__Stark_, ___— ioe TE shack [| ~ From 6 2the Oulgoing. edg es On th y Which are_already ited. 30 push 6 nd aeturo back tos _—_—_} State [a /6 2 =~ uth o6- From 5,6 is alveady vistked , Se go back 404» Prorn_ Push—&- a Proceeding like thes_ad the end_of the backbrackins Stack z fs Je|¢ 4[3]2/112] 7 _ We have __ completed step 4 - _Nexd glep te © to find the —bransposed _ graph } CEG { Ned toc__proceed fo 374 sep + Keep visited array [6 False Pop # 0 a per forma_D FS: — Stack = 13] 6] slalalal ; | — ol - 1 °° °»+w&t o2* 3 al | “voited = PPeLele [eles 1% sec +931 + Popa fron“ stack» _Snce_A te already visited we _won'l call ps C+ Pop. 2, 2is also__vyisited So Aya) is __mot inveked_- - - call dfs (3) 4 i — Pre Ngee 2 Dales T Stack = tl ele 14 Ae ae 0 2 4 ea — lo do tat % visited = Z| y v\rlF Fel a TIT - I. &. - Foro 3, there {8 Only one “ oulgoin edge bul 2 ts heady, bands Therefore 86 (2) fs not A oS _ Pop 4 4 apes the Same procegs - Fe ol 2 ae CS ae ~_[avietbedr = (PL? ele 1 vetea = [a Tele le PTR] Re ees | Pop 9, 6 then #- a - Finally we get Stack emply re | Sly a te —_ ba aie biti Foun — tong connected ferapantnt 960) Dy} 20 ns et J _ | Slt sg pe a - SCs says bh aN! - : SUly 2 3 4 —t — { Time Com plexi I fe The. above aly calls DF: DRS, finds “novos! frae__Com pleati fi the graph _@. agatn call DFS - _ OFS tats o(vte) ‘ “tora graph aeptescnted ning. ———Aajateney tnd evasing & graph < also tales O(YpE) time. —-j Plexe! iq 2 Ole) Applications 44 SCs + 3 Used__lp" solve _2- Sadesprability problemas. 9 Used_in social miwing sites, 1 tidepeed tne group of peap zoe ate an pent ob ov 1oho_have ony | jn nen — (ypkerest + > Used to convert _q gsaph ints_a Ditech A cyelie giaph eh Shonglyy connected componen |i

You might also like