Lecture 3
Lecture 3
• Algorithms:
1) sorting all the jobs (lectures) in the increasing order of finish (start)
time.
2) select jobs one by one constrained by feasibility.
arrange lectures one by one, don’t open new classrooms unless
necessary.
1
• Proofs:
-Interval scheduling:
Let G be the optimal solution returned by greedy algorithm
Let OPT be an optimal solution.
Gradually change OPT to G without hurting its quality.
- Interval partitioning:
Define “depth” and show that the number of classrooms >=depth.
Show the number of classrooms used in greedy method<=depth.
Therefore, G is optimal.
2
Week 3: Minimum Spanning Tree
(MST)
•Definition of MST
•Generic MST algorithm
•Kruskal's algorithm
•Prim's algorithm
3
• Graph
– Subgraph
• Tree: 1) connected, and
2) no cycle/unique path between two
nodes/the no of edges=the no of nodes -1.
• Forest:
– a disjoint union of trees/a graph without cycles.
4
Problem: Rail Network
5
Naïve Approach
Expensive!
6
Better Approach
8
Given a connected and undirected graph G=(V,E),
each edge in E has a weight (cost, or length).
A minimum spanning tree (MST) is a spanning tree
with minimum total weight (cost, or length).
9
Minimum Spanning Tree: Example
• Example: The graph
11
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 length
is the shortest among all the edges crossing the
cut.
12
8 7
b c d
4 9
2
S↑ a 11 i 14 e ↑S
4
7 6 10
7 h g f
V-S↓ ↓ V-S
1 2
C D
15
The algorithms of Kruskal and Prim
• The two algorithms are elaborations of the generic
algorithm.
• They each use a specific rule to determine a safe
edge in line 3 of GENERIC_MST.
• In Kruskal's algorithm,
– The set A is a forest.
– The safe edge added to A is always a least-weight edge
in the graph that connects two distinct components.
• In Prim's algorithm,
– The set A forms a single tree.
– The safe edge added to A is always a least-weight edge
connecting the tree to a vertex not in the tree.
16
Kruskal’s Algorithm
17
Kruskal's algorithm(basic part)
1 (Sort the edges in an increasing order)
2 A:={}
3 while E is not empty do {
3 take an edge (u, v) that is shortest in E
and delete it from E
4 if u and v are in different components then
add (u, v) to A
}
Note: each time a shortest edge in E is considered.
18
Graph
B 4 C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
A 4 B A 1 D
B 4 C B 4 D
B 4 C B 10 J C 2 E
4
2 1
C 1 F D 5 H
A 4 E
1 F
2 D 6 J E 2 G
D 3
10
G 5
F 3 G F 5 I
5 6 3
4
I G 3 I G 4 J
H
2 3
J H 2 J I 3 J
Sort Edges A 1 D C 1 F
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Cycle A 1 D C 1 F
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Cycle A 1 D C 1 F
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F
C 2 E E 2 G
B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Minimum Spanning Tree Graph
B 4 C 4
B C
4 4
2 1 2 1
A E A 4
1 F E F
1
D 2 2
D 3
10
G G 5
3 5 6 3
4
I I
H H
2 3 3
J 2 J
Kruskal's algorithm
MST_KRUSKAL(G,w)
1 A:={}
2 for each vertex v in V[G]
3 do MAKE_SET(v)
4 sort the edges of E by nondecreasing weight w
5 for each edge (u,v) in E, in order by
nondecreasing weight
6 do if FIND_SET(u) != FIND_SET(v)
7 then A:=A∪{(u,v)}
8 UNION(u,v)
9 return A
34
Disjoint-Set
• Keep a collection of sets S1, S2, .., Sk,
– Each Si is a set, e,g, S1={v1, v2, v8}.
• Three operations
– Make-Set(x)-creates a new set whose only member is x.
– Union(x, y) –unites the sets that contain x and y, say, Sx
and Sy, into a new set that is the union of the two sets.
– Find-Set(x)-returns a pointer to the representative of
the set containing x.
– Each operation takes O(log n) time.
35
• Our implementation uses a disjoint-set data
structure to maintain several disjoint sets of
elements.
• Each set contains the vertices in a tree of the
current forest.
• The operation FIND_SET(u) returns a
representative element from the set that contains u.
• Thus, we can determine whether two vertices u
and v belong to the same tree by testing whether
FIND_SET(u)=FIND_SET(v).
• The combining of trees is accomplished by the
UNION procedure.
• Running time O(|E| log (|E|)).
36
Prim’s Algorithm
37
Prim's algorithm(basic part)
MST_PRIM(G,w,r)
1. A={}
2. S:={r}
3. Q=V-{r};
4. while Q is not empty do {
5 take an edge (u, v) such that (1) u S and v Q (v S ) and
(u, v) is the shortest edge satisfying (1)
6 add (u, v) to A, add v to S and delete v from Q
}
38
Graph
B 4 C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Complete Graph Minimum Spanning Tree
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A E F
1
D 2 3
10 D 2
G 5
G
5 6 3
3
4
I
H I
3 H
2 J
2 3
J
Prim's algorithm
MST_PRIM(G, w, r) Q (priority queue): contain all the vertices that have not
1 for each v in V do yet been included in the tree. V\Q: vertices in the tree
2 key[v]:=∞, parent[v]:=NIL
Parent[v]: the nearest vertex in the tree to v
3 A≔ Key[v]: the length of edge (v, parent[v])
4 key[r]:=0; parent[r]=NIL;
5 Q(V, key) /* initialize Q.
6 while Q!={} do A contains selected edges for MST.
7 u:=EXTRACT_MIN(Q); if parent[u]NIL, A:=A U (u, parent[u]).
8 for each v in Adj[u] do
9 if v in Q and w(u,v)<key[v]
10 then parent[v]:=u
11 k:=w(u,v)
12 Update(v,k)
i.e. u is not r.
Node A B C D E
A 4
Key 0 inf inf inf inf
1
Parent NIL NIL NIL NIL NIL
C
10
After one pass in while loop. The tree has one node A. Q is:
Node B C D E 5 6
Key 4 1 inf inf
Parent A A NIL NIL
D
After second pass in while loop. The tree has two node A C, and one link (A,C), Q is:
2 E
Node B D E
Key 4 5 6
Parent A C C
After third pass in while loop. The tree has two node A C, B and two links
(A,C) and (A,B), Q is:
Node E
Node D E Key 2
Key 5 6 Parent D
Parent C C
B
4
After 1-5: The tree is empty. Q contains:
Node A B C D E
A 4
Key 0 inf inf inf inf
1
Parent NIL NIL NIL NIL NIL
C
10
After one pass in while loop. The tree has one node A. Q is:
Node B C D E 5 6
Key 4 1 inf inf
Parent A A NIL NIL
D
After second pass in while loop. The tree has two node A C, and one link (A,C), Q is:
2 E
Node B D E
Key 4 5 6
Parent A C C
After third pass in while loop. The tree has two node A C, B and two links
(A,C) and (A,B), Q is:
Node E
Node D E Key 2
Key 5 6 Parent D
Parent C C
priority queue Q: data structure containing n
items: each item u has a key value key[u].
three important operations
– EXTRACT-MIN(Q) –takes O(log (n)) times if
there are n items in Q.
– Insert an element (key u) into Q- takes
O(log(n)) time.
– Update(u, k): update the key value of element u
to k - takes O (log (n)) time.
54
• Grow the minimum spanning tree from the root vertex r.
• Q is a priority queue, holding all vertices that are not in the tree now.
• key[v]: the length of the shortest edge linking v with a vertex in the tree.
• parent[v]: the parent of v in the tree. (v, partent[v]) is the shortest among all the
edges linking v with a vertex in the tree.
• When the algorithm terminates, Q is empty; the minimum spanning tree A for G
is thus A={(v,parent[v]):v∈V-{r}}.
• Using Fibonacci heap structure to store Q, we can reduce the complexity to O(|
E|+|V|log|V|) since Update(u, k) can be done in O(1) time for Fibonacci heap.
55
Summary
• MST
• Cut, safe edge.
• Generic MST algorithm
• Kruskal's algorithm
• Prim's algorithm
56
Challenge Problem
1. Consider the problem of computing a maximum spanning
tree, namely the spanning tree that maximizes the sum of
edge costs. Do Prim and Kruskal’s algorithm work for
this problem (assuming of course that we choose the
crossing edge with maximum cost)?
57