graphs in datastructures, Study notes of Data Structures and Algorithms

to help u study graphs using data structures and linked lists

Typology: Study notes

2024/2025

Available from 11/03/2025

ansh-sharma-12
ansh-sharma-12 🇮🇳

1 document

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download graphs in datastructures and more Study notes Data Structures and Algorithms in PDF only on Docsity!

fx oO es Grsy aly f\ Gtaph Us a Callecliey of PS \V ancl E show Visa fone MEN- anply Sut Ge Verhoos LF Ge Suns No émply a] of, colges, Verhios “ars adhe Nelo, wn thos floph . “Top aol facent- VWorlicos A%o Sotmd by ekgos, Poup (a gaph @ < toneted eS (= § Vy I 4 ABxc — Ons ioe] £ 3,€, £5 ETP G=) S UV) Va Vay wy, QEEVE WES EY if Types Graph — Tao Tyres — | — are sated Grsaphs © Dax Graphs © aa thu —ctuachoes © rf net, kid ark a o calbad eee th isle fas hex? pours St - Got r a ie (viva) vo Vi He * deroctel ion othe Sire o, He edge + 8b tts Gap lizdlcon hs detichon. artiow heals WN gogroi of OAC Hiaue ‘s oy, ex" - ; iG ; ~ O——- ¢ Mey . ; 54 Ware 9h WY ; : F(G)< ? Che) 125) 96,02 d, 6D, V4 ‘ P sl seat fo tndoctid Graph — Of Le par of, wotices AF—. @ uneidsrwel they Geph G & @llled en Undriecti | Gxph , Thak = Ineans up thaw ww Vand VW thon uk Gn be Oopresentd a3 (VU, VY) er Ca v9 also: a” a VOad= $ 2,34 & E(q) => Cad 9 ar (2, EY § (® Saqpolial Representation cP) Adfacrey Ci?) Anetdonc, ©) liked Repaczentation (D Set _Reprwerteion — TIT tw gapresentatio . Q Kt Ate cnaertineal en Cty ea % edgar FE, shin i tM Subst dy VV. y VG) = FBG _. an EG) = § Chie? , (2,3), (nds ONDF gaap!s can be al Ropsasentation — Ccpanholl Repsvsen ; ©) sae maby Ww apart Pexcpresenlel Cerunen matx —_ CHP) Garelonce matr®, ity fa csseights d raph — (4) tS 1 . Ph > A B® CD _ A\ O a Oo ¢ B®} 4 Oo 4 2 Clo fo) oO Lt Dl o o Q& 90 bith matarx,— bet G bea Sinpla dnrbchid Fiaph vath yy _berhios Vy... . An man mote Pe [Pee J Aaptnod Qs fyelLouss — Prp = % dow Ga poh from Vpetv Ue { ie) oUcrusrsp ©) |thked — Roprasertebion 1 — 08 Adjaconey kat Repisrlden CD fox _Non= ustightid —Goaghe — ore Cif) Fex_weeiphled _Goagh ~ sane: cee — us ust we fla odfaaney Wat he, de ciate G Sat . Graphs, the mumbo. ae Lid ep ea tag Uuaktnds ua de ging. nede UA each Lest mountains a beat ef, The “hander os of et eda fer ushide cls Consider & Graph — (a) Adjacency. List Roprescrilahon Of; undclisctid Goeaph— tod Implementation of a Crraph— =) ao are useighka Graph — dlobeno max 30 gees Struct neele an worl § Shuck neck Hrnoxt 5 4h mec 15 meds{ *adfLmax]'5 3 Por_weighld Gaph — “gece Shc ned “- water '5 ite weeight' Stich neodle ¥ next 5 % nede 1, meds L jad imax, 1. Weighted Graph : A graph is said to be a weighted graph if all the edger in it are labelled with some numbers. It is shown in the Fig. 12.4 below. Fig. 12.4, 2, Self Loop : Ifthere is an edge whose starting and end ele are ae that s (Ua U9) isan edge then it is called a self loop or simply a loop. It is s on " im an vert 3. Parallel Edges : If there are more than one edge between the s vertices then they are known as parallel edges. yier 10. ii. 12. 13. 14. 15. 16. V7. 18, 19, Regular graph : A graph is regular if og. game number of nodes. every node is adjacent to the Q) Here every node is adjacent to 3 nodes. 7? Planer graph : A graph is planar if it can be : . any two edges intersecting. drawn in a plane without LN Connected graph : In a graph G, two vertices v (3) 4) . : 7 , and v, ar i 7 connected if there is a path in G from v, to v, or » to v,. Aerorhiss a R raion to be connected if there is a path from any node of orenh ‘oie sal ‘egular graph node, i.e., for every pair of distinct vertices in G, there is a ath other ®@ (a) Connected graph (b) Not connected graph Fig. 12.10. (a) Strongly Connected Graph : A directed graph is said to be strongly connected graph if for every pair of distinct vertices in G, there is a path. (vb) Weakly Connected Graph : A diagram is called weakly connected or unilaterally connected if for any pair of nodes u and v, there is a path from u to v or a path from v to u. If from the diagraph we Fig. 12.11. remove the directions and the resulting undirected graph is connected then that diagraph is weakly connected. Figure 12.11 shows a weakly connected graph. Cycle : If there is a path containing one or more edges which starts from a vertex and terminates into the same vertex then the path is called as a cycle. Acyclic graph: Ifa graph (digraph) does not have any cycle then it is called as acyclic graph. Cyclic graph : A graph that has cycles is called a cyclic graph. n(n-1) Maximum edges in graph: Inan undirected graph there can be —_ maximum edges and in a directed graph there can be n(n — 1) maximum edges. Where n is the total number of vertices in the graph. b Articulation point : If on removing a node from the graph, the graph becomes disconnected then that node is called the articulation point. Bridge : If on removing an edge from the graph, the graph becomes disconnected then that edge is called the bridge . eee F ch. Biconnected graph: A graph with no articulation points is calleda biconnected grap == Diference between cycle and Hamiltonian cycle Cycle is a closed walk through the graph with repeated vertices having the same starting and ending vertex called a cycle. Hamiltonian cycle is also a cycle having a closed walk through the graph but the vertices in the cycle are not repeated at all. In other words a cycle having all the vertices with same starting and ending vertex in which every vertex appears only once. For example - Consider Graph G. Hamiltonian cycle is A-B-D-E-C-F-A. -y Ay BDL 5 A,sw HL 4c — A,BWOF,CLF Abs. if nt BFS Hough Quarg _— Z unseat Elomont- An07 ths bie ama al @ Dds re sutee netplbetrs anil ¥ ss “as © ti Luce 8¢ chem . O eepeat Sep] wnlile Ihe qyeave SOME A exe— £ a Cc D \woahe 0 Adyaw lst a A5D p DE C D D £ - mi | {Tj Rear Peto freak “eo An Peo “Toued nade = A trem Te Fraursed =medes=Ary 350 See ® Ageun de Porass u's eneposte prec Frenl? Rea) °) Ye, Aemeue dhe CKeue “& ada tte mMegLbouz ard Vealtar , D o& He E O . te US Roady Slabs (SUTE=1) (Miub the Seedy mate A un Gveve Rchange ub Aatus _ ai ng Stati. (States = a), @ Remat Qep OO will Goeve UG empty. ‘O) Remon We Rrnil nedo N of QIVEVE . Pyrcces3 N <4 charge © the Skulus @f N go le Prrceased Sats (SATS =2) Add to tle oor _Qveve Tie meghbons | BN Mal ae wh Ale Ly Suede Cartes =1) and thor Stedas te th "Loci sdeclo ( Stntas -9)), aaa © exit Depth Pash | Secvedh_ -~CDFS) — Th DFS» we use Black, hid ‘oops 20 Lo nede rca nex Pressing, ‘ Tr Hu qiee? » ushoencuoZ_ Pexible, Gon as npudd HPaph G= CUE) and 9 Sauice Neale x S, fem cohore thee Searchurg gdarls - First ase \waer Stes Mehaggi mede , then ue each ode to usbuicl et S, Theat “ aa a path » lagers ¢ G, mel hbses agar bro ie 2 £ nh @ Noel Qa Meighbour af CXR Coo tes Alar Leeks age ob rel as wUueelf “@2 om Lunoleacle a ZeapAs . Nes Capen A te ths amok. | dingy torte ea 1 Oe OE ler onede As Ae ioe Poealt eae ott foucase. node F 5 mnpcle A. Gtuppess WE cies “Hraerse actyacns” welch B “faljacs oe st Bo xt aie (BO Neda adjacent! e baokusard . nome ets € pane * tosh end. So ue wall ynous so * pauetseal medes - AyPoFoFk he Oo ue Reach Node 5 Se a hoa, vv, onéf ehiucesd! ie goede eXacnt fo ab £ nol Neuer yok yet, Jere Wheels G C, Se cee Peuevse coh, New Hew ts nO Lntaticrscol nade aclfacont to Nede C, Go. “We ust =m eue backwwed , eaduig mede A ue Boe dhal mode PD & audliaconf lo uF L dos nel han datas. So we ctecruerse od, Nod Haas S AM wondered mele adjocent Je modo Do So use vail Step. So, dha tratersal wg — A,B, EyF5c5D Step L Push mnedo 4 fila Steck. Top=0 2 Skok= 1 Step? - for neds) from Slack & treuuasc tb, So Fraucrsed nedo=| & Noo gud, ald Le uns aoljacont retes 254 ez a am of the Popped Cllomaiten tho SPack | Tep =a xz Shad = 2,5, “Poauwrsal) = | cain 54 Sep> - Pop the Elomout (GY) -facom Stack , teersect ut gg push ath iB LWrudilecl aclticet Modes, Then 8 mo adaant Rados SO, Frewersed node = 4 Tepe! , Sdack= 3 8S “Trouwasel = 14 y = Mgeadthun — Mg ve (i) © Tribals | . Csaba — 4 “—* Oran te “ee i soadly Sdele = i ‘to ny ned, oe SO i ae fop de & ACS) until ip Sate teas) oe sp ode Nob elek 7 aes a a Process NE Rah ee ol Jo capone of on ‘is oa C a ian . | Ley 6rd vs. ura iia Depth first search Breadth first search 1. This type of search is done with This type of search is done with the a the help of stack. help of queue. | This algorithm works in two stages This algorithm works in single | - In the first stage the visited stage. - The visited vertices are | vertices are pushed onto the stack | removed from the queue and then | and later on when there is no displayed at once. | vertex further to visit, those are popped off.