Transcript

Algorithms: Minimum Spanning Trees

Amotz Bar-Noy

CUNY

Spring 2012

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 1 / 59

Minimum Spanning Trees (MST)

Input:An undirected and connected graph G = (V ,E) with n vertices andm edges.A weight function w : E → < on the edges.

A spanning tree: A connected sub-graph with n − 1 edges.

A minimum spanning tree (MST): A spanning tree for which thesum of the weights of the n − 1 edges is minimum.

Two greedy algorithms:Kruskal - O(m log m)-time, a distributed algorithm.Prim - O(m + n log n)-time, a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 2 / 59

Minimum Spanning Trees (MST)

Input:An undirected and connected graph G = (V ,E) with n vertices andm edges.A weight function w : E → < on the edges.

A spanning tree: A connected sub-graph with n − 1 edges.

A minimum spanning tree (MST): A spanning tree for which thesum of the weights of the n − 1 edges is minimum.

Two greedy algorithms:Kruskal - O(m log m)-time, a distributed algorithm.Prim - O(m + n log n)-time, a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 2 / 59

Minimum Spanning Trees (MST)

Input:An undirected and connected graph G = (V ,E) with n vertices andm edges.A weight function w : E → < on the edges.

A spanning tree: A connected sub-graph with n − 1 edges.

A minimum spanning tree (MST): A spanning tree for which thesum of the weights of the n − 1 edges is minimum.

Two greedy algorithms:Kruskal - O(m log m)-time, a distributed algorithm.Prim - O(m + n log n)-time, a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 2 / 59

Example: A weighted Graph

BC

D

EF

A

1236

4

3

3

55

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 3 / 59

Example: A BFS Spanning Tree

BC

D

EF

A

1236

4

3

3

55

W (T ) = 21

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 4 / 59

Example: A DFS Spanning Tree

BC

D

EF

A

1236

4

3

3

55

W (T ) = 14

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 5 / 59

Example: An MST

BC

D

EF

A

1236

4

3

3

55

W (T ) = 13

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 6 / 59

Example: Another MST

BC

D

EF

A

1236

4

3

3

55

W (T ) = 13

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 7 / 59

Cuts

A cut 〈S, (V − S)〉 in a graph is a partition of the set of vertices Vinto two disjoint sets: V = S ∪ (V − S).

An edge (u, v) crosses a cut 〈S, (V − S)〉 if(u ∈ S & v ∈ (V − S)) or (v ∈ S & u ∈ (V − S)).

An edge (u, v) is contained in a cut 〈S, (V − S)〉 if(u, v) ∈ S or (u, v) ∈ (V − S).

A set A of edges is contained in a cut 〈S, (V − S)〉 if all of theedges of A are contained in the cut.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 8 / 59

Example: A Cut

BC

D

EF

A

An 〈ABDE , CF〉 cut

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 9 / 59

Example: Another Cut

BC

D

EF

A

An 〈AEF , BCD〉 cut

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 10 / 59

Minimal Forests

A set A of edges is a minimal forest if it is possible to add edgesto A to become a minimal spanning tree.

An empty set is in particular a minimal forest.

A minimum spanning tree is in particular a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 11 / 59

A Minimal Forest

A weighted graph:

D

CB

12

1

11

A

Minimal forests:A

D

CB

1 1

A

D

CB

1

1

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 12 / 59

A Minimal Forest

A weighted graph:

D

CB

12

1

11

A

Not a minimal forest:

D

CB 2

A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 13 / 59

The Greedy Lemma

Input:A weighted connected graph G = (V ,E).

A minimal forest A ⊂ E .

A cut 〈S, (V − S)〉 that contains A.

An edge (u, v) crossing 〈S, (V − S)〉 with minimum weight.

Lemma: A ∪ (u, v) is also a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 14 / 59

The Greedy Lemma

Input:A weighted connected graph G = (V ,E).

A minimal forest A ⊂ E .

A cut 〈S, (V − S)〉 that contains A.

An edge (u, v) crossing 〈S, (V − S)〉 with minimum weight.

Lemma: A ∪ (u, v) is also a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 14 / 59

Proof

A vertex in V−S

A vertex in S

x

y

v

u

An edge not in T

An edge in T−A

An edge in A

An MST T that contains A and (u, v) /∈ T .

(x , y) ∈ T crossing 〈S, (V − S)〉 ⇒ w(u, v) ≤ w(x , y).

T ′ = T − (x , y) ∪ (u, v) ⇒ T ′ is also an MST.

⇒ A ∪ (u, v) is a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 15 / 59

A Schematic Greedy Algorithm

(1) A = ∅(2) while |A| < n − 1 do(3) find (u, v) s.t. A ∪ (u, v) is a minimal forest(4) A = A ∪ (u, v)(5) return(A)

Correctness:Due to the lemma, step (3) is always possible.By definition, A is always a minimal forest.At the end A has n − 1 edges.A forest with n − 1 edges is a tree.

Complexity: Depends on the implementation of step (3).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 16 / 59

A Schematic Greedy Algorithm

(1) A = ∅(2) while |A| < n − 1 do(3) find (u, v) s.t. A ∪ (u, v) is a minimal forest(4) A = A ∪ (u, v)(5) return(A)

Correctness:Due to the lemma, step (3) is always possible.By definition, A is always a minimal forest.At the end A has n − 1 edges.A forest with n − 1 edges is a tree.

Complexity: Depends on the implementation of step (3).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 16 / 59

A Schematic Greedy Algorithm

(1) A = ∅(2) while |A| < n − 1 do(3) find (u, v) s.t. A ∪ (u, v) is a minimal forest(4) A = A ∪ (u, v)(5) return(A)

Correctness:Due to the lemma, step (3) is always possible.By definition, A is always a minimal forest.At the end A has n − 1 edges.A forest with n − 1 edges is a tree.

Complexity: Depends on the implementation of step (3).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 16 / 59

Kruskal Algorithm

Sort the edges from the lightest to the heaviest.

Consider the edges following this order.

Start with an empty minimal forest.

Add an edge to the minimal forest if it doesn’t close a cycle.

Terminate when there are n − 1 edges in the minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 17 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 18 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 19 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 20 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 21 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 22 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 23 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 24 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 25 / 59

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 26 / 59

Kruskal Algorithm – Data Structure

A collection S of disjoint sets of vertices: S1,S2, . . . ,SkSi ∩ Sj = ∅ for all 1 ≤ i 6= j ≤ k .Initially, the collection is empty: S = ∅.At the end S contains one set of all the vertices: S = V.

Make-Set(x): Creates a new set containing only x and adds it tothe collection S: S = S ∪ x.

Find-Set(x): Finds the set in the collection S that contains x :Si ∈ S such that x ∈ Si .

Union(Si ,Sj ): replaces in the collection S the two sets Si and Sjwith their union: S = S −

Si ,Sj

Si ∪ Sj

.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 27 / 59

Kruskal Algorithm – Code

Variables:A: A minimal forest.Each tree in A is represented by a set of vertices.

Algorithm:(1) A = ∅(2) for all v ∈ V do Make-Set(v )(3) Sort(E) all edges from min to max(4) for each edge (u, v) in sorted order do(5) if Find-Set(u) 6=Find-Set(v ) then(6) A = A ∪ (u, v)(7) Union(Find-Set(u),Find-Set(v ))(8) return (A)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 28 / 59

Kruskal Algorithm – Correctness

Assume to the contrary that the output set A is not an MST.

Let (u, v) be the first edge that was added to A such thatA ∪ (u, v) is not a minimal forest.

In particular, at that time, A is a minimal forest.

Let C = 〈S, (V − S)〉 be a cut for the set u ∈ S.

(u, v) crosses C since v belongs to another set.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 29 / 59

Kruskal Algorithm – Correctness

Any lighter edge (x , y) is contained in C:If (x , y) 6∈ A, then both x and y belong to the same set before (x , y)was examined.

If (x , y) ∈ A, then both x and y belong to the same set after (x , y)was examined.

Sorting⇒ (u, v) is a minimal crossing edge for the cut C.

Greedy lemma⇒ A ∪ (u, v) is a minimal forest.

A contradiction.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 30 / 59

Kruskal Algorithm – Complexity

Sorting complexity: O(m log m).

Set operations complexity:There are n Make-Set operations.There are O(m) Find-Set operations.There are n − 1 Union operations.Possible to implement in time: O(m · α(m,n)).

α(m, n) is the inverse of the Ackerman’s function.The α(m, n) function grows very slowly.For example, m, n ≈ 1080 ⇒ α(m, n) ≤ 4.

Overall complexity: O(m log m).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 31 / 59

The Ackerman’s Function

Ak (n) =

2n for k = 0 and n ≥ 0Ak−1(1) for k ≥ 1 and n = 1Ak−1(Ak (n − 1)) for k ≥ 1 and n ≥ 2

A0(n) = 2 + · · ·+ 2 = 2n – The multiply-by-2 function.

A1(n) = 2× · · · × 2 = 2n – The power-of-2 function.

A2(n) = 22. ..2

– With n 2’s, the tower-of-2 function.

Each recursive level does the previous level’s operation n times.

A2(4) = 2222

= 216 = 65536.

Already A3(4) and A4(4) must be extremely large!

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 32 / 59

The Ackerman’s Function

Ak (n) =

2n for k = 0 and n ≥ 0Ak−1(1) for k ≥ 1 and n = 1Ak−1(Ak (n − 1)) for k ≥ 1 and n ≥ 2

A0(n) = 2 + · · ·+ 2 = 2n – The multiply-by-2 function.

A1(n) = 2× · · · × 2 = 2n – The power-of-2 function.

A2(n) = 22. ..2

– With n 2’s, the tower-of-2 function.

Each recursive level does the previous level’s operation n times.

A2(4) = 2222

= 216 = 65536.

Already A3(4) and A4(4) must be extremely large!

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 32 / 59

The Ackerman’s Function

Ak (n) =

2n for k = 0 and n ≥ 0Ak−1(1) for k ≥ 1 and n = 1Ak−1(Ak (n − 1)) for k ≥ 1 and n ≥ 2

A0(n) = 2 + · · ·+ 2 = 2n – The multiply-by-2 function.

A1(n) = 2× · · · × 2 = 2n – The power-of-2 function.

A2(n) = 22. ..2

– With n 2’s, the tower-of-2 function.

Each recursive level does the previous level’s operation n times.

A2(4) = 2222

= 216 = 65536.

Already A3(4) and A4(4) must be extremely large!

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 32 / 59

Very Slow Growing Functions

log∗ n – the inverse of the tower-of-2 function – is the least x such

that 22. ..2

x times is greater or equal to n.

For example, log∗(265536) = 5.

α(n) – the inverse Ackerman’s function – is the least x such thatAx (x) ≥ n.

α(n) is much slower than log∗ n.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 33 / 59

Kruskal Algorithm – A Simple Implementation

A set is represented by the following linked list:

Set Name

Set Size

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 34 / 59

Kruskal Algorithm – A Simple Implementation

A set is represented by the following linked list:

Set Name

Set Size

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 34 / 59

Kruskal Algorithm – A Simple Implementation

The head of the set contains two fields: the name and the size ofthe set.

The head of the set has two pointers: to the head and the tail of alinked list of vertices.

An array of n vertices each has two pointers: to the head of its setand to the next vertex in its linked list.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 35 / 59

Kruskal Algorithm – A Simple Implementation

Make-Set(x)⇒ O(1) complexity.

Sx

1

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 36 / 59

Kruskal Algorithm – A Simple Implementation

Find-Set(x)⇒ O(1) complexity.

xS

s

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 37 / 59

Kruskal Algorithm – A Simple Implementation

Union(R,S)⇒ O(s) complexity.

a b

a b c d

c d

RS

r+s

R

r

S

s

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 38 / 59

The Union Operation Implementation

Worst case complexity:Connect the larger set to the smaller set.Consider the n − 1 Union operations:

Union(S2,S1), Union(S3,S2), . . ., Union(Sn,Sn−1)The cost of Union(Si+1,Si ) is Ω(i).Total cost for the Union operations:

Ω(1) + Ω(2) + · · ·+ Ω(n − 1) = Ω(n2).

Modification:Connect the smaller set to the larger set.The pointer of each vertex is changed at most log n times, sinceafter each Union operation the pointer points to a set whose size isat least twice the size of the previous set.All together, for the n − 1 Union operations, for all vertices,O(n log n) complexity.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 39 / 59

The Union Operation Implementation

Worst case complexity:Connect the larger set to the smaller set.Consider the n − 1 Union operations:

Union(S2,S1), Union(S3,S2), . . ., Union(Sn,Sn−1)The cost of Union(Si+1,Si ) is Ω(i).Total cost for the Union operations:

Ω(1) + Ω(2) + · · ·+ Ω(n − 1) = Ω(n2).

Modification:Connect the smaller set to the larger set.The pointer of each vertex is changed at most log n times, sinceafter each Union operation the pointer points to a set whose size isat least twice the size of the previous set.All together, for the n − 1 Union operations, for all vertices,O(n log n) complexity.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 39 / 59

Implementation Complexity

n − 1 Union operations: O(n log n).

n Make-Set operations: n ·Θ(1) = Θ(n).

O(m) Find-Set operations: O(m) ·Θ(1) = Θ(m).

Sorting complexity: Θ(m log m) = Θ(m log n).

Kruskal algorithm complexity: Θ(m log m).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 40 / 59

Implementation Complexity

n − 1 Union operations: O(n log n).

n Make-Set operations: n ·Θ(1) = Θ(n).

O(m) Find-Set operations: O(m) ·Θ(1) = Θ(m).

Sorting complexity: Θ(m log m) = Θ(m log n).

Kruskal algorithm complexity: Θ(m log m).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 40 / 59

Prim Algorithm

Start with an arbitrary vertex as a singleton tree.

Maintain a minimal forest initially set to be this tree.

Find the closest vertex to the minimal forest.

Add this vertex and its closest edge to the minimal forest.

Continue until all vertices are in the minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 41 / 59

Example: Prim Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 42 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 43 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 44 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 45 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 46 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 47 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 48 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 49 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 50 / 59

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 51 / 59

Prim Algorithm – Implementing the Greedy Idea

Data structure:A minimal tree (forest) A that will be the MST.A starting vertex r that is the root of the MST.A priority queue Q for vertices not yet in A.A distance key(v) from A for vertices in Q.A candidate edge (v ,Π(v)) for any vertex v in Q.

The greedy idea:Repeat adding to A the edge (u,Π(u)) for the vertex u that has theminimum value for key(·) in Q.Update, if necessary, the distance key(v) and the candidate edge(v ,Π(v)) for each neighbor v of u that is still in Q.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 52 / 59

Prim Algorithm – Implementing the Greedy Idea

Data structure:A minimal tree (forest) A that will be the MST.A starting vertex r that is the root of the MST.A priority queue Q for vertices not yet in A.A distance key(v) from A for vertices in Q.A candidate edge (v ,Π(v)) for any vertex v in Q.

The greedy idea:Repeat adding to A the edge (u,Π(u)) for the vertex u that has theminimum value for key(·) in Q.Update, if necessary, the distance key(v) and the candidate edge(v ,Π(v)) for each neighbor v of u that is still in Q.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 52 / 59

Prim Algorithm – Code

(1) initialize Π(r) = nil ; A = ∅; Q = V − r(2) for all u ∈ Q do key(u) =∞(3) u = r(4) Repeat(5) for each neighbor v of u do(6) if (v ∈ Q) and w(u, v) < key(v) then(7) key(v) = w(u, v); Π(v) = u(8) u =Extract-Min(Q)(9) A = A ∪ (u,Π(u))(10) until Q = ∅(11) return (A)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 53 / 59

Prim Algorithm – Correctness

Assume to the contrary that the output set A is not an MST.

Let (u, v) be the first edge that was added to A such thatA ∪ (u, v) is not a minimal forest.

In particular, at that time, A is a minimal forest.

Let C = 〈Q, (V −Q)〉 be a cut.

The algorithm guarantees that C contains A.

The priority queue implies that (u, v) is a minimal crossing edgefor the cut C.

Greedy lemma⇒ A ∪ (u, v) is a minimal forest.

A contradiction.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 54 / 59

Prim Algorithm – Correctness

Assume to the contrary that the output set A is not an MST.

Let (u, v) be the first edge that was added to A such thatA ∪ (u, v) is not a minimal forest.

In particular, at that time, A is a minimal forest.

Let C = 〈Q, (V −Q)〉 be a cut.

The algorithm guarantees that C contains A.

The priority queue implies that (u, v) is a minimal crossing edgefor the cut C.

Greedy lemma⇒ A ∪ (u, v) is a minimal forest.

A contradiction.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 54 / 59

Prim Algorithm – Complexity I

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with an unsorted array:Θ(n) to build a queue.Θ(n) for the Extract-Min operation.Θ(1) for the Update-Queue operation.

Unsorted array total complexity:1×Θ(n) + (n − 1)×Θ(n) + Θ(m)×Θ(1) = Θ(n2)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 55 / 59

Prim Algorithm – Complexity I

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with an unsorted array:Θ(n) to build a queue.Θ(n) for the Extract-Min operation.Θ(1) for the Update-Queue operation.

Unsorted array total complexity:1×Θ(n) + (n − 1)×Θ(n) + Θ(m)×Θ(1) = Θ(n2)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 55 / 59

Prim Algorithm – Complexity II

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with a sorted array:Θ(n) to build a queue.Θ(1) for the Extract-Min operation.Θ(n) for the Update-Queue operation.

Sorted array total complexity:1×Θ(n) + (n − 1)×Θ(1) + Θ(m)×Θ(n) = Θ(nm)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 56 / 59

Prim Algorithm – Complexity III

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with a heap:Θ(n) to build a queue.Θ(log n) for the Extract-Min operation.Θ(log n) for the Update-Queue operation.

Heap total complexity:1×Θ(n) + (n − 1)×Θ(log n) + Θ(m)×Θ(log n) = Θ(m log n)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 57 / 59

Prim Algorithm – Complexity IV

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with a Fibonacci heap:Θ(n) to build a queue.Θ(log n) for the Extract-Min operation.Θ(1) (amortized) for the Update-Queue operation.

Fibonacci heap total complexity:1×Θ(n) + (n − 1)×Θ(log n) + Θ(m)×Θ(1) = Θ(m + n log n)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 58 / 59

Kruskal vs. Prim

Complexity:Kruskal is an O(m log m) algorithm.

Prim is an O(m + n log n) algorithm.

Implementation:Kruskal is a distributed algorithm.

Prim is a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 59 / 59

Kruskal vs. Prim

Complexity:Kruskal is an O(m log m) algorithm.

Prim is an O(m + n log n) algorithm.

Implementation:Kruskal is a distributed algorithm.

Prim is a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 59 / 59


Top Related