29.06.2013 Views

Curs 8 - Drumuri de cost minim [pdf] - Andrei

Curs 8 - Drumuri de cost minim [pdf] - Andrei

Curs 8 - Drumuri de cost minim [pdf] - Andrei

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Construcție w 1<br />

I<strong>de</strong>e 2: w 1(u..v) = w(u..v) + h(u) - h(v);<br />

un<strong>de</strong> d hh:V->ℜ; V ℜ<br />

se adaugă un nod s;<br />

se unește s cu toate nodurile grafului prin muchii <strong>de</strong><br />

<strong>cost</strong> 0;<br />

se aplica BF pe acest graf => h(v) = δ(s,v);<br />

w 1(u,v) = w(u,v) + h(u) - h(v).<br />

Algoritm Johnson<br />

Proiectarea Algoritmilor 2010<br />

Johnson(G)<br />

G’ = (V’,E’);<br />

V’ = V ∪ { {s}; } // adăugăm dă ă nodul d l s<br />

E’ = E ∪ (s,u), ∀u∈V; w(s,u) = 0; // si îl legăm <strong>de</strong> toate nodurile<br />

Dacă BF(G’) e fals // aplic BF pe G’<br />

Eroare “ciclu negativ”<br />

Altfel<br />

Pentru fiecare v∈V<br />

h(v) = δ(s,v); // calculat prin BF<br />

Pentru fiecare (u,v)∈E ( , )<br />

w1(u,v) = w(u,v) + h(u) - h(v) // calculez noile <strong>cost</strong>uri pozitive<br />

Pentru fiecare (u∈V)<br />

Dijkstra(G,w1,u) // aplic Dijkstra pentru fiecare nod<br />

Pentru fiecare (v∈V)<br />

d(u,v) = δ1(u,v) + h(v) - h(u) // calculez <strong>cost</strong>urile pe graful inițial<br />

Proiectarea Algoritmilor 2010<br />

5/8/2010<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!