application of greedy method

26
APPLICATION OF GREEDY METHOD KRUSKAL’S ALGORITHM TO FIND MINIMUM COST SPANNING TREES

Upload: techmx

Post on 29-Oct-2014

103 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Application of Greedy Method

APPLICATION OF GREEDY METHOD

KRUSKAL’S ALGORITHM TO FIND MINIMUM

COST SPANNING TREES

Page 2: Application of Greedy Method

INTRODUCTION

An algorithm is a mathematical procedure often used to solve optimization problems in a finite number of steps.

Most solution algorithms make choices based on a global overview of all current and future possibilities aiming at reaching the single global optimum solution.

Greedy algorithms make choices that look best at that every moment.

Page 3: Application of Greedy Method

When compared to algorithms that guarantee to yield a global optimum solution, greedy algorithms have several advantages:

They are easier to implement, they require much less computing resources, they are much faster to execute.

Their only disadvantage being that they do not always reach the global optimum solution

Page 4: Application of Greedy Method

when the global optimum solution is not reached, most of the times the reached sub- optimal solution is a very good solution.

Several classical optimization problems like minimum spanning tree and optimal prefix codes for data compression yield global optimum solutions using greedy algorithm.

Page 5: Application of Greedy Method

SPANNING TREES

A tree T is said to be a spanning tree of a connected graph G if T is a subgraph of G and T contains all vertices of G.

Since the vertices of G are barely hanging together in a spanning tree, it is a sort of skeleton of the original graph g. so, a spanning tree is called a skeleton or scaffolding of G.

Among the spanning trees of G, one with the smallest weight is called minimal spanning tree or shortest spanning tree or shortest-distance spanning tree.

Page 6: Application of Greedy Method

KRUSKAL’S ALGORITHM

A second greedy algorithm was developed by Joseph Kruskal.

Steps in Kruskal’s algorithm: list all edges of G in order of increasing weight. Select a smallest edge of G. For each successive step, select (from all

remaining edges of G) another smallest edge that makes no circuit

with the previously selected edges. Continue until n-1 edges have been selected.

Page 7: Application of Greedy Method

At each stage, the algorithm chooses an edge

to add to its current partial solution. To do so, it needs to test each candidate edge (u ,v ) to

see whether the endpoints u and v lie in different components; otherwise the edge produces a

cycle.

Page 8: Application of Greedy Method

Kruskal's Algorithm

EXAMPLE 1

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 9: Application of Greedy Method

ALGORITHM.

enqueue edges of G in a queue in increasing order of cost.T = ;while(queue is not empty){

dequeue an edge e; if(e does not create a cycle with edges in T)

add e to T;}return T;

Page 10: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 11: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 12: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 13: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 14: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

cycle!!

Page 15: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 16: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 17: Application of Greedy Method

Kruskal's Algorithm

C

FE

A B

D

3

2

1 2

2

minimum- spanning tree

Page 18: Application of Greedy Method

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

EXAMPLE 2

Page 19: Application of Greedy Method

19

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

Page 20: Application of Greedy Method

20

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

Page 21: Application of Greedy Method

21

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

Page 22: Application of Greedy Method

22

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

Page 23: Application of Greedy Method

23

MINIMAL SPANNING TREEE

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

a

b

h

c d

e

fg

i

4

8 7

9

10

144

2

2

6

1

7

11

8

Page 24: Application of Greedy Method

Kruskal Running Time

Initialization O(V) time Sorting the edges Q(E lg E) = Q(E lg V) (why?) O(E) calls to FindSet Union costs

Let t(v) – the number of times v is moved to a new cluster

Each time a vertex is moved to a new cluster the size of the cluster containing the vertex at least doubles: t(v) £ log V

Total time spent doing Union Total time: O(E lg V)

24

( ) logv V

t v V V

Page 25: Application of Greedy Method

Applications

Where ever the minimum distance is needed to

achieve there this algorithm gets place.

For example.

In telephone lining under the ground, they have to

connect all the cities with the minimum amount of wire.

If the new connection is in position to add between cities,

there we have to consider the minimum cost distance.

Assume, if the minimum distance is not calculated, the

wastage of wires will be in peak.

Page 26: Application of Greedy Method

Railways track construction.

National highway construction.

Flyover bridge construction.

Network between two intranets.

The another non-technical example will be as,

The by-pass rider or point to point buses will move on with

the minimum distance path to reach the destination much

faster. They will never circulate their bus through

unwanted towns and villages.