Shortest Paths - Label Correcting Algorithms

Werbung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Shortest Paths
Label Correcting Algorithms
Florian Reitz
Universität Trier
Fachbereich IV
Fach Informatik
Seminar Netzwerkalgorithmen WS 2005/2006
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Einleitung: Problemübersicht
• Eben: Schnelle Algorithmen für spezielle Graphen
gewichtete Graphen
• Azyklisch oder
• Nicht negative Kantenlängen
• Wünschenswert: Algorithmus für allgemeinen Graphen:
Der ist NP Vollständig
• Hier: ein Mittelweg
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Einleitung: Was wir wollen
• Betrachte Netzwerk G=(V,E)
• Finde kürzesten Weg von Knoten a nach Knoten b
falls vorhanden
oder
• Finde einen negativen Kreis im Netzwerk
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Einleitung: Begriffserklärung
G=(V,E), |V|=n, |E|=m
C = max{|cij | : (i, j) ∈ E}
Negativer Kreis
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Gliederung
1
Single Source Sortest Path
Optimalitätbedingungen
Der generische Label Correcting Algorithmus
Die O(mn) Implementierung
2
Negative Kreise
3
All-Pair Shortest Path Problem
Repeated Shortest Path Algorithm
Floyd-Warshall Algorithm
4
Minimum Mean Cycle
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Optimalitätbedingungen
Abstandsmarken
Definition
• Sei G ein Netzwerk und P ein Pfad in G von s nach t
Dann beschreibe d(t) den kürzesten bisher gefundenen
Weg von s nach t.
P
• c(P) = P cij mit (i,j) Kante in P
• Ziel ist: d(t)=inf {c(P) : P Pfad von s nach t}
• Falls |{c(P) : P Pfad von s nach t}|=0: d(j) =∞
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Optimalitätbedingungen
Idee eines Algorithmus
• Iteriere über das Netzwerk, ändere Abstandmarken bis
diese konstant
• d(j) gibt dann kürzesten Abstand zu Startknoten an
• Genaueres siehe unten.
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Optimalitätbedingungen
Optimaltitätsbedingung
Satz (Dreiecksungleichung)
Abstandsmarken d(.) optimal
⇐⇒ d(j) ≤ d(i) + cij , ∀(i, j) ∈ E
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Optimalitätbedingungen
Beweis
Beweis.
• ”⇒” d(.) seien optimal.
Annahme: ∃(i, j) ∈ E mit cij + d(i) < d(j)
Dann kann d(j) auf cij + d(i) reduziert werden
⇒Widerspruch
• “⇐” Sei d(i) + cij ≥ d(j), ∀(i, j) ∈ E und P=(j0 ,j1 ,...,jk −1 ,jk )
ein Pfad
jk = t. Dann gilt:
P mit j0 =s und P
c(P)= 0≤i<k cji ji+1 ≥ 0≤i<k (d(ji+1 ) − d(ji ))
= d(jk ) − d(j0 ) = d(t)
Damit ist d(t) untere Schranke für einen Weg von s nach t
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Optimalitätbedingungen
Reduzierte Kosten
Definition
cijd = cij + d(i) − d(j)
Sind die reduzierten Kanten Längen von Kante (i,j)
Bemerkung
• Sei W Kreis:
P
(i,j)∈W
cijd =
P
(i,j)∈W
cij
• d(.) beschreiben kürzesten Pfad ⇒ cijd ≥ 0
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Optimalitätbedingungen
Negative Kreise
Korollar
G enthält einen negativen Kreis ⇒
mindesten ein d(.) verletzt Optimalitätsbedingung
Beweis.
Sei W ein Kreis
d(.) beschreiben die kürzesten Pfade
P in G und
d
d
⇒ cP
(i,j)∈W cij ≥ 0
ij ≥ 0 ⇒
⇒ (i,j)∈W cij ≥ 0
Also kann W kein negativer Kreis sein
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Der generische Label Correcting Algorithmus
Der GLC Algorithmus
Voraussetzung: Keine negativen Kreise
Algorithmus: LCA
begin
d(s)=0; pred(s) :=0 ;
d(j)=inf; für alle Knoten außer s
while(d(j) > d(i) + c[i,j] für eine Kante (i,j))
d(j):=d(i)+c[i,j];
pred(j)=i;
end;
Single Source Sortest Path
Negative Kreise
Der generische Label Correcting Algorithmus
Beispiel 1
Beispiel
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Single Source Sortest Path
Negative Kreise
Der generische Label Correcting Algorithmus
Beispiel 2
Beispiel
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Single Source Sortest Path
Negative Kreise
Der generische Label Correcting Algorithmus
Beispiel 3
Beispiel
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Der generische Label Correcting Algorithmus
Endlichkeit und Laufzeit
Satz
Seien die Kantengewichte ganzzahlig, dann terminiert der
Algorithmus in O(min(n2 mC, m2n ))
Beweis.
Es gilt: −Cn ≤ d(j) ≤ Cn für beliebige j ∈ V
d(j)wird pro Update mind. um 1 reduziert
⇒ Höchstens 2nC Updates pro Abstandsmarken,also maximal
2n2 C Updates insgesamt. O(m) zum Finden der Kante
⇒ Laufzeit von O(n2 mC)
Aber: C kann exponentiell mit der Anzahl der Knoten wachsen
⇒ Laufzeit von O(2n m)
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Der generische Label Correcting Algorithmus
Beobachtungen
• Zeitverbrauch zum Finden einer Kante, die
die Optimalitätsbedingungen verletzt: O(m)
• Nach jeder Änderung einer Marke werden alle Kanten
kontrolliert
• Idee: Phasenweises Absuchen des Netzwerkes nach noch
nicht optimalen Marken
• Keine Veränderung mehr ⇒ Lösung gefunden
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Die O(mn) Implementierung
O(mn) Implementierung
Algorithmus: O(mn);
begin
d(s)=0; pred(s):=0;
d(j)=inf für alle Knoten außer s
while( ein d(i) im letzten Durchgang geändert)
forall Kanten (i,j) in E
if(d(j)>d(i)+c[i,j])
d(j):=d(i)+c[i,j];
pred(j):=i;
end;
Single Source Sortest Path
Die O(mn) Implementierung
Beispiel
Beispiel
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Die O(mn) Implementierung
Lemma Teil 1
Lemma
Der Algorithmus durchläuft das Netzwerk maximal n-1 Mal
Beweis.
Zu zeigen: Nach k Durchläufen wurden die kürzesten Wege zu
allen Knoten gefunden die maximal k Kanten von s entfernt sind
Induktion über Anzahl der Durchläufe
√
• k=1
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Die O(mn) Implementierung
Lemma Teil 2
Beweis.
• k ⇒ k+1
Betrachte den Pfad P=(s=i0 ,i1 ,...,ik ,ik +1 =j) mit k+1 Kanten.
Annahme: P kürzester Pfad. Es gebe keinen kürzesten
Pfad nach j mit weniger Kanten.
IV : (i0 ,i1 ,...,ik ) ist kürzester Pfad nach ik und d(ik ) ist die
Länge des Pfades.
Betrachte Kante (ik ,ik +1 ). d(k+1) wird auf c(P) gesetzt
⇒ Beh.
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Die O(mn) Implementierung
Laufzeit
Satz
Die Laufzeit der O(mn) Implementierung ist O(mn) :-)
Beweis.
Es gibt maximal n-1 Durchläufe.
In jedem werden maximal m Kanten untersucht in Zeit O(1) ⇒
Laufzeit O(mn)
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Negative Kreise
• Bisher: negative Kreise ausgeschlossen
• Erinnerung: Negativer Kreis vorhanden ⇒ Optimalität
immer verletzt
• Da das Durchlaufen eines negativen Kreises immer zur
Verbesserung der d(.) führt: ⇒ d(i)→ −∞ Für mindestens
ein i ∈ V
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Die O(mn) Implementierung
• Implementierung brauch maximal n-1 Durchgänge
• Es wird jede Marke also höchstens n-1 Mal verändert
• Also mitzählen!
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Vorgängergraph
• Erinnerung: LCA liefert Vorgängergraphen
• Bei negativen Kreisen gilt: Es gibt mindestens ein Kreis
im Vorgängergraphen !
• Also: Suche gerichteten Kreis im Vorgängergraphen
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Finden negativer Kreise
Algorithmus: Neg Kreis;
begin
d(s)=0;
while(Es gibt einen nicht markierte Knoten j)
d(j):=j;
i:=j;
while(pred(i) unmarkiert)
d(i):=j;
i:= pred(i);
if(d(i)=d(j))
Kreis gefunden
end;
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Laufzeit
• Laufzeit: O(n)
• Wenn dieser Algorithmus nur alle a Schritte durchgeführt
wird ändert sich die Laufzeit des LCA nicht.
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Fragestellung
• Bisher: ein Startknoten s,
gesucht: Weg von s zu allen anderen Knoten
• Jetzt: gesucht: Wege zwischen allen Knotenpaaren (i,j)
• Annahme: Das Netzwerk ist stark zusammenhängend
d.h. jeder Knoten kann von jedem anderen erreicht werden
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Einfache Idee
• Führe das Problem auf das Problem auf Single
SourceShortest Path zurück
• Wende die O(mn) Implementierung für jeden Knoten in V
einmal an
• Betrachte dabei die jeweiligen Knoten als Startknoten
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Repeated Shortest Path Algorithm
Abstandsmarken
Definition
d[i,j] sei die Länge eines gerichteten Pfades
von Knoten i nach j.
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Repeated Shortest Path Algorithm
Optimalitätsbedingungen Teil 1
Satz
Die d[i,j] repräsentieren den kürzsten Abstand zwischen i und j
⇐⇒ d[i, j] ≤ d[i, k ] + d[k , j]
∀i, j, k ∈ V
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Repeated Shortest Path Algorithm
Optimalitätsbedingungen Teil 2
Beweis.
• ”⇒” Gegenannahme: d[i,k] + d[k,j] < d[i,j]
Betrachte den Weg(i...k...j): Ein direkter Weg P, eventuell
Kreise W Die W sind nicht negativ, also gilt
c(P)≤d[i,k]+d[k,j]<d[i,j] Widerspruch
• “⇐” Sei P ein direkter Weg mit c(P)=d[i,j]
P = (i = i1 , ..., ik = j) Es gilt:
d[i, j] = d[i1 , ik ] ≤ d[i1 , i2 ] + d[i2 , ik ]
d[i2 , ik ] ≤ ci2 i3 + d[i3 , ik ] ... d[iP
k −1 , ik ] ≤ cik −1 ik
⇒ d[i, j] ≤ ci1 i2 +...+cik −1 ik = (i,j)∈P cij
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Repeated Shortest Path Algorithm
Der All Pair Generic LCA
Algorithmus: APG LCA;
begin
d[j,i]=inf; für alle (j,i) aus NxN;
d[i,i]=0; für alle i
forall (i,j) in E do d[i,j]=c[i,j]
while(Knoten i,j,k mit d[i,j]>d[i,k]+d[k,j])
d[i,j]:=d[i,k]+d[k,j];
end;
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Repeated Shortest Path Algorithm
Laufzeit
• −nC ≤ d[i, j] ≤ nC ⇒ max 2nC Updates pro Marke
• Es gibt n2 Marken.
• ⇒ Laufzeit O(n3 C)
• Vorsicht bei großen C
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Floyd-Warshall Algorithm
Ein verbesserter Algorithmus
Definition
d k [i, j] sei die Länge eines kürzsten Pfades von Knoten i nach
Knoten j
der nur die Knoten 1 bis k-1 benutzt
Folgerung
d n+1 [i, j] beschreibt den kürzesten Weg von i nach j
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Floyd-Warshall Algorithm
Optimalitätsbedingungen
Satz
d k +1 [i, j] = min{d k [i, j], d k [i, k ] + d k [k , j]}
Beweis.
• 1. Fall: k wird nicht erreicht ⇒ d k +1 [i, j] = d k [i, j]
• 2. Fall: k wird erreicht ⇒ d k +1 [i, j] = d k [i, k ] + d k [k , j]
⇒ Behauptung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Floyd-Warshall Algorithm
Floyd-Warshall Algorithmus
Algorithmus: Floyd-Warshall;
begin
d[j,i]=inf; für alle (j,i) aus NxN;
d[i,i]=0; für alle i
forall (i,j) in E do d[i,j]=c[i,j]
for k=1 to n do
forall((i,j) in NxN do)
if(d[i,j]>d[i,k]+d[k,j]);
d[i,j]:=d[i,k]+d[k,j];
end;
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Floyd-Warshall Algorithm
Negative Kreise
Satz
Ein Netzwerk enthält min. einen negativen Kreis wenn eine der
folgenden Bedingungen wahr ist
• 1. d[i,i]<0
• 2. i 6= j: d[i,j] < -nC
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Floyd-Warshall Algorithm
Beweis
Beweis.
1.d[i, i] = d[i, k ] + d[k , i]
2. d[i, j] < −nC
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Floyd-Warshall Algorithm
Ablauf
• Überprüfe bei jeder Veränderung von d[i,i], d[i,j] ob eine
der Bedingungen erfüllt ist.
• Wenn ja gibt es einen negativen Kreis.
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Das Minimum Mean Cycle Problem
Definition
Das Minimum Mean Cycle Problem MMCP ist folgendermaßen
definiert:
Gesucht ist ein gerichteter Kreis W mit den kleinsten
mittleren Kosten
P
( (i,j)∈W cij )
|W |
In einem stark zusammenhängendem Netzwerk
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Definitionen
Definition
d k (j) k= 1...n sei Länge der kürzesten Weges von Knoten s
nach Knoten j, wobei exakt k Knoten genutzt werden d 0 (s) = 0
d 0 (j) = ∞∀j 6= s
Satz
d k (j) = min{i:(i,j)∈E} {d k −1 (i) + cij }
Bemerkung
d k (j) 6= ∞ ⇒ ∃ Kreis im Graphen
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Vorgehen
• Berechne d 1 (j) aus d 0 (j) dann d 2 (j)...d n (j)
• O(m) pro Schritt
• Maximal n Schritte
• Laufzeit O(mn)
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Beschränkung der Kosten
Satz
P
(i,j)∈W
ci,j
|W |
Beweis.
Fallunterscheidung :-)
= min max
j∈V 0≤k ≤n−1
d n (j) − d k (j)
n−k
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Minimum mean Cycle
Algorithmus: Minimum Mean Cycle;
begin
d_0(s):=0;
d_0(j):=inf;
for k=1...n;
forall j in V do
d_k(x)=inf;
forall (i,j) in E do
if(d_k-1(i)+c[i,j]<d_k(x))
d_k(j):=d_k-1+c[i,j];
pred_k(j):=i;
if(Ein d_n() ist nicht inf)
Finde j gemäß Satz
Konstruiere Kreis aus pred
end;
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Konstruktion des Kreises
• Wissen es gibt einen Kreis C
• Setze C auf
pred_n(j),pred_n-1(pred_n(j)),
pred_n-2(pred_n-1(pred_n(j)))...
• Bis ein Knoten sich Wiederholt
• Startknoten im Kreis egal
Minimum Mean Cycle
Zusammenfassung
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Zusammenfassung
Shortest Path Problem
Algorithmus
LCA
Laufzeit
O(min(n2 mC, m2n ))
O(mn)
O(mn)
Eigenschaften
Findet negative Kreise
Finden von Kante:O(m)
Schlechte Laufzeit
Arbeitet Phasenweise
“merkt” sich Knoten
Findet negative Kreise
Single Source Sortest Path
Negative Kreise
All-Pair Shortest Path Problem
Minimum Mean Cycle
Zusammenfassung
Zusammenfassung
All Pair Shortest Path Problem
Algorithmus
All Pair
Floyd-Warshall
Laufzeit
O(n3 C)
O(n3 )
Eigenschaften
Große C beachten
Schnellster Algorithmus
Herunterladen