1
En kisa yollarin bulunmasi
Verilen bir undirected (yonsuz) graf ve kaynak vertex s, bir yol (path) in uzunlugu bu yol uzerindeki edge (kenar) lerin sayisidir. Amac, grafta s den diger vertex lere olan en kisa yollari bulmak
2
Breadth-First-Search (BFS)
Verilen:– G = (V, E) – Belli bir source vertex
Sistematik olarak s den erisilebilinen her vertexi bulmak icin G nin edge lerini inceler– S den erisilebilen vertex lerin s ye olan en kisa
yollarini hesaplar– Root u s olan ve butun erisilebilen vertex leri iceren
breadth-first-tree uretir
3
Breadth-First-Search (BFS)
BFS colors each vertex:white -- kesfedilmemis (undiscovered)
gray -- kesfedilmis fakat “hala bitirilmemis”
black -- bitisik tum vertex leri kesfedilmis
6
BFS for Shortest Paths
Finished Discovered Undiscovered
S11
1S2
2
2
2
22
S
3
3 3
3
3
7
BFS(G,s)1. for each vertex u in (V[G] \ {s})2 do color[u] white3 d[u] 4 [u] nil5 color[s] gray6 d[s] 07 [s] nil8 Q 9 enqueue(Q,s)10 while Q 11 do u dequeue(Q)12 for each v in Adj[u]13 do if color[v] = white14 then color[v] gray15 d[v] d[u] + 116 [v] u17 enqueue(Q,v)18 color[u] black
white: undiscoveredgray: discoveredblack: finished
Q: a queue of discovered verticescolor[v]: color of vd[v]: distance from s to v[v]: predecessor of v
16
Operations of BFS on a Graph
17
Breadth-First Tree
Graf G = (V, E) ve kaynak vertex s icin, G nin predecessor subgraph i G = (V , E) – V ={vV : [v] NIL}
– E ={([v],v)E : v V - {s}}
G nin subgraph i breadth-first tree dir eger– V s den erisilebilinen vertex lerden (vertices)
olusuyorsa– Her vV icin, G de s den v ye tek bir yolsa (ayni
zamanda bu yol en kisa yolsa) E deki edge ler tree edges olarak adlandirilir.
|E | = |V | - 1
18
Breadth-First Tree
Verilen bir graf icin bir cok BFS tree bulunabilir.– Search in hangi vertex den basladigina ve
kuyruga vertex ler hangi sirada yerlestirildigine bagli olarak
BFS tree nin edge lerine tree edges G nin geri kalan edge lerine de cross edges denir.
19
Analysis of BFS
Initialization O(V).
Traversal Loop– Her bir vertex en fazla bir kez kuyruga itilir ve
kuyruktan cekilir, ve her bir islem O(1) zaman alir. Dolayisiyle toplam zaman O(V).
– Her bir vertex in adjacency list en fazla bir kez taranir. Adjacency liste lerin boylarinin toplami (E).
BFS nin calisma zamani O(V+E).
20
Shortest Paths
Shortest-Path distance (s, v) s den v ye minimum sayida edge sahip yolun uzunlugu, eger boyle bir yol yoksa
21
Depth-First-Search (DFS)
En son kesfedilen vertex v den itibaren edge leri incele
Mumkun oldugunca derinlige in“Search as deep as possible first”
22
Depth-First Trees
Boyama teknigi BFS dekine benzer. DFS nin predecessor subgraph i G = (V , E), burada E
={([v],v) : v V ve [v] NIL}. G nin predecessor subgraph i bir kac depth-first trees iceren bir depth-first forest olusturur. E deki edge ler tree edges olarak adlandirilir.
Her bir vertex u 2 timestamps e sahip: d[u] u ilk olarak discover edildigi zamani kaydeder (grayed) ve f[u] search in bitis zamanini (blackens) kaydeder. Her bir vertex u, d[u] < f[u].
23
DFS(G)
1. for each vertex u V[G]
2. do color[u] WHITE
3. [u] NIL
4. time 0
5. for each vertex u V[G]
6. do if color[v] = WHITE
7. then DFS-Visit(v)
24
DFS-Visit(u)
1. color[u] GRAY White vertex u discover edildi2. d[u] ++time3. for each vertex v Adj[u]4. do if color[v] = WHITE5. then [v] u6. DFS-Visit(v)7. color[u] BLACK Blacken u; it is finished.8. f[u] time++
25
Operations of DFS
26
Analysis of DFS
1-2 & 5-7 satirlarindaki loop lar (V) zaman alir (DFS-Visit i saymazsak).
DFS-visit her bir white vertex vV ilk olarak gray e boyandiginda bir kez cagrilir. DFS-Visit deki 3-6 line lari |Adj[v]| kadar execute edilir. DFS-Visit in toplam calisma suresi vV|Adj[v]| = (E)
DFS nin toplam calisma suresi (V+E).