Anda di halaman 1dari 19

Kruskals and Dijkstras Algorithm

Agenda

INTRODUCTION ABOUT GRAPH SPANNING TREE MINIMAL SPANNING TREE KRUSKALS ALGORITHM DIJKSTRAS ALGORITHM

Graph
A Graph G=(V, E), Where V is a nonempty set of vertices & E is a set

of edges. every edge is associated with unordered pair of vertices. V={V1, V2, V3, V4} ={e1, e2}

V3
e2 e1 V4

V1

e1=<V4,V1> e2=<V3,V2>

Tree

V2

A Graph G is called a Tree if G is a connected acyclic graph

Spanning Tree
Let G be a Graph & H be a Spanning Sub graph of

G. If H is a tree then H is called as spanning tree.


V2 1 V1 3 V5 8 V4 4 5 7

V3

Weighted Connected Graph and its Cost adjacent Matrix :

G=(V, E) be a connected graph if there is a function f : V(G) -> R then it is called weighted connected graph. A(G),The cost adjacency matrix of G, whose entries are given by

ij=

cij if <i,j> E(G)


0/ otherwise

V2 1 V1

V3

4 3
V5

V4

Result: G is connected iff G has at least one spanning tree.

Minimal Spanning Tree-MST


Let G be a weighted connected Graph among all the

spanning tree of G, whose weight is Minimal is called Minimal Spanning Tree(MST).


They are 3 algorithms to find minimal spanning tree

Prims Algorithm (Based on Vertices ) (order is O(E +V lg V) Kruskals Algorithm(Based on Edges)(order is O(E lg E) Sollin's Algorithm

Kruskals Algorithm
Input Connected weighted graph with |v(G)|=n Or Cost adjacency matrix(n*n)
Output Minimal Spanning Tree

Kruskals Algorithm contd..


Step-1: Arrange the edges in ascending order according to their weights choose the minimum weight edge say e1. Step-2: Having had chosen <e1, e2, . ,eK>, choose eK+1 as follows :<e1, e2,,eK, eK+1> is acyclic & the remaining edges w(eK+1) is minimal. Step-3: Repeat step-2 until (n-1) edges are included.

Example
1 2

Dijkstras Algorithm
[Single Source Shortest Path Problem] Input:

Connected weighted graph G(V|G|=n) Or Cost adjacency matrix (n*n) with a specified vertex as source say v.

Output: Shortest path from v, to all other vertices in G.

Dijkstras Algorithm(Labeling procedure)


Procedure: Dijkstra(G:weighted connected simple

graph,with all weights positive) {G has vertices a=V0,V1,Vn=Z and weights w(Vi,Vj) where w(Vi,Vj) = if {Vi,Vj} is not an edge in G} for i:=1 to n
L(Vi):= L(a):= 0 S:= { the labels are now initialized so that the label of a is 0 and all other labels are , and S is the empty set}

Dijkstras Algorithm contd


While z s

Begin

U:=a vertex not in S with L(u) minimal S:=S { u}

For all vertices V not in S


If L(u)+w(u , v)<L(v) then L(v):=L(u)+w( u,v) {this adds a vertex to S with minimal label and updates the labels

of vertices not in S}

End{L(z)=length of a shortest path from a to z}

Example( Solved by other method)


V2 3 6 V4 6 5 2 V3 Result <V1,V3>V1-V3 <V1,V2>V1-V2 <V1,V4>V1-V3-V4 <V1,V5>V1-V3-V5 <V1,V6>V1-V3-V4-V6 L(V2) 3 3 3 3 3 L(V3) 2 2 2 2 2 7 L(V4) V5 L(V5) L(V6) T {V1,V3} {V1,V3, V2} {V1,V3, V2, V4} {V1,V3, V2, V4, V5} {V1,V3, V2, V4, V5, V6}

V1

8 4

V6

7 7 7 7

9 9 9 9


13 13

Result Vertices L(V) <V1,V2> V1 - V2 3 <V1,V3> V1 V3 2 7 <V1,V4> V1 V4 9 <V1,V5> V1 V5 <V1,V6> V1 V6 13

Application of MST & Single Source Shortest Path

1.Avoid Looping in the network 2.In I/p Multicasting 3.Routing

References
1.Discrete Mathematics and its Applications ,6th edition., Tata McGraw-Hill.,2007.,by Kenneth.H.Rosen 2.Graph Theory and its Applications ,2nd edition.,Chapman & Hall/CRC.,2006., By Jonathan L.Gross Jay Yellen 3.Introduction to Algorithms.,Prentice Hall of India.,2004.,by Thomas H.Cormen,Charles E.Leiserson,Ronald L.Rivest

Thank You

Anda mungkin juga menyukai