3.5 Prims and Kruskals Algorithms - Greedy Method

Abdul Bari
8 Feb 201820:12

Summary

TLDRThis script explains the concept of a minimum cost spanning tree (MCST) in graph theory. It defines a spanning tree and illustrates how to calculate the number of possible spanning trees in a graph. The focus then shifts to finding the MCST using greedy algorithms like Prim's and Kruskal's, which efficiently select edges to minimize the total cost without forming cycles. The script also addresses scenarios with missing edge weights and non-connected graphs, emphasizing the importance of connected components in spanning tree formation.

Takeaways

  • ๐ŸŒณ A spanning tree is a subgraph of a connected graph that includes all the vertices and exactly (n-1) edges where n is the number of vertices.
  • ๐Ÿ”„ The number of different spanning trees possible for a graph can be calculated using combinations, specifically 'n choose (n-1)' where n is the number of edges.
  • ๐Ÿ”ข The formula to calculate the number of different spanning trees is (number of edges) choose (number of vertices - 1) minus the number of cycles.
  • ๐Ÿ—๏ธ Prim's algorithm is a greedy method to find the minimum cost spanning tree by initially selecting the smallest edge and then always choosing the smallest edge connected to the already selected vertices.
  • ๐Ÿ› ๏ธ Kruskal's algorithm is another greedy method that selects the smallest edge and continues to do so without forming cycles until (n-1) edges are chosen.
  • โฑ๏ธ Kruskal's algorithm has a time complexity of O(n^2), but this can be improved to O(n log n) using a min-heap data structure.
  • ๐Ÿ”„ Both Prim's and Kruskal's algorithms can be applied to non-connected graphs, but they will only find spanning trees for the individual connected components.
  • ๐Ÿ’ก The minimum cost of a missing edge in a graph can be deduced by understanding the cycle it would form if included in a spanning tree.
  • ๐Ÿ“‰ In a weighted graph, different spanning trees can have varying costs, and the goal is to find the one with the minimum cost.
  • ๐Ÿšซ A graph must be connected to have a spanning tree; disconnected graphs do not support the concept of a single spanning tree.

Q & A

  • What is a spanning tree?

    -A spanning tree is a subgraph of a graph that includes all the vertices of the graph but only a subset of the edges, specifically the number of edges is equal to the number of vertices minus one, and it contains no cycles.

  • How many different spanning trees can be formed from a graph with six vertices and six edges?

    -The number of different spanning trees that can be formed is given by the combination formula \( \binom{6}{5} \), which equals 6.

  • What is the formula to calculate the number of different spanning trees possible from a given graph?

    -The formula to calculate the number of different spanning trees is to take the number of edges available, subtract the number of cycles formed by those edges, and then subtract one to account for the number of vertices.

  • How does the cost of a spanning tree in a weighted graph differ from one spanning tree to another?

    -The cost of a spanning tree in a weighted graph can vary because different sets of edges can be chosen to form the tree, each with a different total weight.

  • What is the minimum cost spanning tree?

    -The minimum cost spanning tree is the spanning tree that has the lowest possible total edge weight among all possible spanning trees for a given weighted graph.

  • What are the two greedy algorithms for finding the minimum cost spanning tree?

    -The two greedy algorithms for finding the minimum cost spanning tree are Prim's algorithm and Kruskal's algorithm.

  • How does Prim's algorithm work to find the minimum cost spanning tree?

    -Prim's algorithm starts with an arbitrary vertex and grows the minimum cost spanning tree by always adding the smallest edge that connects a new vertex to the tree.

  • How does Kruskal's algorithm work to find the minimum cost spanning tree?

    -Kruskal's algorithm sorts all the edges in the graph by weight and adds them to the minimum cost spanning tree in ascending order, ensuring no cycles are formed.

  • What is the time complexity of Kruskal's algorithm without using a data structure like a min-heap?

    -The time complexity of Kruskal's algorithm without using a min-heap is \( \Theta(n^2) \), where n is the number of vertices.

  • How can Kruskal's algorithm be optimized using a min-heap?

    -Using a min-heap, Kruskal's algorithm can achieve a time complexity of \( \Theta(n \log n) \) by efficiently finding the next minimum edge to add to the tree.

  • Can Prim's or Kruskal's algorithm find a minimum cost spanning tree for a non-connected graph?

    -No, Prim's or Kruskal's algorithm cannot find a minimum cost spanning tree for a non-connected graph because a spanning tree requires all vertices to be connected.

  • What is the minimum possible weight for an edge that is not included in the minimum cost spanning tree?

    -The minimum possible weight for an edge not included in the minimum cost spanning tree is the weight that would have been chosen if it were available before the edge that actually formed a cycle.

Outlines

00:00

๐ŸŒณ Understanding Spanning Trees

The paragraph introduces the concept of a minimum cost spanning tree (MCST) and explains what a spanning tree is. A spanning tree is a subgraph of a larger graph that includes all the vertices but only a subset of the edges, specifically 'n-1' edges where 'n' is the number of vertices. The paragraph uses an example graph to illustrate this, showing how to select edges to form a spanning tree without creating any cycles. It also discusses the number of possible spanning trees that can be formed from a given graph, which is calculated by choosing 'n-1' edges from the total number of edges available.

05:05

๐Ÿ” Prim's Algorithm for Minimum Cost Spanning Trees

This section delves into Prim's algorithm, a greedy method for finding the minimum cost spanning tree of a connected, weighted graph. The algorithm starts with an arbitrary vertex and iteratively adds the cheapest edge that connects a non-selected vertex to the growing tree. The paragraph explains the step-by-step process of Prim's algorithm using an example graph, highlighting how to avoid cycles while selecting edges. It concludes with a discussion of the algorithm's time complexity and how it cannot be applied to non-connected graphs.

10:09

๐ŸŒ Kruskal's Algorithm for Finding Minimum Cost Spanning Trees

The paragraph describes Kruskal's algorithm, another greedy method for finding the minimum cost spanning tree. Unlike Prim's algorithm, which starts from a vertex and grows the tree, Kruskal's algorithm begins with the edges, sorting them by weight and adding them to the tree as long as they do not form a cycle. The paragraph explains how Kruskal's algorithm can be optimized using a min-heap data structure to improve its efficiency. It also addresses how Kruskal's algorithm can be applied to non-connected graphs to find spanning trees for each component.

15:10

๐Ÿ”„ Handling Edges with Missing Weights in Spanning Trees

This section discusses a scenario where a graph has edges with missing weights and how to determine the minimum possible values for these edges to form a valid spanning tree. The paragraph explains that if certain edges are not included in the spanning tree because they form cycles, their weights must be greater than the smallest available edge that could be included. It uses an example to illustrate how to find the minimum cost spanning tree even with missing edge weights and emphasizes that there can only be one optimal solution for the minimum cost, despite the possible variation in the set of input edges.

Mindmap

Keywords

๐Ÿ’กSpanning Tree

A spanning tree is a subgraph of a connected graph that includes all the vertices of the graph and the minimum possible number of edges, specifically, one less than the number of vertices. It is a tree in the sense that it is acyclic. In the video, the concept is introduced by explaining that a spanning tree must include all vertices and edges that connect them without forming any cycles. The script uses examples to illustrate how to select edges to form a spanning tree from a given graph.

๐Ÿ’กGraph

A graph is a mathematical structure consisting of a set of vertices (also called nodes or points) and a set of edges connecting these vertices. The script represents a graph as G(V, E), where V is the set of vertices and E is the set of edges. The graph is fundamental to the discussion of spanning trees and is used throughout the script to demonstrate various concepts.

๐Ÿ’กVertices

Vertices are the fundamental units of a graph, often represented by points or nodes. In the context of the video, vertices are the elements that must be included in a spanning tree. The script mentions vertices when explaining the structure of a graph and when discussing how to form a spanning tree.

๐Ÿ’กEdges

Edges are the connections between vertices in a graph. They represent the relationships or paths between different points. The script discusses edges in the context of forming a spanning tree, emphasizing that a spanning tree must include the minimum number of edges required to connect all vertices without cycles.

๐Ÿ’กCycle

A cycle in graph theory is a sequence of edges and vertices that starts and ends at the same vertex, passing through other vertices along the way. The video script explains that a spanning tree cannot contain a cycle, as this would violate the definition of a tree structure.

๐Ÿ’กWeighted Graph

A weighted graph is a graph where edges have an associated weight or cost. The script introduces the concept of weighted graphs to discuss how to find the minimum cost spanning tree, which is a spanning tree with the lowest possible total edge weight.

๐Ÿ’กMinimum Cost Spanning Tree

The minimum cost spanning tree is a spanning tree that has the minimum possible total edge weight. The script focuses on algorithms to find this tree in a weighted graph, which is a key concept in network design and optimization.

๐Ÿ’กPrim's Algorithm

Prim's Algorithm is a greedy algorithm used to find the minimum cost spanning tree of a connected, weighted graph. The script explains how Prim's Algorithm works by starting with an arbitrary vertex and iteratively adding the smallest edge that connects a new vertex to the existing tree.

๐Ÿ’กKruskal's Algorithm

Kruskal's Algorithm is another greedy algorithm for finding the minimum cost spanning tree. The script describes how Kruskal's Algorithm works by sorting all edges by weight and adding them to the tree as long as they do not form a cycle.

๐Ÿ’กGreedy Method

A greedy method is an approach to problem-solving that makes the locally optimal choice at each step with the hope that these local choices will lead to a global optimum. The video script mentions greedy methods in the context of Prim's and Kruskal's algorithms, which are examples of greedy algorithms used to find minimum cost spanning trees.

๐Ÿ’กMin Heap

A min heap is a binary tree-based data structure that satisfies the heap property: the key present at each node is either less than or equal to the keys of its children. The script mentions min heaps in relation to optimizing Kruskal's Algorithm, as they can efficiently retrieve the minimum edge when selecting edges for the spanning tree.

Highlights

Definition of a spanning tree in a graph

Explanation of how to select edges for a spanning tree

The importance of avoiding cycles in a spanning tree

The formula for calculating the number of possible spanning trees

The concept of a weighted graph and its impact on spanning trees

The cost of a spanning tree and how it is calculated

Introduction to Prim's algorithm for finding the minimum cost spanning tree

Step-by-step explanation of Prim's algorithm

The limitation of Prim's algorithm for non-connected graphs

Introduction to Kruskal's algorithm as an alternative method

The greedy approach of Kruskal's algorithm in selecting edges

The impact of using a Min Heap to improve Kruskal's algorithm

The time complexity analysis of Kruskal's algorithm

The application of Kruskal's algorithm on non-connected graphs

How to determine the minimum possible value of missing edges in a graph

The uniqueness of the minimum cost in spanning tree problems

The possibility of multiple spanning trees with the same minimum cost

Transcripts

play00:00

the topic is minimum cause spanning tree

play00:03

so for that first of all we'll

play00:05

understand what does it mean by a

play00:06

spanning

play00:08

tree here I have an example

play00:11

graph graph is represented as G and v e

play00:18

v is the set of

play00:20

vertices 1 2 3 4 5 6 and E is the set of

play00:27

edges the set of edges are from 1 to

play00:31

2 and 2 to

play00:34

3 3 to 4 and so

play00:38

on graphs are represented as set of

play00:41

vertices and edges now what is this

play00:43

spanning tree spanning tree is a

play00:46

subgraph of a graph me I should take the

play00:49

subset of this so subset of what what is

play00:53

this and it just both subset or only one

play00:56

subset so I should take the subset of

play00:58

edges vertices must be as it is so I

play01:02

should take all vertices

play01:05

six spanning Tre means I should take all

play01:09

vertices then how many edges how many

play01:12

vertices I have number of vertices are n

play01:16

that is six so I should take number of

play01:18

vertices minus one that is only five

play01:23

edges SP Tre is a subgraph of a graph

play01:27

having all vertices but only nus one

play01:31

edges so out of these edges I should

play01:35

take only five so 1 2 3 4

play01:40

5 I did not take this one so this is

play01:43

spinning

play01:45

tree see a tree will not have a cycle so

play01:49

there is no cycle here yes there's no

play01:52

cycle here if I this you can say it's a

play01:55

cycle if you start from one vertex you

play01:56

can again reach back to that vertex but

play01:59

here

play02:00

there is no

play02:01

cycle is it possible that I will take

play02:04

this one but I will not take this yes

play02:07

there's also an spanning

play02:09

tree I will take this but I will not

play02:12

take this yes this is also an spanning

play02:15

tree so which Edge to select that is a

play02:18

choice that is a choice for a spanning

play02:21

tree so I can say that s is a subgraph

play02:26

of a graph where s is defined as set of

play02:30

of vertices and set of edges where set

play02:33

of vertices are same as the vertices of

play02:35

a

play02:37

graph and edges are number of vertices

play02:41

minus one so the number of edges are

play02:44

number of vertices minus one this is how

play02:46

formally we can divide in mathematics we

play02:49

can Define like this now next thing for

play02:51

a given graph how many different

play02:53

spanning trees can be generated I want

play02:56

to know that how many different spanning

play02:58

trees are possible so see number of

play03:01

edges are how many six 1 2 3 4 5 6 I

play03:07

have and a spanning tree I should take

play03:08

only

play03:09

five I should take only five so 6

play03:15

C5 out of six edges out of six edges I

play03:20

have to select five how many ways I can

play03:22

do that so 65 C ways so F 65 C is how

play03:28

much six only so I can have six

play03:32

different spanning trees just now you

play03:35

can see it's a simple one that I have

play03:37

skip this Edge like this I can leave one

play03:40

one Edge right and I can form a spanning

play03:42

tree right I can take all except this

play03:45

take all except this take all except

play03:46

this six are possible suppose if I have

play03:50

one more Edge now now I have seven edges

play03:54

and I want to select five now how many

play03:57

spaning trees are possible see I can

play04:00

have this spanning tree

play04:02

also or I can have this spanning tree

play04:06

also no possibilities are more now this

play04:09

is 7

play04:11

C5 7 C5 but this Edge will form cycle so

play04:17

how many cycles it is forming this is

play04:20

one cycle and this is another cycle so

play04:23

this Edge is forming two cycles of

play04:26

number of vertices less than six so if

play04:30

it is forming number of vertices less

play04:32

than 6 then you will

play04:34

be 7 C5 -

play04:38

2 so that's it so what's the formula to

play04:41

know how many different spanning trees

play04:43

are possible from a given graph so

play04:47

whatever the number of edges you have

play04:49

you should take all those and see of how

play04:52

many edges you need you need five so

play04:55

five is what number of vertices minus

play04:57

one so this is number of vertices minus

play04:59

1 and minus number of

play05:05

Cycles you have to subtract number of

play05:08

Cycles I have a graph where the edges

play05:12

are having weights so this is a weighted

play05:15

graph this a weighted

play05:18

graph I want to know what are the

play05:20

possible spanning trees possible so I'll

play05:22

draw a few spanning trees let us draw

play05:25

one spanning tree this is vertex 1 and 2

play05:28

and 3 and four so four vertices are

play05:32

there then how many edges I should take

play05:34

4 - 1 only three so I'll select this one

play05:37

1 2 3 so what is the weight of this one

play05:40

5 3 and six so what is the cost of this

play05:43

spanning tree cost of this spanning tree

play05:46

is

play05:47

14 I'll take one more spanning tree 1 2

play05:52

3 4 and I'll take these edges let us see

play05:56

what is the cost of this 4 63 then what

play06:00

is the cost of this one

play06:04

13 I'll take one more spanning Tre 1 2 3

play06:10

4 and I'll take these edges this one and

play06:14

this one and this one 4 2 and 3 so this

play06:18

is cost is how much

play06:23

9 now from this example some observation

play06:28

we can understand something if you have

play06:31

a weighted graph and you get different

play06:34

spanning trees the cost of the spanning

play06:36

trees may be varying you can get a

play06:39

different cost of spanning

play06:42

trees now the problem is can I find out

play06:47

the minimum cost of spanning tree can I

play06:50

find out the minimum one which gives me

play06:53

minimum yes you can find out how you can

play06:57

try all possible spanning trees

play07:00

and then from that you can check

play07:01

whichever is minimum you can pick up

play07:03

that answer this is one

play07:06

method trying all possible spanning

play07:08

trees will be too lengthy is there any

play07:11

greedy

play07:12

method yes the greedy methods are

play07:15

available for finding the minimum cause

play07:18

spanning tree if a weighted graph is

play07:20

given you can find out the minimum cause

play07:22

spanning Tree by following the method

play07:25

without finding out all possible span

play07:28

entries so what are those those methods

play07:30

one method

play07:32

is prims algorithm and the second method

play07:36

is cuscal

play07:41

algorithm there are two algorithms for

play07:43

finding minimum costes pan inry I have a

play07:46

weighted graph here and I have to find a

play07:49

minimum cost of spanning tree by

play07:51

following prim's algorithm let's see how

play07:53

Prim algorithm Works through an example

play07:56

now as per prims algorithm he says that

play07:59

from a graph first of all you select a

play08:02

minimum cause Edge so the smallest Edge

play08:05

in the entire graph is this

play08:08

one

play08:10

then this is the initial one now for the

play08:13

rest of the procedure always select a

play08:16

minimum cost Edge from the graph but

play08:19

make sure that it should be connected to

play08:22

already selected

play08:25

vertices now the next minimum is which

play08:27

one 3 to 4

play08:30

don't take that it will not be connected

play08:31

to one or six so take only those which

play08:34

are connected to one or six means what

play08:37

he is trying to do is always maintain a

play08:42

tree primce thinks that if some farther

play08:46

away Edge is selected then it may not be

play08:49

forming a tree

play08:50

finally so that is the reason he

play08:53

suggested that always select a minimum

play08:55

Edge which should be connected so let us

play08:58

see what's the next one so for this

play09:00

connected one are this one and this one

play09:02

so out of this 25 and 28 25 is minimum

play09:05

so select this

play09:08

25 then this 28 and five connected to

play09:13

five are 22 and 24 so out of this 28 24

play09:16

and 22 so from 1 and 6 and 5 I'm

play09:19

checking so already six is over so 1 and

play09:21

five so out of those this is smallest

play09:24

one so four is selected now from 1 six

play09:27

and 5 4 who is smallest the connected

play09:30

one so this is 24 and 18 and 12 so 12 is

play09:33

a smaller so we will take

play09:39

12 then out of all these which connected

play09:42

is the smallest so here I have 24 18 and

play09:45

16 so 16 is a smaller so take that

play09:51

16 then out of all these who is next

play09:54

connected so from 2 to 7 there is an

play09:57

edge which is connected so this is

play10:01

7 that's

play10:03

it there are seven vertices so 1 2 3 4 5

play10:08

6 six edges are selected and this how we

play10:11

got a minimum C cost spanning tree and

play10:15

and this is how we got a minimum cost

play10:17

spanning tree what is the cost of a tree

play10:20

the cost of a tree is if I add all

play10:24

these if I add all these it is 99

play10:30

so the approach of prims is initially

play10:33

select the smallest one then always

play10:35

select the connected smallest

play10:38

one all right so this is the method of

play10:41

Prim

play10:42

algorithm next if I have a graph 1

play10:47

2

play10:49

3

play10:51

4 5 6

play10:56

7 if I have a graph like this which is

play10:59

not connected there are total seven

play11:01

vertices but there are two pieces in a

play11:03

graph can Prim find out the minimum cost

play11:07

spanning tree for this one no no

play11:10

algorithm can find out because we cannot

play11:13

find a tree for this one spanning tree

play11:16

for this one in a spanning tree it must

play11:18

be connected so for non-connected graph

play11:21

we cannot find spanning trees at all but

play11:25

if I start prims on this one starting

play11:27

from vertex one if it is starting then

play11:29

it may find the spanning tree for only

play11:31

one component not the other component

play11:33

next method for finding minimum cost of

play11:36

spanning tree is Cru sculls method this

play11:38

also follows a greedy approach instead

play11:41

of trying out all possible spanning

play11:43

trees it shows a procedure by following

play11:47

which we can get a minimum cost of

play11:49

spanning tree it says that always select

play11:52

a minimum cost Edge always select a

play11:56

minimum cost Edge let us follow this one

play11:59

select smallest

play12:01

Edge 1 2 6 that is 10 this one is

play12:05

smallest and the next smallest test is 3

play12:09

to 4 so the second Edge I'm selecting is

play12:13

3 to 4 and it's cost is 12 next minimum

play12:17

is 2 to 7 select 2 to 7 and this is 14

play12:23

and the next minimum is 2 to 3 that is

play12:27

16 and after this

play12:29

the next minimum Ed is 7 to 4 but that

play12:33

is 18 if I select that it will form a

play12:37

cycle yes cral method says that always

play12:40

select a minimum cost Edge but if it is

play12:43

forming a cycle don't con include it in

play12:45

the solution discard that edge don't

play12:48

include an edge so it means always

play12:50

select such that it is not forming a

play12:52

cycle so if I include 4 to 7 it will

play12:55

form a cycle so next one you check so

play12:57

the next one is 22 so this is from 5 to

play13:00

4 or 4 to 5 so this is 22 next minimum

play13:04

is 24 so that is 7 and 5 if I include

play13:07

this also it will form a cycle don't

play13:09

include that one then the next minimum

play13:11

one is 25 that is from 5 to 6 so this is

play13:15

25 and the cost of this one is if you

play13:19

add all these weights it's

play13:23

99 so here also I got the same result

play13:26

just how I got in prim's algorithm so

play13:29

the so the prim algorithm and cral

play13:30

algorithm has given the same result on

play13:32

this particular graph now time taken by

play13:35

Cal's algorithm how much time it takes

play13:38

see it has to always select a minimum

play13:41

cost Edge from the set of edges so let

play13:44

us say there are e number of edges so

play13:46

out of these edges it will find out the

play13:48

minimum one and how many such edges it

play13:51

will be selecting it will be selecting V

play13:54

minus one that is number of vertices

play13:56

minus one like we know in a spanning

play13:58

tree if there there are seven vertices

play14:00

we need six edes for forming a spanning

play14:03

tree so that's 7 - 1 so as many vertices

play14:05

we have minus one number of edges we

play14:08

have to select so the total time will be

play14:10

Theta of V into e there e v is number of

play14:17

vertices and E is number of edges so it

play14:20

is n into e n is the number of vertices

play14:24

and E is the number of edges which can

play14:27

also be written as n square if we say

play14:30

both are n then it is n squ so the time

play14:33

taken by chal algorithm is

play14:36

nยฒ

play14:38

but cisal algorithm can be improve when

play14:42

we have to always select a minimum cost

play14:44

Edge then there is one data structure

play14:47

which will always give you minimum value

play14:50

that is min

play14:52

heap if Min Heap is used then Min Heap

play14:55

will always give you a minimum value

play14:58

whenever you delete a value you get a

play14:59

minimum value so if all these edges are

play15:02

kept in a Min he whenever you delete you

play15:05

will be getting a next minimum Edge so

play15:07

you don't have to search so from Min

play15:10

whenever you delete the time taken is

play15:12

login so how much time it will take for

play15:15

finding a minimum cost Edge log n time

play15:19

so how many times you have to do that

play15:21

that is end time number of vertices

play15:23

minus one time so n so by using Min he

play15:27

the time time complexity of crysal

play15:29

algorithm will be n log n so that's how

play15:32

crysal algorithm can be made

play15:36

faster now if there is a graph given

play15:39

that is not connected graph it is having

play15:41

two components four vertices on this

play15:43

side five vertices on that side can we

play15:45

find a spanning tree on a non-connected

play15:47

graph no spanning tree is not possible

play15:50

but what happens if I run a scal

play15:52

algorithm so csal algorithm will try to

play15:55

find out total number of edges that is 9

play15:57

- 1 so it will try to select eight edges

play16:00

and it will not be forming a spanning

play16:02

tree for entire graph but it may be

play16:04

finding the spanning tree for each

play16:07

component let us say if the first

play16:09

minimum the one it will select is this

play16:11

one then the next minimum it is having

play16:13

more than one edes let us say it selects

play16:17

next minimum and the next minimum it

play16:18

will select this next minimum it selects

play16:20

this and this is the next minimum so in

play16:22

this way it is selecting the minimum

play16:24

adjust from both the components or it

play16:27

will be selecting from all the

play16:28

components so it may be finding spanning

play16:30

trees for all the components but not for

play16:34

the entire graph so cisal algor them may

play16:37

work for non-connected graphs also but

play16:41

it may give spanning tree for those

play16:45

components let us take a problem here a

play16:48

graph is given and the weights of the

play16:50

edges are also given but here two edges

play16:53

are not having their weights if I take

play16:56

only these edges which are having a

play16:58

weight they are forming a spanning tree

play17:00

of a graph just hide these two edges it

play17:03

becomes a spanning tree now the question

play17:05

is what could be the minimum values of

play17:08

these two edges what could be the

play17:10

minimum possible value maximum it can be

play17:12

anything but at least what could be the

play17:14

value see as this is included in a

play17:18

spanning tree this is included in a

play17:20

spanning tree this is not included

play17:21

because it is forming a cycle so it is

play17:24

forming a cycle if it is included this

play17:26

will form a cycle so it may be

play17:28

considered after selecting these two

play17:31

after selecting these two so the weight

play17:33

of this could be

play17:37

four right first this is selected then

play17:39

this is selected then this is not

play17:41

selected means definitely it is four if

play17:43

it is less than four then that would

play17:45

have been selected first right so it

play17:48

came later so it is forming a cycle so

play17:50

it is not taken then this four is taken

play17:52

then this four is taken this five is

play17:54

taken so then what could be the minimum

play17:55

weight of this one so it is not selected

play17:58

at last it is left over means minimum

play18:00

weight of this one could be six so this

play18:03

is four and that is

play18:05

six so if any missing edges are there

play18:07

you can find out these type of problems

play18:09

we find in examinations so some missing

play18:13

edges you can find out so if you know

play18:15

spanning tree you can answer this type

play18:16

of question let us take this example and

play18:19

let us find out spanning tree for this

play18:21

one minimum cost is spaning tree so if I

play18:24

follow crysals method so first I will

play18:26

select the minimum cause Edge this Edge

play18:29

I have selected then the next minimum is

play18:31

this three and that three so I can

play18:33

select anyone so I'll select this one

play18:36

first this selected next I'll select

play18:39

this one so is it forming a cycle

play18:41

inclusion of this one no so I'll select

play18:44

that one then which are the edges four

play18:46

and four these two are there so I can

play18:48

select anyone so I'll select this one

play18:51

whether this will form any cycle no so

play18:54

total how many edges I have selected 1 2

play18:57

3 4 and total how many vertices are

play19:00

there five vertices are there so four

play19:02

edges are selected that's how I got a

play19:04

spanning tree so what is the cost of the

play19:06

spanning tree 2 + 3 + 3 + 4 and this is

play19:11

12 spanning tree is a minimization

play19:14

result so definitely there will be only

play19:16

one result so for a problem there can be

play19:18

only one optimal solution so we should

play19:21

have exactly one spanning tree only

play19:23

there cannot be more than one spanning

play19:25

tree right but in this example you can

play19:29

see instead of taking this

play19:31

four this Edge I can take this four

play19:38

also now also it's forming a spanning

play19:40

tree yes so I can select one more but

play19:44

what is the cost cost is 12 so cost is

play19:47

only one the optimal cost is 12 only you

play19:50

will get definitely only one answer in

play19:53

the minimization OR maximization problem

play19:55

but for that answer the set of input may

play19:58

be changed

play20:00

so either you can include this four or

play20:02

this four so total how many spanning

play20:04

trees are trees are possible two

play20:06

spanning trees are possible but what is

play20:08

the minimum cost that is 12

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Graph TheoryAlgorithmsPrim's AlgorithmKruskal's AlgorithmSpanning TreeMinimum CostData StructuresGreedy MethodWeighted GraphOptimization