PRIM’S AND KRUSKAL’S
ALGORITHM
Prim's algorithm is a minimum spanning tree
algorithm that takes a graph as input and finds
the subset of the edges of that graph which
•form a tree that includes every vertex
•has the minimum sum of weights among all
the trees that can be formed from the graph
Prim's Algorithm
Prim's Spanning
Tree Algorithm
Prim's algorithm to find minimum cost
spanning tree (as Kruskal's algorithm) uses
the greedy approach. Prim's algorithm
shares a similarity with the shortest path
first algorithms.
Prim's algorithm, in contrast with Kruskal's
algorithm, treats the nodes as a single tree
and keeps on adding new nodes to the
spanning tree from the given graph.
Prim's algorithm
Step 1 - Remove all loops and parallel edges
Remove all loops and parallel edges from the
given graph. In case of parallel edges, keep the
one which has the least cost associated and
remove all others.
Step 2 - Choose any arbitrary node as
root node
In this case, we choose S node as the
root node of Prim's spanning tree. This
node is arbitrarily chosen, so any node
can be the root node. One may wonder
why any video can be a root node. So
the answer is, in the spanning tree all
the nodes of a graph are included and
because it is connected then there
must be at least one edge, which will
join it to the rest of the tree
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges
with weight 7 and 8, respectively. We choose the edge S,A as it is
lesser than the other.
Now, the tree S-7-A is treated as
one node and we check for all
edges going out from it. We select
the one which has the lowest cost
and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again
treat it as a node and will check all the edges again.
However, we will choose only the least cost edge. In this
case, C-3-D is the new edge, which is less than other
edges' cost 8, 6, 4, etc.
After adding node D to the spanning tree, we
now have two edges going out of it having the
same cost, i.e. D-2-T and D-2-B. Thus, we can
add either one. But the next step will again yield
edge 2 as the least cost. Hence, we are showing
a spanning tree with both edges included.
Kruskal's Spanning Tree
Algorithm
Kruskal's algorithm to find the minimum
cost spanning tree uses the greedy
approach. This algorithm treats the graph
as a forest and every node it has as an
individual tree. A tree connects to another
only and only if, it has the least cost
among all available options and does not
violate MST properties.
How Kruskal's algorithm works
It falls under a class of algorithms called
greedy algorithms that find the local optimum in the
hopes of finding a global optimum.
We start from the edges with the lowest weight and
keep adding edges until we reach our goal.
The steps for implementing Kruskal's algorithm are
as follows:
1. Sort all the edges from low weight to high
Take the edge with the lowest weight and add it to
the spanning tree.
2.If adding the edge created a cycle, then reject this
edge.
3.Keep adding edges until we reach all vertices.
Kruskal's algorithm −
Step 1 - Remove all loops graph.
and Parallel Edges
Remove all loops and parallel edges
from the given
In case of parallel edges, keep
the one which has the least cost
associated and remove all
others.
Step 2 - Arrange all edges in their increasing order of
weight
The next step is to create a set of edges and weight, and
arrange them in an ascending order of weightage (cost).
Step 3 - Add the edge which has the least
weightage
Now we start adding edges to the graph beginning
from the one which has the least weight.
Throughout, we shall keep checking that the
spanning properties remain intact. In case, by
adding one edge, the spanning tree property does
not hold then we shall consider not to include the
edge in the graph.
The least cost is 2 and edges involved are B,D
and D,T. We add them. Adding them does not
violate spanning tree properties, so we
continue to our next edge selection.
Next cost is 3, and associated
edges are A,C and C,D. We add
them again −
Next cost in the table is 4, and we observe that
adding it will create a circuit in the graph. −
the process we shall ignore/avoid all
edges that create We ignore it. In a
circuit.
We observe that edges with cost 5
and 6 also create circuits. We ignore
them and move on.
Now we are left with only one node to be
added. Between the two least cost edges
available 7 and 8, we shall add the edge
with cost 7.
PRIM’S ALGORITHM KRUSKAL’S ALGORITHM
It starts to build the Minimum
Spanning Tree from any vertex
in the graph.
It starts to build the Minimum
Spanning Tree from the vertex
carrying minimum weight in the
graph.
It traverses one node more than
one time to get the minimum
distance.
It traverses one node only once.
Prim’s algorithm has a time
complexity of O(V2
), V being the
number of vertices and can be
improved up to O(E + log V)
using Fibonacci heaps.
Kruskal’s algorithm’s time
complexity is O(E log V), V being
the number of vertices.
Prim’s algorithm gives
connected component as well as
it works only on connected
graph.
Kruskal’s algorithm can
generate forest(disconnected
components) at any instant as
well as it can work on
disconnected components
Prim’s algorithm runs faster in
dense graphs.
Kruskal’s algorithm runs faster
in sparse graphs.
B e l l m a n – F o r d
a l g o r i t h m
Negative edge weights are found in various applications of graphs, hence the
usefulness of this algorithm. If a graph contains a "negative cycle" ( a cycle whose
edges sum to a negative value) that is reachable from the source, then there is no
cheapest path: any path that has a point on the negative cycle can be made
cheaper by one more walk around the negative cycle. In such a case, the Bellman–
Ford algorithm can detect negative cycles and report their existence.

Shortest path network
Driected graph
Source s, Destination t
Cost(V-U) cost of using edge from v to u
Shortest path problem
Find shortest directed path from s to t
Cost of path = sum of arc cost in path
δ (S , Vi-1) + W(Vi-1,Vi)
S h o r t e s t p a t h p r o b l e m
B e l l m a n – F o r d E X a m p l e
A
B D
E
C
Vertices A B C D E
Cost 0 α α α α
Prevertices ─ ─ ─ ─ ─
1
5
2
-3
-2
2
1
2
0 α
α α
α
1 s t
:
B e l l m a n – F o r d
E X a m p l e
Vertices A B C D E
Cost 0 2 5 0 2
Pre ─ C A B D
A
B D
E
C
1
5
2
-3
-2
2
1
2
0 5
2 0
2
Shortest path are –
E = ACBDE = {5+(-3)+(-2)+2 }= 2
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph
theory.
Works on both directed and undirected graphs. However, all edges must have
nonnegative weights.
Approach: Greedy
Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge weights are
nonnegative
Output: Lengths of shortest paths (or the shortest paths themselves) from a given
source vertex v∈V to all other vertices
Dijkstra Animated Example-1
THANK YOU

uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx

  • 1.
  • 2.
    Prim's algorithm isa minimum spanning tree algorithm that takes a graph as input and finds the subset of the edges of that graph which •form a tree that includes every vertex •has the minimum sum of weights among all the trees that can be formed from the graph Prim's Algorithm
  • 3.
    Prim's Spanning Tree Algorithm Prim'salgorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses the greedy approach. Prim's algorithm shares a similarity with the shortest path first algorithms. Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on adding new nodes to the spanning tree from the given graph.
  • 4.
  • 5.
    Step 1 -Remove all loops and parallel edges
  • 6.
    Remove all loopsand parallel edges from the given graph. In case of parallel edges, keep the one which has the least cost associated and remove all others.
  • 7.
    Step 2 -Choose any arbitrary node as root node In this case, we choose S node as the root node of Prim's spanning tree. This node is arbitrarily chosen, so any node can be the root node. One may wonder why any video can be a root node. So the answer is, in the spanning tree all the nodes of a graph are included and because it is connected then there must be at least one edge, which will join it to the rest of the tree
  • 8.
    Step 3 -Check outgoing edges and select the one with less cost After choosing the root node S, we see that S,A and S,C are two edges with weight 7 and 8, respectively. We choose the edge S,A as it is lesser than the other.
  • 9.
    Now, the treeS-7-A is treated as one node and we check for all edges going out from it. We select the one which has the lowest cost and include it in the tree.
  • 10.
    After this step,S-7-A-3-C tree is formed. Now we'll again treat it as a node and will check all the edges again. However, we will choose only the least cost edge. In this case, C-3-D is the new edge, which is less than other edges' cost 8, 6, 4, etc.
  • 11.
    After adding nodeD to the spanning tree, we now have two edges going out of it having the same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the next step will again yield edge 2 as the least cost. Hence, we are showing a spanning tree with both edges included.
  • 12.
    Kruskal's Spanning Tree Algorithm Kruskal'salgorithm to find the minimum cost spanning tree uses the greedy approach. This algorithm treats the graph as a forest and every node it has as an individual tree. A tree connects to another only and only if, it has the least cost among all available options and does not violate MST properties.
  • 13.
    How Kruskal's algorithmworks It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding a global optimum. We start from the edges with the lowest weight and keep adding edges until we reach our goal. The steps for implementing Kruskal's algorithm are as follows: 1. Sort all the edges from low weight to high Take the edge with the lowest weight and add it to the spanning tree. 2.If adding the edge created a cycle, then reject this edge. 3.Keep adding edges until we reach all vertices.
  • 14.
  • 15.
    Step 1 -Remove all loops graph. and Parallel Edges Remove all loops and parallel edges from the given
  • 16.
    In case ofparallel edges, keep the one which has the least cost associated and remove all others.
  • 17.
    Step 2 -Arrange all edges in their increasing order of weight The next step is to create a set of edges and weight, and arrange them in an ascending order of weightage (cost).
  • 18.
    Step 3 -Add the edge which has the least weightage Now we start adding edges to the graph beginning from the one which has the least weight. Throughout, we shall keep checking that the spanning properties remain intact. In case, by adding one edge, the spanning tree property does not hold then we shall consider not to include the edge in the graph.
  • 19.
    The least costis 2 and edges involved are B,D and D,T. We add them. Adding them does not violate spanning tree properties, so we continue to our next edge selection.
  • 20.
    Next cost is3, and associated edges are A,C and C,D. We add them again −
  • 21.
    Next cost inthe table is 4, and we observe that adding it will create a circuit in the graph. −
  • 22.
    the process weshall ignore/avoid all edges that create We ignore it. In a circuit.
  • 23.
    We observe thatedges with cost 5 and 6 also create circuits. We ignore them and move on.
  • 24.
    Now we areleft with only one node to be added. Between the two least cost edges available 7 and 8, we shall add the edge with cost 7.
  • 25.
    PRIM’S ALGORITHM KRUSKAL’SALGORITHM It starts to build the Minimum Spanning Tree from any vertex in the graph. It starts to build the Minimum Spanning Tree from the vertex carrying minimum weight in the graph. It traverses one node more than one time to get the minimum distance. It traverses one node only once. Prim’s algorithm has a time complexity of O(V2 ), V being the number of vertices and can be improved up to O(E + log V) using Fibonacci heaps. Kruskal’s algorithm’s time complexity is O(E log V), V being the number of vertices. Prim’s algorithm gives connected component as well as it works only on connected graph. Kruskal’s algorithm can generate forest(disconnected components) at any instant as well as it can work on disconnected components Prim’s algorithm runs faster in dense graphs. Kruskal’s algorithm runs faster in sparse graphs.
  • 26.
    B e ll m a n – F o r d a l g o r i t h m Negative edge weights are found in various applications of graphs, hence the usefulness of this algorithm. If a graph contains a "negative cycle" ( a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path that has a point on the negative cycle can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman– Ford algorithm can detect negative cycles and report their existence.
  • 27.
     Shortest path network Driectedgraph Source s, Destination t Cost(V-U) cost of using edge from v to u Shortest path problem Find shortest directed path from s to t Cost of path = sum of arc cost in path δ (S , Vi-1) + W(Vi-1,Vi) S h o r t e s t p a t h p r o b l e m
  • 28.
    B e ll m a n – F o r d E X a m p l e A B D E C Vertices A B C D E Cost 0 α α α α Prevertices ─ ─ ─ ─ ─ 1 5 2 -3 -2 2 1 2 0 α α α α 1 s t :
  • 29.
    B e ll m a n – F o r d E X a m p l e Vertices A B C D E Cost 0 2 5 0 2 Pre ─ C A B D A B D E C 1 5 2 -3 -2 2 1 2 0 5 2 0 2 Shortest path are – E = ACBDE = {5+(-3)+(-2)+2 }= 2
  • 30.
    Dijkstra's algorithm Dijkstra's algorithm- is a solution to the single-source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Approach: Greedy Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v∈V to all other vertices
  • 31.
  • 35.