Professional Documents
Culture Documents
Greedy Algorithm Immediate Benefit Optimal Solution
Greedy Algorithm Immediate Benefit Optimal Solution
Greedy Algorithms
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 2
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 3
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 4
An Example
5 4
0 1 2
3
4 1 3 4
2
1 3
3 4 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 5
w(1,3) = 1
4 w(3,4) = 1
0 1 2
w(1,4) = 2
1
3 w(1,5) = 3
w(0,4) = 3
3 w(4,5) = 3
4 5 w(0,3) = 4
3
1 w(1,2) = 4
w(2,5) = 4
Total weight: 12
w(0,1) = 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 6
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 7
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 8
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 9
T
e’
S V\S
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 10
✫ ✪
is smallest across the cut
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 11
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 12
Kruskal’s Algorithm
The Example
5 4
0 1 2
3
4 1 3 4
2
1 3
3 4 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 14
w(1,3) = 1
0 w(3,4) = 1
1 2
w(1,4) = 2
w(1,5) = 3
w(0,4) = 3
w(4,5) = 3
4 5 w(0,3) = 4
3
w(1,2) = 4
w(2,5) = 4
w(0,1) = 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 15
w(1,3) = 1
4 w(3,4) = 1
0 1 2
w(1,4) = 2
1
3 w(1,5) = 3
w(0,4) = 3
3 w(4,5) = 3
4 5 w(0,3) = 4
3
1 w(1,2) = 4
w(2,5) = 4
Total weight: 12
w(0,1) = 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 16
Kruskal’s Algorithm
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 17
Joining Components
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 19
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 21
✫ ✪
ponent by 1.
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 22
Directed Trees
3 2
5 4 7 0 6
{3,4,5}
{0,1,2,6,7}
1
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 24
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 25
makeset (x)
1 π(x) ← x // parent pointer points to itself
2 rank(x) ← 0
find(x)
1 while x 6= π(x):
2 x ← π(x) // root is the representative
3 return x
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 26
union (x, y)
1 rx ← find(x)
2 ry ← find(y)
3 if rx = ry : return // in the same set
4 if rank(rx ) > rank(ry ): // x in a deeper tree
5 π(ry ) = rx
6 else: // x in a lesser or equal height tree
7 π(rx ) = ry
8 if rank(rx ) = rank(ry ):
9 rank(ry ) ← rank(ry ) + 1.
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 27
Building Trees
0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0
3 0 2 5 6
1 1 0 0
1
4 1 7
0 0 0
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 28
Building Trees
3 0 2 5 6
1 1 0 0
1
4 1 7
0 0 0
union(1,7), union(4,5)
2 3 6
2 1 0
0
1
7 5 4
0 0 0
✫ ✪
1
0
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 29
Complexity of Operations
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 30
Properties
Properties
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 32
• Sorting of edges:
O(|E| log |E|) = O(|E| log |V |) as
log |E| = O(log |V |).
• Another O(|E| log |V |) for union() and find()
operations.
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 33
Modified find()
find(x)
1 if x 6= π(x):
2 π(x) ← find(π(x)), Make x and its
ancestors point to root
3 return π(x)
The benefit is not immediate, but in long
sequence of find(), the amortized cost is much
better than O(log n).
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 34
An Example
3 3
1 1
find(10) 10
0
0 2 1 3 2 5 0 2 1 3 2 5
0 4 1
6 1 7 8 0 0 4 1
6 1 7 8 0
0 9 0 10 11 0 0 11 0
9
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 35
An Example
0
3 11
3
1 1
10 find(11) 10
0 0
2
3 0 2 1 3 5
0 2 1 2 5
0 4 1
0 4 1
6 1 7 8 0 6 1 7 8 0
0 11 0 0 9
9
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 36
Prim’s Algorithm
Prim’s Algorithm
Prim’s Algorithm
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 39
An Example
5 4
0 1 2
3
4 1 3 4
2
1 3
3 4 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 40
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 41
(cost, previous)
Set S 0 1 2 3 4 5
{0} (5, 0) (∞, •) (4, 0) (3, 0) (∞, •)
{0, 4} (2, 4) (∞, •) (1, 4) (3, 4)
{0, 4, 3} (1, 3) (∞, •) (3, 4)
{0, 4, 3, 1} (4, 1) (3, 4)
{0, 4, 3, 1, 5} (4, 1)
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 42
Prim’s MST
4
0 1 2
3
1
1 3
3 4 5
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 43
Prim’s Algorithm
Prim’s Algorithm
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 45
Data Encoding
Data Encoding
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 47
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 48
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 49
A Prefix-Free Code
0 1
Codes
(32) a: 0
a (68) 0 1 b: 100
c: 11
(16) d: 101
0 1 c (16)
b (2) d (14)
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 50
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 51
Decoding
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 52
Decoding
1000011101 ⇒ baacd.
1
Codes
0 a: 0
a (68) b: 100
c: 11
0 d: 101
c (16)
b (2)
d (14)
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 53
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 54
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 55
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 57
(16)
0
c (16)
1
b (2)
d (14)
Cost-I = 68 × 1 + 2 × 3 + 14 × 3 + 16 × 2 = 148
Cost-II = 68 + 2 + 14 + 16 + 16 + 32 = 148
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 58
Greedy Algorithm
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 59
Greedy Algorithm
An Example
Frequencies: 5 8 14 18 25 30
(13)
0
1
(5) (8)
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 61
An Example
Frequencies: 13 14 18 25 30
(27)
0 1
(13) (14)
0
1
(5) (8)
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 62
An Example
Frequencies: 18 25 27 30
(43)
(27)
0 1 0
(18) (25) (13) (14)
0
1
(5) (8)
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 63
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 66
Boolean Satisfiability
Boolean Satisfiability
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 68
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 69
Horn-SAT
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 70
Greedy Algorithm
HornSat()
Input: a Horn Formula
Output: satisfying truth assignment (if any)
1 Assign false to all variables
2 while there is an implication whose left-hand
side is true, but the right hand variable is false,
change the assignment of right side variable to true.
3 if all negative clauses are satisfied:
return the assignment
4 else: return ”unsatisfiable”.
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 71
Correctness Proof
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 72
Greedy Algorithm
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas
✬ ✩
BS-MS & MS-PhD Maths & Computing: SMCS (IACS) 73
References
✫ ✪
Greedy Algorithms (7): COM 4201: Design and Analysis of Algorithms Goutam Biswas