/3/0 Prvw Grph Grph Rprsntton Grph Srch Algorthms Brdth Frst Srch Corrctnss of BFS Dpth Frst Srch Mnmum Spnnng Tr Kruskl s lgorthm Grph Drctd grph (or dgrph) G = (V, E) V: St of vrt (nod) E: St of dgs (ordrd vrt pr) Undrctd grph G = (V, E) V: St of vrt E: St of dgs (unordrd vrt pr) Grph Grph A B E A B E C D F C D F Dgrph G = (V, E) Undrctd Grph G = (V, E) V = A, B, C, D, E, F V = A, B, C, D, E, F E = (A, B), (C, A), (B, D), (B, C), (C, D), (D, C), (B, B) (F, E) E = (A, B), (A, C), (B, D), (B, C), (E, F) 3 Grph Rprsntton Grph Rprsntton Two stndrd wy to rprsnt grph. Adjcnt Lst Sprs grph. Adjcnt Mtr Dns Grph 3 3 3 Adjcnt Lst 3 3 0 0 0 0 0 0 0 0 0 0 0 Adjcnt Mtr Adjcncy lst If G s drctd grph, th sum of lngths of djcncy lst s E. If G s undrctd grph, th sum of lngths of ll th djcncy lst s E. Wghtd grph s grph for whch ch dg hs n ssoctd wght. Adjcnt lsts cn rdly b dptd to rprsnt wghtd grphs by sv mor nformton for ch nod. A dsdvntg Thr s no quckr wy to dtrmn f gvn dg (u, v) s prsnt n th grph thn srch for v n th djcncy lst A[u].
/3/0 Grph Rprsntton Grph Rprsntton Adjcncy Mtr Th djcncy-mtr rprsntton of grph G conssts of V V mtr A = (, j ) such tht, j f (, j) E 0 othrws Th djcncy mtr rprsntton cn b usd for wghtd grphs. If grph s unwghtd, th djcncy mtr uss only on bt pr ntry. 3 3 3 3 3 Adjcnt Lst 3 0 0 0 0 0 0 0 0 0 0 0 Adjcnt Mtr Grph Rprsntton 3 Grph Rprsntton Wghtd dgrph or grph cn b rprsntd wth djcnt mtr 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 0 3 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 Brdth-Frst Srch Brdth-Frst Srch From sourc nod s n grph G = (V, E), BFS plors th dgs of G vry vrt tht s rchbl from s. It clcult shortst dstnc from s to ny vrt u. BFS d s usd n Djkstr s sngl sourc shortst pth lgorthm nd Prm s mnmum spnnng tr lgorthm. Brdth-Frst srch Th lgorthm dscovrs ll vrtcs t dstnc k from s bfor dscovrng ny vrtcs t dstnc k +. Ech nod hs thr lmnts: color, dstnc nd prnt.. Color s usd for plorng grph,. Dstnc s usd for dnotng th hght from s n brdth-frst srch tr nd 3. Prnt s usd for kp th brdth-frst tr.
/3/0 Brdth-Frst Srch Brdth-Frst Srch (G, s) for ch vrt u V(G) s u.color = wht u.dstnc = u.prnt = Nl s.color = gry s.dstnc = 0 s.prnt = Nl EnQuu (Q, s) whl not mpty(q) u = Top(Q) for ch v djcnt to u f v.color = wht thn v.color = gry v.prnt = u v.dstnc = u.dstnc + ; EnQuu(Q, v) DQuu(Q) u.color = blck Th runnng tm of BFS Intlzton O(V) Ech dgs must plor on tm for BFS O(E) Totl: O(V+ E) 3 Brdth-Frst Srch 0 s Quu Brdth-Frst Srch Brdth-Frst Srch 0 0 s w s w r Quu Quu Brdth-Frst Srch Brdth-Frst Srch 0 0 w r w r t Quu Quu 3
/3/0 Brdth-Frst Srch Brdth-Frst Srch 0 0 w r t r t Quu Quu 0 Brdth-Frst Srch Brdth-Frst Srch 0 0 r t v t v Quu Quu Brdth-Frst Srch Brdth-Frst Srch 0 3 0 3 t v u v u Quu Quu 3
/3/0 Brdth-Frst Srch Brdth-Frst Srch 0 3 0 3 3 3 v u y v u y Quu Quu Brdth-Frst Srch Brdth-Frst Srch 0 3 0 3 3 3 u y y Quu Quu Brdth-Frst Srch Corrctnss of Brdth-Frst Srch Lts s, v V n G = (V, E) 0 3 (s, v): shortst pth from s to v s th mnmum numbr of dgs n ny possbl pth from s to v. 3 BFS crt shortst pth from vrt s to ny vrt v f thr s pth btwn s nd v Quu 30
/3/0 Dpth Frst Srch In dpth-frst srch strtgy s to srch dpr n th grph whnvr possbl. In dpth-frst srch, dgs r plord out of th most rcntly dscovrd vrt v tht stll hs unplord dgs lvng t. Whn ll of v s dgs hv bn plord, th srch bcktrcks to plor dgs lvng th vrt from whch v ws dscovrd Dpth Frst Srch Ech vrt hs four lmnts: color, prnt nd tmstmps (vst_tm nd fnsh_tm).. Color s usd for plorng th grph.. v.vst_tm rcords whn nod v s dscovrd(nd gry). 3. v.fnsh_tm rcords whn th srch fnshs mnng v s djcncy lst.. Prnt s usd for kp th brdth-frst tr or forst. Ths tmstmps r usd n mny grph lgorthms nd r hlpful n rsonng bout th bhvor of dpth-frst srch. Runnng tm : (V + E) 3 3 Dpth Frst Srch Dpth Frst Srch Dpth_Frst_Srch(G) for ch vrt u V(G) u.color = wht u.prnt = nl tm = 0 for ch vrt u V(G) f u.color = wht Dpth_Frst_Vst(u) Dpth_Frst_Vst(u) u.color = gry u.vst_tm = tm + for ch v djcnt u f v.color == wht v.prnt = u Dpth_Frst_Vst(v) u.color = blck u.fnsh_tm = tm + / 33 3 Dpth Frst Srch Dpth Frst Srch / / / / 3/ 3 3
/3/0 Dpth Frst Srch Dpth Frst Srch / / / / / 3/ / 3/ 3 3 Dpth Frst Srch Dpth Frst Srch / / / / / 3/ / 3/ 3 0 Dpth Frst Srch Dpth Frst Srch / / / / / 3/ / 3/
/3/0 Dpth Frst Srch Dpth Frst Srch / / / / / / / 3/ / 3/ 3 Dpth Frst Srch Dpth Frst Srch / / / / / / / 3/ 0/ / 3/ 0/ Dpth Frst Srch / / / / 3/ 0/ Input: connctd undrctd grph G = (V, E) whr for ch dg (u, v) E, w hv wght w(u, v) Output: Fnd n cyclc subst T E tht conncts ll of th vrtcs nd whos totl wght s mnmzd.
/3/0 Mnmum Spnnng Tr us grdy lgorthm Grdy lgorthm lwys mks th choc tht looks bst t th momnt. It mks loclly optml choc n th hop tht ths choc wll ld to globlly optml soluton. Grdy lgorthms do not lwys ylds optml solutons. Kruskl s lgorthm Kruskl s lgorthm s grdy lgorthm, bcus t ch stp t dds to th forst n dgs of lst possbl wght. 0 Kruskl (G, w) A = Strt wth forst whr thr r V trs Sort th dgs of E by non-dcrsng wght w. for ch dg (u, v) E, n ordr by ncrsng wght f u nd v r not blongs to sm tr A = A (u, v) Mk two tr to connct wth dg (u, v). 0 0 0 3
/3/0 0 0 0 0 0 0 0 0
/3/0 0 0 0 0 3 0 0
/3/0 0 0 0 0 0 0 0
/3/0 0 0 3 0 0 0 0 3