0% found this document useful (0 votes)
61 views20 pages

Ada MTE Presentation

The document discusses Kruskal's and Prim's algorithms for finding minimum spanning trees. Kruskal's algorithm sorts the edges by weight and builds the spanning tree by adding edges without creating cycles. Prim's algorithm builds up the minimum spanning tree by iteratively adding the lowest-weight edge that connects an unvisited vertex to the partially-built tree. Both algorithms run in O(E log E) time, where E is the number of edges. Minimum spanning trees have applications in network design, electrical wiring layout, and more.

Uploaded by

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

Ada MTE Presentation

The document discusses Kruskal's and Prim's algorithms for finding minimum spanning trees. Kruskal's algorithm sorts the edges by weight and builds the spanning tree by adding edges without creating cycles. Prim's algorithm builds up the minimum spanning tree by iteratively adding the lowest-weight edge that connects an unvisited vertex to the partially-built tree. Both algorithms run in O(E log E) time, where E is the number of edges. Minimum spanning trees have applications in network design, electrical wiring layout, and more.

Uploaded by

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

Greedy’s Approach:

Krushkal’s and prim’s algorithm for


Minimum spanning tree
ADA SE-208 MTE PRESENTATION

Submitted By:
Submitted to:
ANUJ (2K20/SE/21) ANUJ BORICHA (2K20/SE/22)
Ms. JYOTI BUDHWAR ANUPAMA(2K20/SE/23) ANURAAG KUMAR(2K20/SE/24)
Greedy Algorithm
➢ It is an algorithmic paradigm that builds up a solution piece by piece, always
choosing the next piece that offers the most obvious and immediate benefit.

➢ For example consider the Fractional Knapsack Problem. The local optimal
strategy is to choose the item that has maximum value vs weight ratio. This
strategy also leads to global optimal solution because we allowed to take
fractions of an item.
The algorithm never reverses the earlier decision even if the choice is wrong. It works in a top-down
approach.

This algorithm may not produce the best result for all the problems. It's
because it always goes for the local best choice to produce the global best
result.
However, we can determine if the algorithm can be used with any problem if
the problem has the following properties:

1. Greedy Choice Property

If an optimal solution to the problem can be found by choosing the best choice at
each step without reconsidering the previous steps once chosen, the problem can
be solved using a greedy approach. This property is called greedy choice property.

2. Optimal Substructure

If the optimal overall solution to the problem corresponds to the optimal solution to
its subproblems, then the problem can be solved using a greedy approach. This
property is called optimal substructure.
Advantages of Greedy Approach
● The algorithm is easier to describe.
● This algorithm can perform better than other algorithms (but, not in all cases).

Drawback of Greedy Approach


As mentioned earlier, the greedy algorithm doesn't always produce the optimal solution. This is
the major disadvantage of the algorithm

For example, suppose we want to find the longest path in the graph below from root to leaf.
Let's use the greedy algorithm here.
1. Let's start with the root node 20. The weight of the right child is 3 and the weight of the left
child is 2.

2. Our problem is to find the largest path. And, the optimal solution at the moment is 3. So,
the greedy algorithm will choose 3.

3. Finally the weight of an only child of 3 is 1.


This gives us our final result 20 + 3 + 1 = 24.

However, it is not the optimal solution. There is another path that carries more weight (20 + 2
+ 10 = 32) as shown in the image below.
Spanning Tree
A connected subgraph ‘S’ of Graph G(V,E) is said to be a spanning tree of
graph G if and only if:

● All vertices of G must be present in S.


● No. of edges in S should be V-1.
● A spanning tree does not have any cycles or loop.

Connected subgraph S ( A
Graph G
spanning tree of graph G)
Some Possible spanning Tree from previous graph ‘G’
Properties of spanning tree
Following are a few properties of the spanning tree connected to graph G −

★ A connected graph G can have more than one spanning tree.

★ All possible spanning trees of graph G, have the same number of


edges and vertices.

★ The spanning tree does not have any cycle (loops).

★ Removing one edge from the spanning tree will make the graph
disconnected, i.e. the spanning tree is minimally connected.

★ Adding one edge to the spanning tree will create a circuit or loop, i.e.
the spanning tree is maximally acyclic.

★ A spanning tree has n-1 edges, where 'n' is the number of nodes.
No. Of spanning Trees For complete graphs
The total number of spanning trees with n vertices that can be created
from a complete graph is equal to n^n-2.

In this example, the no. of


vertices=3, hence 3^3-2
i.e. 3 spanning trees are
possible.
Minimum spanning tree
★ A minimum spanning tree can be defined as the spanning tree in
which the sum of the weights of the edge is minimum.
★ The weight of the spanning tree is the sum of the weights given to
the edges of the spanning tree.

Let’s take an example of graph

The sum of the edges of the graph on right side is 16. Now,
some of the possible spanning trees created from the above
graph are -
So, the minimum spanning tree that is
selected from the above spanning
trees for the previously given weighted
graph is -

Spanning Tree Applications


➢ Computer Network Routing Protocol
➢ Cluster Analysis
➢ Civil Network Planning

Minimum Spanning tree Applications


➢ To find paths in the map.
➢ To design networks like telecommunication networks,
water supply networks, and electrical grids.
KRuskal’s algorithm
★ It is used to find the minimum spanning tree for a connected
weighted graph.
★ The main target of the algorithm is to find the subset of edges by
using which we can traverse every vertex of the graph.
★ It follows the greedy approach that finds an optimum solution at
every stage instead of focusing on a global optimum.

STEPS FOR ALGORITHM:

1. Sort all the edges from low weight


to high.
2. Take the edge with the lowest
weight and add it to the spanning
tree. If adding the edge created a
cycle, then reject this edge.
3. Keep adding edges until we reach
all vertices.
PSEUDOCODE FOR Kruskal's Algorithm
1. Any minimum spanning tree algorithm
revolves around checking if adding an
edge creates a loop or not.

2. The most common way to find this out


is an algorithm called Union Find.

3. This algorithm divides the vertices


into clusters and allows us to check if
two vertices belong to the same
cluster or not as shown in given figure
and hence decide whether adding an ★ The time complexity in the
edge creates a cycle. above image of Kruskal's
Algorithm is: O(E log E).

Kruskal's Algorithm Applications :


● In order to layout electrical wiring
● In computer network (LAN connection)
Prim’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

The STEPS to implement the prim’s algorithm are as follows -

1. First we have initialize an MST with the randomly chosen


vertex.
2. Now, we have to find all the edges that connect the tree in
the above step with the new vertices. From the edges
found, select the minimum edge and add it to the tree.
3. Repeat step 2 until the minimum spanning tree is formed.
Step 1 St
ep
2

p3
Ste

p 4
Ste
Step 5

So, the graph produced in step 5 is the minimum spanning tree of the
previous show graph. The cost of the MST is -
Cost of MST = 4 + 2 + 1 + 3 = 10 units.

Prim’s Algorithm Complexity - O( E log V) where E is number of edges


V is number of vertices

Prim’s Algorithm Application are -


● In network designed
● To make protocol in network cycles
● Laying cables of electrical wiring
Pseudocode for prim’s Algorithm

1. The pseudocode for prim's


algorithm shows how we create
two sets of vertices U and
V-U.

2. U contains the list of vertices


that have been visited and V-U
the list of vertices that
haven't.

3. One by one, we move vertices The time complexity of above image of


from set V-U to set U by Prim's algorithm is O(E log V).
connecting the least weight
edge.
Kruskal’s Vs Prim’s Algorithm

Kruskal Algorithm is easier to implement whereas


Prim's Algorithm is harder to implement.
ThANK YOU.
Have A Nice Day!

You might also like