Chapter Three - Greedy Algorithms
Chapter Three - Greedy Algorithms
Greedy Algorithms
Greedy Algorithm
• An algorithm is designed to achieve optimal solution for
a given problem.
• In greedy approach, decisions are made from the given
solution domain. The closest solution that seems to
provide optimal solution is chosen.
• It tries to find localized optimal solution which may
eventually land in globally optimized solutions.
• Counting Coins : This problem is to count to a desired
value by choosing the least possible coins and the greedy
approach forces the algorithm to pick the largest possible
coin. If we are provided coins of € 1, 2, 5 and 10 and we
are asked to count € 18 then the greedy procedure will be;
Complied by Abebe Z. 2
• 1 - Select one € 10 coin, the remaining count is 8
• 2 - Then select one € 5 coin, the remaining count is 3
• 3 - Then select one € 2 coin, the remaining count is 1
• 3 - And finally, the selection of one € 1 coins solves the
problem
• Though, it seems to be working fine, for this count we need
to pick only 4 coins. But if we slightly change the problem
then the same approach may not be able to produce the
same optimum result.
• Hence, we may conclude that the greedy approach picks an
immediate optimized solution and may fail where global
optimization is a major concern
Complied by Abebe Z. 3
Graph Minimum Spanning Tree
(MST)
• A tree is acyclic, undirected and connected graph.
• A spanning tree of a graph is a tree containing all vertices
from the graph.
• A minimum spanning tree is a spanning tree where the sum
of the weights on the tree’s edges are minimal.
• Find a minimum-cost set of edges that connect all vertices of
a graph.
• Applications:-
• Connect a nodes with minimum of wire
• Networking
• Circuit design
Minimum Spanning tree
Cont…
• Problem formulation
• Given an undirected weighted graph G=(V,E) with
weights w(u,v) for each edge (u,v) C E.
• Find acyclic subset T C E that connects all of the
vertices V, and minimizes the total weight.
1 1 6
4
2
6 5
1 1 6
4
2
6 5
8 3
Final Cost: 1 + 3 + 4 + 1 + 1 = 10 2 3 4
1 1 6
4
2
6 5
1
10 5
1 1
8 3
2 3 4
1 1 6
4
2
6 5
8 3
2 3 4
1 1 6
4
2
6 5
6 5
6 5
6 5
6 5
6 5
6 5
• E.g., v 8
4 4 ?
2 If d(w) > d(v) + w(v, w)
s 0
11 ? d(w) = d(v) + w(v, w)
7 6
8 ? ?
1
Set d(s) = 0 ;
while (there is unvisited vertex) {
v = unvisited vertex with smallest d ;
Visit v, and Relax all its outgoing edges;
}
return d ;
Example
8 7
4 ∞ ∞ ∞ 9
s 11
2
4
0 ∞ 14 ∞
7 6
8 ∞ ∞ 10
∞ 1 2
Relax
8 7
4 4 ∞ ∞ 9
s 2
0
11 ∞ 4 14 ∞
7 6
8 8 ∞ ∞ 10
1 2
Example
8 7
4 4 ∞ ∞ 9
s 11
2
4
0 ∞ 14 ∞
7 6
8 8 10
1 ∞ 2 ∞
Relax
8 7
4 4 12 ∞ 9
s 2
11 4 14 ∞
0 ∞
7 6
8 8 ∞ ∞ 10
1 2
Example
8 7
4 4 12 ∞ 9
s 11
2
4
0 ∞ 14 ∞
7 6
8 8 ∞ ∞ 10
1 2
Relax
8 7
4 4 12 ∞ 9
s 2
11 15 4 14
0 ∞
7 6
8 8 9 ∞ 10
1 2
Example
8 7
4 4 12 ∞ 9
s 11
2
4
0 15 14 ∞
7 6
8 8 9 ∞ 10
1 2
Relax
8 7
4 4 12 ∞ 9
s 2
0
11 15 4 14 ∞
7 6
8 8 9 11 10
1 2
Example
8 7
4 4 12 ∞ 9
s 11
2
4
0 15 14 ∞
7 6
8 8 9 11 10
1 2
Relax
8 7
4 4 12 25 9
s 2
0
11 15 4 14 21
7 6
8 8 9 11 10
1 2
Example
8 7
4 4 12 25 9
s 11
2
4
0 15 14 21
7 6
8 8 9 11 10
1 2
Relax
8 7
4 4 12 19 9
s 2
0
11 14 4 14 21
7 6
8 8 9 11 10
1 2
Example
8 7
4 4 12 19 9
s 11
2
4
0 14 14 21
7 6
8 8 9 11 10
1 2
Relax
8 7
4 4 12 19 9
s 11 2
4
0 14 14 21
7 6
8 8 9 11 10
1 2
Example
8 7
4 4 12 19 9
s 11
2
4
0 14 14 21
7 6
8 8 9 11 10
1 2
Relax
8 7
4 4 12 19 9
s 11 2
4
0 14 14 21
7 6
8 8 9 11 10
1 2
Example
8 7
4 4 12 19 9
s 11
2
4
0 14 14 21
7 6
8 8 9 11 10
1 2
Relax
8 7
4 4 12 19 9
s 11 2
4
0 14 14 21
7 6
8 8 9 11 10
1 2
Correctness
Theorem:
The kth vertex closest to the source s is
selected at the kth step inside the while
loop of Dijkstra’s algorithm
Also, by the time a vertex v is selected,
d(v) will store the length of the shortest
path from s to v
Topological
Sort
11
s 6
4 3 2
8
Example
11
s 6
4 3 2
0
5
Process 8
this node
Relax
11
s 6
4 3 2
0
8
Example
11
s 6
4 3 2
0
5
Process 8
this node
Relax
11
s 6
4 3 2
0 3 11
8
Example
11
s 6
4 3 2
0 3 11
5
Process 8
this node
Relax
11
s 6
4 3 2
0 3 5 11
8
Example
11
s 6
4 3 2
0 3 5 11
5
Process 8
this node
Relax
11
s 6
4 3 2
0 3 5 11 10
8
Example
11
s 6
4 3 2
0 3 5 11 10
8
Process
Relax this node
11
s 6
4 3 2
0 3 5 11 10
8
Example
11
s 6
4 3 2
0 3 5 11 10
8
Process
Relax this node
11
s 6
4 3 2
0 3 5 11 10
8
Correctness
Theorem:
By the time a vertex v is selected,
d(v) will store the length of the shortest
path from s to v
E.g., v -7
4
s 11
What is the shortest
-7
8 path from s to v?
Handling Negative Weight Edges
• The problem is due to the presence of a
cycle C, reachable by the source, whose
total weight is negative
C is called a negative-weight cycle
• How to handle negative-weight edges ??
if input graph is known to be a DAG,
DAG-Shortest-Path is still correct
For the general case, we can use
Bellman-Ford algorithm
Bellman-Ford Algorithm
Bellman-Ford(G, s) // runs in O(VE) time
8 ∞ ∞ Relax all 8 8 ∞
-2
Relax all
8 8
4 4 0 Relax all 4 4 1
s 0 3 -7 10 s 0 3 -7 10
8 -2
11 8
7 7 ∞
-2 -2
Example 1
After the 4th Relax all
8
4 4 0
s 0 3 -7 10
8 7 10
-2
8 ∞ ∞ Relax all 8 8 ∞
-2 -2
Relax all
8 8
4 1 -7 Relax all 4 4 1
s 0 3 -7 1 s 0 3 -7 1
8 8
0 2 7 ∞
-2 -2
Example 2
After the 4th Relax all
This edge shows
8 something must
4 -7 -15
be wrong …
s 0 3 -7 1
8 -8 -6
-2