Lecture 1: The Greedy Method. 主講人 : 虞台文. Content. What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree Kruskal’s Algorithm Prim’s Algorithm Shortest Path Problem Dijkstra’s Algorithm Huffman Codes. Lecture 1: The Greedy Method. What is it?.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Lecture 1: The Greedy Method
主講人:虞台文
Lecture 1: The Greedy Method
What is it?
Is the solution alwaysoptimal?
FOR i = 1 to k
pick out the largest number and
delete this number from the input.
ENDFOR
Is the greedy solution alwaysoptimal?
Is the solution optimal?
Is the greedy solution optimal?
Is the greedy solution optimal?
The optimal path
Is the greedy solution optimal?
What algorithm can be used to find the optimum?
The optimal path
Lecture 1: The Greedy Method
Activity Selection Problem
ai = [si, fi)
Assume thatfi’s are sorted.
1
2
3
4
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Is the solution optimal?
1
2
3
4
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Is the solution optimal?
1
2
3
4
5
6
7
8
9
10
11
Greedy-Activity-Selector (s, f)
// Assume that f1 f2 ... fn
n length [s]
A { 1 }
j 1
for i 2 to n
if sifj then
AA{ i }
ji
return A
Is the algorithm optimal?
Lecture 1: The Greedy Method
Fractional Knapsack Problem
1
2
3
4
5
Value:
($ per ml)
10 ml
“knapsack”
Items:
wi:
4 ml
8 ml
2 ml
6 ml
1 ml
bi:
$12
$32
$40
$30
$50
3
4
20
5
50
AlgorithmfractionalKnapsack(S,W)
Input:set S of items w/ benefit biand weight wi; max. weight W
Output:amount xi of each item i to maximize benefit w/ weight at most W
for each item i in S
xi 0
vi bi / wi{value}
w 0{total weight}
whilew < W
remove item i with highest vi
xi min{wi , W w}
w w + min{wi , W w}
Does the algorithm always gives an optimum?
Which boxes should be chosen to maximize the amount of money while still keeping the overall weight under 15 kg ?
Is the fractional knapsack algorithm applicable?
Lecture 1: The Greedy Method
Minimum
Spanning Tree
B
B
B
A
A
A
C
C
C
D
D
D
E
E
E
Undirected Graph
Some Spanning Trees
8
7
4
9
2
14
11
4
7
16
b
c
d
8
10
1
2
a
i
e
b
c
d
h
g
f
a
i
e
h
g
f
b
c
d
a
i
e
h
g
f
8
7
4
9
2
14
11
4
7
16
8
10
1
2
b
c
d
a
i
e
h
g
f
G = (V, E) – Graph
w: ER+– Weight
T Tree
MST-Kruksal(G)
T← Ø
for each vertex vV[G]
Make-Set(v) // Make separate sets for vertices
sort the edges by increasing weight w
for each edge (u, v)E, in sorted order
if Find-Set(u) ≠ Find-Set(v) // If no cycles are formed
T ← T {(u, v)}// Add edge to Tree
Union(u, v) // Combine Sets
return T
O(|E|log|E|)
G = (V, E) – Graph
w: ER+– Weight
T Tree
MST-Kruksal(G , w)
T← Ø
for each vertex vV[G]
Make-Set(v) // Make separate sets for vertices
sort the edges by increasing weight w
for each edge (u, v)E, in sorted order
if Find-Set(u) ≠ Find-Set(v) // If no cycles are formed
T ← T {(u, v)}// Add edge to Tree
Union(u, v) // Combine Sets
return T
O(1)
O(|V|)
O(|E|log|E|)
O(|E|)
O(|V|)
O(1)
8
7
4
9
2
14
11
4
7
16
b
c
d
8
10
1
2
a
i
e
b
c
d
h
g
f
a
i
e
h
g
f
8
7
d
b
b
c
d
c
4
9
2
a
a
i
e
e
i
14
11
4
7
16
8
10
h
g
f
h
g
f
1
2
b
b
c
c
d
d
a
i
e
a
i
e
h
g
f
h
g
f
G = (V, E) – Graph
w: ER+– Weight
r – Starting vertex
Q – Priority Queue
Key[v] – Key of Vertex v
π[v] –Parent of Vertex v
Adj[v] – Adjacency List of v
MST-Prim(G, w, r)
Q← V[G]// Initially Q holds all vertices
for each uQ
Key[u] ← ∞// Initialize all Keys to ∞
Key[r] ← 0 // r is the first tree node
π[r] ← Nil
while Q ≠ Ø
u ← Extract_min(Q) // Get the min key node
for each v Adj[u]
if vQ and w(u, v) < Key[v]// If the weight is less than the Key
π[v] ← u
Key[v] ← w(u, v)
O(|E|log|V|)
G = (V, E) – Graph
w: ER+– Weight
r – Starting vertex
Q – Priority Queue
Key[v] – Key of Vertex v
π[v] –Parent of Vertex v
Adj[v] – Adjacency List of v
MST-Prim(G, r)
Q← V[G]// Initially Q holds all vertices
for each uQ
Key[u] ← ∞// Initialize all Keys to ∞
Key[r] ← 0 // r is the first tree node
π[r] ← Nil
while Q ≠ Ø
u ← Extract_min(Q) // Get the min key node
for each v Adj[u]
if vQ and w(u, v) < Key[v]// If the weight is less than the Key
π[v] ← u
Key[v] ← w(u, v)
Are the algorithms optimal?
Yes
Lecture 1: The Greedy Method
Shortest Path Problem
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
0
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
0
5
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
0
5
5
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
0
5
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
8
14
0
7
5
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
8
14
0
7
5
7
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
8
14
0
5
7
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
8
14
13
0
5
7
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
9
8
8
14
13
0
5
7
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
8
14
13
0
5
7
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
8
14
9
13
0
5
7
u
v
1
9
2
3
9
0
s
4
6
7
5
2
y
x
8
13
14
9
9
0
5
7
G = (V, E) – Graph
w: ER+– Weight
s – Source
d[v] – Current shortest distance from s to v
S – Set of nodes whose shortest distance is known
Q – Set of nodes whose shortest distance is unknown
Dijkstra(G, w ,s)
for each vertex vV[G]
d[v] // Initialize all distances to
π[v] Nil
d[s] 0// Set distance of source to 0
S
QV[G]
while Q≠
u Extract_Min(Q) // Get the min in Q
SS {u}// Add it to the already known list
for each vertex v Adj[u]
if d[v] > d[u] + w(u, v)// If the new distance is shorter
d[v] d[u] + w(u, v)
π[v] u
Lecture 1: The Greedy Method
Huffman Codes
Suppose we have a data consists of 100,000 characters with following frequencies.
Suppose we have a data consists of 100,000 characters with following frequencies.
Total Bits:
Fixed Length Code
345,000 + 313,000 + 312,000 + 316,000 + 39,000 + 35,000= 300,000
Variable Length Code
145,000 + 313,000 + 312,000 + 316,000 + 49,000 + 45,000= 224,000
0
1
a:45
0
1
0
1
0
1
b:13
c:12
d:16
0
1
f:5
e:9
In which no codeword is a prefix of other codeword.
Encode
aceabfd
=0100110101011100111
Decode
0100110101011100111
a
c
e
a
b
f
d
0
1
a:45
0
1
0
1
0
1
b:13
c:12
d:16
0
1
f:5
e:9
100
55
25
30
14
14
0
1
f:5
e:9
f:5
e:9
c:12
b:13
d:16
a:45
14
14
0
0
1
1
f:5
f:5
e:9
e:9
f:5
e:9
c:12
b:13
d:16
a:45
c:12
b:13
d:16
a:45
25
0
1
14
b:13
c:12
0
1
f:5
e:9
c:12
b:13
d:16
a:45
25
25
0
0
1
1
14
14
b:13
b:13
c:12
c:12
0
0
1
1
f:5
f:5
e:9
e:9
a:45
d:16
c:12
b:13
d:16
a:45
25
30
0
1
0
1
14
14
b:13
c:12
d:16
0
0
1
1
f:5
f:5
e:9
e:9
a:45
d:16
25
25
30
30
0
0
1
1
0
0
1
1
14
14
14
b:13
b:13
c:12
c:12
d:16
d:16
0
0
0
1
1
1
f:5
f:5
f:5
e:9
e:9
e:9
a:45
a:45
d:16
55
0
1
25
25
30
30
0
0
1
1
0
0
1
1
14
14
b:13
b:13
c:12
c:12
d:16
d:16
0
0
1
1
f:5
f:5
e:9
e:9
a:45
55
55
0
0
1
1
25
25
25
30
30
30
0
0
0
1
1
1
0
0
0
1
1
1
14
14
14
b:13
b:13
b:13
c:12
c:12
c:12
d:16
d:16
d:16
0
0
0
1
1
1
f:5
f:5
f:5
e:9
e:9
e:9
a:45
a:45
100
0
1
55
55
a:45
0
0
1
1
25
25
25
30
30
30
0
0
0
1
1
1
0
0
0
1
1
1
14
14
14
b:13
b:13
b:13
c:12
c:12
c:12
d:16
d:16
d:16
0
0
0
1
1
1
f:5
f:5
f:5
e:9
e:9
e:9
a:45
100
0
1
55
55
a:45
0
0
1
1
25
25
25
30
30
30
0
0
0
1
1
1
0
0
0
1
1
1
14
14
14
b:13
b:13
b:13
c:12
c:12
c:12
d:16
d:16
d:16
0
0
0
1
1
1
f:5
f:5
f:5
e:9
e:9
e:9
Huffman tree built
a:45
Huffman (C)
n |C|
QC
for i 1 to n 1
z Allocate-Node ()
x left[z] Extract-Min (Q) // least frequent
y right[z] Extract-Min (Q)// next least
f[z] f[x] + f[y] // update frequency
Insert ( Q, z )
return Extract-Min (Q)
Exercise