You are on page 1of 60

Amity School of Engineering & Technology

DATA STRUCTURES
Module 6
Graph
Dr Anant K Jayswal
MTech(CSE), PhD(CSE)-JNU GATE(CS), UGC-NET(CS)

1
Amity School of Engineering & Technology

Contents:
❑ Basic Concepts of Graph
❑Different types of Graph
❑Representation of Graph

2
Amity School of Engineering & Technology

Objectives

After completing this section, you will be able to


1) Understand the Concepts of Graphs
2) Apply the knowledge of Graph theory in computer science.

3
Amity School of Engineering & Technology

Graphs…
Graph is a non linear Data Structure. Example:
It is a collection of nodes(vertices)
and edges(arcs) that relate nodes to
each other in the graph.
Generally, A Graph is represented as
G = (V, E)
Where,
V is the set of vertices(nodes or
points) and
E is the set of edges(links or arcs). This graph G can be defined as
G= ( V , E )
where, V= {A,B,C,D,E} and
E= {(A,B),(A,C)(A,D),(B,D), (C,D),
(B,E),(E,D)}.
4
Amity School of Engineering & Technology

Graph…
❖ Vertex: An individual data element of a graph is called as
vertex. Vertex is also known as node.
❖ Edge: An edge is a connecting link between two vertices.

Edges are of three types


Undirected Edge : An edge with no (A,B) is equal to

specific direction. It is Bidirectional (B, A)

Directed Edge : An edge which has (A,B) is not

specific direction. It is Unidirectional equal to (B, A)

Weighted Edge - An edge with cost


on it. 5
Amity School of Engineering & Technology

Graph Terminology
Adjacent Nodes: Any two nodes which are connected by an
edge are said to be adjacent nodes.

Source and Destination: If an edge is directed, its first endpoint


is origin and the other endpoint is destination of the edge.

Adjacent edges- Any two edges which are having one common
vertex

Loop or Self Loop: An edge that starts and end at the same
vertex

Parallel Edges- If more than one edge exists between the nodes
then the edges are called the parallel ledges.
6
Amity School of Engineering & Technology

Types of Graphs
Null Graph: A graph with no
edges is called a Null Graph E(G)=φ
V(G)≠ φ

Finite Graph: A graph with


finite number of vertices and
edges is called as a finite
graph.

Infinite Graph: A graph with


infinite number of vertices V(G)=E(G)=∞

and edges is called as a


finite graph.
Amity School of Engineering & Technology

Types of Graphs

Directed Graph: A graph with


only directed edges is called as
a directed graph.

Undirected Graph: A graph with


only undirected edges is called
as a undirected graph.

Mixed Graph: A graph with


directed and undirected edges
is called as mixed graph
8
Amity School of Engineering & Technology

Types of Graphs

Simple Graph: A graph without


parallel edges and loops is
called as simple graph

9
Amity School of Engineering & Technology

Graph Terminology
Degree: Total number of edges connected to a vertex.

In-Degree(deg(u)): Total number of edges in which u is the


terminal vertex.

Out-Degree(deg(u)): Total number of edges in which u is the initial


vertex.

Isolated vertex: Vertex with degree zero(0).

Path: A sequence of vertices and edges in which no vertex and no


edge can be repeated

Note: In-Degree and Out-degree is possible in case of Directed Graphs 10


Amity School of Engineering & Technology

Types of Graphs

11
Amity School of Engineering & Technology

Graph Representations in Memory

1. Sequential Representation
2. Linked Representation

12
Amity School of Engineering & Technology

Sequential Representation

❖ Adjacency Matrix Representation


❖ Incidence Matrix Representation

13
Amity School of Engineering & Technology

Adjacency Matrix Representation

In this representation, a graph is ▪ Undirected Graph


represented with the help of matrix
of size N x N
where, N is the number of vertices.
It is given by A=[aij]
aij =1if there is an edge from i to j.
aij =0 if there is no edge from i to j. ▪ Directed Graph

14
Amity School of Engineering & Technology

Incidence Matrix Representation

In this representation, a graph • Undirected Graph


is represented with the help of
matrix of size N x E
where, N is the number of
vertices and E is the number of
edges. It is given by T=[aij]
• Directed Graph
For Undirected graph,
aik = ajk = 1iff ek=(vi,vj) else 0
For Undirected graph,
aik = 1; ajk = -1iff ek=(vi,vj) else 0

15
Amity School of Engineering & Technology

Linked List Representation


In this representation, a graph of n nodes is represented
by a node list and Adjacency list for all the nodes
• M[i] is the linked list containing all the nodes adjacent
from node i.
• The nodes in the list M[i] are not in particular order.
Example:

16
Contents:

❑ Basic Concepts of Graph


❑Different types of Graph
❑Representation of Graph

17
Graph Representations in Memory
1. Sequential Representation
2. Linked Representation

18
Sequential Representation
❖ Adjacency Matrix Representation
❖ Incidence Matrix Representation

19
Adjacency Matrix Representation
In this representation, a graph is ▪ Undirected Graph
represented with the help of matrix of size
NxN
where, N is the number of vertices.
It is given by A=[aij]
aij =1if there is an edge from i to j.
▪ Directed Graph
aij =0 if there is no edge from i to j.

20
Incidence Matrix Representation
In this representation, a graph is • Undirected Graph

represented with the help of matrix


of size N x E
where, N is the number of vertices
and E is the number of edges. It is
given by T=[aij]
• Directed Graph
For Undirected graph,
aik = ajk = 1iff ek=(vi,vj) else 0
For Undirected graph,
aik = 1; ajk = -1iff ek=(vi,vj) else 0
21
Linked List Representation
In this representation, a graph of n nodes is represented by a node list
and Adjacency list for all the nodes
• M[i] is the linked list containing all the nodes adjacent from node i.
• The nodes in the list M[i] are not in particular order.
Example:

22
Amity School of Engineering Technology

Graph Traversal
Graph traversal is technique used for searching a vertex in a graph. The
graph traversal is also used to decide the order of vertices to be visit in
the search process.
DFS (Depth First Search)
BFS (Breadth First Search)

• DFS uses a STACK and BFS uses a QUEUE data structure.


• During the execution of our Algorithms, each node N of G will be in one of three states,
called the status on N, as follows:
STATUS=1: (Ready state): The initial state of N
STATUS=2: (Waiting state): The node N is on the STACK or QUEUE, waiting to be
processed.
STATUS=3: (Processed state): The node N has been processed.

23
Amity School of Engineering Technology

BFS (Breadth First Search)

24
Amity School of Engineering Technology

Example
Amity School of Engineering Technology

Example
Amity School of Engineering Technology

Example
Amity School of Engineering Technology

Practice Question
Consider the graph G along with its adjacency list, given in the figure below. Calculate
the order to print all the nodes of the graph starting from node H, by using Breadth
first search (DFS) algorithm.
Amity School of Engineering Technology

DFS (Depth First Search)


Amity School of Engineering Technology

Example
Amity School of Engineering Technology

Example
Amity School of Engineering Technology

Example
Amity School of Engineering Technology

Practice Question
Consider the graph G along with its adjacency list, given in the figure below. Calculate
the order to print all the nodes of the graph starting from node H, by using depth first
search (DFS) algorithm.
PATH Matrix / Reachability Matrix
If G is the simple graph with n-vertices & e –edges then the Path Matrix
of G is the n-square matrix P=(pij) defined as
1, 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗
𝑃𝑖𝑗 = ቊ
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

From the adjacency matrix, we can find path matrix i.e


Bn=A+A2+A3+…+An
Then, replace all non-zero entries of Bn with 1. The resultant matrix
would be the Path matrix.
The entries (i,j) in matrix Bn gives the number of paths of length n or less
from node 𝑣𝑖 𝑡𝑜 𝑣𝑗
34
The entries in 𝐴𝐾 matrix gives the number of path of length K

Example:

Type equation here.

35
Amity School of Engineering Technology

Warshall’s Algorithm [ An efficient way to find path matrix P]


Main idea: Main idea: a path exists between two vertices i, j, iff
• there is an edge from i to j; or
• there is a path from i to j going through vertex 1; or
• there is a path from i to j going through vertex 1 and/or 2; or
• …
• there is a path from i to j going through any of the other vertices
On the kth iteration, the algorithm determine if a path exists between two
vertices i, j using just vertices among 1,…,k allowed as intermediate

36
Amity School of Engineering Technology

Pseudo code for Warshall’s Algorithm


1. Create a Adjacency Matrix
Repeat for i, j = 1, 2, …, n // initialize p
if (A[i,j] = 0 then set p[i, j]=0)
else p[i, j]=1 // End of loop
// Update p
2. Repeat for k = 1, 2, …, n
Repeat for i = 1, 2, …, n
Repeat for j = 1, 2, …, n
p[i, j] = p[i, j] or (p[ i,k] and p[k, j])
Exit

Time efficiency: 𝜽(n3)

Space efficiency: Matrices can be written over their predecessors

37
Amity School of Engineering Technology

𝑅(1) =
Warshall’s Algorithm cont’d 1
1 2
1
3 4
0
5
0 1 0
2 0 0 0 1 1
3 0 0 0 1 0
4 1 1 1 1 1
5 0 0 0 0 0
𝑅(2) = 1 2 3 4 5
1 0 1 1 1 1
Find the path matrix for the above graph
2 0 0 0 1 1
p[i, j] = p[i, j] or (p[i,k] and p[k, j]) 3 0 0 0 1 0
Or 4 1 1 1 1 1
𝑅 (1) 𝑖, 𝑗 = 𝑅 0 𝑖, 𝑗 𝑜𝑟(𝑅 0 𝑖, 𝑘 𝑎𝑛𝑑𝑅 0 𝑘, 𝑗
5 0 0 0 0 0
(0)
𝑅 = A/1 B/2 C/3 D/4 E/5 𝑅(3) = 1 2 3 4 5
A/1 0 1 1 0 0 1 0 1 1 1 1
B/2 0 0 0 1 1 2 0 0 0 1 1
C/3 0 0 0 1 0 3 0 1
0 0 0
D/4 1 0 0 1 1 4 1 1 1 1 1
E/5 0 0 0 0 0 5 38
0 0 0 0 0
Amity School of Engineering Technology

Warshall’s Algorithm cont’d 𝑅(4) = 1 2 3 4 5


1 1 1 1 1 1
2 1 1 1 1 1
3 1 1 1 1 1
4 1 1 1 1 1
5 0 0 0 0 0
𝑅(5) = 1 2 3 4 5
1 1 1 1 1 1
Find the path matrix for the above graph
2 1 1 1 1 1
p[i, j] = p[i, j] or (p[i,k] and p[k, j]) 3 1 1 1 1 1
Or
4 1 1 1 1 1
𝑅 (1) 𝑖, 𝑗 = 𝑅 0 𝑖, 𝑗 𝑜𝑟(𝑅 0 𝑖, 𝑘 𝑎𝑛𝑑𝑅 0 𝑘, 𝑗
5 0 0 0 0 0
𝑅(3) = 1 2 3 4 5
1 0 1 1 1 1
This is the final path matrix
2 0 0 0 1 1
3 0 0 0 1 0
4 1 1 1 1 1
5 0 0 0 0 0 39
Amity School of Engineering Technology

Exercise F C
A

B D
H

G E

Find the path matrix for the above graph


Shortest Path Algorithm Amity School of Engineering Technology
Shortest Path Algorithm Amity School of Engineering Technology
Shortest Path Algorithm Amity School of Engineering Technology
Types of Graphs

Subgraph: A Subgraph S of a graph G is a


graph whose vertex set is a subset of the
vertex set of G and whose edge set is a
subset of the edge set of G.

44
Spanning Tree
• Spanning Tree of a connected undirected graph is a connected subgraph which
has all the vertices covered with minimum possible number of edges.

• It does not have cycles.

• If G is a connected graph with n vertices and m edges, Spanning tree of G must


have n-1 edges.

• If G is a complete graph with n vertices, then total number of spanning trees will
be n(n-2).

45
General properties of spanning tree
• A connected graph G can have more than one spanning tree.

• All possible spanning trees of graph G, have same number of edges and vertices.

• Spanning tree does not have any cycle (loops)​

• Removing one edge from spanning tree will make the graph disconnected i.e.
spanning tree is minimally connected.

• Adding one edge to a spanning tree will create a cycle or loop i.e. spanning tree
is maximally acyclic.
46
Example: Number of Spanning trees for a complete graph

Note: Here n=3, number of spanning trees= 3(3-2)=3


47
Minimum Spanning Tree (MST)
• In a weighted graph, a minimum spanning tree is a spanning tree that has
minimum weight than all other spanning trees of the same graph. In real world
situations, this weight can be measured as distance, congestion, traffic load or
any arbitrary value denoted to the edges.

48
Minimum Spanning trees
There are two important algorithms for finding minimum cost spanning trees.
• Kruskal's Algorithm
• Prim's Algorithm
Both are greedy algorithms.

49
Kruskal’s Algorithm

1. Sort all the edges in non-decreasing order of their weight.


2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed
so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step2 until all the vertices are added in the spanning tree.

50
Kruskal’s Algorithm
Example:

Arrange all edges in their increasing order of weight

51
Kruskal’s Algorithm

Note: costs 4, 5 and 6 will create circuits so will be


discarded
52
Prim’s Algorithm
Step 1: Select any vertex​ as the starting

Step 2: Select the shortest edge connected to that vertex​

Step 3: Select the shortest edge connected to any vertex already connected​

Step 4: Repeat step 3 until all vertices have been connected​

53
Prim’s Algorithm

Example:

Choose any arbitrary node as root node, say S

54
Prim’s Algorithm

55
Practice Questions:
Find minimum Spanning tree for the following using Kruskal’s and Prim’s Algorithm
1. 2.

3. 44.

56
Dijkstra’s Algorithm(Single Source Shortest Path Algorithm)

57
58
Practice Question

59
60

You might also like