SlideShare a Scribd company logo
G R A P H
CONTENTS:
• Introduction to Graph
• Graph Terminologies
• Representation of Graph
• Graph Traversal
Introduction to Graph
• A graph is a non-linear data structure, which
contains a set of vertices (or nodes) and edges
which connect themselves.
• In case of Tree structure, there is a
hierarchical relationship between parent and
child node i.e One parent and many children.
• On the other hand, in case of a graph, the
relationship among the vertices is many
parents to many children.
Introduction to Graph
Mathematically, A graph can be defined as : G = { V, E }
Where V = Set of Vertices and E = Set of Edges
E:g: In the above graph:
Here, V = { A,B,C,D,E}
E = { (A,B),(B,C),(A,D),(B,D),(D,E),(C,E) }
Graph Terminologies
• Undirected Graph
• Directed Graph
• Weighted Graph
• Adjacent Vertices
• Self Loop
• Parallel Edges
• Cyclic Graph
• Acyclic Graph
• Isolated Vertex
• Degree of Vertex
• Pendant Vertex
Undirected Graph
It is a graph such that G = { V,E} where V is set of
vertices and E is the set of unordered pair of vertices.
Here,
V = { A,B,C,D}
E={(A,B),(B,C),(C,D),(A,D),(A,C),(B,D)}
Note: Unordered pair means same edge can be represented
in any combination. For example, the edge (A,B) can be
represented as (B,A).
Directed Graph
It is a graph such that G = {V , E} where V is set of
vertices and E is the set of ordered pair of vertices.
Here,
V = { A,B,C,D}
E={(A,B),(B,C),(C,D),(D,A),(C,A),(B,D)}
Note: If an ordered pair (A,B) is in set E, then there is an
edge directed from A to B (indicated by arrow sign)
Weighted Graph
A graph is termed as weighted if all the edges are
labeled with some weights. A weighted graph can be
undirected or directed.
Adjacent Vertices
In case of directed graph, a vertex X is adjacent to Y, if
there is an edge from X to Y.
Here, Vertex A is adjacent to Vertex B,
Vertex D is adjacent to Vertex A
Self Loop
If there is an edge exist, where starting and end
vertex are same i.e (A,A) is an edge, then it is called a
self loop.
Here, there is a self loop at vertex A
Parallel Edges
If there are more than one edge exist between the
same pair of vertices, then they are known as parallel
edges.
Here, parallel edge exist between Vertex B and Vertex C.
Cyclic & Acyclic Graph
Cycle: If there is path containing edges which start from
vertex A and ends vertex A, then the path is called Cycle.
A graph which has one or more cycles is called Cyclic graph.
Otherwise, it is called Acyclic graph.
Cyclic Graph
Here, three cycles exist.
The path is : (A,B),(B,C),(C,A)
(A,B),(B,D),(D,A)
(B,C),(C,D),(D,A),(A,B)
Isolated Vertex
• A Vertex is isolated, if there is no edge connected
from any other vertex to that vertex.
• Here Vertex E is an isolated vertex as there is no
edge connected to any other vertex of the graph.
Degree of a Vertex ( Undirected Graph)
For undirected graph, the number of edges
connected with a vertex X is degree of that
vertex X.
Here, Degree(A) = 2, Degree(B) = 3
Degree(C) = 2, Degree(D) = 3
Degree of a Vertex ( Directed Graph)
For directed graph, there are two types of
degrees. They are:
1.In-degree : In-degree of a vertex A is the
number of edges incident into A(i.e. the
number of edges ending at A)
2.Out-degree : Out-degree of vertex A is the
number of edges eminating from A (i.e.
number of edges beginning at A )
Degree of a Vertex ( Directed Graph)
In the above directed graph,
Indegree(A) = 2 Outdegree(A) = 1
Indegree(B) = 1 Outdegree(B) = 2
Indegree(C) = 1 Outdegree(C) = 2
Indegree(D) = 2 Outdegree(D) = 1
Pendant Vertex
A vertex X is called pendant if in-degree(X) is 1
and out-degree(X) is 0.
Here, Vertex C, D, E are pendant vertex
Representation of Graph
The graph can be represented using :
1.Sequential representation ( Using Matrix)
 Adjacency Matrix
 Incidence Matrix
2. Linked representation (Using Linked List)
Matrix Representation of Graph
Adjacency Matrix: For representing a graph
“G” with “N” number of vertices, a matrix of
NxN size can be considered.
Assume A[N][N] is matrix representing
Adjacency matrix.
If “i” represents row position and “j”
represents column position, then if there is an
edge from Vi to Vj , Then A[i][j] = 1 else A[i]
[j] = 0 .
Adjacency Matrix: An Example
Represent the following graph in Adjacency
Matrix.
A B C D
A 0 1 0 0
B 0 0 1 1
C 1 0 0 1
D 1 0 0 0
Disadvantage of Adjacency Matrix
The Adjacency matrix has following
disadvantages:
1.It requires n2
space to represent a graph and
also takes n2
time solve most the graph
related problems.
2.It may be difficult to insert and delete nodes,
because the size of matrix need to be
changed.
Matrix Representation of Graph
Incidence Matrix: An Incidence matrix consists
of a row for every vertex and a column for
every edge.
For representing a graph “G” with “N” number
of vertices and “E” number of edges, a matrix
of NxE size can be considered.
Assume A[N][E] is matrix representing
Incidence matrix.
Here, if there is an edge Ek exist from Vi to Vj
then ViEk = 1, VjEk = -1 and other at Ek = 0
Incidence Matrix: An Example
Represent the following graph in Incidence
Matrix.
E1 E2 E3 E4 E5
A 1 0 -1 0 0
B -1 -1 0 0 1
C 0 0 0 -1 -1
D 0 1 1 1 0
Linked Representation of Graph
In the linked representation, an adjacency list
is used to store the Graph into the computer's
memory.
The structure of node will be as follow:
1.VERTEX : It consists the name of the Vertex
2.NEXT : It is a pointer to next vertex
3.LIST : It points to 1st
node of adjacency list
VERTEX NEXT LIST
Linked Representation : An Example
Represent the following graph Using Linked List
VERTEX ADJACENCY LIST
A B
B C,D
C E
D A
E D
Linked Representation : An Example
Graph Traversal
A graph can be traversed in two standard ways. They
are:
1. Depth First Search (DFS)
2. Breadth First Search (BFS)
In DFS, we will take the help of a Stack and in BFS,
we will take the help of a Queue to hold the nodes
for future processing.
During execution of above traversal algorithms, we
consider three states for each vertex :
1. Ready State : It is the initial state of each vertex
2. Waiting State : When the vertex is within Stack or Queue
3. Processed State : When the vertex is visited
Depth First Search (DFS)
• Depth first search (DFS) algorithm starts with the
initial node of the graph G, and then goes to deeper
and deeper until we find the node which has no
children.
• The algorithm, then backtracks from the dead end
towards the most recent node that is yet to be
completely unexplored.
• The data structure which is being used in DFS is
stack.
Depth First Search (DFS) Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3
(processed state)
Step 5: Push on the stack all the neighbours of N that are in the
ready state (whose STATUS = 1) and set their STATUS = 2
(waiting state)
[END OF LOOP – Step 3]
Step 6: EXIT
Depth First Search : An Example
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.
Depth First Search : An Example
Push H onto the stack
STACK : H
Pop the top element of the stack i.e. H, print it and push all the
neighbours of H onto the stack that are is ready state.
Print H
STACK : A
Pop the top element of the stack i.e. A, print it and push all the
neighbours of A onto the stack that are in ready state.
Print A
STACK: B, D
Pop the top element of the stack i.e. D, print it and push all the
neighbours of D onto the stack that are in ready state.
Depth First Search : An Example
Print D
STACK : B, F
Pop the top element of the stack i.e. F, print it and push all
the neighbours of F onto the stack that are in ready state.
Print F
STACK : B
Pop the top of the stack i.e. B and push all the neighbours
Print B
STACK : C
Pop the top of the stack i.e. C and push all the neighbours.
Depth First Search : An Example
Print C
STACK : E, G
Pop the top of the stack i.e. G and push all its neighbours.
Print G
STACK : E
Pop the top of the stack i.e. E and push all its neighbours.
Print E
STACK :
Hence, the stack now becomes empty and all the nodes of
the graph have been traversed.
The printing sequence of the graph will be :
H → A → D → F → B → C → G → E
Breadth First Search (BFS)
• Breadth first search is a graph traversal algorithm
that starts traversing the graph from root node and
explores all the neighbouring nodes.
• Then, it selects the nearest node and explore all the
unexplored nodes. The algorithm follows the same
process for each of the nearest node until it finds the
goal.
• The data structure which is being used in DFS is
queue.
Breadth First Search (BFS) Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Insert the starting node A into queue and set its STATUS = 2
(waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Delete node N and Process it and set its STATUS = 3
(processed state).
Step 5: Delete all the neighbours of N that are in the ready state
(whose STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Breadth First Search : An Example
Consider the following graph G shown below , calculate the
minimum path p from node A to node E. Given that each edge
has a length of 1.
Breadth First Search : An Example
Minimum Path P can be found by applying breadth first search
algorithm that will begin at node A and will end at E.
The algorithm uses a QUEUE which holds all the nodes that are
to be processed.
Lets start examining the graph from Node A.
1. Add A to QUEUE.
QUEUE = {A}
2. Delete the Node A from QUEUE and insert all its neighbours.
Process the node A.
QUEUE = {B, D}
PRINT = {A}
Breadth First Search : An Example
3. Delete the node B from QUEUE and insert all its neighbours.
Process node B.
QUEUE = {D, C, F}
PRINT = {A, B}
4. Delete the node D from QUEUE and insert all its neighbours.
Since F is the only neighbour of it which has been inserted, we
will not insert it again. Process node D.
QUEUE1 = {C, F}
PRINT = { A, B, D}
5. Delete the node C from QUEUE1 and insert all its neighbours.
Process node C.
QUEUE = {F, E, G}
PRINT = {A, B, D, C}
Breadth First Search : An Example
6. Remove F from QUEUE and add all its neighbours. Since all of
its neighbours has already been added, we will not add them
again. Process node F.
QUEUE = {E, G}
PRINT = {A, B, D, C, F}
7. Remove E from QUEUE, all of E's neighbours has already been
added to QUEUE, therefore we will not add them again. All the
nodes are visited and the target node i.e. E is encountered.
QUEUE = {G}
PRINT = {A, B, D, C, F, E}
The minimum path will be A → B → C → E.
Thank You
Ad

Recommended

Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Graphs
Graphs
KomalPaliwal3
 
Data Structure of computer science and technology
Data Structure of computer science and technology
bhaskarsai499
 
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
Data structure Graph PPT ( BFS & DFS ) NOTES
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
Data Structures-Non Linear DataStructures-Graphs
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
 
Data structure computer graphs
Data structure computer graphs
Kumar
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Data structure
Data structure
jagjot singh chopra
 
logic.pptx
logic.pptx
KENNEDY GITHAIGA
 
6. Graphs
6. Graphs
Mandeep Singh
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptx
KENNEDY GITHAIGA
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
babanazar7204
 
graph.pptx
graph.pptx
hijigaf
 
ppt 1.pptx
ppt 1.pptx
ShasidharaniD
 
UNIT III discrete mathematice notes availiable
UNIT III discrete mathematice notes availiable
CHHAYANAYAK5
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptx
ARVIND SARDAR
 
Graph 1
Graph 1
International Islamic University
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Graphs
Graphs
amudha arul
 
Graphs in data structures
Graphs in data structures
Savit Chandra
 
Unit 9 graph
Unit 9 graph
Dabbal Singh Mahara
 
graph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
graphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.ppt
ssuser7b9bda1
 
Graphs aktu notes computer networks.pptx
Graphs aktu notes computer networks.pptx
TalhaKhan528682
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 

More Related Content

Similar to 14. GRAPH in data structures and algorithm.ppt (20)

Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Data structure
Data structure
jagjot singh chopra
 
logic.pptx
logic.pptx
KENNEDY GITHAIGA
 
6. Graphs
6. Graphs
Mandeep Singh
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptx
KENNEDY GITHAIGA
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
babanazar7204
 
graph.pptx
graph.pptx
hijigaf
 
ppt 1.pptx
ppt 1.pptx
ShasidharaniD
 
UNIT III discrete mathematice notes availiable
UNIT III discrete mathematice notes availiable
CHHAYANAYAK5
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptx
ARVIND SARDAR
 
Graph 1
Graph 1
International Islamic University
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Graphs
Graphs
amudha arul
 
Graphs in data structures
Graphs in data structures
Savit Chandra
 
Unit 9 graph
Unit 9 graph
Dabbal Singh Mahara
 
graph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
graphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.ppt
ssuser7b9bda1
 
Graphs aktu notes computer networks.pptx
Graphs aktu notes computer networks.pptx
TalhaKhan528682
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
babanazar7204
 
graph.pptx
graph.pptx
hijigaf
 
UNIT III discrete mathematice notes availiable
UNIT III discrete mathematice notes availiable
CHHAYANAYAK5
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptx
ARVIND SARDAR
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Graphs in data structures
Graphs in data structures
Savit Chandra
 
graphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.ppt
ssuser7b9bda1
 
Graphs aktu notes computer networks.pptx
Graphs aktu notes computer networks.pptx
TalhaKhan528682
 

Recently uploaded (20)

Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
Streamlining CI/CD with FME Flow: A Practical Guide
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
Streamlining CI/CD with FME Flow: A Practical Guide
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Ad

14. GRAPH in data structures and algorithm.ppt

  • 1. G R A P H
  • 2. CONTENTS: • Introduction to Graph • Graph Terminologies • Representation of Graph • Graph Traversal
  • 3. Introduction to Graph • A graph is a non-linear data structure, which contains a set of vertices (or nodes) and edges which connect themselves. • In case of Tree structure, there is a hierarchical relationship between parent and child node i.e One parent and many children. • On the other hand, in case of a graph, the relationship among the vertices is many parents to many children.
  • 4. Introduction to Graph Mathematically, A graph can be defined as : G = { V, E } Where V = Set of Vertices and E = Set of Edges E:g: In the above graph: Here, V = { A,B,C,D,E} E = { (A,B),(B,C),(A,D),(B,D),(D,E),(C,E) }
  • 5. Graph Terminologies • Undirected Graph • Directed Graph • Weighted Graph • Adjacent Vertices • Self Loop • Parallel Edges • Cyclic Graph • Acyclic Graph • Isolated Vertex • Degree of Vertex • Pendant Vertex
  • 6. Undirected Graph It is a graph such that G = { V,E} where V is set of vertices and E is the set of unordered pair of vertices. Here, V = { A,B,C,D} E={(A,B),(B,C),(C,D),(A,D),(A,C),(B,D)} Note: Unordered pair means same edge can be represented in any combination. For example, the edge (A,B) can be represented as (B,A).
  • 7. Directed Graph It is a graph such that G = {V , E} where V is set of vertices and E is the set of ordered pair of vertices. Here, V = { A,B,C,D} E={(A,B),(B,C),(C,D),(D,A),(C,A),(B,D)} Note: If an ordered pair (A,B) is in set E, then there is an edge directed from A to B (indicated by arrow sign)
  • 8. Weighted Graph A graph is termed as weighted if all the edges are labeled with some weights. A weighted graph can be undirected or directed.
  • 9. Adjacent Vertices In case of directed graph, a vertex X is adjacent to Y, if there is an edge from X to Y. Here, Vertex A is adjacent to Vertex B, Vertex D is adjacent to Vertex A
  • 10. Self Loop If there is an edge exist, where starting and end vertex are same i.e (A,A) is an edge, then it is called a self loop. Here, there is a self loop at vertex A
  • 11. Parallel Edges If there are more than one edge exist between the same pair of vertices, then they are known as parallel edges. Here, parallel edge exist between Vertex B and Vertex C.
  • 12. Cyclic & Acyclic Graph Cycle: If there is path containing edges which start from vertex A and ends vertex A, then the path is called Cycle. A graph which has one or more cycles is called Cyclic graph. Otherwise, it is called Acyclic graph. Cyclic Graph Here, three cycles exist. The path is : (A,B),(B,C),(C,A) (A,B),(B,D),(D,A) (B,C),(C,D),(D,A),(A,B)
  • 13. Isolated Vertex • A Vertex is isolated, if there is no edge connected from any other vertex to that vertex. • Here Vertex E is an isolated vertex as there is no edge connected to any other vertex of the graph.
  • 14. Degree of a Vertex ( Undirected Graph) For undirected graph, the number of edges connected with a vertex X is degree of that vertex X. Here, Degree(A) = 2, Degree(B) = 3 Degree(C) = 2, Degree(D) = 3
  • 15. Degree of a Vertex ( Directed Graph) For directed graph, there are two types of degrees. They are: 1.In-degree : In-degree of a vertex A is the number of edges incident into A(i.e. the number of edges ending at A) 2.Out-degree : Out-degree of vertex A is the number of edges eminating from A (i.e. number of edges beginning at A )
  • 16. Degree of a Vertex ( Directed Graph) In the above directed graph, Indegree(A) = 2 Outdegree(A) = 1 Indegree(B) = 1 Outdegree(B) = 2 Indegree(C) = 1 Outdegree(C) = 2 Indegree(D) = 2 Outdegree(D) = 1
  • 17. Pendant Vertex A vertex X is called pendant if in-degree(X) is 1 and out-degree(X) is 0. Here, Vertex C, D, E are pendant vertex
  • 18. Representation of Graph The graph can be represented using : 1.Sequential representation ( Using Matrix)  Adjacency Matrix  Incidence Matrix 2. Linked representation (Using Linked List)
  • 19. Matrix Representation of Graph Adjacency Matrix: For representing a graph “G” with “N” number of vertices, a matrix of NxN size can be considered. Assume A[N][N] is matrix representing Adjacency matrix. If “i” represents row position and “j” represents column position, then if there is an edge from Vi to Vj , Then A[i][j] = 1 else A[i] [j] = 0 .
  • 20. Adjacency Matrix: An Example Represent the following graph in Adjacency Matrix. A B C D A 0 1 0 0 B 0 0 1 1 C 1 0 0 1 D 1 0 0 0
  • 21. Disadvantage of Adjacency Matrix The Adjacency matrix has following disadvantages: 1.It requires n2 space to represent a graph and also takes n2 time solve most the graph related problems. 2.It may be difficult to insert and delete nodes, because the size of matrix need to be changed.
  • 22. Matrix Representation of Graph Incidence Matrix: An Incidence matrix consists of a row for every vertex and a column for every edge. For representing a graph “G” with “N” number of vertices and “E” number of edges, a matrix of NxE size can be considered. Assume A[N][E] is matrix representing Incidence matrix. Here, if there is an edge Ek exist from Vi to Vj then ViEk = 1, VjEk = -1 and other at Ek = 0
  • 23. Incidence Matrix: An Example Represent the following graph in Incidence Matrix. E1 E2 E3 E4 E5 A 1 0 -1 0 0 B -1 -1 0 0 1 C 0 0 0 -1 -1 D 0 1 1 1 0
  • 24. Linked Representation of Graph In the linked representation, an adjacency list is used to store the Graph into the computer's memory. The structure of node will be as follow: 1.VERTEX : It consists the name of the Vertex 2.NEXT : It is a pointer to next vertex 3.LIST : It points to 1st node of adjacency list VERTEX NEXT LIST
  • 25. Linked Representation : An Example Represent the following graph Using Linked List VERTEX ADJACENCY LIST A B B C,D C E D A E D
  • 27. Graph Traversal A graph can be traversed in two standard ways. They are: 1. Depth First Search (DFS) 2. Breadth First Search (BFS) In DFS, we will take the help of a Stack and in BFS, we will take the help of a Queue to hold the nodes for future processing. During execution of above traversal algorithms, we consider three states for each vertex : 1. Ready State : It is the initial state of each vertex 2. Waiting State : When the vertex is within Stack or Queue 3. Processed State : When the vertex is visited
  • 28. Depth First Search (DFS) • Depth first search (DFS) algorithm starts with the initial node of the graph G, and then goes to deeper and deeper until we find the node which has no children. • The algorithm, then backtracks from the dead end towards the most recent node that is yet to be completely unexplored. • The data structure which is being used in DFS is stack.
  • 29. Depth First Search (DFS) Algorithm Step 1: SET STATUS = 1 (ready state) for each node in G Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state) Step 3: Repeat Steps 4 and 5 until STACK is empty Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state) Step 5: Push on the stack all the neighbours of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) [END OF LOOP – Step 3] Step 6: EXIT
  • 30. Depth First Search : An Example 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.
  • 31. Depth First Search : An Example Push H onto the stack STACK : H Pop the top element of the stack i.e. H, print it and push all the neighbours of H onto the stack that are is ready state. Print H STACK : A Pop the top element of the stack i.e. A, print it and push all the neighbours of A onto the stack that are in ready state. Print A STACK: B, D Pop the top element of the stack i.e. D, print it and push all the neighbours of D onto the stack that are in ready state.
  • 32. Depth First Search : An Example Print D STACK : B, F Pop the top element of the stack i.e. F, print it and push all the neighbours of F onto the stack that are in ready state. Print F STACK : B Pop the top of the stack i.e. B and push all the neighbours Print B STACK : C Pop the top of the stack i.e. C and push all the neighbours.
  • 33. Depth First Search : An Example Print C STACK : E, G Pop the top of the stack i.e. G and push all its neighbours. Print G STACK : E Pop the top of the stack i.e. E and push all its neighbours. Print E STACK : Hence, the stack now becomes empty and all the nodes of the graph have been traversed. The printing sequence of the graph will be : H → A → D → F → B → C → G → E
  • 34. Breadth First Search (BFS) • Breadth first search is a graph traversal algorithm that starts traversing the graph from root node and explores all the neighbouring nodes. • Then, it selects the nearest node and explore all the unexplored nodes. The algorithm follows the same process for each of the nearest node until it finds the goal. • The data structure which is being used in DFS is queue.
  • 35. Breadth First Search (BFS) Algorithm Step 1: SET STATUS = 1 (ready state) for each node in G Step 2: Insert the starting node A into queue and set its STATUS = 2 (waiting state) Step 3: Repeat Steps 4 and 5 until QUEUE is empty Step 4: Delete node N and Process it and set its STATUS = 3 (processed state). Step 5: Delete all the neighbours of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) [END OF LOOP] Step 6: EXIT
  • 36. Breadth First Search : An Example Consider the following graph G shown below , calculate the minimum path p from node A to node E. Given that each edge has a length of 1.
  • 37. Breadth First Search : An Example Minimum Path P can be found by applying breadth first search algorithm that will begin at node A and will end at E. The algorithm uses a QUEUE which holds all the nodes that are to be processed. Lets start examining the graph from Node A. 1. Add A to QUEUE. QUEUE = {A} 2. Delete the Node A from QUEUE and insert all its neighbours. Process the node A. QUEUE = {B, D} PRINT = {A}
  • 38. Breadth First Search : An Example 3. Delete the node B from QUEUE and insert all its neighbours. Process node B. QUEUE = {D, C, F} PRINT = {A, B} 4. Delete the node D from QUEUE and insert all its neighbours. Since F is the only neighbour of it which has been inserted, we will not insert it again. Process node D. QUEUE1 = {C, F} PRINT = { A, B, D} 5. Delete the node C from QUEUE1 and insert all its neighbours. Process node C. QUEUE = {F, E, G} PRINT = {A, B, D, C}
  • 39. Breadth First Search : An Example 6. Remove F from QUEUE and add all its neighbours. Since all of its neighbours has already been added, we will not add them again. Process node F. QUEUE = {E, G} PRINT = {A, B, D, C, F} 7. Remove E from QUEUE, all of E's neighbours has already been added to QUEUE, therefore we will not add them again. All the nodes are visited and the target node i.e. E is encountered. QUEUE = {G} PRINT = {A, B, D, C, F, E} The minimum path will be A → B → C → E.