0% found this document useful (0 votes)
15 views161 pages

Unit 3 - Part - II Graph - 17 Dec 2024

The document covers the design and analysis of algorithms related to graph theory, including basic terminology, types of graphs, and various representations. It discusses graph traversals such as Depth First Search (DFS) and Breadth First Search (BFS), as well as algorithms for minimum spanning trees and shortest paths. Additionally, it explores concepts like connected components, subgraphs, and the use of adjacency matrices and lists for graph representation.

Uploaded by

palashninawe378
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views161 pages

Unit 3 - Part - II Graph - 17 Dec 2024

The document covers the design and analysis of algorithms related to graph theory, including basic terminology, types of graphs, and various representations. It discusses graph traversals such as Depth First Search (DFS) and Breadth First Search (BFS), as well as algorithms for minimum spanning trees and shortest paths. Additionally, it explores concepts like connected components, subgraphs, and the use of adjacency matrices and lists for graph representation.

Uploaded by

palashninawe378
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 161

CSE 2PM09A /AID 2PM07A - Design and Analysis of Algorithm

Semester - IV
S. Y. B. Tech CSE /AIDS

SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY


Graph

Graph- Basic Terminology, Graphs (Directed, Undirected), Various Representations,


Traversals & Applications of graph- Prim’s and Kruskal’s Algorithms, Dijsktra's Single
source shortest path, Analysis complexity of algorithm, topological sorting.

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

Social Map Computer


Network s Network

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

•The degree of a vertex is the number of edges incident to that vertex.


•For directed graph,
⮚the in-degree of a vertex v is the number of edges that have v as the head
⮚the out-degree of a vertex v is the number of edges that have v as the tail

⮚If di is the degree of a vertex i in a graph G with n vertices and e edges,


the number of edges (of undirected graph) are :-

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

▪If (v0, v1) is an edge in an undirected graph,


⮚ v0 and v1 are adjacent
⮚ The edge (v0, v1) is incident on vertices v0 and v1

▪If <v0, v1> is an edge in a directed graph


⮚ v0 is adjacent to v1, and v1 is adjacent from v0
⮚ The edge <v0, v1> is incident on v0 and v1

2/22/2024 10
Complete graph

•A complete graph is a graph that has the maximum number of edges

⮚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

No. of edges (complete undirected graph) : n(n-1)/2


No. of edges (complete directed graph): n(n-1)

2/22/2024 13
Subgraph and Path

•A subgraph of G is a graph G’ such that V(G’) is a subset of V(G) and E(G’) is a


subset of E(G)

•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

•The length of a path is the number of edges on it

2/22/2024 14
Example for Subgraph
0 0 0 1 2

1 2 1 2 3
3 (i) (ii) (iii)

G1 (a) Some of the subgraph of G1

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

•A simple path is a path in which all the vertices, are distinct.


•A cycle is a path, in which the first and the last vertices are same.
•In an undirected graph G, two vertices, v0 and v1, are connected if there is a path in G
from v0 to v1
•An undirected graph is connected if, for every pair of distinct vertices vi, vj, there is a
path from vi to vj.

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

•A connected component of an undirected graph is a maximal connected subgraph.


•A tree is a graph that is connected and acyclic.
•A directed graph is strongly connected if there is a directed path from vi to vj and also
from vj to vi.
•A strongly connected component is a maximal subgraph that is strongly connected.

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

Two Connected Components for Graphs G4: H1 and H2

2/22/2024 19
Examples for Strongly Connected Component

0
0
1
2

2 1

G3 (Not strongly connected) Strongly connected components of G3

2/22/2024 20
Graph Representation (Various Representations)

•Adjacency Matrix
•Adjacency Lists

2/22/2024 21
Adjacency Matrix

•Let G=(V,E) be a graph with n vertices.


•The adjacency matrix of G is a two-dimensional n × n array, say adj_mat
⮚If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
⮚If there is no such edge in E(G), adj_mat[i][j]=0
•The adjacency matrix for an undirected graph is symmetric; the adjacency matrix
for a digraph need not be symmetric

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

•From the adjacency matrix, to determine the connection of vertices is


easy
•The degree of a vertex is
•For a digraph, the row sum is the out_degree, while the column sum is
the in_degree

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

•Depth First Traversal


•Breadth First Traversal

2/22/2024 28
Depth First Traversal (Recursive)

Algorithm DFS() Algorithm DFS(int v)


{ { print v;
//initially no vertex will be visited visited[v]=1;
for( int i=0 ; i<n; i++) for(each vertex w adjacent to v)
visited[i]=0; if(!visited[w])
//start search at vertex v DFS(w);
accept starting vertex v }
DFS(v);
}

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 }

Task: Conduct a depth-first search of the


graph starting with node D
2/22/2024 30
Depth First SearchTraversal
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 :
D Visit D

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

Find DFT for given graph G1


0
starting at vertex 0
1 2

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

How is this accomplished? Simply replace the stack


with a queue! Rules: (1) Maintain an enqueued
array. (2) Visit node when dequeued.
2/22/2024 75
Breadth First Traversal

Enqueued Array
F C A
A B
Q :D
C
B D
D √
H
E
G E F
G
H
Nodes visited:

Enqueue D. Notice, D not yet 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

Dequeue D. Visit D. Enqueue unenqueued nodes


adjacent to D.
2/22/2024 77
Breadth First Traversal

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

Dequeue C. Visit C. Enqueue unenqueued nodes


adjacent to C.
2/22/2024 78
Breadth First Traversal

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

Dequeue E. Visit E. Enqueue unenqueued nodes


adjacent to E.
2/22/2024 79
Breadth First Traversal

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

Dequeue F. Visit F. Enqueue unenqueued nodes


adjacent to F.
2/22/2024 80
Breadth First Traversal

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

Dequeue G. Visit G. Enqueue unenqueued nodes


adjacent to G.
2/22/2024 81
Breadth First Traversal

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

Dequeue H. Visit H. Enqueue unenqueued nodes


adjacent to H.
2/22/2024 82
Breadth First Traversal

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

Dequeue A. Visit A. Enqueue unenqueued nodes


adjacent to A.
2/22/2024 83
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
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

Find BFT for given graph G1


0
starting at vertex 0
1 2

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

Graph G1 Possible spanning


trees

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

•A minimum cost spanning tree is a spanning tree of least cost


•n-1 edges from a weighted graph of n vertices with minimum cost.

•Two different algorithms can be used


⮚Kruskal
⮚Prim

2/22/2024 90
Minimum Spanning Tree

•Applications of MST in Network design


⮚ Telephone
⮚ Electrical
⮚ TV cable
⮚ Computer
⮚ road

2/22/2024 91
Greedy Strategy
• An optimal solution is constructed in stages

• At each stage, the best decision is made at this time

• Since this decision cannot be changed later, we make sure that the decision will
result in a feasible solution

• Typically, the selection of an item at each stage is based on a least cost or a


highest profit criterion

2/22/2024 92
Kruskal’s Algorithm

• Build a minimum cost spanning tree T by adding edges to T one at a time

• Select the edges for inclusion in T in nondecreasing order of the cost

• An edge is added to T if it does not form a cycle

• 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

Consider an undirected, weight graph

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

Sort the edges by increasing edge weight


edge dv edge dv
3
10
F C (D,E) 1 (B,E) 4
(D,G) 2 (B,F) 4
A 4
4
3
8 (E,G) 3 (B,H) 4
6
5
4
B D (C,D) 3 (A,H) 5
4
H 1
(G,H) 3 (D,F) 6
2 (C,F) 3 (A,B) 8
3
G 3
E (B,C) 4 (A,F) 10

2/22/2024 96
Kruskal’s Algorithm

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 √ (B,E) 4
8
6 (D,G) 2 (B,F) 4
5
4
B D
4 (E,G) 3 (B,H) 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

2/22/2024 97
Kruskal’s Algorithm

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 √ (B,E) 4
8
6 (D,G) 2 √ (B,F) 4
5
4
B D (E,G) 3
4 (B,H) 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

2/22/2024 98
Kruskal’s Algorithm

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv

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

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv

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

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv
(D,E) 1 √ (B,E) 4
A 4
4
3
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 √
3 (D,F) 6
G 3
E (C,F) 3
(A,B) 8
(B,C) 4
(A,F) 10

2/22/2024 101
Kruskal’s Algorithm

Select first |V|–1 edges which do not


3
generate a cycle
10
F C edge dv edge dv
A 4
4
3
(D,E) 1 √ (B,E) 4
8
6
5 (D,G) 2 √
4
B D
(B,F) 4
4 (E,G) 3 χ (B,H) 4
H 1 (C,D) 3 √
2 (A,H) 5
3 (G,H) 3 √
G 3
E (D,F) 6
(C,F) 3 √
(A,B) 8
(B,C) 4
(A,F) 10

2/22/2024 102
Kruskal’s Algorithm

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv
(D,E) 1 √ (B,E) 4
A 4
4
3
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 √
3 (D,F) 6
G 3
E (C,F) 3 √
(A,B) 8
(B,C) 4 √
(A,F) 10

2/22/2024 103
Kruskal’s Algorithm

Select first |V|–1 edges which do not


3 generate a cycle
10
F C
edge dv edge dv
A 4
4
3
8 (D,E) 1 √ (B,E) 4 χ
6
5
4
B D (D,G) 2 √ (B,F) 4
4
H 1
(E,G) 3 χ (B,H) 4
2 (C,D) 3 √
3
(A,H) 5
G 3
E (G,H) 3 √ (D,F) 6
(C,F) 3 √
(A,B) 8
(B,C) 4 √
(A,F) 10

2/22/2024 104
Kruskal’s Algorithm

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 √ (B,E) 4 χ
8
6 (D,G) 2 √ (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

2/22/2024 105
Kruskal’s Algorithm

Select first |V|–1 edges which do not


3 generate a cycle
10
F C
edge dv edge dv
A 4
4
3
(D,E) 1 √ (B,E) 4 χ
8
6
5
4
B D
(D,G) 2 √ (B,F) 4 χ
4 (E,G) 3 χ (B,H) 4 χ
H 1
2 (C,D) 3 √ (A,H) 5
3
(G,H) 3 √
G 3
E (D,F) 6
(C,F) 3 √ (A,B) 8
(B,C) 4 √ (A,F) 10

2/22/2024 106
Kruskal’s Algorithm

Select first |V|–1 edges which do not


generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 √ (B,E) 4 χ
8
6 (D,G) 2 √ (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

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

//Assume G has at least one vertex


TV={0}; //start with vertex 0 and no edges

for (T=Ø; T contains less than n-1 edges; add(u,v) to T)


{
let (u,v) be a least cost edge such that u ∈ TV and v ∉ TV;
if (there is no such edge ) break;
add v to TV;
}

if (T contains fewer than n-1 edges)


cout<<“No spanning tree\n”;

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

2 Start with any node, say D

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

Select node with minimum


2 distance

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

Select node with minimum


2 distance

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

Select node with minimum


2 distance

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

Select node with minimum


distance
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
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

Table entries unchanged

2/22/2024 120
Prim’s Algorithm

Select node with minimum


2 distance
3
10
F C K dv pv
A 10 F
A 7
4
3
8 B 4 C
18
4
9
B D C T 3 D
10
H 25
D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G

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

Select node with minimum


2
distance

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

Table entries unchanged

2/22/2024 124
Prim’s Algorithm

Select node with minimum


distance
2
K dv pv
3
10
F C A T 4 H
B T 4 C
A 7
4
3
8 C T 3 D
18
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 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

1 ∞ 8 4 10 ∞ Near[3] =1 cost[3,near[3]] =4 j=3

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

Put near[1]= -1 as it has been included in ST


5 ∞ 5 6 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

2 8 ∞ 9 ∞ 5 cost[2,3] < 9<8


Near[2] =1
cost[2,near[2]]

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
--

For i=2 find minimum edge connecting to v1 / v3

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

Near[4] =1 cost[4,near[4]] =10


3 4 9 ∞ 12 6

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

cost[4,5] < 7< 10


5 ∞ 5 6 7 ∞ cost[4,near[4]] Near[4] =5

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 ∞

Put near[2]= -1 as it has been included in ST

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

• Run time will be O(n^2).

• 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

• Directed weighted graph.

• Path length is sum of weights of edges on path.

• The vertex at which the path begins is the source vertex.

• The vertex at which the path ends is the destination vertex.

2/22/2024 138
Shortest Path Problems

• Single source single destination.

• Single source all destinations.

• All pairs (every vertex is a source and destination).

2/22/2024 139
Dijkstra Algorithm

• Finds Single source all destination shortest paths


• Uses Greedy Method
• No negative weights are allowed
• Application
⮚ Routing protocols in computer networks
⮚ Google Maps and many more..

Google Maps Routing Protocols


2/22/2024 140
Dijkstra Algorithm

shortest paths from v0(Single source) to all destinations

2/22/2024 141
Dijkstra Algorithm

Algorithm shortestPath(v) for(j=2;j<n; j++)


{ //v is source vertex {
for (i=0;i<n; i++) //choose u from among those vertices not in s
{ such that dist[u] is minimum;
s[i] = 0; s[u] = 1;
dist[i]=g[v][i]; for each(w adjacent to u with s[w] = 0)
} {
//put v into s if(dist[w] > dist[u]+g[u][w] )
s[v] = 1; dist[w] = dist[u]+cost[u][w];
dist[v] =0; }
}
}

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

• O(n) to select next destination vertex.


• O(out-degree) to update d() and p() values when adjacency lists are
used.
• O(n) to update d() and p() values when adjacency matrix is used.
• Selection and update done once for each vertex to which a shortest
path is found.
• Total time is O(n2 + e) = O(n2).

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

• We have a set of tasks and a set of dependencies (precedence constraints) of form


“task A must be done before task B”
• Topological sort: An ordering of the tasks that conforms with the given dependencies
• Goal: Find a topological sort of the tasks or decide that there is no such ordering

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.

• Each edge <i,j> implies that course i is a perquisite of course j

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

Possible Topological orders:


c1, c2, c4, c5, c3, c6, c8, c7, c10, c13, c12, c14, c15, c11, c9 C9

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

for (i = 0; i <n; i++) { 1

if every vertex has a predecessor { V V V

fprintf (stderr, “Network has a cycle. \n “ );


0 2 4

exit(1); V V
} 3 5

pick a vertex v that has no predecessors;


output v;
delete v and all edges leading out of v from the network;
}

Topological order
vgenerated:
0 v3 v2 v5 v1 v4
Internal representation used by topological sorting
algorithm

count first data link


[0] 0 1 2 3 0
[1] 1 4 0
[2] 1 4 5 0

[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

(a) Initial (b) Vertex 0 deleted (c) Vertex 3 deleted

1 1

4
4 4

(e) Vertex 5 deleted (f) Vertex 1 deleted


(d) Vertex 2 deleted
2/22/2024 163

You might also like