Pasi Fränti
Download
1 / 31

Greedy Algorithms - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

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.

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

PowerPoint Slideshow about ' Greedy Algorithms' - denis


An Image/Link below is provided (as is) to download presentation

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

Pasi Fränti

Greedy Algorithms

8.10.2013


Greedy algorithm
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

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;


Example of Prim

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

C

A

A

B

B

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

Cycle

No cycle

Remove

Link BC


C

C

2

2

A

A

2

2

1

1

B

B

Proof of optimalityCase: minimum link

Add AB

Link AB is minimum

Suppose it is not in MST

Path A→B must exist

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


E

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Manchester

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


Generalized Knapsack problem

Will appear 2014…



Traveling salesman problem
Traveling salesman problem

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


Greedy algorithms
Greedy algorithms

Not

  • Coin problem

  • Minimum spanning tree

  • Generalized knapsack problem

  • Traveling salesman problem

Solved

Solved

Not


ad