Post on 02-May-2015
Alberi di copertura
minimi
Alberi di copertura minimi
Dato un grafo pesato G = (V,E), si richiede di trovare un albero T = (V,E’), E’E, tale che la somma dei pesi associati agli archi di T sia minima.
L’albero T è detto albero di copertura minimo (minimum spanning tree, MST) di G.
MST: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
MST: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
MST - Algoritmo di Kruskal
MST-Kruskal(G,w)A = foreach vertice v V[G] do
Make-Set(v)ordina gli archi di E[G] per pesi non decrescentiforeach (u,v) E[G] in ordine di peso non decr. do
if Find-Set(u) Find-Set(v) then A = A {(u,v)}
Union(u,v)return A
Make-Set(v): crea un insieme con unico membro vFind-Set(v): restituisce il rappresentante dell’insieme contenenete
v Union(u,v): unisce i due insiemi che contengono u e v
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Kruskal: correttezzaTeoremaSianoG(V,E) il grafo dato in input
G’(V,E’) sottografo di qualche MST di G, contenente tutti i vertici e alcuni archi di G
G1(V1,E1) G2(V2,E2) partizione di G’ in due componenti non connesse
e E arco di peso minimo che connette G1 e G2
Allora anche G’(V,E’ {e}) è un sottografo di qualche MST
Kruskal: correttezza
G1 G2
e
e’
Sia T’ un albero di copertura contenente e’, con w(e’)(e). Se si sostituisce e’ con e si ottiene un altro albero di copertura T con w(T’) w(T). Quindi T è un albero di copertura di costo inferiore a T’.
Kruskal: complessità
T(V,E) = O(V) + O(E log E) + O(E log E) = O(E log E)
Inizializzazione: O(V)Ordinamento archi: O(E lg E)Operazioni nella foresta di insiemi disgiunti: O(E)Tempo complessivmante richiesto: O(E (E,V)) = O(E lg* E)
MST: algoritmo di Prim
MST-Prim(G, w, r)Q = V[G]foreach u Q do key[u] = key[r] = 0[r] = nilwhile Q do
u = Extract-Min(Q)foreach v Adj(u) do
if v Q and w(u,v) < key[v] then[v] = u
key[v] = w(u,v)
Nell’algoritmo di Prim gli archi dell’insieme in costruzione formano sempre un unico albero
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: esempio
e
d
h
b
a
h
c
i
f
8 7
1 2
9
10
14
4
8
117
2
64
Prim: complessità
T(V,E) = O(V) + O(V log V) + O(E) = O(E + V log V)