알고리즘_4장-Greedy Approach
-
Upload
jungwoo-cho -
Category
Documents
-
view
221 -
download
0
Transcript of 알고리즘_4장-Greedy Approach
-
8/3/2019 _4 -Greedy Approach
1/17
CH4. Greedy Approach Grab data items in sequence,
each time with best choice,
without thinking future
Efficient, Simple!!!But, cannot solve many problems
G.A. : choose locally optimal choice 1-by-1 D.P. : solve smaller problems optimal solution
-
8/3/2019 _4 -Greedy Approach
2/17
CH4. Greedy Approache.g. coin change problem:
Goal: correct change + as few coins as possible
Approach : choose largest possible coin
Locally optimal choice optimal solutionNo reconsideration!!!
Q?: Is the solution really optimal? (proof required!!!)
e.g. U.S. coin system : Greedy Approach O.K. Suppose : 16 ? Dynamic Programming
-
8/3/2019 _4 -Greedy Approach
3/17
Outline of Greedy Approachwhile (there are more coins and the instance is not solved) {
grab the largest remaining coin;// selection procedure
if(adding the coin makes the change exceed target amount)// feasibility check
reject the coin;else
add the coin to the change;
if(the total value of the change equals to the target amount)// solution check
the instance is solved;}
-
8/3/2019 _4 -Greedy Approach
4/17
G.A. vs D.P. - Knapsack Problem 0/1 knapsack problem
S = {item 1, item 2,,item n}
wi= weight of item i
pi= profit of item i
W = maximum weight the knapsack can hold
(wi, pi, W : positive integers)
Determine AS s.t. is maximized underBrute Force Approach : consider all subsets
exponential time algo !!!
A
ip
A
iWw
-
8/3/2019 _4 -Greedy Approach
5/17
G.A. for Knapsack Problem G.A. to 0/1 knapsack problem
: Largest profit first : incorrectEx. (w1, w2, w3) = (25, 10, 10), (P1, P2, P3 ) = (10, 9, 9), W = 30
Greedy(item1) : $10 vs. Optimal(item 2 & 3) : $18
: Lightest item first :
: Largest profit per unit weight first
Ex. (w1, w2, w3) = (5,10,20), (P1, P2, P3 ) = (50,60,140), W = 30profit per unit weight = item1($10), item3($7), item2($6)
Greedy(item1 & 3) : $190 vs. Optimal(item 2 & 3) : $200
-
8/3/2019 _4 -Greedy Approach
6/17
G.A. for Knapsack Problem G.A. to fractional knapsack problem
: optimal solution guaranteed
Ex. $50+$140+5/10($60)=$220
(proof necessary)
-
8/3/2019 _4 -Greedy Approach
7/17
4.3. Scheduling Minimization Total Time in the System
e.g. t1=5, t2=10, t3=4
Schedule Total time in the System
[1, 2, 3] 5 + (5+10) + (5+10+4) = 39[1, 3, 2] 5 + (5+4) + (5+4+10) = 33[2, 1, 3] 10 + (10+5) + (10+5+4) = 44[2, 3, 1] 10 + (10+4) + (10+4+5) = 43
[3, 1, 2] 4 + (4+5) + (4+5+10) = 32[3, 2, 1] 4 + (4+10) + (4+10+5) = 37
Algorithm :Sort in non-decreasing order, and Schedule
-
8/3/2019 _4 -Greedy Approach
8/17
4.2. Single-Source Shortest Paths
All-Pairs Shortest Path Prob.: (n3) Floyds algo (D.P.)
(from each vertex to all other vertices) Single-Source Shortest Path Prob.: Dijkstras algo (G.A.)
(from one particular vertex to all the others)
v5
v4 v3
v2v13
5
1
9
1 2
2
4
3
3
v5
v4 v3
v2v13
5
1
9
1 2
2
4
3
3
-
8/3/2019 _4 -Greedy Approach
9/17
Dijkstras Algorithm for Shortest Paths Start with {v1}; //source
Choose nearest vertex from v1,
Y={v1};
F=; //shortest paths treewhile (not solved) do {
select v from V-Y nearest from v1 using only Y as intermediates;add v to Y;add the edge touching v to F;if
Y=V then Exit;}
Time Complexity ???
-
8/3/2019 _4 -Greedy Approach
10/17
4.1 Minimum Spanning Trees(MST) Connected weighted undirected graph: G=(V,E)
Disconnected graph
Tree : connected acyclic graph
Rooted tree, spanning tree
A conn. weightedundirected G
v1
v3 v4
v5
v21
3 3 64
2 5
A S.T.
1
36
5
1
34
2
An MST
Spanning forest
-
8/3/2019 _4 -Greedy Approach
11/17
Minimum Spanning Tree Weight of a spanning tree T :
M.S.T : a S.T. with min. such weight
Input : Adjacency Matrix W of G=(V,E)
Output : An MST T=(V,F) (FE) Uniqueness? (|F|=|V|-1)
Greedy Approach :
F= //initializationwhile (not solved yet) do {Select an edge (L.O.C) //selection
if create not cycle then add //feasibilityifT=(V,F) is a S.T. then exit //solution check
}
-
8/3/2019 _4 -Greedy Approach
12/17
Prims Algorithm for MST
F=; Y={v1}; //initializationwhile (not solved) do {
select vV-Y nearest to Y; //selection & feasibilityadd v to Y; add the edge to F;ifY=V then exit; //solution check
}
v1
v3 v4
v5
v21
33
6
4
2 5
v1
v3 v4
v5
v21
33
6
4
2 5
-
8/3/2019 _4 -Greedy Approach
13/17
Prims Algorithm for MST Data Structures :
nnadjacency Matrix W nearest[i]=index of vertex in Y nearest to vi distance[i]= weight of edge (vi, nearest[i])
Time Complexity ??? Q1: Spanning tree? : Yes. Easy Q2: Minimum S.T.? Formal Proof required
-
8/3/2019 _4 -Greedy Approach
14/17
Kruskals Algorithm for MST
F=;
create ndisjoint subsets {v1},{v2},,{vn};sort the edges in E;while (not solved) do {
select next edge; //selectionifthe edge connects 2 disjoint subsets { //feasibility check
merge the subsets; add the edge to F;}
ifall the subsets are merged, then exit; //solution check}
v1
v3
v4
v5
v21
33
6
4
2 5
v1
v3
v4
v5
v21
33
6
4
2 5
-
8/3/2019 _4 -Greedy Approach
15/17
Kruskals Algorithm for MST Worst-case time complexity Analysis : (m log m)
Sorting : (mlog m) Initialization : (n)
Total time for find, merge : (m(m, n)) Remaining ops : O(m)
Prims algo : (n2) where n-1 m n(n-1)/2
-
8/3/2019 _4 -Greedy Approach
16/17
4.4 Huffman Code Suppose encoding a text(chars from n-char alphabet)by assigning codeword(sequence of bits) to each charEx. a(45), b(13), c(12), d(16), e(9), f(5)
Fixed-length encodinga(000), b(001), c(010), d(011), e(100), f(101): 300-bits
Variable-length encodinga(0), b(1), c(00), d(01), e(10), f(11): 142-bitsaaba 0010 ce???
Prefix codea(0), b(101), c(100), d(111), e(1101), f(1100): 224-bitsaaba 001010 aaba
-
8/3/2019 _4 -Greedy Approach
17/17
Huffman Code Input: alphabet A, aA, an associated freq f(a) Output: tree T so that B(T)=f(a)dT(a) is minimized
100a(45) 550 25 30c(12) b(13) 14 d(16)100 101 111f(5) e(9)
1100 1101