SlideShare a Scribd company logo
Graphs
By
Dr. P. Amudha
Associate Professor
School of Engineering
Avinashilingam Institute
Overview
 Graphs
◦ undirected graphs
◦ directed graphs
 Graph Traversals
◦ depth-first (recursive vs. stack)
◦ breadth-first (queue)
Introduction to graph
 Non-linear data structure used in
solving games and puzzles
 No root node as in trees
 Complex Data structure
 Graph Theory : study of graphs in CS
Contd.,
• Graphs are used to represent network
structures
Computer network Route map of Coimbatore
• Graphs unlike trees, do not represent
hierarchical relationship
Applications of graph
 Analysis of electrical circuits
 Finding shortest routes
 Project Planning
 Social sciences
 Cybernetics
 Linguistics ………..
What is a graph?
 A data structure that consists of a set of
nodes (vertices) and a set of edges that
relate the nodes to each other
 The set of edges describes relationships
among the vertices
Formal definition of graphs
 A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of
vertices
E(G): a set of edges (pairs of
vertices)
 Where,
V = { v1,v2,v3,…vn}
E = {e1,e2,e3,…en}
8
Examples of graphs
•Vertices drawn with circles
•Edges drawn with lines
 Trees are special cases of graphs!!
Trees vs graphs
Types of Graphs
Basically two types:
 Directed Graphs
 Undirected graphs
11 Lecture 7: Graphs
Directed Graphs
 A directed graph is a graph which
consists of directed edges, where
each edge in E is unidirectional.
 Also referred as digraph.
 Each edge connects two vertices,
called the source and target
 If (v,u) is a directed edge, then (v,u) 
(u,v)
12 Lecture 7: Graphs
Directed Graphs
 A directed graph is a finite set of
vertices and a finite set of edges.
 Each edge connects two vertices,
called the source and target; the edge
connects the source to the target
(order is significant)
13 Lecture 7: Graphs
Directed Graphs
V1
V3
V2
Useful for modelling directional
phenomena, like geographical
travel, or game-playing where
moves are irreversible
Examples: modelling states in a
chess game, or Tic-Tac-Toe
Arrows are used to represent
the edges; arrows start at the
source vertex and point to the
target vertex
(V1,V2)  (V2,V1)
Example:
Directed graphs (cont.)
E(Graph2) = {(1,3), (3,1), (11,1), (5,9), (9,11), (9,9),
(5,7)
Undirected Graphs
 An undirected graph is a graph, which
consists of undirected edges.
 If (v,u) is an undirected edge then
(v,u)=(u,v)
 Each edge connects two vertices
 The order of the connection is
unimportant
V1 V2
V3
(V1,V2) = (V2,V1)
L23 16
Simple Graphs
Different purposes require different types
of graphs.
Eg: Suppose a local computer network
◦ Is bidirectional (undirected)
◦ Has no loops (no “self-communication”)
◦ Has unique connections between computers
L23 17
Multigraphs
 If computers are connected via
internet, there may be several routes
to choose from each connection.
 Depending on traffic, one route could
be better than another.
 It allows multiple edges, but still no
self-loops
0 2
1
(a)
2
1
0
3
(b)
Example of a graph with feedback loops and a multigraph
self edge multigraph:
multiple occurrences
of the same edge
Graph terminologies
 Adjacent Nodes/ Adjacent vertex
 Path
 Length
 Cycle
 Degree
 Weighted graph
 Complete graph
 Subgraph
 Strongly Connected graph
Adjacent nodes and path
 Two nodes are adjacent if they are connected
by an edge
 A path in a graph is a sequence of vertices,
each adjacent to the next
A D
EB
FC
adjacent (A) ={B,C,D}
adjacent (B) ={A,C,E}
adjacent (C) ={A,B,D,E}
adjacent (D) ={A,C,E,F}
adjacent (E) ={B,C,D,F}
adjacent (F) ={E,D}
Path 1: A -> B -> E -> F
Path 2: A -> C -> E -> F
Path 3: A -> D -> F
Path 4: A -> C -> D -> F
Length of the path and Cycle
 Length of the path is the number of edges
on the path, which is equal to N-1, where N
is the number of vertices.
 Cycle in a graph is a path in which first and
last vertices are the same.
V1 V2 V3 V1
 A graph which has cycles is referred to as
cyclic graph
V3 V2
V1 Length of the path V1 to V2
is 2.
ie., (V1,V2), (V2,V3)
Degree
 The number of edges incident on a
vertex V determines its degree.
 In undirected graph, degree(V) = number
of edges incident at V.
 In directed graph, it is categorized into
indegree and outdegree.
 Indegree (V) = no. of edges entering into
the vertex V
 outdegree (V) = no. of edges exiting from
the vertex V
Contd.,
Vertex Degree
A
B
C
D
3
2
2
3
A B
C D
Table 1:Degree of each vertex of an undirected graph
A
DC
B Vertex Indegree Outdegree
A
B
C
D
2
1
2
1
1
2
1
2
Table 2: indegree and outdegree of each vertex of directed graph
Acyclic graph
 A directed graph which has no cycle is
referred to as acyclic graph.
 It is abbreviated as DAG (Directed
Acyclic Graph)
A
B C
D
E
Weighted Graph
 A graph is said to be weighted graph if every edge
in the graph is assigned some weight or value.
 The weight of the edge is a positive value the may
be representing the cost or distance between the
vertices.
 For example, in a road map represented as a
graph, weight of the edges can be based on the
distance between the cities or functions of distance
and traffic in the road
Complete and Incomplete Graph
0
1 2
3
0
1 2
3 4 5 6
G1
G2
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
complete graph incomplete graph
A complete graph is a graph in which every vertex is
directly connected to every other vertex
 What is the number of edges in a
complete directed graph with N
vertices?
N * (N-1)
Here in this example,
N = 4
No. of edges = 4 * 3
= 12
Contd..
 What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
Here in this example,
N = 5
No. of edges = 5 * 4 / 2
= 10
Contd.,
Subgraph
 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)
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G2
0
1 2
3
G1
0
1
2
G2
Subgraphs of G1 and G2
Connected Graph
 A graph is called connected if there is
a path from any vertex to any other
vertex.
A B
C D
Connected Graph
A
C D
B
Unconnected Graph
Graph representation
 A graph is a mathematical structure
and it must be represented in some
kind of data structure.
 Two ways to represent the graph:
 Adjacency matrix
 Adjacency list
Array representation
 Simple way to represent a graph is to
use 2D array.
 This is known as adjacency matrix
representation.
Adjacency matrix A for a graph G =(V,E)
with n vertices is an n x n matrix such
that,
Aij = 1, if there is an edge Vi to Vj
Aij = 0, if there is no edge
Contd.,
 Adjacency matrix for undirected graph
 Adjacency matrix for directed graph
V1
V4V3
V2 V1 V2 V3 V4
V1 0 1 1 0
V2 1 0 1 1
V3 1 1 0 1
V4 0 1 1 0
V1
V4V3
V2 V1 V2 V3 V4
V1 0 1 1 0
V2 0 0 0 1
V3 0 1 0 0
V4 0 0 1 0
Contd.,
 Adjacency matrix for weighted graph
 Aij = Cij if there exists an edge from Vi to Vj
 Aij = 0 if there is no edge and i = j
 If there is no edge for i to j, assume C[i,j] =
V1
V4V3
V2
V1 V2 V3 V4
V1 0 3 9 
V2  0  7
V3  1 0 
V4  1 8 0
3
7
8
9 1
Adjacency list representation
 In this representation, graph is stored
as linked structure.
 Linked list representation of undirected graph:
A
DC
B
A
B
C
D
B
A
C
A
C Null
C
B
B Null
D Null
D Null
Contd.,
 Linked list representation of directed graph:
A
DC
B
A
B
C
D
B
D Null
C Null
A Null
D Null
Topological Sort
 A topological sort is a linear ordering of vertices in a
directed acyclic graph such that if there is a path from
Vi to Vj, then Vj appears after Vi in the linear ordering.
 For example, a directed edge between (C1,C2)
indicates that C1 must be completed before
attempting to course C2.
10th
Arts
Eng
g
Med
PG
12th
Dip
Topological Order Algorithm
Algorithm Topological order (Graph G)
1. Find any vertex with no incoming
edges.
2. If such vertex is found, remove it
along with its edges from the graph.
3. Repeat step 1 for all the vertices.
END Topological order;
Implementing topological sort
1. Find the indegree for every vertex.
2. Enqueue the vertices whose indegree is ‘0’ on
the empty queue.
3. Dequeue the vertex V and decrement the
indegree’s of all its adjacent vertices.
4. Enqueue the vertex on the queue, if the indegree
falls to ‘0’.
5. Repeat from step 3 until the queue becomes
empty.
6. The topological ordering is the order in which the
vertices are dequeued.
Example
a b c d
a 0 1 1 0
b 0 0 1 1
c 0 0 0 1
d 0 0 0 0
a
cb
d
Adjacency matrix
The figure states that a precedes b, c; b precedes c, d and so on.
Step 1: Number of 1’s present in each column of adjacency matrix
represents the indegree of the corresponding vertex.
(ie.,) indegree(a) = 0 indegree(b) = 1
indegree(c) = 2 indegree(d) = 2
Step 2: Enqueue the vertex whose indegree is ‘0’.
vertex a is ‘0’, so place it on the queue.
Graph implementation
 Array-based implementation
◦ A 1D array is used to represent the vertices
◦ A 2D array (adjacency matrix) is used to
represent the edges
Array-based implementation
Graph implementation (cont.)
 Linked-list implementation
◦ A 1D array is used to represent the vertices
◦ A list is used for each vertex v which contains
the vertices which are adjacent from v
(adjacency list)
Linked-list implementation

More Related Content

What's hot

Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
full subtractor
full subtractorfull subtractor
full subtractor
Amodh Pandey
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
hafsa komal
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
Yosuke Mizutani
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
LikhithaGunturi
 
Digital Electronics Question Bank
Digital Electronics Question BankDigital Electronics Question Bank
Digital Electronics Question Bank
Mathankumar S
 
Data structures using c
Data structures using cData structures using c
Data structures using c
Prof. Dr. K. Adisesha
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Graph theory
Graph theoryGraph theory
Graph theory
AparnaKumari31
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
hmnasim15
 
Floyd Warshall Algorithm
Floyd Warshall Algorithm Floyd Warshall Algorithm
Floyd Warshall Algorithm
Imamul Kadir
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
AJAL A J
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithm
A. S. M. Shafi
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
ishwarya516
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
Archana Gopinath
 

What's hot (20)

Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
full subtractor
full subtractorfull subtractor
full subtractor
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
 
Digital Electronics Question Bank
Digital Electronics Question BankDigital Electronics Question Bank
Digital Electronics Question Bank
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Floyd Warshall Algorithm
Floyd Warshall Algorithm Floyd Warshall Algorithm
Floyd Warshall Algorithm
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithm
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 

Similar to Graphs

Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Saurabh Kaushik
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
shashankbhadouria4
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas
 
Graph
GraphGraph
Graph representation
Graph representationGraph representation
Graph representation
DEEPIKA T
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
Prasanna David
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
ChristianKapsales1
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
Prof Ansari
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
miki304759
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
graph theory
graph theorygraph theory
graph theory
Shashank Singh
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
SnehilKeshari
 
Graphs.pdf
Graphs.pdfGraphs.pdf
Graphs.pdf
pubggaming58982
 
chapter6.PPT
chapter6.PPTchapter6.PPT
chapter6.PPT
krish448427
 
Dsa.PPT
Dsa.PPTDsa.PPT
Dsa.PPT
boltkat
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
satvikkushwaha1
 

Similar to Graphs (20)

Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
 
Graph
GraphGraph
Graph
 
Graph representation
Graph representationGraph representation
Graph representation
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
graph theory
graph theorygraph theory
graph theory
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
 
Graphs.pdf
Graphs.pdfGraphs.pdf
Graphs.pdf
 
Graph theory
Graph theoryGraph theory
Graph theory
 
chapter6.PPT
chapter6.PPTchapter6.PPT
chapter6.PPT
 
Dsa.PPT
Dsa.PPTDsa.PPT
Dsa.PPT
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 

Recently uploaded

addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 

Graphs

  • 1. Graphs By Dr. P. Amudha Associate Professor School of Engineering Avinashilingam Institute
  • 2. Overview  Graphs ◦ undirected graphs ◦ directed graphs  Graph Traversals ◦ depth-first (recursive vs. stack) ◦ breadth-first (queue)
  • 3. Introduction to graph  Non-linear data structure used in solving games and puzzles  No root node as in trees  Complex Data structure  Graph Theory : study of graphs in CS
  • 4. Contd., • Graphs are used to represent network structures Computer network Route map of Coimbatore • Graphs unlike trees, do not represent hierarchical relationship
  • 5. Applications of graph  Analysis of electrical circuits  Finding shortest routes  Project Planning  Social sciences  Cybernetics  Linguistics ………..
  • 6. What is a graph?  A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other  The set of edges describes relationships among the vertices
  • 7. Formal definition of graphs  A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)  Where, V = { v1,v2,v3,…vn} E = {e1,e2,e3,…en}
  • 8. 8 Examples of graphs •Vertices drawn with circles •Edges drawn with lines
  • 9.  Trees are special cases of graphs!! Trees vs graphs
  • 10. Types of Graphs Basically two types:  Directed Graphs  Undirected graphs
  • 11. 11 Lecture 7: Graphs Directed Graphs  A directed graph is a graph which consists of directed edges, where each edge in E is unidirectional.  Also referred as digraph.  Each edge connects two vertices, called the source and target  If (v,u) is a directed edge, then (v,u)  (u,v)
  • 12. 12 Lecture 7: Graphs Directed Graphs  A directed graph is a finite set of vertices and a finite set of edges.  Each edge connects two vertices, called the source and target; the edge connects the source to the target (order is significant)
  • 13. 13 Lecture 7: Graphs Directed Graphs V1 V3 V2 Useful for modelling directional phenomena, like geographical travel, or game-playing where moves are irreversible Examples: modelling states in a chess game, or Tic-Tac-Toe Arrows are used to represent the edges; arrows start at the source vertex and point to the target vertex (V1,V2)  (V2,V1)
  • 14. Example: Directed graphs (cont.) E(Graph2) = {(1,3), (3,1), (11,1), (5,9), (9,11), (9,9), (5,7)
  • 15. Undirected Graphs  An undirected graph is a graph, which consists of undirected edges.  If (v,u) is an undirected edge then (v,u)=(u,v)  Each edge connects two vertices  The order of the connection is unimportant V1 V2 V3 (V1,V2) = (V2,V1)
  • 16. L23 16 Simple Graphs Different purposes require different types of graphs. Eg: Suppose a local computer network ◦ Is bidirectional (undirected) ◦ Has no loops (no “self-communication”) ◦ Has unique connections between computers
  • 17. L23 17 Multigraphs  If computers are connected via internet, there may be several routes to choose from each connection.  Depending on traffic, one route could be better than another.  It allows multiple edges, but still no self-loops
  • 18. 0 2 1 (a) 2 1 0 3 (b) Example of a graph with feedback loops and a multigraph self edge multigraph: multiple occurrences of the same edge
  • 19. Graph terminologies  Adjacent Nodes/ Adjacent vertex  Path  Length  Cycle  Degree  Weighted graph  Complete graph  Subgraph  Strongly Connected graph
  • 20. Adjacent nodes and path  Two nodes are adjacent if they are connected by an edge  A path in a graph is a sequence of vertices, each adjacent to the next A D EB FC adjacent (A) ={B,C,D} adjacent (B) ={A,C,E} adjacent (C) ={A,B,D,E} adjacent (D) ={A,C,E,F} adjacent (E) ={B,C,D,F} adjacent (F) ={E,D} Path 1: A -> B -> E -> F Path 2: A -> C -> E -> F Path 3: A -> D -> F Path 4: A -> C -> D -> F
  • 21. Length of the path and Cycle  Length of the path is the number of edges on the path, which is equal to N-1, where N is the number of vertices.  Cycle in a graph is a path in which first and last vertices are the same. V1 V2 V3 V1  A graph which has cycles is referred to as cyclic graph V3 V2 V1 Length of the path V1 to V2 is 2. ie., (V1,V2), (V2,V3)
  • 22. Degree  The number of edges incident on a vertex V determines its degree.  In undirected graph, degree(V) = number of edges incident at V.  In directed graph, it is categorized into indegree and outdegree.  Indegree (V) = no. of edges entering into the vertex V  outdegree (V) = no. of edges exiting from the vertex V
  • 23. Contd., Vertex Degree A B C D 3 2 2 3 A B C D Table 1:Degree of each vertex of an undirected graph A DC B Vertex Indegree Outdegree A B C D 2 1 2 1 1 2 1 2 Table 2: indegree and outdegree of each vertex of directed graph
  • 24. Acyclic graph  A directed graph which has no cycle is referred to as acyclic graph.  It is abbreviated as DAG (Directed Acyclic Graph) A B C D E
  • 25. Weighted Graph  A graph is said to be weighted graph if every edge in the graph is assigned some weight or value.  The weight of the edge is a positive value the may be representing the cost or distance between the vertices.  For example, in a road map represented as a graph, weight of the edges can be based on the distance between the cities or functions of distance and traffic in the road
  • 26. Complete and Incomplete Graph 0 1 2 3 0 1 2 3 4 5 6 G1 G2 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} complete graph incomplete graph A complete graph is a graph in which every vertex is directly connected to every other vertex
  • 27.  What is the number of edges in a complete directed graph with N vertices? N * (N-1) Here in this example, N = 4 No. of edges = 4 * 3 = 12 Contd..
  • 28.  What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2 Here in this example, N = 5 No. of edges = 5 * 4 / 2 = 10 Contd.,
  • 29. Subgraph  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)
  • 30. 0 0 1 2 3 1 2 0 1 2 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G2 0 1 2 3 G1 0 1 2 G2 Subgraphs of G1 and G2
  • 31. Connected Graph  A graph is called connected if there is a path from any vertex to any other vertex. A B C D Connected Graph A C D B Unconnected Graph
  • 32. Graph representation  A graph is a mathematical structure and it must be represented in some kind of data structure.  Two ways to represent the graph:  Adjacency matrix  Adjacency list
  • 33. Array representation  Simple way to represent a graph is to use 2D array.  This is known as adjacency matrix representation. Adjacency matrix A for a graph G =(V,E) with n vertices is an n x n matrix such that, Aij = 1, if there is an edge Vi to Vj Aij = 0, if there is no edge
  • 34. Contd.,  Adjacency matrix for undirected graph  Adjacency matrix for directed graph V1 V4V3 V2 V1 V2 V3 V4 V1 0 1 1 0 V2 1 0 1 1 V3 1 1 0 1 V4 0 1 1 0 V1 V4V3 V2 V1 V2 V3 V4 V1 0 1 1 0 V2 0 0 0 1 V3 0 1 0 0 V4 0 0 1 0
  • 35. Contd.,  Adjacency matrix for weighted graph  Aij = Cij if there exists an edge from Vi to Vj  Aij = 0 if there is no edge and i = j  If there is no edge for i to j, assume C[i,j] = V1 V4V3 V2 V1 V2 V3 V4 V1 0 3 9  V2  0  7 V3  1 0  V4  1 8 0 3 7 8 9 1
  • 36. Adjacency list representation  In this representation, graph is stored as linked structure.  Linked list representation of undirected graph: A DC B A B C D B A C A C Null C B B Null D Null D Null
  • 37. Contd.,  Linked list representation of directed graph: A DC B A B C D B D Null C Null A Null D Null
  • 38. Topological Sort  A topological sort is a linear ordering of vertices in a directed acyclic graph such that if there is a path from Vi to Vj, then Vj appears after Vi in the linear ordering.  For example, a directed edge between (C1,C2) indicates that C1 must be completed before attempting to course C2. 10th Arts Eng g Med PG 12th Dip
  • 39. Topological Order Algorithm Algorithm Topological order (Graph G) 1. Find any vertex with no incoming edges. 2. If such vertex is found, remove it along with its edges from the graph. 3. Repeat step 1 for all the vertices. END Topological order;
  • 40. Implementing topological sort 1. Find the indegree for every vertex. 2. Enqueue the vertices whose indegree is ‘0’ on the empty queue. 3. Dequeue the vertex V and decrement the indegree’s of all its adjacent vertices. 4. Enqueue the vertex on the queue, if the indegree falls to ‘0’. 5. Repeat from step 3 until the queue becomes empty. 6. The topological ordering is the order in which the vertices are dequeued.
  • 41. Example a b c d a 0 1 1 0 b 0 0 1 1 c 0 0 0 1 d 0 0 0 0 a cb d Adjacency matrix The figure states that a precedes b, c; b precedes c, d and so on. Step 1: Number of 1’s present in each column of adjacency matrix represents the indegree of the corresponding vertex. (ie.,) indegree(a) = 0 indegree(b) = 1 indegree(c) = 2 indegree(d) = 2 Step 2: Enqueue the vertex whose indegree is ‘0’. vertex a is ‘0’, so place it on the queue.
  • 42. Graph implementation  Array-based implementation ◦ A 1D array is used to represent the vertices ◦ A 2D array (adjacency matrix) is used to represent the edges
  • 44. Graph implementation (cont.)  Linked-list implementation ◦ A 1D array is used to represent the vertices ◦ A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)