Greedy Algorithms for Optimization
Greedy Algorithms for Optimization
(AMMM)
Greedy Algorithms
(for Combinatorial Optimization)
Luis Velasco
(lvelasco @ ac.upc.edu)
Campus Nord D6-107
Combinatorial Optimization
s.t. SF
2
Example: The Travelling Salesman Problem (TSP)
• Given a graph G(V,E) with a set of cities (V) and their pairwise distances
• The task is to find a shortest possible tour that visits each city exactly
once (Hamiltonian cycle).
• TSP is a combinatorial problem. Its search space is (n-1)!/2
the ground set is that of all edges connecting the cities to be visited,
F is formed by all edge subsets that determine a Hamiltonian cycle.
f(S) is the sum of the distances of all edges in each Hamiltonian cycle
• What factorial means?
n search space
n search space 20 6.08E+16
3 1 30 4.42E+30
4 3 40 1.02E+46
5 12 50 3.04E+62 Information on the largest TSP instances solved
6 60 60 6.93E+79 to date can be found in:
7 360 70 8.56E+97 http://www.math.uwaterloo.ca/tsp/optimal/index.html
8 2,520 80 4.47E+116
9 20,160 90 8.25E+135
10 181,440 100 4.67E+155
2 2 2 2
1 1 1 1
3 3 3 3
5 5 5 5
4 4 4 4
2 2 2 2
1 1 1 1
3 3 3 3
5 5 5 5
4 4 4 4
2 2 2 2
1 1 1 1
3 3 3 3
5 5 5 5
4 4 4 4
4
Greedy algorithm
6
Example: TSP
Example: TSP
2
1
1 2 3 4 5
1 - 10 12 7 9
2 - 11 17 4
3
3 - 5 14
5 4 - 9
4 5 -
10 2 10 2 10 2
1 1 12 1 11
12
9 7 3 9 3 9 14 3
17
5 5 5 5
4 9 4 4
2 2 2
1 11 1 11 1
4
3 3 3
5 5 5
4 4 4
8
Example: TSP
u
• Nearest Insertion (greedy): From a
cip+civ-cpv
small cycle, the algorithm expands the v
cycle by adding the nearest vertex. c ip+ciu-cpu
p
10
Example 1: Assign tasks to computers (lab session 2)
Tasks Computers
t1
<t2, c1> c1
𝑟𝑒𝑠𝑖𝑑𝑢𝑎𝑙𝐶𝑎𝑝𝑎𝑐𝑖𝑦 𝑐 − 𝑟
1−
rt t2 <t2, c2> 𝑟
c2 rc 𝑞 < t, c > = 𝑚𝑎𝑥
𝑟𝑒𝑠𝑖𝑑𝑢𝑎𝑙𝐶𝑎𝑝𝑎𝑐𝑖𝑦 𝑐′
t3
1− | 𝑐 𝑖𝑛 𝐶, 𝑐 ≠ 𝑐
<t2, c3> 𝑟
c3
t4
S←∅
sortedT ← sort(T, rt, DESC)
for each c in C do residualCapacity(c) = rc
for each t in T do
C(t)←∅
for each c in C do
if rt ≤ residualCapacity(c) then C(t) ← C(t) U {c}
if |C(t)|=0 then return INFEASIBLE
cbest ← argmin{q(<t,c>) | c in C(t)}
residualCapacity(cbest) ← residualCapacity(cbest) - rt
S ← S U {<t, cbest>}
return S
Algorithmic Methods for Mathematical Models (AMMM) Luis Velasco 11
11
Computers c1 c2 c3
residualCap 195.16 503.68 140.89 Solution
load 0.6141 0 0.799 S {<t2,c3>,<t3,c1>,<t1,c2>,<t4,c2>}
S {<t2,c3>,<t3,c1>} f(S) 0.799
12
Set Covering
13
14
Example 2: Network planning
• A set of users U needs to be connected to the Internet. For that purpose, we
have a set of access point locations A where we could install routers (one per
access point at the most).
For each user u, the amount cru of capacity units it consumes from the router it is
connected to is given.
15
16
Network planning: Greedy algorithm
qu min{q(u , a)}
Infeasible either because of the reach or the load
if d (u, a) max{d m} cru max{km } cru '
u 'U ( a )
User u can be served with the router currently
qu , a 0 if d (u, a) d a cru ka cru ' installed in location a
u 'U ( a )
f m fa if d (u, a) d a cru ka cru ' , d (u , a ) d m cru km cru '
u 'U ( a ) u 'U ( a )
Router currently installed in location a needs to be upgraded because of the reach or the load
to serve user u
S ← ∅, C←U
Evaluate the incremental costs q(u) for all u ∈ C
while C ≠ ∅ do
umin ← argmin {q(u) | u ∈ C}
S ← S U {umin}
C ← C \ {umin}
Reevaluate the incremental costs q(u) for all u ∈ C
return S
Algorithmic Methods for Mathematical Models (AMMM) Luis Velasco 17
17
2
3 3
1
5 6
2 2
4 2
3,7 1,8
4
1 1
3
0 0
0 1 2 3 4 0 1 2 3 4
u 1 2 3 4 5 6 7 8 9 10
R1 R2 R3 d(u,a) x 1 2 0 4 1 2 0 1 3 2
f 100 140 180 y 1 3 1 1 2 2 1 1 4 4
k 6 8 10 1 2 3 2.2 0.0 2.8 2.8 1.4 1.0 2.8 2.2 1.4 1.0
d 2 3 4 2 1 2 1.0 1.4 1.4 3.2 0.0 1.0 1.4 1.0 2.8 2.2
3 1 1 0.0 2.2 1.0 3.0 1.0 1.4 1.0 0.0 3.6 3.2
4 0 2 1.4 2.2 1.0 4.1 1.0 2.0 1.0 1.4 3.6 2.8
5 1 3 2.0 1.0 2.2 3.6 1.0 1.4 2.2 2.0 2.2 1.4
a x y
Algorithmic Methods for Mathematical Models (AMMM) Luis Velasco 18
18
Network planning: Iterative execution (1/5)
#1
u 1 2 3 4 5 6 7 8 9 10
cr 2 3 4 1 2 2 1 2 3 4
q(u) 100 100 100 140 100 100 100 100 100 100
a 3 1 3 1 2 1 3 3 1 1
d(u,a) 0.0 0.0 1.0 2.8 0.0 1.0 1.0 0.0 1.4 1.0
a 1 2 3 4 5
m R1
U(a) {1}
km-cr 4
#2
u 1 2 3 4 5 6 7 8 9 10
cr 2 3 4 1 2 2 1 2 3 4
q(u) 40 0 40 0 0 0 0 80 80
a 3 3 3 3 3 3 3 3 3 3
d(u,a) 0.0 2.2 1.0 3.0 1.0 1.4 1.0 0.0 3.6 3.2
a 1 2 3 4 5
m R1
U(a) {1,8}
km-cr 2
19
a 1 2 3 4 5
m R1
U(a) {1,5,8}
km-cr 0
#4
u 1 2 3 4 5 6 7 8 9 10
cr 2 3 4 1 2 2 1 2 3 4
q(u) 80 80 40 40 40 80 80
a 3 3 3 3 3 3 3 3 3 3
d(u,a) 0.0 2.2 1.0 3.0 1.0 1.4 1.0 0.0 3.6 3.2
a 1 2 3 4 5
m R2
U(a) {1,5,7,8}
km-cr 1
20
Network planning: Iterative execution (3/5)
#5
u 1 2 3 4 5 6 7 8 9 10
cr 2 3 4 1 2 2 1 2 3 4
q(u) 100 100 0 40 100 100
a 3 1 4 3 3 3 3 3 1 1
d(u,a) 0.0 0.0 1.0 3.0 1.0 1.4 1.0 0.0 1.4 1.0
a 1 2 3 4 5
m R2
U(a) {1,4,5,7,8}
km-cr 0
#6
u 1 2 3 4 5 6 7 8 9 10
cr 2 3 4 1 2 2 1 2 3 4
q(u) 100 100 40 100 100
a 3 1 4 3 3 3 3 3 1 1
d(u,a) 0.0 0.0 1.0 3.0 1.0 1.4 1.0 0.0 1.4 1.0
a 1 2 3 4 5
m R3
U(a) {1,4,5,6,7,8}
km-cr 0
21
a 1 2 3 4 5
m R1 R3
U(a) {2} {1,4,5,6,7,8}
km-cr 3 0
#8
u 1 2 3 4 5 6 7 8 9 10
cr 4 3 4 1 2 2 1 2 3 4
q(u) 40 0 40
a 3 1 1 3 3 3 3 3 1 1
d(u,a) 0.0 0.0 2.8 3.0 1.0 1.0 1.0 0.0 1.4 1.0
a 1 2 3 4 5
m R1 R3
U(a) {2,9} {1,4,5,6,7,8}
km-cr 0 0
22
Network planning: Iterative execution (5/5)
#9
u 1 2 3 4 5 6 7 8 9 10
cr 4 3 4 1 2 2 1 2 3 4
q(u) 80 80
a 3 1 1 3 3 3 3 3 1 1
d(u,a) 0.0 0.0 2.8 3.0 1.0 1.0 1.0 0.0 1.4 1.0
a 1 2 3 4 5
m R3 R3
U(a) {2,9,10} {1,4,5,6,7,8}
km-cr 0 0
#10
u 1 2 3 4 5 6 7 8 9 10
cr 4 3 4 1 2 2 1 2 3 4
q(u) 100
a 3 1 4 3 3 3 3 3 1 1
d(u,a) 0.0 0.0 1.0 3.0 1.0 1.0 1.0 0.0 1.4 1.0
a 1 2 3 4 5
m R3 R3 R1
Solution Cost=460 U(a) {2,9,10} {1,4,5,6,7,8} {3}
km-cr 0 0 2
23
Greedy Algorithms
Luis Velasco
(lvelasco @ ac.upc.edu)
Campus Nord D6-107
24