Gaph pa Moiaion: Gaph fndamenal o man poblem. Web gaph. Biolog. Ohe. Connecii i newok conneced and can o ge fom one node o anohe, o wha i he hoe pah? Eample: Diing diecion; ge o one conac hogh anohe; ocial media o conac pah. Some noaion: We ll all decibe he eice (V) and edge (E) of gaph G=(V, E). We omeime alk abo eice in V, and edge e in E. We denoe he nmbe of eice and edge a n and m epeciel. (The book omeime alo e V and E o denoe nmbe of eice and edge). Two fom of epeenaion: () Adjacenc li: each ee ha a linked li o each adjacen ee (can ae in pace O(n + m); lowe lookp; good in he cae of a elaiel pae gaph) () Adjacenc Mai (ake p moe pace O(n qaed), fae lookp; good in he cae of a dene gaph) Noe: nmbe of edge can be beween O() and O(n qaed), depending on whehe gaph i pae o dene. (a) (b) (c) Fige. Two epeenaion of an ndieced gaph. (a) An ndieced gaph G wih eice and 7 edge. (b) An adjacenc-li epeenaion of G. (c) The adjacenc-mai epeenaion of G. 6 6 6 6 6 6 (a) (b) (c)
How do we find he eice eachable fom a gien ee? Two wa o each gaph: BFS; DFS Beadh Fi Seach (BFS): Find all eice diance ; hen diance ; hen diance ; ec b beadh. Like iiing accoding o leel (of diance ; hen ; hen ; ec). Leel%% S% Leel%% a% Leel%% c% b% d% Alo in ndieced gaph: Leel%% S% Leel%% a% Leel%% c% b% d% Applicaion: finding hoe pah beween eice (eg, fom oce ee o all ohe eice). Rn ime: we will ee ha fa; linea in nmbe of eice and node: O(n + m) We colo he node o keep ack of hem (in BFS, DFS): Whie: Veice ae iniiall whie;
Ga: Fi ime ee enconeed, make ga Black: Onl when all i adjacen neighbo dicoeed, n o black. Main appoach: Ue fi in fi o qee o manage he dicoeed ga eice, nil we go hogh all hei adjacenc neighbo, and he hen n black. Same fo dieced and ndieced gaph. We ll wie hi o highe leel han book: BFS(G, ) // G i a gaph and i ome oce ee - iniialize all eice o whie, and diance infini - iniiall make ee ga, diance, and Enqee(, ) - while no emp = Deqee() fo ee ee in adjacenc li of if whie // fi dicoeed; p in he back of qee make ga.d =.d + // diance.paen = Enqee(,) change ee o black // afe going hogh adjacenc
(a) (b) w (c) (d) (e) (f) (g) (h) (i) ; A he ame ime: make Beadh Fi ee. If node i dicoeed afe, hen edge (,) i added o he ee and we a ha i a pedeceo (paen) of. A ee i dicoeed a mo once. Rn ime: n =nmbe eice m = nmbe edge - Each ee enqeed and deqeed a mo once (wh? Becae nee whie again). Co of hee i O(). n = nmbe of eice. Toal oe all eice O(n). - Scan adjacenc li onl when deqeed, heefoe each adjacenc li onl a mo once. Sm of all adjacenc li depend on nmbe of edge O(m) whee m = nmbe of edge. - Toal O(n + m). Linea in nmbe of eice and edge. BFS allow o find Shoe pah: BFS dicoe hoe pah diance. Shoe pah diance fom o : dela(,) = minimm nmbe of edge fom an pah fom ee o. (infini if no pah ei)
Main heoem (we e implified fom book foma): Le G=(V,E) be a gaph. Sppoe he BFS i n on G fom gien oce. Then pon eminaion, fo each ee in V,.d comped b BFS aifie.d = dela(,) Tha i,.d i he hoe pah, hoe nmbe of edge fom oce. Main idea of poof: indcion oe diance i (node wih diance i- fom oce dicoe ee, and i diance one awa fom, and heefoe diance i fom oce).