1 / 155

Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

Datenstrukturen, Algorithmen und Programmierung 2 (DAP2). Graphalgorithmen. Single Source Shortest Path (SSSP) Startknoten s Aufgabe: Berechne kürzeste Wege von s zu allen anderen Knoten All Pairs Shortest Path (APSP) Aufgabe: Berechne kürzeste Wege zwischen allen Knotenpaaren

kenna
Télécharger la présentation

Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

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. Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

  2. Graphalgorithmen Single Source Shortest Path (SSSP) • Startknoten s • Aufgabe: Berechne kürzeste Wege von s zu allen anderen Knoten All Pairs Shortest Path (APSP) • Aufgabe: Berechne kürzeste Wege zwischen allen Knotenpaaren SSSP in ungerichteten Graphen (Breitensuche) • Graph in Adjazenzlistendarstellung • Startknoten s • Nutze Kanten von G, um alle Knoten zu finden, die von s aus erreichbar sind • Finde kürzeste Distanz (Anzahl Kanten) zu jedem anderen Knoten

  3. Graphalgorithmen Invariante (Breitensuche) • Knoten haben 3 Farben: weiß, grau und schwarz • Zu Beginn: Alle Knoten sind weiß • Ein nicht-weißer Knoten heißt „entdeckt“ • Unterscheidung grau-schwarz dient zur Steuerung des Algorithmus • Wenn (u,v)E ist und u ist schwarz, dann sind seine adjazenten Knoten grau oder schwarz • Graue Knoten können adjazente weiße Knoten haben

  4. Graphalgorithmen Beispiel (mögl. Zustand bei einer Breitensuche) s

  5. Graphalgorithmen BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz

  6. Graphalgorithmen d[u]: Abstand zu s (zu Beginn ) p[u]: Vaterknoten von u (zu Beginn nil) BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz

  7. Graphalgorithmen • Für jeden Knoten u: • color[u] = weiß • d[u] =  • p[u] = nil BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz

  8. Graphalgorithmen • Für jeden Knoten u: • color[u] = weiß • d[u] =  • p[u] = nil BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz • Für Knoten s: • color[s] = grau • d[s]=0 • p[s]=nil • s wird in Schlange Q eingefügt

  9. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz c g s 0 f b i Q: s

  10. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz c g s 0 u f b i Q: s

  11. Graphalgorithmen d v=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz c g s 0 u f b i Q: s

  12. Graphalgorithmen d v=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz c g s 0 u f b i Q: s

  13. Graphalgorithmen d v=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz c g s 0 u f b i Q: s

  14. Graphalgorithmen d v=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u f b i Q: s

  15. Graphalgorithmen d v=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u f b i Q: s, a

  16. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u v=b f i Q: s, a

  17. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u v=b f i Q: s, a

  18. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u v=b f i Q: s, a

  19. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u v=b f i 1 Q: s, a

  20. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u v=b f i 1 Q: s, a, b

  21. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u b f i 1 Q: a, b

  22. Graphalgorithmen d a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u b f i 1 Q: a, b

  23. Graphalgorithmen d u=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 u b f i 1 Q: a, b

  24. Graphalgorithmen d u=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 v=c g s 0 u b f i 1 Q: a, b

  25. Graphalgorithmen d u=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 v=c g s 0 2 u b f i 1 Q: a, b, c

  26. Graphalgorithmen v=d u=a e h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u b f i 1 Q: a, b, c

  27. Graphalgorithmen v=d u=a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u b f i 1 Q: a, b, c, d

  28. Graphalgorithmen v=d u=a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u b f i 1 Q: b, c, d

  29. Graphalgorithmen d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u u=b f i 1 Q: b, c, d

  30. Graphalgorithmen d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u u=b f i 1 Q: b, c, d

  31. Graphalgorithmen d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u b f i 1 Q: c, d

  32. Graphalgorithmen d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 u=c g s 0 2 u b f i 1 Q: c, d

  33. Graphalgorithmen d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 u=c g s 0 2 u b f i 3 1 Q: c, d, f

  34. Graphalgorithmen d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u b f i 3 1 Q: d, f

  35. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 1 c g s 0 2 u b f i 3 1 Q: d, f

  36. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c g s 0 2 u b f i 3 1 Q: d, f, e

  37. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c g s 0 2 u b f i 3 1 Q: f, e

  38. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c g s 0 2 u u=f b i 3 1 Q: f, e

  39. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c g s 0 2 u 4 u=f b i 4 3 1 Q: f, e, g, i

  40. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c g s 0 2 u 4 u=f b i 4 3 1 Q: e, g, i

  41. Graphalgorithmen u=d a u=e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c g s 0 2 u 4 f b i 4 3 1 Q: g, i

  42. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c 5 u=g s 0 2 u 4 f b i 4 3 1 Q: i, h

  43. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c 5 g s 0 2 u 4 f b u=i 4 3 1 Q: h

  44. Graphalgorithmen u=d a e 2 u=h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c 5 g s 0 2 u 4 f b i 4 3 1 Q:

  45. Graphalgorithmen u=d a e 2 h BFS(G,s) 1. „initialisiere BFS“ 2.while Qdo 3. u  head[Q] 4. for each vAdj[u] do 5. if color[v]=weiß then 6. color[v]  grau 7. d[v]  d[u]+1; p[v]  u 8. enqueue(Q,v) 9. dequeue(Q) 10. color[u]  schwarz 3 1 c 5 g s 0 2 u 4 f b i 4 3 1

  46. Graphalgorithmen Satz 47 Sei G=(V,E) ein Graph. Die Laufzeit des Algorithmus BFS beträgt O(|V|+|E|). Beweis • Laufzeit Initialisierung: O(|V|) • Nach der Initialisierung wird kein Knoten weiß gefärbt • Daher ist jeder Knoten nur einmal in der Schlange •  Zeit für Schlangenoperationen ist O(|V|) • Adjazenzliste jedes Knotens wird nur durchlaufen, wenn er aus der Schlange entfernt wird • Damit wird jede Adjazenzliste maximal einmal durchlaufen (d.h. jede Kante maximal zweimal)  Laufzeit für Liste: O(|E|) • Gesamtlaufzeit: O(|V|+|E|)

  47. Graphalgorithmen Kürzeste Wege in ungewichteten Graphen • Sei d(s,t) die minimale Anzahl Kanten in einem s-t-Weg • Ein s-t-Weg der Länge d(s,t) heißt kürzester Weg • Wollen zeigen, dass BFS korrekt kürzeste Wege berechnet

  48. Graphalgorithmen Lemma 48 Sei G=(V,E) ein gerichteter oder ungerichteter Graph und sei sV ein beliebiger Knoten. Dann gilt für jede Kante (u,v)E: d(s,v) d(s,u)+1 Beweis • Ist u erreichbar von s, dann ist es auch v • Der kürzeste Weg von s nach v kann nicht länger sein, als der kürzeste Wegvon s nach u gefolgt von der Kante (u,v). Damit gilt die Ungleichung. • Ist u nicht erreichbar von s, dann ist d(s,u)= und die Ungleichung gilt.

  49. Graphalgorithmen Lemma 49 Sei G=(V,E) ein gerichteter oder ungerichteter Graph und es laufe die Breitensuche von einem Startknoten sV. Nach Abschluss der Breitensuche gilt für jeden Knoten v, dass d[v]d(s,v) ist. Beweis • Induktion über Anzahl von Zeitpunkten, an denen ein Knoten in Q eingefügt wird • (I.A.) Nach Initialisierung gilt d[s]=0=d(s,s) und d[v]=≥d(s,v) für alle vV-{s} • (I.V.) Aussage gilt nach m Einfügeoperationen • (I.S.) Betrachte nach m Einfügeoperationen einen weißen Knoten v, der während einer Suche von u entdeckt wird. Nach (I.V.) gilt d[u]≥d(s,u). • Zeile 7: d[v] wird auf d[u]+1 gesetzt • Es gilt: d[v] = d[u]+1 ≥ d(s,u)+1 ≥ d(s,v) nach Lemma 48

  50. Graphalgorithmen Lemma 49 Sei G=(V,E) ein gerichteter oder ungerichteter Graph und es laufe die Breitensuche von einem Startknoten sV. Nach Abschluss der Breitensuche gilt für jeden Knoten v, dass d[v]d(s,v) ist. Beweis • Knoten v wird dann in die Schlange eingefügt und grau gefärbt • Damit ändert sich d[v] im Laufe des Algorithmus nicht mehr und die Aussage des Lemmas bleibt erhalten

More Related