greedy techniques

Post on 14-Apr-2018

252 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 1/26

Greedy Techniques

Minimum Spanning Tree

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 2/26

Minimum spanning Tree

 A minimum spanning tree in a connected weighted graph

is a spanning tree that has the smallest possible sum of 

weights its edges.

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 3/26

Greedy Algorithms

Greedy algorithms is a procedure that makes an optimal choice at each

of its steps.

The two algorithms Presented for constructing minimum spanningtrees are greedy algorithms that do produce optimal solution. Those

are:-

->Prim’s Algorithms 

->Kruskal’s Algorithms 

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 4/26

Prim’s Algorithms 

The First Algorithms was given by Robert Prim in 1957.

Rules for constructing minimum spanning tree in case of prim’salgorithms.

->Begin by choosing any edge with smallest weight (from a givenweighted graph) putting it into the spanning tree.

-> Successively add edges of minimum weight that are incident to avertex already in the tree and not forming a simple circuit with

those edges already in the tree.

->Stop when (v-1)edges have been added.

Here v is vertices in a actual weighted graph,

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 5/26

Let us take one example

 A Weighted Graph. (Fig. 1)

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 6/26

FIRSTLY

Before Constructing a minimum spanning

tree of fig. 1,Firstly write the cost of all the

edges one by one in Ascending order.

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 7/26

EDGES (e) AND VERTICES (v)

Total no. of edges (e) in fig.1 is 7.

Total no. of vertices (v) in fig.1 is 6.

Cost or weights of edges in ascending order:-

S/no.  Edges Weight

1) A------D 1

2) A------B 2

3) B------E 2

4) D------E 4

5) B------C 5

6) E-------F 7

7) C-------F 8

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 8/26

MINIMUM SPANNING TREE

Now our task is to construct minimum

spanning tree using prim’s Algorithms.

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 9/26

FINDING A MINIMUM SPANNING

TREE

Firstly in a spanning tree writes vertices only:-Note:-(But the pattern of writing the vertices must be same as given in

a weighted graph ( for eg. :-in fig. 1)

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 10/26

First Step in a prim’s Algorithms for 

constructing a minimum spanning tree is:-

Begin by choosing any edge with smallest weight (from agiven weighted graph) putting it into the spanning tree.

In a fig.1:-(First edges with smallest weight is)

S/no.  Edges Weight

1) A------D 1

Now our first step is to put this edges in spanning tree.

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 11/26

This is a spanning tree but not a

minimum spanning tree

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 12/26

Second Step

• Successively add edges of minimum weight that are

incident to a vertex already in the tree and not

forming a simple circuit with those edges already in the

tree

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 13/26

SECOND EDGE WITH A WEIGHT 2

S/no.  Edges Weight

1) A------B 2

Before adding this edge we have to see whether this edge incident

to a vertex already in the tree (and it obeying the rule so this edge

will be added to tree.)

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 14/26

THIRD EDGE WITH A WEIGHT 2

S/no.  Edges Weight

1) B------E 2

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 15/26

FOURTH EDGE WITH A WEIGHT 4

S/no.  Edges Weight

1) D------E 4

NOTE:- After adding this edge, the spanning tree form a simple circuit

which is against the second rule’s of prim’s that it should not form a

simple circuit so, this edge will not included in a spanning tree.)

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 16/26

SO FOURTH EDGE WITH A WEIGHT 5 WILL BE

S/no.  Edges Weight

1) B------C 5

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 17/26

FIFTH EDGE WITH A WEIGHT 7

S/no.  Edges Weight

1) E------F 7

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 18/26

THIRD STEP IS:-

Stop when (v-1)edges have been added.

Here v is vertices in a actual weighted graph.

Hence in a actual weighted graph our total number of vertices is 6.

So According to rule number 3 stop when (v-1) edgeshave been added.

(6-1)=5 (in our spanning tree we have added a 5 edgesso, it is our final step and also a minimum spanningtree).

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 19/26

MINIMUM SPANNING TREE OF FIG.1.

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 20/26

MINIMUM WEIGHT OF FIG.1

Choice  Edges Weight

1) A------D 1

2) A------B 2

3) B------E 2

4) B------C 5

5) E------F 7

Minimum Weight is sum of weight and answer is:-17

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 21/26

Prim’s Algorithm 

• procedure Prim( G: weighted connectedundirected graph with v vertices)

• T:=a minimum-weight edge

• for i :=1 to v-2• begin

• e :=an edge of minimum weight incident to avertex in T and not forming a simple circuit in T

if added to T• T:= T with e added

• end {T is a minimum spanning tree of G)

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 22/26

Kruskal’s Algorithms 

Rules for constructing minimum spanning tree in case of Kruskal’s algorithms.

->Begin by choosing any edge with smallest weight (from a given weightedgraph) putting it into the spanning tree.

-> Successively add edges of minimum weight, but not add the edge that

after adding that edge it’s form a simple circuit with those edges already inthe tree.

->Stop when (v-1)edges have been added.

Here v is vertices in a actual weighted graph,

**NOTE:- Here same step will be followed but in this case one point will beskipped that we have no need to check,after adding 1 edge in a spanningtree,that whether that edge is incident to a vertex already in the spanningtree ,but we have to check the condition of simple circuit (it should not forma simple circuit).

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 23/26

Kruskal’s Algorithms 

• procedure Kruskal’s (G :weighted connected

undirected graph with v vertices)

• T:=empty graph

• For i :=1 to v-1

• begin

• e := any edge in G with smallest weight that

does not form a simple circuit when added toT• T:=T with e added

• end {T is a minimum spanning tree of G

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 24/26

COMPLEXITY

 A minimum spanning tree of a graph with e edges and v

vertices, Kruskal's algorithm can be carried out using

a (e log e) operations and Prim's algorithm can be

carried out using a (e log v) operations.

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 25/26

PRACTISE

7/27/2019 Greedy Techniques

http://slidepdf.com/reader/full/greedy-techniques 26/26

THANK YOU

MANISH KUMAR RAI

LOVELY PROFESSIONAL

UNIVERSITYBTECH C.S.E

top related