kruskal’s and dijkstra’s algorithm
Post on 23-Oct-2014
161 Views
Preview:
TRANSCRIPT
Kruskal’s and Dijkstra’s Algorithm
INTRODUCTION ABOUT GRAPH
SPANNING TREE
MINIMAL SPANNING TREE
KRUSKAL’S ALGORITHM
DIJKSTRA’S ALGORITHM
Agenda
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}
e1
e2
V1V3
V4 V2
e1=<V4,V1>
e2=<V3,V2>
Tree• 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 6
V1
3
V5
1
4
V4
V3
8
75
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
cij if <i,j> ϵ E(G)
0/∞ otherwise
Weighted Connected Graph and its Cost adjacent Matrix :
aij
=
Result: G is connected iff G has at least one spanning tree.
V2 6
V1
3
V5
1
4
V4
V3
8
75
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
Prim’s Algorithm (Based on Vertices ) (order is O(E +V lg V)
Kruskal’s Algorithm(Based on Edges)(order is O(E lg E)
Sollin's Algorithm
Kruskal’s Algorithm
Input Connected weighted graph with |v(G)|=n Or Cost adjacency matrix(n*n)
Output Minimal Spanning Tree
Kruskal’s 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
3 4
5 6
7
Dijkstra’s 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.
Dijkstra’s 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}
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}
Dijkstra’s Algorithm contd…
Example( Solved by other method)
V2
V1
V3 V5
V4
V65
6
63
2
7
4
85
Result L(V2) L(V3) L(V4) L(V5) L(V6) T
<V1,V3>V1-V3
<V1,V2>V1-V2
<V1,V4>V1-V3-V4
<V1,V5>V1-V3-V5
<V1,V6>V1-V3-V4-V6
3 2 ∞ ∞ ∞3 2 7 9 ∞3 2 7 9 ∞3 2 7 9 13
3 2 7 9 13
{V1,V3}
{V1,V3, V2}
{V1,V3, V2, V4}
{V1,V3, V2, V4, V5}
{V1,V3, V2, V4, V5, V6}
<V1,V2><V1,V3><V1,V4><V1,V5><V1,V6>
V1 - V2
V1 – V3V1 – V4
V1 – V5
V1 – V6
327913
Result Vertices L(V)
1.Avoid Looping in the network
2.In I/p Multicasting
3.Routing
Application of MST & Single Source Shortest Path
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
References
Thank You
top related