Upload
shanon-neal
View
222
Download
1
Embed Size (px)
Citation preview
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 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
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
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
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
Depth-first example
A
B
C
D
E
F
G
HI
JK
Depth-first example
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
Continue: ABEDCHIJK (BACKTRACK to D) GF
Depth-first example-from K
A
B
C
D
E
F
G
HI
JK
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)
Breadth-first example
A
B
C
D
E
F
G
HI
JK
Add arcs connected to a
A
B
C
D
E
F
G
HI
JK
ABCDE
Add arcs connected to b
A
B
C
D
E
F
G
HI
JK
ABCDEF
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 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
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
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
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
Depth-first example
A
B
C
D
E
F
G
HI
JK
Depth-first example
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
A
B
C
D
E
F
G
HI
JK
Continue: ABEDCHIJK (BACKTRACK to D) GF
Depth-first example-from K
A
B
C
D
E
F
G
HI
JK
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)
Breadth-first example
A
B
C
D
E
F
G
HI
JK
Add arcs connected to a
A
B
C
D
E
F
G
HI
JK
ABCDE
Add arcs connected to b
A
B
C
D
E
F
G
HI
JK
ABCDEF
Continue:
A
B
C
D
E
F
G
HI
JK
ABCDEFHGJIK
Do breadth-first from D
A
B
C
D
E
F
G
HI
JK
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.
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.
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
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?
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.