• Non ci sono risultati.

Minimal Spanning Trees

N/A
N/A
Protected

Academic year: 2021

Condividi "Minimal Spanning Trees"

Copied!
15
0
0

Testo completo

(1)

Minimal Spanning Trees

Chapter 23

Cormen Leiserson Rivest&Stein:

Introduction to Algorithms

(2)

Minimal Spanning Trees

Weighted Graphs G(V, E, w) W: E R

If w=1 for all edges BFS is the solution.

The MST is the way of connecting n

vertices at minimal cost of connections.

Two greedy algorithms : Kruskal

Prim

(3)

Minimal Spanning Trees

First a generic method utilized by the two algorithms:

Prior of each iteration, A is a subset of a MST

Determine a safe edge (u,v): A U (u,v) is still a subset of a MST.

(4)

Minimal Spanning Trees

S vertices in MST (black). V-S vertices to be selected.

The line is the cut. Light edges crossing the cut are the only edges that can be selected for the MST.

They are safe!

(5)

Kruskal Algorithm for MST

Uses a disjoint-set data structure to maintain several disjoint sets of elements. FIND-SET(u) returns a

representative element of the set containing u.

(Disjoint set forest chapter. 21)

(6)

Kruskal Algorithm for MST

(7)

Kruskal Algorithm for MST

(8)

Kruskal Algorithm for MST

(9)

Kruskal Algorithm complexity

Complexity depends on how we implement the disjoint-set data structure.

(Disjoint set forest chapter. 21.3) Sorting of the edges O(|E|log |E|)

Loop 5-8 O(|E|) FIND-SET and UNION operations on the disjoint set forest.

| V | operations of MAKE-SET

In total the loop takes O((|V| +|E|) α(V)) time , α is very slowly growing function (inverse of Ackermann fun.) α(|V|)=O(log|V|) the loop takes O(|E|log|V|).

In total the Kruskal alg. takes O(|E| log|E|)

Since |E|<V2, log|E|< O(log|V|) hence Kruskal takes O(|E| log|V|)

(10)

PRIM algorithm for MST

Starts from an arbitrary vertex r the root.

The set A forms a single tree at any step.

All vertices not in the spanning tree form a min Heap Q based on the minimum weight of any edge connecting vertex v and the tree. Value v.key.

The termination condition is that the heap Q is empty, that means that all vertices have been connected to the MST.

(11)

PRIM algorithm for MST

The parent in the MST

(12)

PRIM algorithm for MST

c

(13)

PRIM algorithm for MST

(14)

Loop 6-11 invariant

(15)

Complexity PRIM algorithm

Row 1-5 : Build min heap takes O(|V|)

The while loop is executed O(V) times and EXTRACT- MIN take O(log V) time hence in totla O(|V|log|V|) The for loop is executed O(|E|) in total and the last operation is a DECREASE-KEY operation O(log|V|).

Prims algorithm takes O(|V|log|V| + |E|log|V|) = O(|E|

log|V|) the same as Kruskal algortihm! It can be improved to:

O(|E| + |V| log |V|)

using for Q the data structure Fibonacci Heap

Riferimenti

Documenti correlati

– Ad ogni passo viene aggiunto l'arco di peso minimo che collega un nodo già raggiunto dell'albero con uno non ancora raggiunto. Prim: Shortest connection networks and

[r]

Per refutare l’affermazione occorre invece trovare un controesempio.. Date: 11

Quindi tutti i minimum spanning tree di G avranno lista dei pesi uguale

Upon startup a bridge sets all its link to PRE-FORWARDING and claims to be Root and designated bridge on every port When the information about root are expired the bridge tries

It is to find a suitable form and pattern that can be applied to building shell to ease building maintenance operation beside to enhance the aesthetic value of a

Meccanicamente: se avete le tabelle una sopra l’altra e avete segnato in qualche modo la trama minore copiate per ogni router le prime due cifre di tale trama nella riga

- lo stabilimento produzione è un capannone su unico piano, con un locale controllo dove si prevedono 2 postazioni di lavoro, un vano tecnico per quadri e impianti, e il resto