prim’s algorithm a proof. suppose we have a tree t that is the minimal spanning tree for a graph....

40
Prim’s Algorithm A Proof

Upload: shanon-neal

Post on 16-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Prim’s Algorithm

A Proof

Page 2: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

• Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then there is an arc, a, in P that is not in T. If arc a connects node x to node y, then there is a path within the tree of T that also connects x to y:

x

y

P

T

P T

Page 3: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Case 1: The weight of P is smaller than one of the edges on the path from the tree T

• Then we remove the edge with highest weight from T and replace it with the edge from P. This gives a smaller spanning tree than T – and contradicts the fact that T is the minimal spanning tree.

x

y

P

T

Page 4: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Case 2: The weight of P is greater than the weight of all the edges in the path from T.

• Then consider the step – in Prim’s algorithm- when we add edge P.

• P is the shortest edge remaining which can be added to the graph. • But we would add xs before P (or st or tu or uy – whichever is still

available to add.• So we never add P – a contradiction!

x

P

T

y

s

t

u

Page 5: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first search

• Algorithm DF(G,n)– Start with a given node, n.

• For all arcs a connected to n Do– DF(G\{n}, a)

End - Do

Page 6: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first example

A

B

C

D

E

F

G

HI

JK

Page 7: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first example

A

B

C

D

E

F

G

HI

JK

Page 8: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Page 9: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Page 10: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Page 11: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Continue: ABEDCHIJK (BACKTRACK to D) GF

Page 12: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first example-from K

A

B

C

D

E

F

G

HI

JK

Page 13: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Breadth-First

• Algorithm BF(G,n)– Start with a given node, n.

• For all arcs a connected to n Do– Add nodes at end of each vertex.

For all arcs a connected to n Do

- BF(G\{arcs}, a)

Page 14: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Breadth-first example

A

B

C

D

E

F

G

HI

JK

Page 15: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Add arcs connected to a

A

B

C

D

E

F

G

HI

JK

ABCDE

Page 16: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Add arcs connected to b

A

B

C

D

E

F

G

HI

JK

ABCDEF

Page 17: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then
Page 18: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Prim’s Algorithm

A Proof

Page 19: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

• Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then there is an arc, a, in P that is not in T. If arc a connects node x to node y, then there is a path within the tree of T that also connects x to y:

x

y

P

T

P T

Page 20: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Case 1: The weight of P is smaller than one of the edges on the path from the tree T

• Then we remove the edge with highest weight from T and replace it with the edge from P. This gives a smaller spanning tree than T – and contradicts the fact that T is the minimal spanning tree.

x

y

P

T

Page 21: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Case 2: The weight of P is greater than the weight of all the edges in the path from T.

• Then consider the step – in Prim’s algorithm- when we add edge P.

• P is the shortest edge remaining which can be added to the graph. • But we would add xs before P (or st or tu or uy – whichever is still

available to add.• So we never add P – a contradiction!

x

P

T

y

s

t

u

Page 22: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first search

• Algorithm DF(G,n)– Start with a given node, n.

• For all arcs a connected to n Do– DF(G\{n}, a)

End - Do

Page 23: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first example

A

B

C

D

E

F

G

HI

JK

Page 24: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first example

A

B

C

D

E

F

G

HI

JK

Page 25: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Page 26: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Page 27: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Page 28: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

A

B

C

D

E

F

G

HI

JK

Continue: ABEDCHIJK (BACKTRACK to D) GF

Page 29: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Depth-first example-from K

A

B

C

D

E

F

G

HI

JK

Page 30: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Breadth-First

• Algorithm BF(G,n)– Start with a given node, n.

• For all arcs a connected to n Do– Add nodes at end of each vertex.

For all arcs a connected to n Do

- BF(G\{arcs}, a)

Page 31: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Breadth-first example

A

B

C

D

E

F

G

HI

JK

Page 32: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Add arcs connected to a

A

B

C

D

E

F

G

HI

JK

ABCDE

Page 33: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Add arcs connected to b

A

B

C

D

E

F

G

HI

JK

ABCDEF

Page 34: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Continue:

A

B

C

D

E

F

G

HI

JK

ABCDEFHGJIK

Page 35: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Do breadth-first from D

A

B

C

D

E

F

G

HI

JK

Page 36: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Be able to:

• Do depth first search and list nodes

• Do breadth-first search and list nodes

• Do either search and give the search tree.

Page 37: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Boolean Algebras

• Named after mathematician George Boole

• Created Boolean-algebras around 1850

• He saw a connection between logic and certain algebraic properties

• He turned logic into algebra.

• Used in computers about 100 years after its invention – pure mathematics becomes applied mathematics.

Page 38: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Definition – Boolean Algebra

• A Boolean algebra is a set B, with two binary operations, + and x, a unary operator ‘ and in which there are special elements 0 and 1 such that

x+y=y+x x y = y x(x+y)+z=x+(y+z) (x y) z = x (y z)x+(y z) = (x+y) x (x + z) x (y+z)=xy+xzx+0 = x x 1 = 1x + x’= 1 x x’ = 0

Page 39: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Examples

• 1. Sets with union (+), intersection (x) and set complement. What are 0 and 1?

• 2. Logic statements with or(+) and (x) and not(‘). Again, what are zero and one?

• 3. B={0,1} with usual multiplication and addition, except with 1+1 = 0. What is complement?

Page 40: Prim’s Algorithm A Proof. Suppose we have a tree T that is the minimal spanning tree for a graph. Let P be the spanning tree from Prim’s alg. and Then

Check that every Boolean algebra has the idempotent property

• Show x+x=x.

• Proof• (x+x)=(x+x) x 1 = (x+x) (x+x’)• = x + x x’ (dist prop)• X + 0 = x.

• Look at each example in terms of the idempotent property.