Lecture - 04 - Minimum Spanning Tree (Prim and Krushkal) 2
Lecture - 04 - Minimum Spanning Tree (Prim and Krushkal) 2
Algorithms
u e v
Introduction…
u and v are called endpoints of e, and also said to be adjacent
or neighbors.
degree(u) is no. of edges containing u.
degree(u)=0 means if u does not belong to any edge (isolated
node)
u e v
A path P of length n from node u to v is defined as a sequence
of n+1 nodes.
P(v0, v1, v2,……..vn)
u=v0 ; vi-1 is adjacent to vi for i=1, 2, 3……., n and vn=v
Introduction…
A cycle is closed simple path with length 3 or more. A cycle of
length k is called k-cycle.
A graph G is said to be connected if there is a path between any
two of nodes.
A E
B
C D
There are 2 simple paths of length 2 from B to E-(B, A, E) & (B, C, E).
There is only 1 simple path of length 2 from B to D- (B, C, D).
2 4-cycles in the graph: [A, B, C, E, A] & [A, C, D, E, A]
Deg(A)=3, Deg(C ) = 4, Deg(D)=2.
Introduction…
A graph G is said to be complete, if every node u in G is
adjacent to every other node v in G.
A E
B
C D
A B C
D E F
Introduction…
A graph G is said to be labeled or weighted if its edges are
assigned data. .
5
2 A E
B 4 7
3
1 C D
2
Introduction…
A Multiple edges. connects the same endpoints (e4 and e5
connects C and D),
An edge is called Loop if it has identical endpoints i.e. e6
connects (D, D)
e1
A E e6
e2 e3
C
e5
D
e4
Introduction…
A Directed Graph G, also called a digraph or graph in which
each edge is assigned a direction.
Suppose G is directed graph with directed edge e=(u, v).
e begins at u and ends at v.
u is origin and v is destination.
u is predecessor of v and v is successor of u.
u is adjacent to v.
A E
C D
Introduction…
A Directed Graph G, is said to be connected or strongly
connected, if for each pair there is a path from u to v and v to
u.
C D
Introduction…
Degree 3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3
G13 1 1 G2 1 1
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2
G3 2 in: 1, out: 0
Graph Representation of Graphs…
Sequential Representation
Adjacency matrix
Path Matrix
Linked List Representation
Graph Representation of Graphs…
Sequential Representation
Adjacency matrix: Adjacency matrix representation of a
graph G consists of V x V matrix A=( aij ) such that
aij 1 if(i, j)E
0 otherwise
Graph Representation of Graphs…
Graph Representation of Graphs…
Adjacency matrix:
0 0 1 1 1
1 0 1 1
1 2 1 1 0 1
3 1 1 1 0
Graph Representation of Graphs…
Sequential Representation
Path matrix: Adjacency matrix representation of a directed
graph G consists of m nodes then P= (pij)
Pij
i j
0 otherwise
Graph Representation of Graphs…
Path matrix:
0
0 1 0
1 0 1
1 0 0 0
2
Graph Representation of Graphs…
Sequential Representation
Linked List Representation
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G2 G4
2
Graph Traversal
Systematic search of vertex and edges of a graph.
Graph G(V, E) is either directed or undirected.
| |
| | |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | | |
2 | |
| | |
Graph Traversal
DFS (Depth First Search)
source
vertex
d f
1 | | |
2 | |
3 | | |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | | |
2 | |
3 | 4 | |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | | |
2 | |
3 | 4 5 | |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | | |
2 | |
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | 8 | |
2 | 7 9 |
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | 8 | |
2 | 7 9 |10
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 | 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 |12 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
2 | 7 9 |10
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
2 | 7 9 |10
3 | 4 5 | 6 14|
Graph Traversal
DFS (Depth First Search)
source
vertex
2 | 7 9 |10
3 | 4 5 | 6 14|15
Graph Traversal
DFS (Depth First Search)
source
vertex
2 | 7 9 |10
3 | 4 5 | 6 14|15
Design and Analysis of
Algorithms
A= ᵩ
for each vertex vϵG.V
MAKE-SET(V)
sort the edges of G,E into non-decreasing order by weight w
for each edge (u, v) ϵ G,E, taken in nondecreasing order by weight
if FIND-SET(u)!=FIND-SET(v)
A =A U {u,v}
UNION(u,v)
return A
Minimum Cost Spanning Tree
Kruskal’s Algorithm
Minimum Cost Spanning Tree
Kruskal’s Algorithm :
Remove loops and parallel edges (save minimum weight
edge)
Minimum Cost Spanning Tree
Kruskal’s Algorithm :
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight
Minimum Cost Spanning Tree
Kruskal’s Algorithm : Add the edge which has least weight