1 / 31

# Greedy Algorithms - PowerPoint PPT Presentation

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Greedy Algorithms' - denis

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Greedy Algorithms

8.10.2013

• Coin problem

• Minimum spanning tree

• Generalized knapsack problem

• Traveling salesman problem

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

10

1

Coin problemAnother coin set

Amount to be paid: 30

Greedy:

Optimal:

Minimum spanning treeWhen greedy works

Needs problem definition!

Tree = … (no cycles)

Spanning Tree = …

Minimum = … (couple of examples with simple graph)

Minimum spanning treePrim’s algorithm

Prim(V, E): RETURN T

Select (u,v)E with min weight

SS{u,v}; PP{(u,v)}; EE\{(u,v)};

REPEAT

Select (u,v) with min weight  (u,v)E, uS, vS

SS{v}; PP{(u,v)}; EE\{(u,v)};

UNTIL S=V

Return P;

117

216

110

246

199

182

170

121

231

315

142

79

242

136

191

148

78

120

191

126

178

149

89

116

234

170

112

51

79

131

109

86

73

163

143

72

90

63

53

105

59

27

58

135

116

C

A

A

B

B

Proof of optimalityGeneral properties of spanning trees

There are no cycles

Minimum spanning tree is the one with the smallest weights

Cycle

No cycle

Remove

C

2

2

A

A

2

2

1

1

B

B

Suppose it is not in MST

Path A→B must exist

Path A→C exists

All nodes reached from C can now be reached from B

3

C

A

4

D

B

Proof of optimalityInduction step

MST solved for

Subset S

ReplaceCD by CE

Suppose CE is minimum connecting S outside

Path D→E must exist and is outside S

Proof of optimalityInduction step

E

MST solved for

Subset S

3

C

A

4

D

B

Path D→E still exist as before

All nodes reachable via D can now be reached via C→D

Minimum spanning treeKruskal’s algorithm

1. A   // initially A is empty

2. for each vertex v  V[G] // line 2-3 takes O(V) time

3. do Create-Set(v) // create set for each vertex

4. sort the edges of E by nondecreasing weight w

5. for each edge (u,v)  E, in order bynondecreasing weight

6. do if Find-Set(u)  Find-Set(v) // u&v on different trees

7. then A  A  {(u,v)}

8. Union(u,v)

9. return A

Total running time is O(E lg E).

Needs revisions:

• Remove numbers

• Change terminology

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

40

30

Liverpool

Sheffield

110

70

40

Shrewsbury

50

50

Nottingham

80

B/ham

110

Aberystwyth

70

100

90

120

Oxford

50

Bristol

Cardiff

80

70

Southampton

Generalized Knapsack problemProblem definition

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

Will appear 2014…

GreedyTSP(V, E, home): RETURN T

X[1]home;

FOR i 1 TO N-1 DO

Select (u,v) with min weight  (u,v)E, uS, vS

X[………….. SS{v}; ….

UNTIL V≠{}

Return something;

Needs to be done

Not

• Coin problem

• Minimum spanning tree

• Generalized knapsack problem

• Traveling salesman problem

Solved

Solved

Not