Professional Documents
Culture Documents
Spanning Trees 100%
Spanning Trees 100%
Spanning Trees
Prim’s Algorithm
Kruskal Algorithm
Group
• Members
Maha Azkar
• Amna Ahad
• Eeman Khalid
• Shane Hassan
Introduction to
Spanning Trees
Graph
“M”
M is a Set of { A,B,C,D}
Vertex /
Nodes A B
Edges
D C
Tree A Sub-Graph of M
“H”
. Should contain all vertices of G
. Should have (V-1) edges of G
. It must not complete a cycle
A B
4 Connected Vertex
3 Edges
D C
Making a Spanning Tree
from :
Complete Graph
N(N-2)
Minimum Spanning Trees
A minimum spanning tree is a spanning tree in which the sum of the weight of the edges is as minimum as possible.
1
1 1
4 2 4 4 2
3
3
Sum = 8 Sum = 7
If the graph has unique weights of edges then there will only be one
MST
Prim’s Algorithm
Minimum Cost Spanning Tree
Minimum spanning tree is the spanning tree where the cost is minimum among all the
spanning trees.
10 2 10 2
5 5 5
Sum = 15 Sum = 7
Rules of
Algorithm
• All the vertices of the graph must be included
• The vertex with the minimum weight must be selected
first.
• All the vertices must be connected
• There must be no cycle
Graph
‘F’
2
A B
4 1 3 7
2 9
D C E
8 4
6 3
F 1
G
Minimum Spanning Tree
‘S’
6
F
Minimum Spanning Tree
‘S’
A
1
Choose the next Minimum Path
2
D C from C
Minimum Cost Spanning Tree
2
A B looking at B, line 3 connects it to
1 3 C and line 7 connects it to E. We
cannot choose line 3 because
2
D C that will form a cycle between
C, A, and B.
Minimum Cost Spanning Tree
2
A B
1
2
D C E
4
3
F 1
G Sum = 13
Implementing the
Prim’sAlgorithm
Adjacency Array
0 1 2 3 4 5
3 0 0 2 0 4 0 0
1 2
2 4 1 2 0 3 1 0 0
2 0 3 0 0 0 4
0 1 5 3
4 1 0 0 2 0
4 4
3 1 0 0 0 2 0 1
4 5
2 0 0 4 0 1 0
int main()
{
int graph[V][V] = { { 0, 2, 0, 4, 0 ,0}, int minKey(int edgearray[], bool booleanarray[]){
{ 2, 0, 3, 1,0,0}, // Initialize min value
{ 0,3,0,0,0,4}, for (int v = 0; v < V; v++)
{ 4,1,0,0,2,0}, if (booleanarray[v] == false && edgearray[v] < min)
{ 0,0,0,2,0,1}, min = key[v], min_index = v;
{0.0.4,0,1,0}}; return min_index;
primsMST(graph); }
void printMST(int parentarray[], int graph[V][V]){
} // Function to construct and print MST for
void primsMST(int graph[V][V]){ // a graph represented using adjacency
int parentarray[V],booleanarray[V],edgearray[V]; // matrix representation
}
for (int i = 0; i < V; i++)
edgearray[i] = 55, booleanarray[i] = false;
edgearray[0] = 0;
parentarray[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(edgearray, booleanarray);
booleanarray[u] = true;
// Update edge array value and parent index of
// the adjacent vertices of the picked vertex.
// Consider only those vertices which are not yet included in MST
printMST(parentarray, graph);
}
Initialize Parent Array
Parent Array 3
1 2
2 4
-1 -1 -1 -1 -1 -1 0 1 5
0 1 2 3 4 5
4 3 1
4
2
The Parent Array will let us know about the
hierarchy of each node
Avoiding Cycles
Initializing the Boolean Array
F F F F F F
0 1 2 3 4 5
55 55 55 55 55 55
0 1 2 3 4 5
Edge Array
3 55 55 55 55 55 55
1 2
2 4 0 1 2 3 4 5
0 1 5 Parent Array
4 -1 -1 -1 -1 -1 -1
3 4 1
2 0 1 2 3 4 5
Boolean Array
F F F F F F
0 1 2 3 4 5
Edge Array
0 55 55 55 55 55
0 1 2 3 4 5
Parent Array
-1 -1 -1 -1 -1 -1
0
0 1 2 3 4 5
Boolean Array
T F F F F F
0 1 2 3 4 5
Edge Array
0 2 55 55 55 55
1 0 1 2 3 4 5
2
Parent Array
-1 0 -1 -1 -1 -1
0
0 1 2 3 4 5
Boolean Array
T T F F F F
0 1 2 3 4 5
Edge Array
3 0 2 3 55 55 55
1 2 0 1 2 3 4 5
2
Parent Array
0 -1 0 1 -1 -1 -1
0 1 2 3 4 5
Boolean Array
T T T F F F
0 1 2 3 4 5
Edge Array
0 2 3 1 55 55
3 0 1 2 3 4 5
1 2
2 Parent Array
-1 0 1 1 -1 -1
0 1
0 1 2 3 4 5
4
3 Boolean Array
T T T T F F
0 1 2 3 4 5
Edge Array
0 2 3 1 2 55
0 1 2 3 4 5
3
1 2 Parent Array
2
-1 0 1 1 3 -1
0 1 0 1 2 3 4 5
3 4 Boolean Array
2
T T T T T F
0 1 2 3 4 5
Edge Array
0 2 3 1 2 1
3 0 1 2 3 4 5
1 2
2 4 Parent Array
1 -1 0 1 1 3 4
0 5
0 1 2 3 4 5
4 1
3 4
2 Boolean Array
T T T T T T
0 1 2 3 4 5
Introduction to
Kruskal’s
Algorithm
Rules of
Algorithm
•Sort all edges in increasing order of their edge weights.
•If it doesn’t form the cycle, then include that edge in MST. Otherwise,
discard it.
10
0 1
6 5 15
2 3
4
Minimum Spanning Trees
0 10 1
6 5 15
2 3
4
2 3
4
Minimum Spanning Trees
0 10 1
6 5 15
0
2 3
4
2 3
4
Minimum Spanning Trees
0 10 1
6 5 15
0
2 3 We will not take the Edge 6 as this will create a cycle
4
6 5
2 3
4
Minimum Spanning Trees
0 10 1
15 10
6 5
0 1
2 3
4
2 3
4
Implementing the
Kruskal’s
Algorithm
Kruskal Algorithm
• We will take edge list the number of vertices
and edges
• Using While Loop we will find absolute parent
of source and destination
• If parents of both source and destination are
same we will skip that edge
• Otherwise we will add the edge
Kruskal Algorithm
i=0, j=0;
While (i=V-1 && j<E)
if ( From FromP == Top )
Skip Edge;
j++;
Continue;
else
Add edge;
i++;
3
1 1 4 Source Destination Weight
3 0 1 1
0 1
1 3 1
3 2 5
1 2 4 1
2
2 3 4 0 2 2
2 3 2
2
3 4 2
1 2 3
1 4 3
Node 0 1 2 3 4 5
4 5 3
Parent -1 -1 -1 -1 -1 -1
3 5 4
Rank 0 0 0 0 0 0
Real Life Example
Continuous Variable
Transmission
Working Of CVT
Algorithm used in CVT
1
Class ( CVTinpt)
Comp:Tirep1>Tirep2>Tirep3>Tirep4
Inst:FRrotation>FLrotation>RRrotation>Rlrotation
Inputclas: throttlebody
Mmfas%
Finnaldrive RPM
=
indentityNum=x
Class ( Compartor )
If:
Identity num > X1
Function < Prims >
Identity num < X3
Function < U’>
THANK YOU
ANY QUESTIONS?