1 / 16

En kisa yollarin bulunmasi

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. Breadth-First-Search (BFS). Verilen: G = ( V, E )

jacob
Télécharger la présentation

En kisa yollarin bulunmasi

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 sourcevertex • 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

  4. 2 3 3 2 2 3 1 S S S 2 1 1 2 2 3 3 BFS for Shortest Paths Discovered Undiscovered Finished

  5. white: undiscovered gray: discovered black: finished Q: a queue of discovered vertices color[v]: color of v d[v]: distance from s to v [v]: predecessor of v BFS(G,s) 1. for each vertex u in (V[G] \ {s}) 2 do color[u]  white 3 d[u]  4 [u]  nil 5 color[s]  gray 6 d[s]  0 7 [s]  nil 8 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] = white 14 then color[v]  gray 15 d[v]  d[u] + 1 16 [v]  u 17 enqueue(Q,v) 18 color[u]  black

  6. Operations of BFS on a Graph

  7. Breadth-First Tree • Graf G = (V, E) ve kaynak vertex s icin, G nin predecessor subgraph i G = (V , E) • V ={vV : [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 vV 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

  8. 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.

  9. 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).

  10. Shortest Paths • Shortest-Path distance (s, v) s den v ye minimum sayida edge sahip yolun uzunlugu, eger boyle bir yol yoksa 

  11. Depth-First-Search (DFS) • En son kesfedilen vertex v den itibaren edge leri incele • Mumkun oldugunca derinlige in • “Search as deep as possible first”

  12. Depth-First Trees • Boyama teknigi BFS dekine benzer. DFS nin predecessor subgraph i G = (V, E), burada E ={([v],v): v  Vve[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].

  13. 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)

  14. DFS-Visit(u) 1. color[u]  GRAY  White vertex u discover edildi 2. d[u]  ++time 3. for each vertex v  Adj[u] 4. do if color[v] = WHITE 5. then [v] u 6. DFS-Visit(v) 7. color[u]  BLACK  Blacken u; it is finished. 8. f[u] time++

  15. Operations of DFS

  16. Analysis of DFS • 1-2 & 5-7 satirlarindaki loop lar (V) zaman alir (DFS-Visit i saymazsak). • DFS-visit her bir white vertex vV 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 vV|Adj[v]| = (E) • DFS nin toplam calisma suresi(V+E).

More Related