Erinnerung VL 06.06.06 Sortierte Folgen: eierlegende Wollmilchsau Suchbäume: binäre und (a, b)-bäume (a, b)-bäume: remove, insert etwas kompliziert (balancieren), aber in logarithmischer Zeit möglich Heute: Augmentierte Suchbäume, Repräsentationen von Graphen KIT Institut für Theoretische Informatik
Erweiterte (augmentierte) Suchbäume Idee: zusätzliche Infos verwalten mehr (schnelle) Operationen. Nachteil: Zeit- und Platzverschwendung, wenn diese Operationen nicht wichtig sind. gold plating KIT Institut für Theoretische Informatik
Elternzeiger Idee: Knoten speichern Zeiger auf Elternknoten 5 7 3 7 3 9 3 5 7 3 7 9 00 Anwendungen: schnelleres remove, insertbefore, insertafter, falls man ein handle des Elements kennt. Man spart die Suche. Frage: was speichert man bei (a, b)-bäumen (zusätzlich)? KIT Institut für Theoretische Informatik 3
Teilbaumgröÿen Idee (Binärbaum): speichere, wie viele Blätter von links erreichbar. (Etwas anders als im Buch!) // return k-th Element in subtree rooted at h Function selectrec(h, k) if h leftsize k then return select(l,k) else return select(r,k leftsize) Zeit: O(log n) Übung: Was ist anders bei (a, b)-bäumen? Übung: Rang eines Elements e bestimmen. Übung: Gröÿe eines Bereichs a..b bestimmen. KIT Institut für Theoretische Informatik 4
Beispiel select 6th element left subtree size i=0 7 3 7>6 4 0+4<6 i=4 4+>6 77 3 5 i=4 4+<6 9 i=5 3 5 7 3 7 9 00 KIT Institut für Theoretische Informatik 5
Zusammenfassung Suchbäume erlauben viele eziente Operationen auf sortierten Folgen. Oft logarithmische Ausführungszeit Der schwierige Teil: logarithmische Höhe erzwingen. Augmentierungen zusätzliche Operationen KIT Institut für Theoretische Informatik 6
Mehr zu sortierten Folgen Karteikasten Array mit Löchern (a, b)-bäume sind wichtig für externe Datenstrukturen Ganzzahlige Schlüssel aus..u Grundoperationen in Zeit O(log logu) Verallgemeinerungen: Zeichenketten, mehrdimensionale Daten KIT Institut für Theoretische Informatik 7
Was haben wir noch gelernt? Invarianten, Invarianten, Invarianten Komplexe verzeigerte Datenstrukturen Datenstruktur-Augmentierung Unterschied Interface Repräsentation Tradeo Array, sortierte Liste, Hash-Tabelle KIT Institut für Theoretische Informatik 8
Kap. 8: Repräsentation von Graphen Einleitung 736 stellt L. Euler die folgende touristische Frage: Straÿen- oder Computernetzwerke Zugverbindungen (Raum und Zeit) Soziale Netzwerke (Freundschafts-, Zitier-, Empfehlungs-,... ) Aufgabenabhängigkeiten Scheduling-Probleme Werte und arithmetische Operationen Compilerbau... KIT Institut für Theoretische Informatik 9
Repräsentation von Graphen self loop s t Was zählt, sind die Operationen! H w z w y x Eine triviale Repräsentation Felder v v G u Verkettete Listen u s x K 5 Matrizen Implizit w v U t Diskussion u u K 3,3 w v undirected w v bidirected KIT Institut für Theoretische Informatik 0
Notation und Konventionen Graph G = ( }{{} V, }{{} E ): Knoten Kanten n = V m = E Knoten: s,t,u,v,w,x,y,z Kanten e E. Oder: Knotenpaare (manchmal Knotenmengen der Gröÿe ) KIT Institut für Theoretische Informatik
Notation und Konventionen Graph G = ( }{{} V, }{{} E ): Knoten Kanten n = V m = E Knoten: s,t,u,v,w,x,y,z Kanten e E. Oder: Knotenpaare (manchmal Knotenmengen der Gröÿe ) WICHTIG: Buchstabenzuordnungen sind unverbindliche Konvention Manchmal werden ganz andere Buchstaben verwendet. Im Zweifel immer genau sagen, was was ist. Das gilt für die ganze theoretische Informatik! KIT Institut für Theoretische Informatik
Ungerichtete gerichtete Graphen Meist repräsentieren wir ungerichtete Graphen durch doppelt gerichtete Graphen wir konzentrieren uns auf gerichtete Graphen 3 3 4 4 KIT Institut für Theoretische Informatik
Operationen Ziel: O(Ausgabegröÿe) für alle Operationen Grundoperationen: Statische Graphen: Konstruktion, Konversion und Ausgabe (O(m + n) Zeit) Navigation: Gegeben v, nde ausgehende Kanten. Dynamische Graphen: Knoten/Kanten einfügen/löschen s y z w v x t u KIT Institut für Theoretische Informatik 3
Weitere Operationen Zugri auf assoziierte Information s z 4 v t Mehr Navigation: Finde eingehende Kanten Kantenanfragen: (z,x) E? 7 y 6 5? w 3 8 6 4 x 5 3 u KIT Institut für Theoretische Informatik 4
Kantenfolgenrepräsentation Folge von Knotenpaaren (oder Tripel mit Kantengewicht) + kompakt + gut für I/O Fast keine nützlichen Operationen auÿer alle Kanten zu durchlaufen Beispiele: Übung: isolierte Knoten suchen, Kruskals MST-Algorithmus (später), Konvertierung. u (u,v),(v,w),(w,u),(u,w) w v KIT Institut für Theoretische Informatik 5
Adjazenzfelder V =..n oder 0..n Kantenfeld E speichert Ziele und zwar gruppiert nach Startknoten V speichert Index der ersten ausgehenden Kante Dummy-Eintrag V [n + ] speichert m + 4 V n 5=n+ 3 5 7 7 3 E 3 3 4 4 m 7=m+ Beispiel: Ausgangsgrad(v) = V[v + ] V[v] KIT Institut für Theoretische Informatik 6
Kantenliste Adjazenzfeld Zur Erinnerung: KSort (BucketSort) Function adjacencyarray(edgelist) V=,0,...,0 : Array [..n + ] of N foreach (u,v) EdgeList do V [u]++ for v := to n + do V [v] += V [v ] foreach (u,v) EdgeList do E[ V [u]] = v return (V,E) // count // prefix sums // place 4 V n 5=n+ 3 5 7 7 3 E 3 3 4 4 m 7=m+ KIT Institut für Theoretische Informatik 7
Beispiel 3 4 n 5=n+ V 0 0 0 0 count 3 0 0 prefix sum 3 5 7 7 7 distribute V 3 5 7 7 E 3 3 4 4 m 7=m+ KIT Institut für Theoretische Informatik 8
Operationen für Adjanzenzfelder Navigation: einfach Kantengewichte: E wird Feld von Records (oder mehrere Felder) Knoteninfos: V wird Feld von Records (oder mehrere Felder) a d c e b f 3 n 5=n+ 4 V 3 5 7 7 E 3 3 4 4 m w a b c d e f 7=m+ KIT Institut für Theoretische Informatik 9
Operationen für Adjanzenzfelder Navigation: einfach Kantengewichte: E wird Feld von Records (oder mehrere Felder) Knoteninfos: V wird Feld von Records (oder mehrere Felder) Eingehende Kanten: umgedrehten Graphen speichern Kanten löschen: explizite Endindizes Batched Updates: neu aufbauen a d c e b f 3 n 5=n+ 4 V 3 5 7 7 E 3 3 4 4 m w a b c d e f 7=m+ KIT Institut für Theoretische Informatik 9
Kantenanfragen Hashtabelle H E speichert (ggf. zusätzlich) alle Kanten. Unabhängig von der sonstigen Graphrepräsentation KIT Institut für Theoretische Informatik 0
Adjazenzlisten speichere (doppelt) verkettete Liste adjazenter Kanten für jeden Knoten. + einfaches Einfügen von Kanten + einfaches Löschen von Kanten (ordnungserhaltend) mehr Platz (bis zu Faktor 3) als Adjazenzfelder mehr Cache-Misses n n 4 3 4 3 4 4 3 m 4 3 4 4 3 KIT Institut für Theoretische Informatik
Adjazenzlisten aufrüsten Knotenlisten für Knotenupdates Eingehende Kanten Kantenobjekte (in globaler Kantenliste) Zeiger auf Umkehrkante 0 3 E list out list in list rev from to (0,) 0 0 0 (0,) 0 0 V list first first deg deg out in out in 0 0 0 0 (,) 0 (,3) 0 0 0 (,) 0 0 0 0 3 (,3) 0 0 0 0 KIT Institut für Theoretische Informatik
Customization (Zuschneiden) Anpassen der (Graph)Datenstruktur an die Anwendung. Ziel: schnell, kompakt. benutze Entwurfsprinzip: make the common case fast Listen vermeiden Mögliches Problem: Software-Engineering-Alptraum Möglicher Ausweg: Trennung von Algorithmus und Repräsentation KIT Institut für Theoretische Informatik 3
Beispiel: DAG-Erkennung Beispiel aus Notations-Kapitel (generisch, leicht variiert): Function isdag(g = (V,E)) while v V : indegree(v) = 0 do invariant G is a DAG i the input graph is a DAG V := V \ {v} E:= E \ ({v} V V {v}) return V =0 KIT Institut für Theoretische Informatik 4
Beispiel: DAG-Erkennung Function isdag(g = (V, E)) // Adjazenzarray! dropped:= 0 compute array indegree of indegrees of all nodes// Zeit O(m + n)! droppable={v V : indegree[v] = 0} : Stack while droppable /0 do invariant G is a DAG i the input graph is a DAG v:= droppable.pop dropped++ foreach edge (v,w) E do indegree[w] if indegree[w] = 0 then droppable.push(w) return V = dropped Laufzeit: O(m + n) (auch ohne dynamische Graphdatenstruktur!) KIT Institut für Theoretische Informatik 5
Adjazenz-Matrix A {0,} n n with A(i,j) = [(i,j) E] + platzezient für sehr dichte Graphen platzinezient sonst. Übung: was bedeutet sehr dicht hier? + einfache Kantenanfragen langsame Navigation ++ verbindet lineare Algebra und Graphentheorie Beispiel: C = A k. C ij =# k-kanten-pfade von i nach j Wichtige Beschleunigungstechniken: O(log k) Matrixmult. für Potenzberechnung 3 4 0 0 0 0 0 0 0 0 0 0 Matrixmultiplikation in subkubischer Zeit, z. B., Strassens Algorithmus KIT Institut für Theoretische Informatik 6
Pfade zählen mittels LA Adjanzenzmatrix: A {0,} n n mit A(i,j) = [(i,j) E] Sei C:= A k. Behauptung: C ij =# k-kanten-pfade von i nach j. Beweis: IA (k = ) C = A = A stimmt nach Denition von A. Schluss k k + : C ij = (A k A) ij = A k ila lj l A k il =#k-kanten-pfade von i nach l (nach IV). A k il A lj =#k + -Kanten-Pfade von i nach j mit (l,j) als letzter Kante. Jede mögliche letzte Kante wird genau einmal gezählt. Übung: zähle Pfade der Länge k 3 0 0 0 0 0 0 0 0 0 0 4 KIT Institut für Theoretische Informatik 7
Beispiel, wo Graphentheorie bei LA hilft Problemstellung: löse Bx = c Sei G = (..n,e = {{i,j} : B ij 0}) Nehmen wir an, G habe zwei Zusammenhangskomponenten tausche Zeilen und Spalten derart, dass ( )( ) ( ) B 0 x c = 0 B x c zu lösen bleibt. Übung: Was passiert, wenn (..n,e = {(i,j) : B ij 0}) ein DAG ist? KIT Institut für Theoretische Informatik 8
Implizite Repräsentation Kompakte Repräsentation möglicherweise sehr dichter Graphen Implementiere Algorithmen direkt mittels dieser Repräsentation Beispiel: Intervall-Graphen Knoten: Intervalle [a,b] R Kanten: zwischen überlappenden Intervallen KIT Institut für Theoretische Informatik 9
Zusammenhangstest für Intervallgraphen V = {[a,b ],...,[a n,b n ]} E = {{[a i,b i ],[a j,b j ]} : [a i,b i ] [a j,b j ] /0} Idee: durchlaufe Intervalle von links nach rechts. Die Anzahl überlappender Intervalle darf nie auf null sinken. Annahme: Startpunkte in Sortierung vor Endpunkten! Function isconnected(l : SortedListOfIntervalEndPoints) : {0, } remove rst element of L; overlap := foreach p L do if overlap= 0 return false if p is a start point then overlap++ else overlap // end point return true O(n logn) Algorithmus für bis zu O ( n ) Kanten! Übung: Zusammenhangskomponenten nden KIT Institut für Theoretische Informatik 30
Beispiel Function isconnected(l : SortedListOfIntervalEndPoints) : {0, } remove rst element of L; overlap := foreach p L do if overlap= 0 return false if p is a start point then overlap++ else overlap // end point return true KIT Institut für Theoretische Informatik 3
Graphrepräsentation: Zusammenfassung Welche Operationen werden gebraucht? Wie oft? Adjazenzarrays gut für statische Graphen Pointer exibler, aber auch teurer Matrizen eher konzeptionell interessant KIT Institut für Theoretische Informatik 3
Kap. 9: Graphtraversierung Ausgangspunkt oder Baustein fast jedes nichttrivialen Graphenalgorithmus KIT Institut für Theoretische Informatik 33
Graphtraversierung als Kantenklassizierung forward s tree backward cross KIT Institut für Theoretische Informatik 34
Graphtraversierung als Kantenklassizierung Baumkanten: Elemente des Waldes, der bei der Suche gebaut wird Vorwärtskanten: verlaufen parallel zu Wegen aus Baumkanten Rückwärtskanten: verlaufen antiparallel zu Wegen aus Baumkanten Querkanten: alle übrigen forward s tree backward cross KIT Institut für Theoretische Informatik 35
Breitensuche Baue Baum von Startknoten s, der alle von s erreichbaren Knoten mit möglichst kurzen Pfaden erreicht. Berechne Abstände: s b c d e f g tree backward cross forward 0 3 KIT Institut für Theoretische Informatik 36
Breitensuche Einfachste Form des Kürzeste-Wege-Problems Umgebung eines Knotens denieren (ggf. begrenzte Suchtiefe) Einfache, eziente Graphtraversierung (auch wenn Reihenfolge egal) s b c d e f g tree backward cross forward 0 3 KIT Institut für Theoretische Informatik 37
Breitensuche Algorithmenidee: Baum Schicht für Schicht aufbauen s b c d e f g tree backward cross forward 0 3 KIT Institut für Theoretische Informatik 38
Function bfs(s) : Q:= s // aktuelle Schicht while Q do exploriere Knoten in Q merke dir Knoten der nächsten Schicht in Q Q:= Q s b c d e f g tree backward cross forward 0 3 KIT Institut für Theoretische Informatik 39
Repräsentation des Baums Feld parent speichert Vorgänger. noch nicht erreicht: parent[v] = Startknoten/Wurzel: parent[s] = s s b c e g tree parent d f KIT Institut für Theoretische Informatik 40