Unit 3 - Part - II Graph - 17 Dec 2024
Unit 3 - Part - II Graph - 17 Dec 2024
Semester - IV
S. Y. B. Tech CSE /AIDS
2/22/2024 2
Graph
•Basic Terminology
•Memory representation
•Creation of graph and traversals
•Minimum spanning tree
•Topological sorting
2/22/2024 3
Graph Applications
Hyperte Circuit
xt s
2/22/2024 5
Definition
•A graph G consists of two sets
⮚ a finite, nonempty set of vertices V(G)
⮚ a finite, possible empty set of edges E(G)
⮚ G(V,E) represents a graph
0
0
1 2 Graph G1
Graph G2
3 1 2
Vertex Set: V(G1)={0,1,2,3} Vertex Set: V(G2)={0,1,2}
Edge Set: E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} Edge Set: E(G2)={(2,0),(2,1)}
2/22/2024 6
Directed and Undirected Graph
•An undirected graph is one in which the pair of vertices in an edge is unordered,
(v0, v1) = (v1,v0)
•A directed graph is one in which each edge is a directed pair of vertices,
<v0, v1> != <v1,v0>
0 0
1 2 1 2
3
Undirected Graph Directed Graph
2/22/2024 7
Degree
2/22/2024 8
Examples for Degree
in:1, out:
3 0
1
0
in: 1, out:
1 2
3 1 23
3 in: 1, out:
3 2 0
Undirected Graph : G1 Directed Graph: G3
2/22/2024 9
Adjacent and Incident
2/22/2024 10
Complete graph
⮚for undirected graph with n vertices, the maximum number of edges is n(n-1)/2
⮚for directed graph with n vertices, the maximum number of edges is n(n-1)
2/22/2024 11
Examples for Graph
0 0
0
1 2
1 2
1
3 complete 3 4 5 6
graph incomplete
G1 G2 2
graph
V(G1)={0,1,2,3}
V(G2)={0,1,2,3,4,5,6}
E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
G3
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>} Directed graph
2/22/2024 12
Complete Graph
2/22/2024 13
Subgraph and Path
•A path from vertex vp to vertex vq in a graph G, is a sequence of vertices, vp, vi1, vi2, ...,
vin, vq, such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges in an undirected graph
2/22/2024 14
Example for Subgraph
0 0 0 1 2
1 2 1 2 3
3 (i) (ii) (iii)
0 0 0 0
0
1 1 1
1
2 2
(i) (ii) (iii)
G3 2 (iv)
(b) Some of the subgraph of G3
2/22/2024 15
Simple path and cycle
2/22/2024 16
Examples for Graph
0 0 0 4
1 2 1 2 2 1 5
3 3 6
3 4 5 6
G1
G2 7
G3
Connected Graphs: G1 ,G2
Graph G3 : (not connected)
CHAPTER 6 17
2/22/2024 17
Connected Component
2/22/2024 18
Examples for Connected Component
0 4 H1 0 H2 4
2 1 5 5
2 1
3 6
3 6
7
Graph G4 7
2/22/2024 19
Examples for Strongly Connected Component
0
0
1
2
2 1
2/22/2024 20
Graph Representation (Various Representations)
•Adjacency Matrix
•Adjacency Lists
2/22/2024 21
Adjacency Matrix
2/22/2024 22
Adjacency Matrix
0 0
1 2
1
3
Graph G1 Adjacency Matrix for Graph G1
2 Adjacency Matrix for Graph G2
Graph G2
2/22/2024 23
Merits: Adjacency Matrix
2/22/2024 24
Adjacency List: Interesting Operations
•The degree of any vertex in an undirected graph is determined by counting the no. of
nodes in its adjacency list.
•No. of edges in a graph is determined in O(n+e)
•Out-degree of a any vertex in a directed graph is determined by counting No. of nodes
in its adjacency list.
2/22/2024 25
class gnode
{
Adjacency Lists
int vertex;
node *next;
friend class graph;
}; 0
class graph
{ 1 2 Graph G1
private:
gnode *head[20]; 3
int n;
public:
0 1 2 3
graph() 1 0 2 3
{ 2 0 1 3
create head nodes for n vertices 3 0 1 2
}
}; Adjacency List for Graph G1
2/22/2024 26
graph() Allocate memory for curr node;
{
Accept no of vertices; curr->vertex=v;
for i=0 to n-1 temp->next=curr;
{Allocate a memory for head[i] node (array)
temp=temp->next;
head[i]->vertex=i; }
} }
accept the choice ;
create()
}while(ans=='y'||ans=='Y');
{
for i=0 to n-1 }
{ }
temp=head[i];
do
{ 3 • 1 • 2 NULL
Accept adjacent vertex v; 0 •
if(v==i) 2 • 0 • 3 NULL
Print Self loop are not allowed; 1 •
else 3 • 0 •
{ 2 • 1 NULL
2 • 1 • 0 NULL
3 •
2/22/2024 27
Graph Traversal
2/22/2024 28
Depth First Traversal (Recursive)
2/22/2024 29
Depth First Search Traversal
Visited Array
F C A Algorithm DFS(int v)
A B
C { print v;
B D
D visited[v]=1;
H
E for(each vertex w adjacent to v)
G E F if(!visited[w])
G DFS(w);
H }
2/22/2024 31
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C { print v;
B D
D 1 visited[v]=1;
H
E for(each vertex w adjacent to v)
G E F if(!visited[w])
G DFS(w);
H D }
The DFT of nodes in graph :
Consider nodes adjacent to D, decide to visit C first
D (Rule: visit adjacent nodes in alphabetical order
or in order of the adjacancy list)
2/22/2024 32
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E for(each vertex w adjacent to v)
G E F if(!visited[w])
G C DFS(w);
H D }
The DFT of nodes in graph :
Visit C
D, C
2/22/2024 33
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E for(each vertex w adjacent to v)
G E F if(!visited[w])
G C DFS(w);
H D }
The DFT of nodes in graph : No nodes adjacent to C; cannot
D, C continue 🡺 backtrack, i.e., pop stack
and restore previous state
2/22/2024 34
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E for(each vertex w adjacent to v)
G E F if(!visited[w])
G DFS(w);
H D }
The DFT of nodes in graph :
Back to D – C has been visited,
D, C decide to visit E next
2/22/2024 35
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E F if(!visited[w])
G E DFS(w);
H D }
The DFT of nodes in graph :
Back to D – C has been visited,
D, C, E decide to visit E next
2/22/2024 36
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E F if(!visited[w])
G E DFS(w);
H D }
The DFT of nodes in graph :
Only G is adjacent to E
D, C, E
2/22/2024 37
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1 E DFS(w);
H D }
The DFT of nodes in graph :
Visit G
D, C, E, G
2/22/2024 38
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1 E DFS(w);
H D }
The DFT of nodes in graph :
Nodes D and H are adjacent to
D, C, E, G G. D has already been
visited. Decide to visit H.
2/22/2024 39
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1 E DFS(w);
H 1 D }
The DFT of nodes in graph :
Visit H
D, C, E, G, H
2/22/2024 40
Depth First Traversal
Visited Array
F C A
Algorithm DFS(int v)
A B
C 1 { print v;
B D
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1 E DFS(w);
H 1 D }
The DFT of nodes in graph :
Nodes A and B are adjacent to H.
D, C, E, G, H Decide to visit A next.
2/22/2024 41
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B
C 1 { print v;
B D A
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1 E DFS(w);
H 1 D }
The DFT of nodes in graph :
Visit A
D, C, E, G, H, A
2/22/2024 42
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B
C 1 { print v;
B D A
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1 E DFS(w);
H 1 D }
The DFT of nodes in graph :
Only Node B is adjacent to A.
D, C, E, G, H, A Decide to visit B next.
2/22/2024 43
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1 B
C 1 { print v;
B D A
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1
E DFS(w);
H 1 D }
The DFT of nodes in graph :
Visit B
D, C, E, G, H, A, B
2/22/2024 44
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D A
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1
E DFS(w);
H 1 D }
The DFT of nodes in graph :
No unvisited nodes adjacent to B.
D, C, E, G, H, A, B Backtrack (pop the stack).
2/22/2024 45
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D
D 1 visited[v]=1;
H H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1
E DFS(w);
H 1 D }
The DFT of nodes in graph :
No unvisited nodes adjacent to A.
D, C, E, G, H, A, B Backtrack (pop the stack).
2/22/2024 46
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E
G if(!visited[w])
F
G 1
E DFS(w);
H 1 D }
The DFT of nodes in graph :
No unvisited nodes adjacent to H.
D, C, E, G, H, A, B Backtrack (pop the stack).
2/22/2024 47
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E F if(!visited[w])
G 1
E DFS(w);
H 1 D }
The DFT of nodes in graph :
No unvisited nodes adjacent to G.
D, C, E, G, H, A, B Backtrack (pop the stack).
2/22/2024 48
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E F if(!visited[w])
G 1 DFS(w);
H 1 D }
The DFT of nodes in graph :
No unvisited nodes adjacent to E.
D, C, E, G, H, A, B Backtrack (pop the stack).
2/22/2024 49
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E F if(!visited[w])
G 1 DFS(w);
H 1 D }
The DFT of nodes in graph :
F is unvisited and is adjacent to
D, C, E, G, H, A, B D. Decide to visit F next.
2/22/2024 50
Depth First Traversal
Visited Array
F C A 1
Algorithm DFS(int v)
A B 1
C 1 { print v;
B D
D 1 visited[v]=1;
H
E 1 for(each vertex w adjacent to v)
G E F 1 if(!visited[w])
G 1
F DFS(w);
D }
H 1
The DFT of nodes in graph :
Visit F
D, C, E, G, H, A, B, F
2/22/2024 51
Depth First Traversal
Visited Array
F C A √
Algorithm DFS(int v)
A B √
C √ { print v;
B D
D √ visited[v]=1;
H
E √ for(each vertex w adjacent to v)
G E F √ if(!visited[w])
G √
DFS(w);
D }
H √
The DFT of nodes in graph :
No unvisited nodes adjacent to F.
D, C, E, G, H, A, B, F Backtrack.
2/22/2024 52
Depth First Traversal
Visited Array
F C A √
Algorithm DFS(int v)
A B √
C √ { print v;
B D
D √ visited[v]=1;
H
E √ for(each vertex w adjacent to v)
G E F √ if(!visited[w])
G √
DFS(w);
}
H √
The order nodes are visited:
No unvisited nodes adjacent to D.
D, C, E, G, H, A, B, F Backtrack.
2/22/2024 53
Depth First Traversal
Visited Array
F C A √
Algorithm DFS(int v)
A B √
C √ { print v;
B D
D √ visited[v]=1;
H
E √ for(each vertex w adjacent to v)
G E F √ if(!visited[w])
G √
DFS(w);
}
H √
The DFT of nodes in graph :
Stack is empty. Depth-first
D, C, E, G, H, A, B, F traversal is done.
2/22/2024 54
Depth First Traversal (Non-recursive)
Algorithm DFS(int v) F C
{
for all vertices of graph A
visited[i]=0;
B D
push(v);
visited[v]=1; H
do
{ G E
v=pop();
print(v); Task: Conduct a depth-first search of the
for(each vertex w adjacent to v)
{
graph starting with node D
if(!visited[w]) Adjacency List – F->C
A->B G->H->D
{ push(w); visited[w]=1;}
B->NULL H->B->A
} //end for C->NULL
} while(stack not empty) D->F->E->C
} //end dfs E->G
2/22/2024 55
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E
do F
{ G E G
v=pop(); D
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for The DFT of nodes in graph : C->NULL
} while(stack not empty) D->F->E->C
} //end dfs D E->G
2/22/2024 56
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
C
do F 1
{ G E G E
v=pop(); F
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D
2/22/2024 57
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G E
v=pop(); F
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C
2/22/2024 58
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G E
v=pop(); F
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C
2/22/2024 59
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G
v=pop(); F
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E,
2/22/2024 60
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1 G
v=pop(); F
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E,
2/22/2024 61
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop(); F
H
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G
2/22/2024 62
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1 H
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G
2/22/2024 63
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A
{ F C B
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H
2/22/2024 64
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
A
do F 1
{ G E G 1 B
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H
2/22/2024 65
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1 B
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A
2/22/2024 66
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1 B
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A
2/22/2024 67
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A, B
2/22/2024 68
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop(); F
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A, B
2/22/2024 69
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop();
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A, B, F
2/22/2024 70
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop();
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A, B, F
2/22/2024 71
Depth First Traversal (Non-recursive)
Visited Array
Algorithm DFS(int v) A 1
{ F C B 1
for all vertices of graph A C 1
visited[i]=0;
B D 1
push(v); D
visited[v]=1; H
E 1
do F 1
{ G E G 1
v=pop();
H 1
print(v);
for(each vertex w adjacent to v) Stack
{ Adjacency List – F->C
if(!visited[w]) A->B G->H->D
{ push(w); visited[w]=1;} B->NULL H->B->A
} //end for C->NULL
The DFT of nodes in graph : D->F->E->C
} while(stack not empty)
E->G
} //end dfs D, C, E, G, H, A, B, F
2/22/2024 72
Depth First Traversal
3 4 5 6
Graph G1
2/22/2024 73
Breadth First Traversal
Algorithm BFS(int v) {
for(int i=0;i<n;i++)
visited[i]=0;
Queue q;
q.insert(v);
while(!q.IsEmpty())
{
v=q.Delete();
for(all vertices w adjacent to v)
if(!visited[w])
{
q.insert(w);
visited[w]=1;
}
}
}
2/22/2024 74
Breadth First Traversal
Enqueued Array
F C A
A B
Q:
C
B D
D
H
E
G E F
G
H
Enqueued Array
F C A
A B
Q :D
C
B D
D √
H
E
G E F
G
H
Nodes visited:
2/22/2024 76
Breadth First Traversal
Enqueued Array
F C A
A B
Q: C, E, F
C √
B D
D √
H
E √
G E F √
G
H
Nodes visited: D
Enqueued Array
F C A
A B
Q:E,F
C √
B D
D √
H
E √
G E F √
G
H
Nodes visited: D, C
Enqueued Array
F C A
A B
Q:F, G
C √
B D
D √
H
E √
G E F √
G
H
Nodes visited: D, C, E
Enqueued Array
F C A
A B
Q:G
C √
B D
D √
H
E √
G E F √
G √
H
Nodes visited: D, C, E, F
Enqueued Array
F C A
A B
Q:H
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G
Enqueued Array
F C A √
A B √
Q:A, B
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H
Enqueued Array
F C A √
A B √
Q:B
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H, A
Enqueued Array
F C A √
A B √
Q empty
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H,
A, B
Dequeue B. Visit B. Enqueue unenqueued nodes
adjacent to B.
2/22/2024 84
Breadth First Traversal
Enqueued Array
F C A √
A B √
Q empty
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H,
A, B
Q empty. Algorithm done.
2/22/2024 85
Breadth First Traversal
3 4 5 6
Graph G1
2/22/2024 86
Spanning Trees
•A spanning tree is any tree that consists solely of edges in G and that includes all the
vertices
•A spanning tree is a minimal subgraph, G’, of G such that V(G’)=V(G) and G’ is
connected.
•Either dfs or bfs can be used to create a spanning tree
⮚When dfs is used, the resulting spanning tree is known as a depth first spanning tree
⮚When bfs is used, the resulting spanning tree is known as a breadth first spanning tree
2/22/2024 87
Examples of Spanning Trees
0 0 0 0
1 2 1 2 1 2 1 2
3 3 3 3
2/22/2024 88
DFS VS BFS Spanning Trees
0 0 0
1 2 1 2 1 2
3 4 5 6 3 4 5 6 3 4 5 6
7 7 7
Graph DFS Spanning Tree BFS Spanning Tree
2/22/2024 89
Minimum Spanning Tree
•The cost of a spanning tree of a weighted undirected graph is the sum of the costs
of the edges in the spanning tree
2/22/2024 90
Minimum Spanning Tree
2/22/2024 91
Greedy Strategy
• An optimal solution is constructed in stages
• Since this decision cannot be changed later, we make sure that the decision will
result in a feasible solution
2/22/2024 92
Kruskal’s Algorithm
• Since G is connected and has n > 0 vertices, exactly n-1 edges will be selected
2/22/2024 93
Kruskal’s Algorithm
T= {};
while (T contains less than n-1 edges && E is not empty)
{
choose a least cost edge (v,w) from E;
delete (v,w) from E;
if ((v,w) does not create a cycle in T)
add (v,w) to T
else
discard (v,w);
}
if (T contains fewer than n-1 edges)
printf(“No spanning tree\n”);
2/22/2024 94
Kruskal’s Algorithm
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
2/22/2024 95
Kruskal’s Algorithm
2/22/2024 96
Kruskal’s Algorithm
2/22/2024 97
Kruskal’s Algorithm
2/22/2024 98
Kruskal’s Algorithm
A 4
4
3 (D,E) 1 √ (B,E) 4
8 (D,G) 2 √
6 (B,F) 4
5
4
B D (E,G) 3 χ (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Accepting edge (E,G) would create a cycle
2/22/2024 99
Kruskal’s Algorithm
A 4
4
3 (D,E) 1 √ (B,E) 4
8 (D,G) 2 √ (B,F) 4
6
5
4
B D (E,G) 3 χ (B,H) 4
4
H 1
(C,D) 3 √ (A,H) 5
2
(G,H) 3 (D,F) 6
3
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
2/22/2024 100
Kruskal’s Algorithm
2/22/2024 101
Kruskal’s Algorithm
2/22/2024 102
Kruskal’s Algorithm
2/22/2024 103
Kruskal’s Algorithm
2/22/2024 104
Kruskal’s Algorithm
2/22/2024 105
Kruskal’s Algorithm
2/22/2024 106
Kruskal’s Algorithm
2/22/2024 107
Kruskal’s Algorithm
Select first |V|–1 edges which do not
generate a cycle
edge dv edge dv
3
F C (D,E) 1 √ (B,E) 4 χ
(D,G) 2 √ (B,F) 4 χ
A 4
3
(E,G) 3 χ (B,H) 4 χ
5
B D (C,D) 3 √ (A,H) 5 √
}
H 1
(G,H) 3 √ (D,F) 6
not
2 (C,F) 3 √ (A,B) 8
3 considered
G E (B,C) 4 √ (A,F) 10
Done
Total Cost = Σ dv = 21
2/22/2024 108
Prim’s Algorithm
2/22/2024 109
Prim’s Algorithm
2 Initialize array
3
K dv pv
10
F C
A F ∞ −
A 7
4
3
B F ∞ −
8
18
4
9
B D C F ∞ −
10 D F ∞ −
H 25
2 E F ∞ −
3
G 7
E F F ∞ −
G F ∞ −
H F ∞ −
2/22/2024 110
Prim’s Algorithm
3
10
F C K dv pv
A
A 7
4
3
8 B
18
4
9
B D C
10
H 25
D T 0 −
2
3 E
G 7
E F
G
H
2/22/2024 111
Prim’s Algorithm
Update distances of
adjacent, unselected nodes
2
3 K dv pv
10
F C A
A 7
4
3
B
8
18
4
9
B D
C 3 D
10 D T 0 −
H 25
2 E 25 D
3
G 7
E F 18 D
G 2 D
H
2/22/2024 112
Prim’s Algorithm
3
K dv pv
10
F C
A
A 7
4
3
B
8
18
4
9
B D C 3 D
10
H 25
D T 0 −
2 E 25 D
3
G 7
E F 18 D
G T 2 D
H
2/22/2024 113
Prim’s Algorithm
Update distances of
2 adjacent, unselected nodes
3
10
F C
K dv pv
A
A 7
4
3
8 B
18
4
9
B D C 3 D
10
H 25
D T 0 −
2 E 7 G
3
G 7
E F 18 D
G T 2 D
H 3 G
2/22/2024 114
Prim’s Algorithm
3
10
F C K dv pv
A 7
4
3 A
8
18 B
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2/22/2024 115
Prim’s Algorithm
Update distances of
adjacent, unselected nodes
2
3 K dv pv
10
F C A
A 7
4
3
B 4 C
8
18
4 C T 3 D
9
B D
10 D T 0 −
H 25 E 7 G
2
3
F 3 C
G 7
E
G T 2 D
H 3 G
2/22/2024 116
Prim’s Algorithm
3
10
F C
K dv pv
A
A 7
4
3
8 B 4 C
18
4
9
B D C T 3 D
10
H 25
D T 0 −
2 E 7 G
3
G 7
E F T 3 C
G T 2 D
H 3 G
2/22/2024 117
Prim’s Algorithm
Update distances of
adjacent, unselected nodes
2
3 K dv pv
10
F C A 10 F
A 7
4
3 B 4 C
8
18 C T 3 D
4
9
B D D T 0 −
10
H 25 E 2 F
2
3 F T 3 C
G 7
E G T 2 D
H 3 G
2/22/2024 118
Prim’s Algorithm
3 K dv pv
10
F C A 10 F
A 7
4
3 B 4 C
8
18 C T 3 D
4
9
B D
10 D T 0 −
H 25 E T 2 F
2
3 F T 3 C
G 7
E
G T 2 D
H 3 G
2/22/2024 119
Prim’s Algorithm
Update distances of
adjacent, unselected nodes
2 K dv pv
3 A 10 F
10
F C B 4 C
A 7
4
3 C T 3 D
8
18 D T 0 −
4
9
B D E T 2 F
10
H 25 F T 3 C
2
3 G T 2 D
G 7
E H 3 G
2/22/2024 120
Prim’s Algorithm
2/22/2024 121
Prim’s Algorithm
Update distances of
adjacent, unselected nodes
2
3 K dv pv
10
F C A 4 H
A 7
4
3
B 4 C
8
18
4 C T 3 D
9
B D
10 D T 0 −
H 25
2 E T 2 F
3
G 7
E F T 3 C
G T 2 D
H T 3 G
2/22/2024 122
Prim’s Algorithm
3 K dv pv
10
F C A T 4 H
A 7
4
3
B 4 C
8
18
4
9
B D
C T 3 D
10 D T 0 −
H 25
2 E T 2 F
3
G 7
E F T 3 C
G T 2 D
H T 3 G
2/22/2024 123
Prim’s Algorithm
Update distances of
adjacent, unselected nodes
2
3
K dv pv
10
F C A T 4 H
A 7
4
3 B 4 C
8
18 C T 3 D
4
9
B D D T 0 −
10
H 25 E T 2 F
2
3 F T 3 C
G 7
E G T 2 D
H T 3 G
2/22/2024 124
Prim’s Algorithm
2/22/2024 125
Prim’s Algorithm
Cost of Minimum Spanning
Tree = Σ dv = 21
2
3 K dv pv
F C A T 4 H
A 4
3 B T 4 C
4 C T 3 D
B D D T 0 −
H E T 2 F
2
3 F T 3 C
G E G T 2 D
H T 3 G
Done
2/22/2024 126
Prim’s Algorithm
for i=1 to n-1 do
{ //find n-1 additional edges for t
min= ∞
Algorithm prims(start_v){ for k=0 to n-1
//cost[i][j] is either +ve or infinity. { // find j : vertex such that;
//A MST is computed & stored as a set of edges in the if (nearest[k]!= -1 and cost[k, nearest[k]] <min)
//array t[n][1]. t[i][0], t[i][1]) is an edge in the MST { j=k ; min= cost[k, nearest[k]];}
//where 0<i<n. }
// start_v be the starting vertex //update tree and total cost
{ t[ r][0]=nearest[j] , t[r][1]=j; t[r][2] = min; r=r+1;
//Initialize nearest mincost = mincost +cost[j, nearest[j]);
nearest[j]=-1;
nearest [start_v] = -1;
for i=0 to n-1 do //update nearest for remaining vertices
{ if(i!=start_v) for k=0 to n-1
nearest[i]= start_v; { if(nearest[k]!= -1 and (cost[k, nearest[k])> cost[k, j]
} nearest[k]=j;
r=0; }
} //end for i=1 to n-1
return mincost;
2/22/2024
} 127
Initial values of Near
For i=1 find minimum edge connecting to v1
Stv=1 Stv=1
Near[1] = -1
Cost Adjacancy Matrix
1 2 3 4 5 Near[2] =1 cost[2,near[2]] =8
Near[4] =1
2 8 ∞ 9 ∞ 5 cost[4,near[4]] =10
Near[5] =1
3 4 9 ∞ 12 6 Cost[5,near[5]] =∞
4 10 ∞ 12 ∞ 7
2/22/2024 128
Update Near matrix i.e. (cost [i][j] < cost[j][near[j]])
1 2 3 4 5 J=3 j=3
1 ∞ 8 4 10 ∞ Near[1] = -1
3 4 ∞ 9 12 6
Cost Adjacancy Matrix Near[3] = -1
4 10 ∞ 12 ∞ 7 cost[4,3] < 12< 10
cost[4,near[4]]
Near[4] =1
5 ∞ 5 6 7 ∞ cost[5,3] < 6 < ∞
cost[5,near[5]]
Near[5] =3
T 1 2 3
(v1) (v2) (cost)
1 1 3 4
Put near[3]= -1 as it has been included in ST
2
6
2/22/2024 129
--
Find next j
j=3 j=3
Cost Adjacancy Matrix Near[1] = -1
1 2 3 4 5 Near[2] =1 cost[2,near[2]] =8
1 ∞ 8 4 10 ∞
Near[3] = -1
2 8 ∞ 9 ∞ 5
Cost[5,near[5]] =6
4 10 ∞ 12 ∞ 7 Near[5] =3
j=5
5 ∞ 5 6 7 ∞
2/22/2024 130
Cost Adjacancy Matrix
Update Near matrix i.e. (cost [i][j] < cost[j][near[j]])
1 2 3 4 5 J=5
j=5
1 ∞ 8 4 10 ∞
Near[1] = -1
2 8 ∞ 9 ∞ 5
cost[2,5] < 5<8
cost[2,near[2]] Near[2] =5
3 4 9 ∞ 12 6
Near[3] = -1
4 10 ∞ 12 ∞ 7
Near[5] = -1
T 1 2 3
(v1) (v2) (cost)
1 1 3 4
Put near[5]= -1 as it has been included in ST
2 3 5 6
3
4
5
6
2/22/2024 131
For i=3 find minimum edge connecting to v1 / v3/v5
Find next j
j=5
j=5
Cost Adjacancy Matrix Near[1] = -1
1 2 3 4 5 Near[2] =5
1 ∞ 8 4 10 ∞ cost[2,near[2]] =5
Near[3] = -1 j=2
2 8 ∞ 9 ∞ 5
Near[4] =5
3 4 9 ∞ 12 6
cost[4,near[4]] =7
4 10 ∞ 12 ∞ 7 Near[5] = -1
5 ∞ 5 6 7 ∞
2/22/2024 132
j=2
1 2 3 4 5
Near[1] = -1
J=2
1 ∞ 8 4 10 ∞
Near[2] = -1
2 8 ∞ 9 ∞ 5
Near[3] = -1
3 4 ∞ 9 12 6
Cost Adjacancy Matrix
4 10 ∞ 12 ∞ 7 cost[4,2] < ∞ <7 Near[4] =5
cost[4,near[4]]
5 ∞ 5 6 7 ∞
Near[5] = -1
T 1 2 3
(v1) (v2) (cost) Put near[4]= -1 as it has been included in ST
1 1 3 4
2 3 5 6
3 2 5 5
4 4 5 7
6
2/22/2024 133
j=2
For i=4 find minimum edge connecting to v1 / v3/v5/v2
1 2 3 4 5
Near[1] = -1
J=2
1 ∞ 8 4 10 ∞
Near[2] = -1
2 8 ∞ 9 ∞ 5
Near[3] = -1
3 4 ∞ 9 12 6
Cost Adjacancy Matrix
4 10 ∞ 12 ∞ 7 cost[4,2] < ∞ <7 Near[4] =5
cost[4,near[4]]
j=4
5 ∞ 5 6 7 ∞
Near[5] = -1
T 1 2 3
(v1) (v2) (cost) Put near[4]= -1 as it has been included in ST
1 1 3 4
2 3 5 6
3 2 5 5
4 4 5 7
6
2/22/2024 134
Analysis of Prim’s Algorithm
• Unlike Kruskal’s, it doesn’t need to see all of the graph at once. It can deal with it one
piece at a time. It also doesn’t need to worry if adding an edge will create a cycle since
this algorithm deals primarily with the nodes and not the edges.
2/22/2024 135
Home Assignment
Find MST for given Graph G1 using Prim’s and Kruskal Algorithm
0
2
1 8 1
0 1 1
4 6
5 6 2
2
2 4 1 1
5 4 8 2
2 3
2
2/22/2024 136
Comparison Prim’s and Kruskal’s Algorithm
• Kruskal’s has better running times if the number of edges is low, while Prim’s
has a better running time if both the number of edges and the number of nodes
are low.
• So, of course, the best algorithm depends on the graph and if you want to bear
the cost of complex data structures.
2/22/2024 137
Shortest Path Problems
2/22/2024 138
Shortest Path Problems
2/22/2024 139
Dijkstra Algorithm
2/22/2024 141
Dijkstra Algorithm
2/22/2024 142
Dijkstra Algorithm
Dijkstra(G)
for each v ∈ V
d[v] = ∞;
d[s] = 0; S = ∅; Q = V;
while (Q ≠ ∅)
u = ExtractMin(Q);
S = S U {u};
for each v ∈ Adj[u]
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);
2/22/2024 143
Dijkstra Algorithm
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
[1] [2] [3] [4] [5] [6] [7]
d 0 6 2 16 - - 14
p - 1 1 1 - - 1
2/22/2024 144
Dijkstra Algorithm
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
d 0 6 2 16 55- 10 - 14
p - 1 1 1 3- -
3 1
2/22/2024 145
Dijkstra Algorithm
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
1 3 5 d 0 66 2 16 9 5- 10 - 14
p - 1 1 51 3- -
3 1
2/22/2024 146
Dijkstra Algorithm
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
1 3 5 d 0 6 2 9 5- 10 - 14
p - 1 1 5 3- -
3 1
1 2 - 3
-
2/22/2024 147
Dijkstra Algorithm
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
1 3 5 d 0 6 2 9 5- 10 - 14
12
p - 1 1 5 3- - 41
3
1 2
1 3 5 4 -
2/22/2024 148
Dijkstra Algorithm
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
14
1 3 6 [1] [2] [3] [4] [5] [6] [7]
d 0 6 2 9 5- 10 - 14
12
11
p - 1 1 5 3- - 416
3
-
2/22/2024 149
Dijkstra Algorithm
Path Length
1 0
1 3 2
1 3 5 5
1 2 6
1 3 5 4 9
[1] [2] [3] [4] [5] [6] [7]
1 3 6 10 0 6 2 9 5- 10 - 14
12
11
- 1 1 5 3- 3 - 416
1 3 6 7 11
2/22/2024 150
Analysis of Dijkstra Algorithm
2/22/2024 151
Single Source Shortest Paths Program (v=0)
visited
:
[0] [1] [2] [3] [4] [5]
4
cost: 5
V 50 V V
10
0 1 4
20 10 15 20 35 30
V 15 V 3 V
2
distance 0 50
45 10
3
1000
25 45
5
1000
:
i 2
3
1
0
u: 4
2
3
1
w: 43
2
1
0
5
:
Topological sort
2/22/2024 154
Topological sort
• Applications
⮚ Assembly lines in industries
⮚ Courses arrangement in schools
⮚ Life related applications: Dressing order
2/22/2024 155
Activity on vertex (AOV) network
• Activity on vertex (AOV) network
⮚ An activity on vertex(AOV)network, is a digraph G in which the vertices
represent tasks or activities and the edges represent precedence relations
between tasks.
• Predecessor :
⮚ Vertex i in an AOV network G is a predecessor of vertex j iff there is a directed
path from vertex i to vertex j
⮚ Vertex i is an immediate predecessor of vertex j iff <i, j> is an edge in G
• Successor :
⮚ If i is a predecessor of j, then j is a successor of i.
⮚ If i is an immediate predecessor of j, then j is an immediate successor of i
2/22/2024 156
Activity on vertex (AOV) network
• Example 1:
⮚ Prerequisites define precedence relations between courses. The
relationship defined may be more clearly represented using a directed
graph in which the vertices represent courses & the directed edges
represent prerequisites.
2/22/2024 157
Activity on vertex (AOV) network
Course number Course name Prerequisites
C1 Programming I None
C2 Discrete Mathematics None
C3 Data Structures C1, C2
C4 Calculus I None
C5 Calculus II C4
C6 Linear Algebra C5
C7 Analysis of Algorithms C3, C6
C8 Assembly Language C3
C9 Operating Systems C7, C8
C10 Programming Languages C7
C11 Compiler Design C10
C12 Artificial Intelligence C7
C13 Computational Theory C7
C14 Parallel Algorithms C13
C15
2/22/2024 Numerical Analysis C5 158
Activity on vertex (AOV) network
AOV n/w representing courses as vertices & prerequisites as edges
c4, c5, c2, c1, c6, c3, c8, c15, c7, c9, c10, c11, c12, c13, c14
C10 C11
C1
C8 C12
C2
C3 C7 C13 C14
C4 C5 C6 C15
2/22/2024 159
Topological sort
void topological_sort()
{
for (i = 0; i <n; i++) {
if every vertex has a predecessor {
fprintf(stderr, “Network has a cycle. \n “ );
exit(1);
}
pick a vertex v that has no predecessors;
output v;
delete v and all edges leading out of v from the network;
}
2/22/2024 160
Topological Sorts
Topological sort Algorithm V
exit(1); V V
} 3 5
Topological order
vgenerated:
0 v3 v2 v5 v1 v4
Internal representation used by topological sorting
algorithm
[3] 1 5 4 0
[4] 3 0
[5] 1
2 0
0 2 4
3 5
2/22/2024 162
An AOV network
1 1 1
0 2 4 2 4 2 4
3 5 3 5 5
1 1
4
4 4