๐ฉ๐ปโ๐พ
Chap 4. The Greedy Approach ๋ณธ๋ฌธ
Greedy Algorithm
-๋งค ์๊ฐ ๊ฐ์ฅ ์ข์๋ณด์ด๋ ๊ฒ์ ๊ณ ๋ฅด๋ ๋ฐฉ๋ฒ
General Greedy Approach
- ๋น ์งํฉ์ผ๋ก ์์ํ์ฌ ์งํฉ ์ธ์คํด์ค์ ๋ํ ๋ต์ ๋ํ๋ผ ๋๊น์ง ์งํฉ์ ์์ดํ ์ ์์๋๋ก ์ถ๊ฐ
- ์ ํ ์ ์ฐจ ( = Selection Procedure)
- ๊ทธ๋ฆฌ๋ ๊ธฐ์ค์ ๋ฐ๋ผ ์งํฉ์ ์ถ๊ฐํ ๋ค์ ์์ดํ ์ ์ ํ
- ํ๋น์ฑ ํ์ธ ( = Feasibility Check)
- ์ธ์คํด์ค์ ๋ต์ ์ ๊ณตํ๋ ๋ฐฉ์์ผ๋ก ์งํฉ์ ์๋ฃํ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ํ์ธ
- ๋ต ํ์ธ ( = Solution Check)
- ์ ์งํฉ์ด ์ธ์คํด์ค์ ๋ํ ๋ต์ ๊ตฌ์ฑํ๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์
Minimum Spanning Trees
์ ์
- Undirected Graph
- Connected Graph
- Acyclic Graph
- Tree
- Spanning Tree for undirected graph
- G์ ๋ชจ๋ ์ ์ ์ ํฌํจํ๊ณ ํธ๋ฆฌ์ธ ์ฐ๊ฒฐ๋ ์๋ธ๊ทธ๋ํ
- Minimum Spanning Tree for undirected graph
- ์ต์ ๋ฌด๊ฒ๋ฅผ ๊ฐ์ง G์ Spanning Tree

์ผ์ชฝ์ ์ฌ์ง์ ๋ณด๋ฉด G1 ๊ทธ๋ํ์์ ๋์ฌ ์ ์๋ 4๊ฐ์ subgraph (= G2, G3, G4, G5)๊ฐ ์๋ค.
G2 : V3-V4-V5๊ฐ cycle์ ํ์ฑํ๋ฏ๋ก spanning tree X
G3, G4, G5 : spanning tree์ ์กฐ๊ฑด์ ๋ชจ๋ ๋ง์กฑ -> G3์ weight = 15, G4, G5์ weight = 10
๋ฐ๋ผ์, G1์ Minimum Spanning Tree๋ G4์ G5๊ฐ ๋๋ค.
Prim's Algorithm
- MST๊ตฌํ์ ์ฌ์ฉ๋๋ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์์ ์ ์ ์์ ์ ์ ์ ์ถ๊ฐํด๊ฐ๋ฉฐ ๋จ๊ณ์ ์ผ๋ก ํธ๋ฆฌ๋ฅผ ํ์ฅํ๋ ๊ธฐ๋ฒ
- ํ์ ์ ์ ์ ๋ํด ์ฐ๊ฒฐ๋ ์ธ์ ์ ์ ๋ค ์ค ๋ฌด๊ฒ๊ฐ ๊ฐ์ฅ ์์ ๊ฐ์ ์ผ๋ก ์ฐ๊ฒฐ๋ ์ ์ ์ ์ ํ

์ ์ฌ์ง์ผ๋ก Prim's Algorithm์ ์ดํดํด๋ณด์.
- V1์ ์์ ์ ์ ์ผ๋ก V1์ ์ฐ๊ฒฐ๋ ๊ฐ์ ์ค ๋ฌด๊ฒ๊ฐ 1๋ก ๊ฐ์ฅ ์์ ๊ฐ์ ์ผ๋ก ์ฐ๊ฒฐ๋ V2๋ฅผ ์ ํํ๋ค.
- ์ด์ V1๊ณผ V2์ ์ฐ๊ฒฐ๋ ์ ์ ๋ค ์ค ๋ฌด๊ฒ๊ฐ ๊ฐ์ฅ ์์ ๊ฐ์ ์ผ๋ก ์ฐ๊ฒฐ๋ ์ ์ ์ธ V3์ ์ ํํ๋ค. ์ด๋, ๊ฐ์ ์ ์ ํํ ์ ์๋ ๊ฒฝ์ฐ๋ 2๊ฐ์ง์ด๋ฏ๋ก ๋ ์ค ํ๋๋ฅผ ์ ํํ๋ฉด ๋๋ค.
- ํ์ ์ ์ ์ ์ฐ๊ฒฐ๋ ์ธ์ ์ ์ ๋ค ์ค ๋ฌด๊ฒ๊ฐ ๊ฐ์ฅ ์์ ๊ฐ์ ์ผ๋ก ์ฐ๊ฒฐ๋ ์ ์ ์ ์ ํํ๋ ๊ณผ์ ์ ๋ฐ๋ณตํ์ฌ ๋ชจ๋ ์ ์ ๋ค์ด ์ ํ๋๋๋ก ํ๋ค.
Prim's Algorithm์ ๊ตฌํํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ ๋ฐฐ์ด๋ค์ ์ฌ์ฉํ๋ค.
- W[i][j] : ๋ฌด๊ฒ ์ ๋ณด (symmetric)
- nearest[i] : Vi์ ๊ฐ์ฅ ๊ฐ๊น์ด Y์ ์กด์ฌํ๋ ์ ์ ์ ์ธ๋ฑ์ค
- distance[i] : Vi์ nearest[i] ์ฌ์ด์ ๋ฌด๊ฒ
public static set_of_edges prim(int n, const number W[][]){
index i, vnear;
index [] nearest = new index[2...n];
number min;
number[] distance = new number[2..n];
edge e;
set_of_edges F = โ
;
for(i = 2; i <= n; i++){
nearest[i] = 1;
distance[i] = W[1][i];
}
repeat(n-1) times { // ์ฃ์ง๋ฅผ n-1๊ฐ ๋ฃ์ผ๋ฉด ๋ผ์
min = โ;
for(i =2; i <= n; i++){
if(0 <= distance[i] < min){
min = distance[i];
vnear = i;
}
e = edge connecting vertices indexed by vnear and nearest[vnear];
add e to F;
distance[vnear] = -1; // ๊ณ ๋ฅธ ์ ์ ํ์
for(i = 2; i<= n; i++)
if(W[i][vnear] < distance[i]){
distance[i] = W[i][vnear];
nearest[i] = vnear;
}
return F;
}
Time Complexity of Prim's Algorithm
Basic Operation : ๋ ๊ฐ์ for๋ฌธ ๋ด๋ถ์ ์กด์ฌํ๋ if๋ฌธ
Input Size : n ( = ์ ์ ์ ๊ฐ์)
T(n) = 2 * (n-1) * (n-1) โ ๐ฏ (n^2)
Kruskal's Algorithm
- ๋ง์ฐฌ๊ฐ์ง๋ก MST๊ตฌํ์ ์ฌ์ฉ๋๋ ์๊ณ ๋ฆฌ์ฆ
- ๊ทธ๋ํ์ ๊ฐ์ ๋ค์ ๊ฐ์ค์น ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ๋ค, ์ ๋ ฌ๋ ๊ฐ์ ๋ค ์ค ์์๋๋ก ์ฌ์ดํด์ ํ์ฑํ์ง ์๋๋ก ๊ฐ์ ๋ค์ ์ ํ

์ ์ฌ์ง์ผ๋ก Kruskal's Algorithm์ ์ดํดํด๋ณด์.
- ๊ฐ์ ๋ค์ ๊ฐ์ค์น ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค
- (v1,v2)์ ๊ฐ์ค์น๊ฐ ๊ฐ์ฅ ์์ผ๋ฏ๋ก ํด๋น ๊ฐ์ ์ MST ์งํฉ์ ๋ฃ๋๋ค.
- ๊ทธ ๋ค์์ผ๋ก ๊ฐ์ค์น๊ฐ ์ ์ (v3, v5) ๊ฐ์ ์ MST ์งํฉ์ ๋ฃ๋๋ค.
- ๋ค์์ (v1, v3)์ (v2, v3)์ ๊ฐ์ค์น ๊ฐ์ด ๊ฐ์ผ๋ฏ๋ก (v1, v3) ๊ฐ์ ์ MST ์งํฉ์ ๋ฃ๊ณ , (v2, v3) ๊ฐ์ ์ ๊ณ ๋ ค๋ง ํ๊ณ ๋ฒ๋ฆฐ๋ค.
- MST ์งํฉ์ (n-1)๊ฐ์ ๊ฐ์ ๋ค์ด ์กด์ฌํ ๋๊น์ง ๊ฐ์ ์ ํ์ ๋ฐ๋ณตํ๋ค.
Kruskal's Algorithm์ ๊ตฌํํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ ํจ์๋ค์ ์ฌ์ฉํ๋ค.
- Initial(n) : n๊ฐ์ disjoing subset์ ๋ง๋ค์ด์ฃผ๋ ํจ์
- p = find(i) : ์ธ๋ฑ์ค๊ฐ i์ธ ์ ์ ์ด ์ด๋ disjoint set์ ํฌํจ๋์ด ์๋์ง ์๋ ค์ฃผ๋ ํจ์
- merge(p, q) : ๋ subset์ ๋ณํฉํ๋ ํจ์
- equal(p, q) : ๋ subset์ด ๊ฐ์ ์งํฉ์ ์ํ๋์ง ์๋ ค์ฃผ๋ ํจ์, true ๋๋ false ๋ฐํ
public static set_of_edges kruskal(int n, int m, set_of_edges E){
index i, j;
set_pointer p, q;
edge e;
set_of_edges F = โ
;
Sort the m edges in E by weight in nondecreasing order;
initial(n);
while(|F| < n-1){
e = edge with the least weight not yet considered;
i, j = indicesof vertices connected by e;
p = find(i);
q = find(j);
if(!Equal(p,q)){
merge(p, q);
add e to F;
}
}
return F;
}
Time Complexity of Kruskal's Algorithm
Basic Operation : ๋น๊ต ์ฐ์ฐ์
Input Size : n ( = ์ ์ ์ ๊ฐ์), m ( = ๊ฐ์ ์ ๊ฐ์)
- Time to sort the edges
- W(m) โ ๐ฏ (m lg m) using MergeSort
- Time in the while loop
- W(m) โ ๐ฏ(m lg m) using DisjoingSet implementation
- Time to initialize n disjoint sets
- T(n) โ ๐ฏ(n)
์ 3๊ฐ์ Time์ ํฉ
โก๏ธ W(m, n) โ ๐ฏ(m lg m), n-1 โค m โค n(n-1) / 2
โก๏ธ worst case : W(m, n) โ ๐ฏ(m lg m) = ๐ฏ(n^2 * lg n^2) = ๐ฏ(n^2 * lg n)
Huffman Code
- ๋ฐ์ดํฐ ์์ถ์ ์ํ ํจ์จ์ ์ธ ์ธ์ฝ๋ฉ ๋ฐฉ๋ฒ
- ๊ฐ๋ณ ๊ธธ์ด ์ด์ง ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ ํ์ผ์ ๋ํ๋
ํ ์คํธ ํ์ผ์ ์ธ์ฝ๋ฉํ๊ธฐ ์ํ ๋นํธ ์ = minimize โfrequency(v) * depth(v)
for(i = 1; i < n; i++){
remove(PQ, p);
remove(PQ, q);
r = new nodetype();
r.left = p;
r.right = q;
r.frequency = p.frequency + q.frequency;
insert(PQ, r);
}
remove(PQ, r);
return r;
// nodetype info
Public class nodetype{
char symbol;
int frequency;
nodetype left;
nodetype right;
}
remove() ํจ์ time complexity -> log n
insert() ํจ์ time complexity -> log n
Time complexity of Huffman Code
n * (3 * log n) ๐ฏ n log n
'Study > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Chap 9. Introduction to the Theory of NP (0) | 2023.06.11 |
---|---|
Chap 7. Introduction to Computational Complexity: The Sorting Problem (0) | 2023.06.08 |
Chap 6. Branch and Bound (0) | 2023.06.06 |
Chap 5. Backtracking (0) | 2023.06.04 |
Chap 3. Dynamic Programming (0) | 2023.04.20 |