Contents of Chapter 4: - Chapter 4 The Greedy Method
Contents of Chapter 4: - Chapter 4 The Greedy Method
북
• Chapter 4 The Greedy method 대
학
– 4.1 The general method 교
– 4.2 Knapsack problem 전
자
– 4.3 Tree vertex splitting 정
보
– 4.4 Job sequencing with deadlines 공
– 4.5 Minimum cost spanning trees 학
부
– 4.6 Optimal storage on tapes
– 4.7 Optimal merge patterns
– 4.8 Single-source shortest paths
– 4.9 References and readings
– 4.10 Additional exercises
4.1 General Method 전
북
• Greedy method control abstraction for subset paradigm 대
학
(Program 4.1) 교
전
SolType Greedy(Type a[], int n) 자
// a[1:n] contains the n inputs. 정
보
{ 공
SolType solution = EMPTY; // Initialize the solution 학
부
for (int i = 1; i <= n; i++) {
Type x = Select(a);
if Feasible(solution, x)
solution = Union(solution, x);
}
return solution;
}
– Terminologies: feasible solution, objective function, optimal solution
– Subset paradigm vs. ordering paradigm
• Subset paradigm: selection of optimal subset (Sec. 4.2 – 4.5)
• Ordering paradigm: finding optimal ordering (Sec. 4.6-4.8)
4.2 Knapsack Problem 전
북
• Problem definition 대
학
– Given n objects and a knapsack where object i has a weight wi and 교
the knapsack has a capacity m 전
– If a fraction xi of object i placed into knapsack, a profit pixi is earned 자
정
– The objective is to obtain a filling of knapsack maximizing the total 보
profit 공
학
• Problem formulation (Formula 4.1-4.3) 부
maximize pi xi (4.1)
1 i n
subject to wi xi m (4.2)
1i n
and 0 xi 1, 1 i n (4.3)
Spanning trees
• Applications
– Obtaining an independent set of circuit equations for an electric
network
– etc
4.5 Minimum-cost Spanning Trees 전
북
• Example of MCST (Figure 4.6) 대
학
– Finding a spanning tree of G with minimum cost 교
전
자
1 1 정
28 보
공
10 2 10 2 학
부
14 16 14 16
6 7 3 6 7 3
24
25 18 12 25 12
5 5
22 4 22 4
(a) (b)
4.5.1 Prim’s Algorithm 전
북
• Example 4.6 (Figure 4.7) 대
학
1 1 1 교
10 10 전
10 2 2 2 자
정
3 3 3 보
6 7 6 7 6 7 공
학
25 25 부
5 5 5
4 4 22 4
(a (b (c)
)1 1) 1
10 10 2 10 2
2 16
16 14
3 6 7 3 6 7 3
6 7
25 25 12 25 12
12
5 5 5
22 4 22 4 22 4
(d (e) (f)
4.5.1 Prim’s Algorithm 전
북
• Implementation of Prim’s algorithm 대
학
– How to determine the next edge to be added? 교
• Associating with each vertex j not yet included in the tree a value 전
near(j) 자
정
• near(j): a vertex in the tree such that cost(j,near(j)) is minimum 보
among all choices for near(j) 공
• The next edge is defined by the vertex j such that near(j)0 (j not 학
already in the tree) and cost(j,near(j)) is minimum 부
– eg, Figure 4.7 (b)
near(1)=0 // already in the tree
near(2)=1, cost(2, near(2))=28
near(3)=1 (or 5 or 6), cost(3, near(3))= // no edge to the tree
near(4)=5, cost(4, near(4))=22
near(5)=0 // already in the tree
near(6)=0 // already in the tree
near(7)=5, cost(7, near(7))=24
z1 15 20 30 30
x1 x5 x2
5 10
x4 x3
– Total number of record moves = weighted external path length
n
d q
i 1
i i
– The optimal 2-way merge pattern = binary merge tree with minimum
weighted external path length
4.7 Optimal Merge Patterns 전
북
• Algorithm (Program 4.13) 대
학
struct treenode { 교
struct treenode *lchild, *rchild; 전
자
int weight; 정
}; 보
typedef struct treenode Type; 공
학
부
Type *Tree(int n)
// list is a global list of n single node
// binary trees as described above.
{
for (int i=1; i<n; i++) {
Type *pt = new Type;
// Get a new tree node.
pt -> lchild = Least(list); // Merge two trees with
pt -> rchild = Least(list); // smallest lengths.
pt -> weight = (pt->lchild)->weight
+ (pt->rchild)->weight;
Insert(list, *pt);
}
return (Least(list)); // Tree left in l is the merge tree.
}
4.7 Optimal Merge Patterns 전
북
• Example 4.10 (Figure 4.12) 대
학
교
전
자
정
보
공
학
부
4.7 Optimal Merge Patterns 전
북
대
학
교
전
자
정
보
공
학
부
• Time
– If list is kept in nondecreasing order: O(n2)
– If list is represented as a minheap: O(n log n)
4.7 Optimal Merge Patterns 전
북
• Huffman codes 대
학
– Obtaining an optimal set of codes for messages M1, M2, .., Mn+1 교
– e.g, 4 messages (Figure 4.14) 전
자
정
0 1 보
공
학
M4 부
0 1
M3
0 1
M1 M2
• M1=000
• M2=001
• M3=01
• M4=1
4.7 Optimal Merge Patterns 전
북
• Huffman codes (Continued) 대
학
– When qi is the relative frequency for Mi 교
• The expected decode time (also the expected message length) 전
q d
자
i i 정
1 i n 1
보
공
• The minimum decode time (also minimum message length) is 학
부
possible by choosing the code words resulting in a binary tree
with minimum weighted external path length (that is, the same
algorithm as 2-way merge problem is applicable)
– Example using Exercise 4
• (q1,q2,…,q7)=(4,5,7,8,10,12,20)
4.8 Single-source Shortest Paths 전
북
• Example 4.11 (Figure 4.15) 대
학
교
전
자
정
보
공
학
부
4.8 Single-source Shortest Paths 전
북
• Design of greedy algorithm 대
학
– Building the shortest paths one by one, in nondecreasing order of 교
path lengths 전
자
• e.g., in Figure 4.15 정
– 14: 10 보
공
– 145: 25 학
부
– …
• We need to determine 1) the next vertex to which a shortest path
must be generated and 2) a shortest path to this vertex
– Notations
• S = set of vertices (including v0 ) to which the shortest paths
have already been generated
• dist(w) = length of shortest path starting from v0, going through
only those vertices that are in S, and ending at w
4.8 Single-source Shortest Paths 전
북
• Design of greedy algorithm (Continued) 대
학
– Three observations 교
• If the next shortest path is to vertex u, then the path begins at 전
자
v0, ends at u, and goes through only those vertices that are in S. 정
• The destination of the next path generated must be that of 보
공
vertex u which has the minimum distance, dist(u), among all 학
vertices not in S. 부
• Having selected a vertex u as in observation 2 and generated
the shortest v0 to u path, vertex u becomes a member of S.
4.8 Single-source Shortest Paths 전
북
• Greedy algorithm (Program 4.14): Dijkstra’s algorithm 대
학
– Time: O(n2) 교
전
자
ShortestPaths(int v, float cost[][SIZE], float dist[], int n) 정
{ 보
int u; bool S[SIZE]; 공
학
for (int i=1; i<= n; i++) { // Initialize S. 부
S[i] = false; dist[i] = cost[v][i];
}
S[v]=true; dist[v]=0.0; // Put v in S.
for (int num = 2; num < n; num++) {
// Determine n-1 paths from v.
choose u from among those vertices not
in S such that dist[u] is minimum;
S[u] = true; // Put u in S.
for (int w=1; w<=n; w++) //Update distances.
if (( S[w]=false) && (dist[w] > dist[u] + cost[u][w]))
dist[w] = dist[u] + cost[u][w];
}
}
4.8 Single-source Shortest Paths 전
북
• Example 4.12 (Figures 4.16) 대
학
교
전
자
정
보
공
학
부
4.8 Single-source Shortest Paths 전
북
• Example 4.12 (Figures 4.17) 대
학
교
전
자
정
보
공
학
부