Design and Analysis of
Algorithms
Minimum Spanning Tree
Prims and Kruskal Algorithm
Introduction
A graph G(V, E) consists of two sets
a finite, nonempty set of vertices V(G) (points or vertices)
a set of edges E, pair of nodes [u, v] in V, denoted by
e=[u, v].
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 complete graph G of n nodes has n*(n-1)/2 edges.
Introduction…
A connected graph without any cycles is called a tree graph
If tree T is finite with m nodes, then T will have m-1 edges.
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.
A Directed Graph G, is said to be unilaterally connected if for
any pair u, v of nodes in G there is a path from u to v or a path
from v to u.
A E
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)
1 if there is path from v to v
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.
BFS (Breadth First Search)
DFS (Depth First Search)
Graph Traversal
BFS (Breadth First Search)
BFS starts at vertex s which is at level 0.
In the first step, we visit all vertices that are at distance 1 edge
away, when we visit there we paint as “visited” , the vertices
adjacent to the start vertex s-these vertices are placed into level 1.
In second step, visit all new vertices, we can reach at the distance
of two edges away from the source vertex s. These new vertices,
which are adjacent to level 1 vertices and not previously assigned
to a level, are placed into level 2 and so on.
The BFS traversal terminates when every vertex has been visited.
Graph Traversal
Algorithm maintains a queue Q to manage set of vertices and
start with s, source vertex
Initially , all vertices except s are colored white, and s is gray.
BFS algorithm maintains the following information for each
vertex u:
color[u] (white, gray and black): indicates status
white: not discovered yet
gray: discovered but not finished
black: finished
d[u]: distance from s to u
ᴨ(u): predecessor of u
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Running time= O(V+E)
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
Graph Traversal
DFS (Depth First Search)
DFS_Visit(u)
DFS(G)
color[u] = GREY;
for each vertex u G->V
time = time+1;
do color[u] = WHITE;
d[u] = time;
ᴨ[u]=Nil
for each v u->Adj[]
time = 0;
if color[v] == WHITE
for each vertex u G->V
DFS_Visit(v);
do if color[u] == WHITE
color[u] = BLACK;
DFS_Visit(u);
time = time+1;
f[u] = time;
Graph Traversal
DFS (Depth First Search)
source
vertex
Graph Traversal
DFS (Depth First Search)
source
vertex
d f
1 | | |
| |
| | |
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
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 |
Graph Traversal
DFS (Depth First Search)
source
vertex
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|
Graph Traversal
DFS (Depth First Search)
source
vertex
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|15
Graph Traversal
DFS (Depth First Search)
source
vertex
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
Design and Analysis of
Algorithms
Minimum Spanning Tree
Prims and Kruskal Algorithm
Rajesh Kumar Tripathi
Assistant Professor, GLA University, Mathura
Minimum Cost Spanning Tree
Electronic circuit design
Network design
Minimum Cost Spanning Tree
Kruskal’s Algorithm
MST-KRUSKAL(G,w)
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
Complexity of the Kruskal-O(E logV)
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Minimum Cost Spanning Tree
Prim’s Algorithm :
Remove loops and more than one edges
Minimum Cost Spanning Tree
Prim’s Algorithm :
Remove loops and more than one edges
Minimum Cost Spanning Tree
Prim’s Algorithm :
Select source node
Minimum Cost Spanning Tree
Prim’s Algorithm :
Select source node
Minimum Cost Spanning Tree
Prim’s Algorithm :
Select source node
Minimum Cost Spanning Tree
Prim’s Algorithm :
Select source node
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Minimum Cost Spanning Tree
Kruskal Algorithm :
Shortest path problems
Dijkstra :
Shortest path problems
Dijkstra :
Shortest path problems
Dijkstra :
Shortest path problems
Dijkstra :
Shortest path problems
Dijkstra :
Shortest path problems
Dijkstra :
Shortest path problems
Dijkstra :
Thank You