Lecture 4.a - Greedy Algorithms
Lecture 4.a - Greedy Algorithms
Lecture 4.a
Heuristic Optimization Methods:
Greedy Algorithms
Slides prepared by Nina Skorin-Kapov and Lea Skorin-
Kapov
Examples:
Knapsack problem
MST – minimum spanning tree problem
TSP – travelling salesman problem
SAT – satisfiability problem
GC - Graph coloring problem
Zavod za telekomunikacije 2
Optimization methods
University of Zagreb, Croatia
Exact Approximate
A*
methods methods
Simplex- Constraint
based for LP programming Approximation
algorithms
Heuristic
Branch and Bound, Dynamic
Cut , Price (B&B, programming algorithms
B&C, B&P)
Improvement (meta)heuristics
Constructive heuristics
• Local search
• Greedy algorithms
•Nature inspired
• Tabu search, Genetic
Hybrid methods
algorithms, Simmulated
• GRASP Annealing, Ant colony
•Problem specific
heuristics
Zavod za telekomunikacije 3
Greedy algorithms
University of Zagreb, Croatia
Constructive heuristic
Always start from an empty solution and
construct a complete solution by assigning
values to decision variables one at a time
In each step, it assigns a value to a decision
variable which contributes most to the objective
function, i.e. has the maximal profit in that step
Hence the name ‘Greedy’
No backtracking: once a decision variable is
assigned a value, it is never changed
Zavod za telekomunikacije 4
Advantages
University of Zagreb, Croatia
SIMPLICITY
Usually the rules to assign the next decision variable
are easy to design and intuitive
Usually easy to implement
Zavod za telekomunikacije 5
Drawbacks
University of Zagreb, Croatia
Zavod za telekomunikacije 7
The greedy step
University of Zagreb, Croatia
Zavod za telekomunikacije 8
Generic greedy algorithm
University of Zagreb, Croatia
Zavod za telekomunikacije 9
Example: Knapsack problem
University of Zagreb, Croatia
v1=8 c1=9
v2=6 c2=5
v3=3 c3=3
v4=2 c4=1
Zavod za telekomunikacije 10
Example: greedy approach for Knapsack
University of Zagreb, Croatia
Zavod za telekomunikacije 11
Example: drawback of local view
University of Zagreb, Croatia
v1=8 c1=9
v2=6 c2=5 Greedy solution
Total value: v1=8 v1=8 c1=9
v3=3 c3=3
Total capacity: c1=9
v4=2 c4=1
Zavod za telekomunikacije 12
Example: Private telephone network
University of Zagreb, Croatia
Zavod za telekomunikacije 13
Example: Private telephone network
University of Zagreb, Croatia
Zavod za telekomunikacije 14
Example: Private telephone network
University of Zagreb, Croatia
Expensive!
Zavod za telekomunikacije 15
Example: Private telephone network
University of Zagreb, Croatia
Zavod za telekomunikacije 16
Example: Kruskal’s algorithm for MST
University of Zagreb, Croatia
Kruskal’s algorithm:
1. Sort all edges according to weight from smallest to
biggest
2. Always add the edge with the smallest weight such
that it doesn’t form a loop until there are n-1 edges
in the tree
OPTIMAL!
Zavod za telekomunikacije 17
Example - Kruskal’s algorithm for MST
University of Zagreb, Croatia
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6
Sorted edges:
6 1 e3 =4 e7=3
5 4 e10 =6 e3=4
e2
=5
3
4 7 e11=4
e7 =
e 9=
3
6
3
2=
e5=4
6
2 e8 = e1
5 e2=5 Makes cycle
1 6
e1=10
e4 =7
0
e8=5
=8
-1
e6
e1
Zavod za telekomunikacije 18
Example: Traveling Salesman Problem
University of Zagreb, Croatia
Zavod za telekomunikacije 19
Example TSP
University of Zagreb, Croatia
Zavod za telekomunikacije 21
Example TSP
University of Zagreb, Croatia
Example:
Zavod za telekomunikacije 23
Example - Greedy approach for SAT
University of Zagreb, Croatia
Zavod za telekomunikacije 24
Example- Greedy approach for SAT
University of Zagreb, Croatia
Example:
x1 ( x1 x2 ) ( x1 x3 )
If x1 is considered first:
If x1=TRUE it satisfies 2 clauses
If x1=FALSE it satisfies 1 clause
The greedy algorithm would assign x1=TRUE.
However, the first clause would always be FALSE and thus
the formula will never be TRUE→ NO SOLUTION
Zavod za telekomunikacije 25
Example- Greedy approach for SAT
University of Zagreb, Croatia
Example:
x1 ( x1 x2 ) ( x1 x3 )
Suppose the order of nodes considred is: x2, x3, x1
For x2: if x2 =T→ 1 clause satisfied; if x2 =F→ 0 clauses satisfied
Set x2 =TRUE; Clause ( x1 x2 ) is now satisfied (no longer
considered)
For x3: if x3 =T→ 1 clause satisfied; if x3 =F→ 0 clauses satisfied
Set x3 =TRUE; Clause ( x1 x3 ) is now satisfied (no longer
considered)
For x1: if x1 =T→ 0 clauses satisfied (clauses ( x1 x2 ) and ( x1 x3 )
are no longer considered); if x1 =F→ 1 clause satisfied
Set x1 =FALSE;
Zavod za telekomunikacije 26
Example- Greedy approach for SAT
University of Zagreb, Croatia
Example:
x1 ( x1 x2 ) ( x1 x3 )
Suppose the order of nodes considred is: x2, x3, x1
For x2: if x2 =T→ 1 clause satisfied; if x2 =F→ 0 clauses satisfied
Set x2 =TRUE; Clause ( x1 x2 ) is now satisfied (no longer
considered)
Dynamic
For x3: if x3 =T→ 1 clause greedyifdecision
satisfied; x3 =F→ 0 clauses satisfied
Set x3 =TRUE; Clause ( x1 x3 )The
heuristic! is number of (no longer
now satisfied
considered) clauses for x1 changed!
For x1: if x1 =T→ 0 clauses satisfied (clauses ( x1 x2 ) and ( x1 x3 )
are no longer considered); if x1 =F→ 1 clause satisfied
Set x1 =FALSE;
Zavod za telekomunikacije 27
Example: Graph coloring (GC)
University of Zagreb, Croatia
5 2
colors = 3
4 3
Zavod za telekomunikacije 28
Example: Wavelength assignment
University of Zagreb, Croatia
Wavelength-routed WDM
optical network
LD4 1
LD1
2 4 5 2
1 LD5 LD2
LD3
6
4 3 W=3
3 5
Conflict graph:
Physical topology (optical fibers)
and routing scheme of lightpaths Nodes=connections
(all-optical connections) Links=connections which
Wavelength clash constraint cannot use the same
wavelength (share optical
Wavelength continuity constraint fibers)
Zavod za telekomunikacije 29
Example: Greedy approach to GC
University of Zagreb, Croatia
Zavod za telekomunikacije 30
Example: Greedy approach to GC
University of Zagreb, Croatia
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6
6 1 e3 =4
5 4 e10 =6 Color 1
Ordering of
e2
=5
3
4 7
e7 =
considered Color 2
e 9=
3
6
3
2=
nodes:
6
2 e8 = e1
5 1,2,3,4,5,6,7 Color 3
1 6
e1=10
e4 =7
0 4 COLORS
=8
1=
4
-1 Color 4
e6
e1
-2 5
-3 2 e 5=4
-4
-5
-6
Zavod za telekomunikacije 31
Example: Greedy approach to GC
University of Zagreb, Croatia
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6
6 1 e3 =4
5 4 e10 =6
e2
=5
3
4 7
e7 =
e 9=
3
6
3
2=
6
2 e8 = e1
5
1 6
e1=10
e4 =7
0
=8
Color 1
1=
-1
e6
e1
-2 5
Ordering of
considered Color 2
-3 2 e 5=4
-4 nodes:
Color 3
-5 7,6,5,4,3,2,1
-6
3 COLORS
Zavod za telekomunikacije 32
An Event Scheduling Problem
University of Zagreb, Croatia
Greedy=Optimal
Greedy
Greedy
Optimal
Zavod za telekomunikacije 34
Example Greedy approaches
University of Zagreb, Croatia
Greedy=Optimal
Greedy
Greedy
Optimal
Zavod za telekomunikacije 35
Example Greedy approaches
University of Zagreb, Croatia
Greedy = Optimal
Greedy
Optimal
Zavod za telekomunikacije 36
University of Zagreb, Croatia
Zavod za telekomunikacije 37
Vehicle routing
University of Zagreb, Croatia
Zavod za telekomunikacije 38
Capacitated Vehicle Routing Problem with
Multiple Depots
University of Zagreb, Croatia
Zavod za telekomunikacije 39
Capacitated Vehicle Routing Problem with
Multiple Depots
University of Zagreb, Croatia
The weight of edge a = (i, j) from the set of edges A is given as Ca. For
each depot i∈I , the capacity of the depot is Wi and the cost of opening
the depot is Oi. Each customer j∈J has a capacity demand of dj. We are
given a set of K identical vehicles, each with a capacity of Q. Each
chosen vehicle has a fixed cost of F and can only execute one route,
i.e., it visits a subset of customers and returns back to the depot where
it started the route from.
Zavod za telekomunikacije 40
Capacitated Vehicle Routing Problem with
Multiple Depots
University of Zagreb, Croatia
Potential solution
with 3 of 5 depots
opened
Zavod za telekomunikacije 41
University of Zagreb, Croatia
Zavod za telekomunikacije 42
Example: Greedy 1
University of Zagreb, Croatia
Zavod za telekomunikacije 43
Example: Greedy 2
University of Zagreb, Croatia
Generally,
Easy to understand
Easy to implement
Fast