Lecture #16
Course: Data Structure
Instructor: Mohammad Dawood Saddiqi
Date: Wednesday, August 13, 2025
Graph
In the name of Allah
1
Outline
• Introduction
• Graph
• Uses of graph
• Type of Graph
• Basic Concepts
• Parent Node
• Loop edge
• Multiple Edges
• Path & Length of graph
• Graph Representation
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
2
Introduction
• Tree data structures represent one to many relationships.
• In real life we frequently come across problems that can be best
described by many to many relationships.
• Such problems cannot be solved using trees or other data structures.
• To deal with such problems, graph data structures are used.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
3
Graph
• Graph is a non-linear data structure. It is made up of sets of nodes and
lines.
• Nodes are called vertices or points.
• Lines are called edges or arcs.
• Lines are used to connect nodes.
• An edge of the graph is represented as:
e = [u, v]
• 'u' and 'v' denote the start and end nodes of edge 'e', respectively.
• They are also known as the tail and head nodes of edge
• Graphs can he used to represent essentially any relationship.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
4
Uses of graph
• Graphs can be used to represent essentially any relationship.
They are used to study problems in a wide variety of areas
including computer science, electrical engineering, chemistry,
etc.
• For example. The transport networks, the following graph
represents transportation links between four cites. The vertices
represent cities. The edges correspond to the roads that link the
cities.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
5
Herat
Kandahar
Kabul
Jalalabad
Type of Graph
• There are different types of graph some of them are mentioned
here.
1. Directed graph
2. Undirected graph
3. Weighted graph
4. Complete graph
5. Regular graph
6. Isomorphic graphs
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
6
(1)- Directed Graph
• A directed graph has edges that have direction. A directed graph
is also called a digraph. E.g.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
7
A B
D E
C
(2)- Undirected graph
• An undirected graph has edges that have no direction. An
undirected graph is also called an undigraph.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
8
A B
D E
C
(3)- Weighted graph
• A graph which has a weight or number associated with each
edge is called weighted graph. Weighted of an edge is sometime
called its cost.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
9
B
A
C
20
08
15
(4)- Complete Graph
• A graph is said to be complete graph if there is an edge between every
pair of vertices.
• or A graph is said to be complete one, if each node of the graph is
adjacent with every other node.
• If a node has n nodes, then each node has (n-1) total degree. i.e. it is
connected with (n-1) nodes, so the number of edges can be calculated
by the formula e=n(n-1)/2; where n is the no of nodes.
• e= n(n-1)/2
• e= 4(4-1)/2
• e=12/2
• e= 6
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
10
A B
C D
(5)- Regular Graph
• A graph in which each node has equal total degree is called regular
graph.
Total- degree: The degree of a vertex is the number of edges incident to
that vertex
For Directed Graph
In-degree: the in-degree of a vertex v is the number of edges that have v as the head
Out –degree: the out-degree of a vertex v is the number of edges that have v as the
tail
About vertices ‘A’
Total Degree: 4
In-Degree:2
Out-Degree:2
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
11
B
A
C
(6)- Isomorphic Graph
• When two graphs have same behavior in term of graphical
properties are called isomorphic graph.
• Condition for isomorphism.
• Total number of nodes in two graphs must be same.
• Total number of edges in two graphs must be same.
• Same number of in-degree and out-degree of the two graphs
• Direction of edges must be same.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
12
E
D
C
A
B
C
B
A
D
E
Basic Concepts:
• Source & Sink Nodes: The node that has a positive out-degree
but zero in-degree is called source node.
The node that that has zero out-degree but positive in-degree is
called sink node.
‘A’ is source node.
‘C’ is sink node.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
13
B
A
C
20
08
15
Parent Node
• A node is called parent node if it has total degree of one. In below
graph A is parent node
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
14
A
B
C
D
E
Loop edge
• An edge is said to be a loop edge if the same node is its tail and
head. E.g.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
15
B
A
C
Multiple Edges
• A graph is said to be have multiple edges if it has more than one
edge have the same tail and head nodes.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
16
B
A
C
Path & Length of graph
• A list of nodes of a graph where each node has an edge from it to
the next node is called path. It is written as a sequence of nodes,
A,B,C.
• A path which repeats no Node, is known as simple path.
• A path which begins and end at same node is called cycle.
• Moving from one node to other node is called walk.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
17
Graph Representation
• There are two ways to represent a graph in computer memory.
1. Adjacency matrix
2. Adjacency list
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
18
Adjacency Matrix
• An adjacency matrix is a table, represents the edges between vertices of
the graph. In case of weight graph the adjacency matrix represent the
weights of the edges.
• If an edge e=(u,v) exists b/w two vertices u and v, it is represented in the
matrix by 1. absence of an edge is represented by 0.
• The adjacency matrix consist only 0’s and 1’s.
• If there are N vertices in the graph, the adjacency matrix will be an N x
N matrix.
• The columns of the matrix represent the “to” end of edge, while rows of
the matrix represent “from” start of edge.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
19
Example
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
20
A B C D
A 0 1 0 1
B 0 1 0 1
C 0 1 0 0
D 1 0 1 0
A
D
B
C
Adjacency List
• In this method the graph is represented in computer memory by two link
list. One Node list and second Adjacency List.
• In Node List we will store the Nodes while in Adjacency List we will
store the adjacent nodes of a node.
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
21
Example
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University
22
A D
E
C
B
Summary
• Tree data structures represent one to many relationships.
• In real life we frequently come across problems that can be best
described by many to many relationships.
• Graph is a non-linear data structure. It is made up of sets of nodes
and lines.
• There are different types of graph some of them are mentioned
here. Directed graph, Undirected graph, Weighted graph,
Complete graph, Regular graph, Isomorphic graphs.
• There are two ways to represent a graph in computer memory.
1. Adjacency matrix
2. Adjacency list
Wednesday, August 13, 2025
Instructor: Mohammad Dawood Saddiqi - Ghalib
University 23
Wednesday, August 13, 2025
The End
Thank You
24
Instructor: Mohammad Dawood Saddiqi - Ghalib
University

Data Structure DSL- Lec #16 - Graph.pptx

  • 1.
    Lecture #16 Course: DataStructure Instructor: Mohammad Dawood Saddiqi Date: Wednesday, August 13, 2025 Graph In the name of Allah 1
  • 2.
    Outline • Introduction • Graph •Uses of graph • Type of Graph • Basic Concepts • Parent Node • Loop edge • Multiple Edges • Path & Length of graph • Graph Representation Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 2
  • 3.
    Introduction • Tree datastructures represent one to many relationships. • In real life we frequently come across problems that can be best described by many to many relationships. • Such problems cannot be solved using trees or other data structures. • To deal with such problems, graph data structures are used. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 3
  • 4.
    Graph • Graph isa non-linear data structure. It is made up of sets of nodes and lines. • Nodes are called vertices or points. • Lines are called edges or arcs. • Lines are used to connect nodes. • An edge of the graph is represented as: e = [u, v] • 'u' and 'v' denote the start and end nodes of edge 'e', respectively. • They are also known as the tail and head nodes of edge • Graphs can he used to represent essentially any relationship. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 4
  • 5.
    Uses of graph •Graphs can be used to represent essentially any relationship. They are used to study problems in a wide variety of areas including computer science, electrical engineering, chemistry, etc. • For example. The transport networks, the following graph represents transportation links between four cites. The vertices represent cities. The edges correspond to the roads that link the cities. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 5 Herat Kandahar Kabul Jalalabad
  • 6.
    Type of Graph •There are different types of graph some of them are mentioned here. 1. Directed graph 2. Undirected graph 3. Weighted graph 4. Complete graph 5. Regular graph 6. Isomorphic graphs Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 6
  • 7.
    (1)- Directed Graph •A directed graph has edges that have direction. A directed graph is also called a digraph. E.g. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 7 A B D E C
  • 8.
    (2)- Undirected graph •An undirected graph has edges that have no direction. An undirected graph is also called an undigraph. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 8 A B D E C
  • 9.
    (3)- Weighted graph •A graph which has a weight or number associated with each edge is called weighted graph. Weighted of an edge is sometime called its cost. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 9 B A C 20 08 15
  • 10.
    (4)- Complete Graph •A graph is said to be complete graph if there is an edge between every pair of vertices. • or A graph is said to be complete one, if each node of the graph is adjacent with every other node. • If a node has n nodes, then each node has (n-1) total degree. i.e. it is connected with (n-1) nodes, so the number of edges can be calculated by the formula e=n(n-1)/2; where n is the no of nodes. • e= n(n-1)/2 • e= 4(4-1)/2 • e=12/2 • e= 6 Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 10 A B C D
  • 11.
    (5)- Regular Graph •A graph in which each node has equal total degree is called regular graph. Total- degree: The degree of a vertex is the number of edges incident to that vertex For Directed Graph In-degree: the in-degree of a vertex v is the number of edges that have v as the head Out –degree: the out-degree of a vertex v is the number of edges that have v as the tail About vertices ‘A’ Total Degree: 4 In-Degree:2 Out-Degree:2 Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 11 B A C
  • 12.
    (6)- Isomorphic Graph •When two graphs have same behavior in term of graphical properties are called isomorphic graph. • Condition for isomorphism. • Total number of nodes in two graphs must be same. • Total number of edges in two graphs must be same. • Same number of in-degree and out-degree of the two graphs • Direction of edges must be same. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 12 E D C A B C B A D E
  • 13.
    Basic Concepts: • Source& Sink Nodes: The node that has a positive out-degree but zero in-degree is called source node. The node that that has zero out-degree but positive in-degree is called sink node. ‘A’ is source node. ‘C’ is sink node. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 13 B A C 20 08 15
  • 14.
    Parent Node • Anode is called parent node if it has total degree of one. In below graph A is parent node Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 14 A B C D E
  • 15.
    Loop edge • Anedge is said to be a loop edge if the same node is its tail and head. E.g. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 15 B A C
  • 16.
    Multiple Edges • Agraph is said to be have multiple edges if it has more than one edge have the same tail and head nodes. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 16 B A C
  • 17.
    Path & Lengthof graph • A list of nodes of a graph where each node has an edge from it to the next node is called path. It is written as a sequence of nodes, A,B,C. • A path which repeats no Node, is known as simple path. • A path which begins and end at same node is called cycle. • Moving from one node to other node is called walk. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 17
  • 18.
    Graph Representation • Thereare two ways to represent a graph in computer memory. 1. Adjacency matrix 2. Adjacency list Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 18
  • 19.
    Adjacency Matrix • Anadjacency matrix is a table, represents the edges between vertices of the graph. In case of weight graph the adjacency matrix represent the weights of the edges. • If an edge e=(u,v) exists b/w two vertices u and v, it is represented in the matrix by 1. absence of an edge is represented by 0. • The adjacency matrix consist only 0’s and 1’s. • If there are N vertices in the graph, the adjacency matrix will be an N x N matrix. • The columns of the matrix represent the “to” end of edge, while rows of the matrix represent “from” start of edge. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 19
  • 20.
    Example Wednesday, August 13,2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 20 A B C D A 0 1 0 1 B 0 1 0 1 C 0 1 0 0 D 1 0 1 0 A D B C
  • 21.
    Adjacency List • Inthis method the graph is represented in computer memory by two link list. One Node list and second Adjacency List. • In Node List we will store the Nodes while in Adjacency List we will store the adjacent nodes of a node. Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 21
  • 22.
    Example Wednesday, August 13,2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 22 A D E C B
  • 23.
    Summary • Tree datastructures represent one to many relationships. • In real life we frequently come across problems that can be best described by many to many relationships. • Graph is a non-linear data structure. It is made up of sets of nodes and lines. • There are different types of graph some of them are mentioned here. Directed graph, Undirected graph, Weighted graph, Complete graph, Regular graph, Isomorphic graphs. • There are two ways to represent a graph in computer memory. 1. Adjacency matrix 2. Adjacency list Wednesday, August 13, 2025 Instructor: Mohammad Dawood Saddiqi - Ghalib University 23
  • 24.
    Wednesday, August 13,2025 The End Thank You 24 Instructor: Mohammad Dawood Saddiqi - Ghalib University