Module 03: Greedy Algorithms
Module Outline:
1. Introduction to greedy based solution approach
2. Minimum Spanning Trees - Prim’s and Kruskal algorithms
3. Shortest Path using Dijkstra’s algorithm
4. Fractional and 0/1 Knapsack problem
5. Coinage problem
6. Bin packing problem
7. Job scheduling problem– Shortest job first, Shortest remaining job first
8. Graph coloring problem
9. Text compression using Huffman coding and Shannon-Fano coding
Introduction
Greedy is a strategy that works well on optimization problems with the
following characteristics:
1. Greedy-choice property: A global optimum can be arrived at by selecting
a local optimum.
2. Optimal substructure: An optimal solution to the problem contains an
optimal solution to subproblems.
Greedy Method
Spanning Trees
Weighted Graphs:
Weighted Graphs: A weighted graph is a graph, in which each edge has a
weight (some real number).
Weight of a Graph: The sum of the weights of all edges.
Minimum Spanning Tree
The minimum spanning tree may not be unique.
Minimum Spanning Tree Problem
MST Problem: Given a connected weighted undirected graph G , design an
algorithm that outputs a minimum spanning tree (MST) of G .
Generic approach: A tree is an acyclic graph. The idea is to start with an
empty graph and try to add edges one at a time, always making sure that what
is built remains acyclic. And if we are sure every time the resulting graph
always is a subset of some minimum spanning tree.
Prim’s Algorithm
Prim’s Algorithm
Single-Source Shortest Path
Variants of Shortest Path Problems
🞂 Single pair shortest path problem
◦ Given source s and destination d, find shortest path from s to d.
🞂 Single source shortest paths problem
◦ Given s, for each d find shortest path from s to d.
🞂 All-pairs shortest paths problem
◦ For each ordered pair <s,d>, find shortest paths
Shortest Path Properties
🞂 Define δ(u,v) to be the weight of the shortest path from u to v
🞂 Shortest paths satisfy the triangle inequality:
δ(u,v) ≤ δ(u,x) + w(x,v)
🞂 A key technique in shortest path algorithms is relaxation
◦ Idea: for all v, maintain upper bound d[v] on δ(s,v)
Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}
Dijkstra’s Algorithm
Dijkstra(G)
{ for each v ∈ V
d[v] = ∞;
d[s] = 0; S = ∅; Q = V;
while (Q ≠ ∅) How many times is
ExtractMin() called?
u = ExtractMin(Q);
S = S U {u};
for each v ∈ u->Adj[] Relaxation
if (d[v] > d[u]+w(u,v)) Step
d[v] = d[u]+w(u,v); How many times is
DecraseKey() called?
What will be the total running time? What happens if G has
negative edges?
Ans: O((V+E) lg V) using binary heap for Queue
Can achieve O(V lg V + E) with Fibonacci heap