0% found this document useful (0 votes)
5 views16 pages

Algorithm 8

A spanning tree of an undirected and connected graph includes all vertices and is acyclic. The minimum spanning tree is the one with the lowest cost among all spanning trees, which can be found using Prim's or Kruskal's algorithms. Prim's algorithm grows the tree from a starting vertex, while Kruskal's algorithm adds edges based on their weights, ensuring no cycles are formed.

Uploaded by

angelina54320291
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Algorithm 8

A spanning tree of an undirected and connected graph includes all vertices and is acyclic. The minimum spanning tree is the one with the lowest cost among all spanning trees, which can be found using Prim's or Kruskal's algorithms. Prim's algorithm grows the tree from a starting vertex, while Kruskal's algorithm adds edges based on their weights, ensuring no cycles are formed.

Uploaded by

angelina54320291
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Algorithm Design & Analysis

CSE 2103
Lecture 8
What is a Spanning Tree?
2

Given an undirected and connected graph


G=(V,E), a spanning tree of the graph G is a
tree that spans G (that is, it includes every
vertex of G ) and is a sub graph of G (every
edge in the tree belongs to G )
. A spanning tree of a graph is a tree that:
 Contains all the original graph’s vertices.
 Reaches out to (spans) all vertices.
 Is acyclic. In other words, the graph doesn’t have any
nodes which loop back to itself
What is a Minimum Spanning
Tree?
3

The cost of the spanning tree is the sum of


the weights of all the edges in the tree. There
can be many spanning trees.
Minimum spanning tree is the spanning tree
where the cost is minimum among all the
spanning trees.
Example
4
Example
5
Algorithms for finding the Minimum
Spanning6
Tree
There are two famous algorithms for finding
the Minimum Spanning Tree:
 Prim’s Algorithm
 Kruskal’s Algorithm
Prim’s Algorithm
7

Prim’s Algorithm also use Greedy approach to find


the minimum spanning tree. In Prim’s Algorithm
we grow the spanning tree from a starting position.
Algorithm Steps:
 Maintain two disjoint sets of vertices. One containing vertices
that are in the growing spanning tree and other that are not in the
growing spanning tree.
 Select the cheapest vertex that is connected to the growing
spanning tree and is not in the growing spanning tree and add it
into the growing spanning tree.
 Check for cycles. To do that, mark the nodes which have been
already selected and insert only those nodes in the Priority Queue
that are not marked.
Prim’s Algorithm Example
8
Prim’s Algorithm Example
9

 In Prim’s Algorithm, we will start with an arbitrary node (it


doesn’t matter which one) and mark it. In each iteration we
will mark a new vertex that is adjacent to the one that we
have already marked.
 As a greedy algorithm, Prim’s algorithm will select the
cheapest edge and mark the vertex. So we will simply choose
the edge with weight 1.
 In the next iteration we have three options, edges with
weight 2, 3 and 4. So, we will select the edge with weight 2
and mark the vertex.
 Now again we have three options, edges with weight 3, 4 and
5. But we can’t choose edge with weight 3 as it is creating a
cycle. So we will select the edge with weight 4 and we end up
with the minimum spanning tree of total cost 7 ( = 1 + 2 +4).
Kruskal’s Algorithm
10

Kruskal’s Algorithm builds the spanning tree


by adding edges one by one into a growing
spanning tree. Kruskal's algorithm follows
greedy approach as in each iteration it finds
an edge which has least weight and add it to
the growing spanning tree.
Algorithm Steps:
 Sort the graph edges with respect to their weights.
 Start adding edges to the MST from the edge with the
smallest weight until the edge of the largest weight.
 Only add edges which doesn't form a cycle , edges which
connect only disconnected components.
Kruskal’s Algorithm Example
11
Kruskal’s Algorithm Example
12

 In Kruskal’s algorithm, at each iteration we will select the


edge with the lowest weight. So, we will start with the lowest
weighted edge first i.e., the edges with weight 1.
 After that we will select the second lowest weighted edge i.e.,
edge with weight 2. Notice these two edges are totally disjoint.
 Now, the next edge will be the third lowest weighted edge i.e.,
edge with weight 3, which connects the two disjoint pieces of
the graph.
 Now, we are not allowed to pick the edge with weight 4, that
will create a cycle and we can’t have any cycles. So we will
select the fifth lowest weighted edge i.e., edge with weight 5.
 Now the other two edges will create cycles so we will ignore
them. In the end, we end up with a minimum spanning tree
with total cost 11 ( = 1 + 2 + 3 + 5).
Exercise
13
Prim’s Algorithm
14
Kruskal’s Algorithm
15
16

THANK YOU

You might also like