greedy algorithms

33
Greedy Algorithms Pasi Fränti 28.9.2015

Upload: denis

Post on 20-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Pasi Fränti. Greedy Algorithms. 8.10.2013. Greedy algorithm. Coin problem Minimum spanning tree Generalized knapsack problem Traveling salesman problem. Coin problem. Task: Given a coin set, pay the required amount (36 snt) using least number of coins. 25. 10. 1. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Greedy Algorithms

Greedy Algorithms

Pasi Fränti28.9.2015

Page 2: Greedy Algorithms

Greedy algorithm

1. Coin problem2. Minimum spanning tree3. Generalized knapsack problem4. Traveling salesman problem

Page 3: Greedy Algorithms

Task: Given a coin set, pay the required amount (36 snt) using least number of coins.

Coin problem

Page 4: Greedy Algorithms

25 110

Coin problemAnother coin set

Amount to be paid: 30

Greedy:

Optimal:

Page 5: Greedy Algorithms

Blank space for notes

Page 6: Greedy Algorithms

Minimum spanning treeWhen greedy works

Input: GraphOutput: Sub-graph

Tree No cyclesSpanning tree All nodes reachableMinimum Sum of weights minimum

Page 7: Greedy Algorithms

Prim(V, E): RETURN T

Select (u,v)E with min weightSS{u,v}; PP{(u,v)}; EE\{(u,v)};

REPEATSelect (u,v) with min weight (u,v)E, uS,

vSSS{v}; PP{(u,v)}; EE\{(u,v)};

UNTIL S=V

Return P;

Minimum spanning treePrim’s algorithm

Page 8: Greedy Algorithms

136

170315

148 78

231

234

12089

131109

116

86

246

182

216110

117

199

121142

24279

191178

191

126149

17051 112

90163

59

14373

63 53

27135

10558

116

72

79

Example of Prim

Page 9: Greedy Algorithms
Page 10: Greedy Algorithms

Proof of optimalityGeneral properties of spanning trees

• Spanning tree includes N-1 links

• There are no cycles• Minimum spanning tree is

the one with the smallest weights

AB

C

AB

CRemoveLink BC

Cycle No cycle

Page 11: Greedy Algorithms

Proof of optimalityCase: minimum link

2

AB

C

21

2

AB

C

21

• Link AB is minimum• Suppose it is not in MST• Path A→B must exist

Add AB

• Adding AB we can remove another link (e.g. AC)

• Path A→C exists• All nodes reached from

C can now be reached from B

Page 12: Greedy Algorithms

Proof of optimalityInduction step

A

B

C

4

3 E

D

ReplaceCD by CE

MST solved forSubset S

• Suppose CE is minimum connecting S outside• Path D→E must exist and is outside S

Page 13: Greedy Algorithms

Proof of optimalityInduction step

A

B

C

4

3 E

D

MST solved forSubset S

• Path D→E still exist as before• All nodes reachable via D

can now be reached via C→E→D

Page 14: Greedy Algorithms

Source of data just for fun

Page 15: Greedy Algorithms

Minimum spanning treeKruskal’s algorithm

Kruskal(V, E): RETURN T

FOR ALL vVCreateSet(v);

P; SORT(E); // according to decreasing weights

FOR each eE DOIF FindSet(u)FindSet(v) THEN

UNION(u,v);PP{(u,v)};

Return P;

Page 16: Greedy Algorithms

Blank space for notes

Page 17: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 18: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 19: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 20: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 21: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 22: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 23: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 24: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 25: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 26: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 27: Greedy Algorithms

Cardiff

Sheffield

Nottingham

Oxford

Southampton

Bristol

Shrewsbury

Liverpool

Aberystwyth

B/ham

Manchester

50

40

40

30

80

7080

50

90

50

110

70

120

110 70 100

Page 28: Greedy Algorithms

Blank space for notes

Page 29: Greedy Algorithms

Input: Weight of N items {w1, w2, ..., wn}

Cost of N items {c1, c2, ..., cn}Knapsack limit S

Output: Selection for knapsack: {x1,x2,…xn}

where xi {0,1}. Sample input:

wi={1,1,2,4,12}

ci ={1,2,2,10,4}

S=15

Knapsack problemProblem definition

Page 30: Greedy Algorithms

Skipped this year

Generalized Knapsack problem

Page 31: Greedy Algorithms

Semi-blank space

Page 32: Greedy Algorithms

Traveling salesman problem

Exercise task

Page 33: Greedy Algorithms

Greedy algorithms

1. Coin problem2. Minimum spanning tree3. Generalized knapsack problem4. Traveling salesman problem

Solved

Not

SolvedNot