Beruflich Dokumente
Kultur Dokumente
5
Central office
WIRING: NAÏVE APPROACH
6
Central office
Expensive!
WIRING: BETTER APPROACH
7
Central office
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
10
HOW TO FIND A SAFE EDGE
We need some definitions and a theorem.
A cut (S,V-S) of an undirected graph
G=(V,E) is a partition of V.
An edge crosses the cut (S,V-S) if one of its
endpoints is in S and the other is in V-S.
An edge is a light edge crossing a cut if its
weight is the minimum of any edge crossing
the cut.
HOW TO FIND A SAFE EDGE
8 7
b c d 9
4
2
S↑ 11 14 e ↑S
a i 4
7 6
10
8
V-S↓ h g f ↓ V-S
1 2
28
MULTIPLE OPERATIONS
Each set as a linked-list, with head and tail, and each node
contains value, next node pointer and back-to
representative pointer.
Example:
MAKE-SET costs O(1): just create a single element list.
FIND-SET costs O(1): just return back-to-representative
pointer.
LINKED-LISTS FOR TWO SETS
Set {c,h,e}
head c h e
tail
Set {f, g}
head f g
tail
UNION of
two Sets
head f g c h e
tail
UNION IMPLEMENTATION
c cf cf
c d
h e d
Three operations
MAKE-SET(x): create a tree containing x. O(1)
FIND-SET(x): follow the chain of parent pointers
until to the root. O(height of x’s tree)
UNION(x,y): let the root of one tree point to the root
of the other. O(1)
It is possible that n-1 UNIONs results in a tree
of height n-1. (just a linear chain of n nodes).
So n FIND-SET operations will cost O(n2).
UNION BY RANK & PATH COMPRESSION
e c d e
c
ALGORITHM FOR DISJOINT-SET FOREST
UNION(x,y)
MAKE-SET(x) 1. LINK(FIND-SET(x),FIND-SET(y))
1. p[x]x
2. rank[x]0 LINK(x,y)
FIND-SET(x)
1. if x p[x]
1. if rank[x]>rank[y]
2. then p[x] FIND-SET(p[x])
2. then p[y] x
3. return p[x]
3. else p[x] y
4. if rank[x]=rank[y]
5. then rank[y]++
MST-Kruskal(G,w)
1 A
2 for each vertex v V[G] do
3 Make-Set(v) //creates set containing v (for initialization)
4 sort the edges of E
5 for each (u,v)E do
6 if Find-Set(u) Find-Set(v) then // different component
7 A A {(u,v)}
8 Union(Set(u),Set(v)) // merge
9 return A
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 a b c d e f g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 a b c d e f g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 a b f c d e g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 b f a c d e g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 b f a e c d g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 b f a e c d g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 b f a e c d g h
EXAMPLE
c-d: 3 WITH DISJOINT SET UNION
b-f: 5 a
b-a: 6
6 12
5 9
f-e: 7 b f g
b-d: 8 14 7
8 15
f-g: 9 c e h
d-e: 10
3 10
a-f: 12 d
b-c: 14
e-h: 15 b f a e c d g h
KRUSKAL'S ALGORITHM
MST-Kruskal(G,w)
1 A
2 for each vertex v V[G] do
3 Make-Set(v) //creates set containing v (for initialization)
4 sort the edges of E
5 for each (u,v)E do
6 if Find-Set(u) Find-Set(v) then // different component
7 A A {(u,v)}
8 Union(Set(u),Set(v)) // merge
9 return A
RUNNING TIME OF KRUSKAL’S ALGORITHM
Kruskal’s Algorithm consists of two stages.
Initializing the set A in line 1 takes O(1) time.
Sorting the edges by weight in line 4.
takes O(E lg E)
Performing
|V| MakeSet() operations for loop in lines 2-3.
|E| FindSet(), for loop in lines 5-8.
|V| - 1 Union(), for loop in lines 5-8.
which takes O(E lg E)
6
5
Not yet discovered
7
6 2
5 9
4 Not yet discovered
7
6 2
5 9
4
7
NOTATION
Tree-vertices: in the tree constructed so far
Non-tree vertices: rest of vertices
53
PRIM’S ALGORITHM IN WORDS
Randomly pick a vertex as the initial tree T
Gradually expand into a MST:
in T
Select the vertex that is closest to T
Add it to T
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
EXAMPLE
a
6 12
5 9
b f g
14 7
8 15
c e h
3 10
d
PRIM ALGORITHM:VARIABLES
r:Grow the minimum spanning tree from the root vertex “r”.
V a b c d e f g h i
T 1 0 0 0 0 0 0 0 0
Key 0 - - - - - - - -
p -1 - - - - - - - -
69
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 0 0 0 0 0 0 0 0
Key 0 4 - - - - - 8 -
p -1 a - - - - - a -
70
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Important: Update Key[v] only if T[v]==0
V a b c d e f g h i
T 1 1 0 0 0 0 0 0 0
Key 0 4 8 - - - - 8 -
p -1 a b - - - - a -
71
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 0 0 0 0 0 0
Key 0 4 8 7 - 4 - 8 2
p -1 a b c - c - a c
72
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 0 0 0 0 0 1
Key 0 4 8 7 - 4 6 7 2
p -1 a b c - c i i c
73
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 0 0 1 0 0 1
Key 0 4 8 7 10 4 2 7 2
p -1 a b c f c f i c
74
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 0 0 1 1 0 1
Key 0 4 8 7 10 4 2 1 2
p -1 a b c f c f g c
75
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 0 0 1 1 1 1
Key 0 4 8 7 10 4 2 1 2
p -1 a b c f c f g c
76
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 1 0 1 1 1 1
Key 0 4 8 7 9 4 2 1 2
p -1 a b c d c f g c
77
The execution of Prim's algorithm
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
V a b c d e f g h i
T 1 1 1 1 1 1 1 1 1
Key 0 4 8 7 9 4 2 1 2
p -1 a b c d c f g c
78
COMPLEXITY: PRIM ALGORITHM
MST-Prim(G,r)
01 Q V[G] // Q – vertices out of T
02 for each u Q
O(V)
03 key[u]
04 key[r] 0
05 p[r] NIL
06 while Q do O(V)
07 u ExtractMin(Q) // making u part of T Heap: O(lgV)
08 for each v Adj[u] do Overall: O(E)
09 if v Q and w(u,v) < key[v] then
10 p[v] u
11 key[v] w(u,v) Decrease Key: O(lgV)