Greedy and Dynamic Algorithm
Greedy and Dynamic Algorithm
&
Dynamic Programming
Greedy Approach
• Any algorithm that solves problems by selecting the
option that is locally optimal at each step is
considered greedy.
• A greedy heuristic can create locally optimal
solutions that approximate a globally optimal
solution in an acceptable amount of time, even if a
greedy method often fails to produce an optimal
solution.
• A heuristic function, also simply called a heuristic,
is a function that ranks alternatives in search
algorithms at each branching step based on
available information to decide which branch to
follow. For example, it may approximate the exact
solution.
Optimization Problems
• For most optimization problems you want to find, not just a solution,
but the best solution.
• A greedy algorithm sometimes works well for optimization
problems. It works in phases. At each phase:
• You take the best you can get right now, without regard for future
consequences.
• You hope that by choosing a local optimum at each step, you will end up at a
global optimum.
Example: Counting Money
• You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15,
18, and 20 minutes.
• You have three processors on which you can run these jobs.
• You decide to do the longest-running jobs first, on whatever processor is
available.
P1 20 10 3
P2 18 11 6
P3
15 14 5
• What would be the result if you ran the shortest job first?
• Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes
P1 3 10 15
P2 5 11 18
P3
6 14 20
P2 18 11 5
P3 15 10 6 3
b 7 c
5 Acyclic subset of edges(E) that connects
a 1
all vertices of G.
3 -3
11
d e f
0 2 b c
5
a 1
3 -3
d e f
0
The Kruskal Algorithm
// input: a graph G with n nodes and m edges
// output: E: a MST for G
1 2
1 2 3 {1}{2}{3}{4}{5}{6}{7}
6 5
4 4 6
3 8
4 5 6
7
4 3
7
Kruskal’s Algorithm
1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm
1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4) {1,2,3,4,5}{6,7}
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm
7
Prim’s Algorithm
Choose arbitrary starting vertex,
set to 0 and delete min
1 2
1 2 3 S = {5}
1: ∞
6 5 2: 4
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
1: ∞
6 5
S = {4,5} 2: 4
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
1: 4
6 5
S = {4,5} 2: 4
4 4 6
3: 5
3 8 6: 8
4 5 6 7: 4
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
1: 4
6 5
S = {4,5} 2: 4
4 4 6
3: 5
3 8 6: 8
4 5 6 7: 4
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
1: 4
6 5
S = {4,5} 2: 4
4 4 6 S = {1,4,5} 3: 5
3 8 6: 8
4 5 6 7: 4
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
2: 1
6 5
S = {4,5} 3: 5
4 4 6 S = {1,4,5} 6: 8
3 8 7: 4
4 5 6
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
2: 1
6 5
S = {4,5} 3: 5
4 4 6 S = {1,4,5} 6: 8
3 8 S = {1,2,4,5} 7: 4
4 5 6
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
3: 2
6 5
S = {4,5} 6: 8
4 4 6 S = {1,4,5} 7: 4
3 8 S = {1,2,4,5}
4 5 6
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
3: 2
6 5
S = {4,5} 6: 8
4 4 6 S = {1,4,5} 7: 4
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
6: 6
6 5
S = {4,5} 7: 4
4 4 6 S = {1,4,5}
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
6: 6
6 5
S = {4,5} 7: 4
4 4 6 S = {1,4,5}
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
S = {1,2,3,4,5,7}
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 S = {5}
6: 3
6 5
S = {4,5}
4 4 6 S = {1,4,5}
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
S = {1,2,3,4,5,7}
8
4 3
7
Prim’s Algorithm
1 2
1 2 3 E: S = {5}
6 5
{4,5} S = {4,5}
4 4 6 {1,4} S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
{4,7} S = {1,2,3,4,5,7}
8
4 3 {6,7} S = {1,2,3,4,5,6,7}
7
Example of Prim’s Algorithm
Not in tree
a/0 5 b/ 7 c/
1 -3 Q=a b c d e f
11 3
0
d/ e/ f/
0 2
Example of Prim’s Algorithm
1 -3 Q=b d c e f
11 3
5 11
d/11 e/ f/
0 2
Example of Prim’s Algorithm
1 -3 Q=e c d f
11 3
3 7 11
d/11 e/3 f/
0 2
Example of Prim’s Algorithm
1 -3 Q=d c f
11 3
0 1 2
d/0 e/3 f/2
0 2
Example of Prim’s Algorithm
1 -3 Q=c f
11 3
1 2
d/0 e/3 f/2
0 2
Example of Prim’s Algorithm
1 -3 Q=f
11 3
-3
d/0 e/3 f/-3
0 2
Example of Prim’s Algorithm
1 -3 Q=
11 3
3 1 -3
23
2 3
9
s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
46
47
Dijkstra’s Pseudo Code
S={ }
Q = ( s, 2, 3, 4, 5, 6, 7, t )
23
2 3
0 9
s
18
14 2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label
Dijkstra's Shortest Path Algorithm
S={ }
Q = ( s, 2, 3, 4, 5, 6, 7, t )
Extract-Min(Q)
23
2 3
0 9
s
18
14 2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label
Dijkstra's Shortest Path Algorithm
S={s}
Q = ( 2, 6, 7, 3, 4, 5, t )
decrease key
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label 15
X
Dijkstra's Shortest Path Algorithm
S={s}
Q = ( 2, 6, 7, 3, 4, 5, t )
Extract-Min(Q)
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label 15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
Q = ( 6, 7, 3, 4, 5, t )
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
Q = ( 6, 7, 3, 4, 5, t )
decrease key
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
Q = ( 6, 7, 3, 4, 5, t )
X
32
X
9
23
2 3
0 9
Extract-Min(Q)
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
Q = ( 7, 3, 5, 4, t )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
44
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
Q = ( 7, 3, 5, 4, t )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
44
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X
Extract-Min(Q)
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
Q = ( 3, 5, t, 4 )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
15
X
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
Q = ( 3, 5, t, 4 )
Extract-Min(Q)
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
15
X
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
Q = ( 5, t, 4 )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
Q = ( 5, t, 4 )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
Extract-Min(Q)
t
7 44
15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
Q = ( 4, t )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6 45 X
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
Q = ( 4, t )
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6 45 X
X 34
X 35
44
30 X 4 19
11
15 5 Extract-Min(Q)
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
Q=(t)
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6 45 X
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
Q=(t)
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6 45 X
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
Extract-Min(Q) 50 51
X 59
X X
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
Q=()
X
32
X
9
23
2 3
0 9
s
18
14 X
14 6
2
6 45 X
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
Dynamic Programming
Fibonacci Numbers
F(n)
F(n-1) + F(n-2)
Fib(5)
+
Fib(4) Fib(3)
+ +
Fib(1) Fib(0)
Dynamic programming
Optimal substructure
An optimal solution to a problem
(instance) contains optimal
solutions to subproblems.
Recursion
Example: Fibonacci numbers
fibDP(n)
F[0] 0
F[1] 1
for (i 2, i≤ n, i++)
F[i] F[i-1]+F[i-2]
return F[n]
• Time = (n), space = (n)