SlideShare a Scribd company logo
1 of 72
Download to read offline
Dr.M.Usha
Assistant Professor,
Department of CSE
Velammal engineering College
Velammal Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
INTRODUCTION TO GRAPH
GRAPH
 A Graph is a non-linear data structure consisting of nodes and edges.
 The nodes are sometimes also referred to as vertices and the edges are lines
or arcs that connect any two nodes in the graph.
 Generally, a graph G is represented as G=(V,E) where V is set of vertices and E
is set of edges.
 In this above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E
= {01, 12, 23, 34, 04, 14, 13}.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 2
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 3
APPLICATIONS
 Graphs are used to solve many real-life problems.
 Graphs are used to represent networks.
 The networks may include paths in a city or telephone network or circuit
network.
 Graphs are also used in social networks like linkedIn, Facebook.
 For example, in Facebook, each person is represented with a vertex(or node).
 Each node is a structure and contains information like person id, name,
gender, locale etc.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 4
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 5
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 6
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 7
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 8
GRAPH TERMINOLOGIES
 A path is a sequence of vertices such that there is an edge
from each vertex to its successor.
 A path is simple if each vertex is distinct/A path with no
repeated vertices is called a simple path.
 A circuit is a path in which the terminal vertex coincides
with the initial vertex.(Vertices may repeat but edges are
not allowed to repeat.)
 Cycle: A circuit that doesn't repeat vertices is called a
cycle.(Neither vertices (except possibly the starting and
ending vertices) are allowed to repeat, Nor edges are
allowed to repeat.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 9
0-1-2-3-0 - CYCLE 0-1-2-4-2-3-0(CIRCUIT)
 Self loop: If there is an edge whose starting and
end vertices are same, that is, (vi, vj) is an edge,
then it is called a self loop.
 Adjacent Vertices
Two vertices are said to be adjacent if there is an
edge (arc) connecting them.
 Adjacent Edges
Adjacent edges are edges that share a common
vertex.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 10
Degree of the Node
 A degree of a node is the number of edges that are connected with that node.
A node with degree 0 is called as isolated node.
 In degree: Number of edges entering a node
 • Out degree: Number of edges leaving a node
 • Degree = Indegree + Outdegree
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 11
TYPES OF GRAPH
 Directed Graph (or) Digraph
 Directed graph is a graph which consists of directed edges, where each edge
in E is unidirectional.
 It is also referred as Digraph. If (v,w) is a directed edge then (v,w) # (w,v)
 Undirected Graph
 An undirected graph is a graph, which consists of undirected edges. If (v,w) is
an undirected edge, then (v,w)=(w,v)
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 12
Connected and Disconnected
 A graph G is said to be connected if there exists a path between every pair
of vertices.
 A connected graph is the one in which some path exists between every two
vertices (u, v) in V.
 There are no isolated nodes in connected graph.
 UNCONNECTED/DisConnected GRAPH: A graph is said as unconnected graph
if there exist any 2 unconnected components.
 Example: • H1 and H2 are connected • H3 is disconnected
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 13
CONT..
 Weighted Graph
 A graph is said to be weighted graph if every edge in the graph is assigned a
weight or value. It can be directed or undirected graph.
 Complete Graph
 A complete graph is a graph in which there is an direct edge between every pair of
vertices.
 A complete graph with n vertices will have n(n-1)/2 edges.
 There is a path from every vertex to every other vertex.
 All complete graphs are connected graphs, but not all connected graphs are
complete graphs.
 A complete digraph is a strongly connected graph.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 14
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 15
CONT..
 Strongly Connected Graph
 If there is a path from every vertex to every other vertex in a directed graph
then it is said to be strongly connected graph.
 Weakly Connected Graph:
 If there does not exist a path from one vertex to another vertex then it is said
to be a weakly connected graph.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 16
 Regular Graph
 A graph G is said to be regular, if all its vertices have the same degree. In a
graph, if the degree of each vertex is ‘k’, then the graph is called a ‘k-regular
graph’.
 In the following graphs, all the vertices have the same degree. So these
graphs are called regular graphs.
 In both the graphs, all the vertices have degree 2. They are called 2-Regular
Graphs.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 17
CYCLIC AND ACYCLIC GRPH
 Cyclic Graph
 A graph with at least one cycle is called a cyclic graph.
 Example
 In the above example graph, we have two cycles a-b-c-d-a and c-f-g-e-c.
Hence it is called a cyclic graph
 Acyclic Graph
 A graph with no cycles is called an acyclic graph.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 18
CYCLIC AND ACYCLIC GRPH
 Acyclic Graph
 A directional graph which has no cycles is referred to as acyclic graph. It is
abbreviated as DAG (Directioinal Acyclic Graph).
 If there is a path containing one or more edges which starts from a vertex vi
and terminates into the same vertex then the path is known as a cycle.
 If a graph(digraph) does not have any cycle then it is called acyclic graph.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 19
GRAPH REPRESENTATION
 Graph data structure is represented using following
representations...
 Adjacency Matrix
 Incidence Matrix
 Adjacency List
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 20
ADJACENCY MATRIX
 The adjacency matrix A for a graph G = (V,E) with n vertices, is an n* n matrix
of bits ,
 such that A ij = 1 , if there is an edge from vi to vj and
 Aij = 0, if there is no such edge
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 21
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 22
ADJACENCY LIST
 A graph containing m vertices and n edges can be represented using a linked list, referred
to as adjacency list.
 The number of vertices in a graph forms a singly linked list.
 Each vertex have a separate linked list, with nodes equal to the number of edges connected
from the corresponding vertex..
 Each nodes has at least 2 fields: VERTEX and LINK.
 The VERTEX fields contain the indices of the vertices adjacent to vertex i.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 23
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 24
Incidence Matrix
 A graph containing m vertices and n edges can be represented by a
matrix with m rows and n columns.
 The matrix formed by storing 1 in the ith row and jth column
corresponding to the matrix, if there exists a ith vertex, connected to
one end of the jth edge, and 0, if there is no ith vertex, connected to
any end of the jth edge of the graph, such a matrix is referred as an
incidence matrix.
 IncMat [i] [j] = 1,if there is an edge Ejfrom vertex Vi
 = 0, otherwise
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 25
EXAMPLE
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 26
Graph Traversals
 A graph traversal is a systematic way of visiting
the nodes in a specific order.
 There are 2 types of graph traversals namely,
 Breadth First Search(BFS)
 Depth First Search(DFS)
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 27
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 28
Depth first search
 Visit the first node initially, and then find the unvisited
node which is adjacent to the first node, is visited and a
DFS is initiated from the adjacent node (considering it as
the first node).
 If all the adjacent nodes have been visited, backtrack to
the last node visited, and find another adjacent node and
again initiate the DFS from adjacent node.
 This traversal continues until all nodes have been visited
once.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 29
Steps to Implement DFS
 1. Select the start vertex
 2. Visit the vertex ( place 1)
 3. Push the vertex on the stack
 4. pop the vertex
 5. find the adjacent vertices and find any one of the
unvisited adjacent vertex
 6. repeat from step 2 to 5 until the stack becomes empty
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 30
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 31
Mark the vertex A as visited. push the vertex A into the stack and pop it out.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 32
 Step 5
 Since all the vertices of D are visited, and all the vertices in the graph are visited,
DFS ends.
 The output is the order in which the vertices are popped out. Output: A C B D.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 33
EXAMPLE 2:
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 34
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 35
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 36
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 37
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 38
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 39
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 40
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 41
A-B-C-E-D-F-G
Applications of DFS
 To check whether the undirected graph is connected or not
 To check if the connected undirected graph is bi-connected or not
 To check whether the directed graph is a-cyclic or not
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 42
BFS (Breadth First Search)
 Breadth First Search (BFS) of a graph G starts from an
unvisited vertex u.
 Then all unvisited vertices vi adjacent to u are visited and
then all unvisited vertices wj adjacent to vi are visited
and so on.
 The traversal terminates when there are no more nodes to
visit.
 BFS uses a queue data structure to keep track of the
order of the nodes whose adjacent nodes are to be
visited.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 43
Steps to Implement BFS
 1. Select the start vertex and mark it as visited (i.e) place the value 1
 2. Enqueue the START vertex.
 3. Dequeue the vertex.
 4. Find all adjacent unvisited vertices of the dequeued vertex.
 5. Mark all unvisited adjacent vertices as visited.
 6. Enqueue all adjacent vertices.
 7. Repeat from step 3 to step 6 until the queue becomes empty
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 44
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 45
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 46
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 47
Pseudo Code For BFS
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 48
Applications of BFS
 1. To find the shortest path from a vertex s to a vertex v in an
unweighted graph
 2. To find the length of such a path
 3. To find out if a graph contains cycles
 4. To construct a BFS tree/forest from a graph
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 49
EXAMPLE 2
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 50
Front, rear
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 51
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 52
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 53
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 54
A-D-B-E-C-F-G
Comparison between DFS and BFS
DFS BFS
DFS visit nodes of graph depth wise. It
visits nodes until reach a leaf or a node
which doesn’t have non-visited nodes.
BFS visit nodes level by level in Graph.
Usually implemented using a stack data
structure.
Usually implemented using a queue data
structure.
Generally requires less memory than BFS. Generally requires more memory than DFS.
Not Optimal for finding the shortest
distance.
optimal for finding the shortest distance.
DFS is better when target is far from
source.
BFS is better when target is closer to
source.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 55
CONT..
DFS BFS
DFS is more suitable for game or puzzle
problems. We make a decision, then
explore all paths through this decision.
And if this decision leads to win
situation, we stop.
BFS considers all neighbors first and
therefore not suitable for decision
making trees used in games or puzzles.
Time Complexity of BFS = O(V+E) where
V is vertices and E is edges.
Time Complexity of DFS is also O(V+E)
where V is vertices and E is edges.
Some Applications:
Finding all connected components in a
graph.
Finding the shortest path between two
nodes.
Finding all nodes within one connected
component.
Testing a graph for bipartiteness.
Some Applications:
Topological Sorting.
Finding connected components.
Solving puzzles such as maze.
Finding strongly connected
components.
Finding articulation points (cut
vertices) of the graph.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 56
TOPOLOGICAL SORTING
 It is a linear ordering of vertices in a directed a-cyclic graph, such that if
there is a path from Vi to Vj, then Vj appears after Vi in the linear ordering.
 Topological sort is not possible if the graph has a cycle.
 Procedure
 1. Find the indegree for every vertex
 2. Place the vertices whose indegree is zero on the empty queue
 3. Dequeue one vertex at a time from the queue and decrement the indegree
of all its adjacent vertices
 4. Enqueue a vertex to the queue if its indegree falls to zero
 5. Repeat from step step 3 unitl the queue becomes empty
 The topological ordering is the order in which the vertices are dequeued.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 57
EXAMPLE
 Find the number of different topological orderings possible for the given
graph.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 58
 Step-02:
 Vertex-1 has the least in-degree.
 So, remove vertex-1 and its associated edges.
 Now, update the in-degree of other vertices.
 Step-03:
 There are two vertices with the least in-degree. So, following 2 cases are possible-
 In case-01,
 Remove vertex-2 and its associated edges.
 Then, update the in-degree of other vertices.
 In case-02,
 Remove vertex-3 and its associated edges.
 Then, update the in-degree of other vertices.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 59
 Step-04:
 Now, the above two cases are continued separately in the similar manner.
 In case-01,
 Remove vertex-3 since it has the least in-degree.
 Then, update the in-degree of other vertices.
 In case-02,
 Remove vertex-2 since it has the least in-degree.
 Then, update the in-degree of other vertices.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 60
 Step-05:
 In case-01,
 Remove vertex-4 since it has the least in-degree.
 Then, update the in-degree of other vertices.
 In case-02,
 Remove vertex-4 since it has the least in-degree.
 Then, update the in-degree of other vertices.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 61
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 62
 Step-06:
 In case-01,There are 2 vertices with the least in-degree.
 So, 2 cases are possible.
 Any of the two vertices may be taken first.
 Same is with case-02.
Applications of Topological Sort
 Few important applications of topological sort are-
 Scheduling jobs from the given dependencies among jobs
 Instruction Scheduling
 Determining the order of compilation tasks to perform in makefiles
 Data Serialization
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 63
Pseudo Code For Topological Sort
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 64
EXAMPLE
 Step 1
 Find the Indegree of vertices 1,2,3,4,5,6.
 Indegree of a vertex is the number of edges entering into the vertex. Indegree of
vertex 1 is 0, vertex 2 is 0, vertex 3 is 1, vertex 4 is 3, vertex 5 is 1, vertex 6 is 3.
 Step 2
 enqueue() the vertices with Indegree 0. Therefore enqueue() vertices 1 and 2.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 65
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 66
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 67
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 68
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 69
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 70
Applications:
 Topological Sorting is mainly used for scheduling jobs from the given
dependencies among jobs.
 The jobs are represented by vertices, and there is an edge from x to y if job x
must be completed before job y can be started
 For example,in constructing a building,the basement must be completed
before the first floor,which must be completed before the second floor and so
on.
 A topological sort gives an order in which we should perform the jobs.
 In computer science, applications of this type arise in instruction scheduling,
ordering of formula cell evaluation when recomputing formula values in
spreadsheets,
 Determining the order of compilation tasks to perform in makefiles
 Data Serialization
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 71
THANK YOU
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 72

More Related Content

What's hot

Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Graph theory and life
Graph theory and lifeGraph theory and life
Graph theory and lifeMilan Joshi
 
Graph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsGraph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsAshikur Rahman
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Graph theory
Graph theoryGraph theory
Graph theoryKumar
 
Graph: Euler path and Euler circuit
Graph: Euler path and Euler circuitGraph: Euler path and Euler circuit
Graph: Euler path and Euler circuitLiwayway Memije-Cruz
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theoryChuckie Balbuena
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory IntroductionMANISH T I
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
 

What's hot (20)

Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph theory
Graph theory Graph theory
Graph theory
 
Graph theory and life
Graph theory and lifeGraph theory and life
Graph theory and life
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsGraph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphs
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graph
GraphGraph
Graph
 
Graph: Euler path and Euler circuit
Graph: Euler path and Euler circuitGraph: Euler path and Euler circuit
Graph: Euler path and Euler circuit
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
graph theory
graph theory graph theory
graph theory
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
graph theory
graph theorygraph theory
graph theory
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
d
dd
d
 
Graph theory
Graph theoryGraph theory
Graph theory
 

Similar to Graph

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 ColouringSaurabh Kaushik
 
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATIONFREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATIONcscpconf
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2showslidedump
 
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.pptxmiki304759
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructuresLikhithaGunturi
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
An analysis between exact and approximate algorithms for the k-center proble...
An analysis between exact and approximate algorithms for the  k-center proble...An analysis between exact and approximate algorithms for the  k-center proble...
An analysis between exact and approximate algorithms for the k-center proble...IJECEIAES
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 

Similar to Graph (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
 
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATIONFREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
Graphs
GraphsGraphs
Graphs
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
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
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
 
Graph therory
Graph theroryGraph therory
Graph therory
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Graphs
GraphsGraphs
Graphs
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
An analysis between exact and approximate algorithms for the k-center proble...
An analysis between exact and approximate algorithms for the  k-center proble...An analysis between exact and approximate algorithms for the  k-center proble...
An analysis between exact and approximate algorithms for the k-center proble...
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 

Recently uploaded

cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabusViolet Violet
 
Power System electrical and electronics .pptx
Power System electrical and electronics .pptxPower System electrical and electronics .pptx
Power System electrical and electronics .pptxMUKULKUMAR210
 
nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxjasonsedano2
 
Graphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesGraphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesDIPIKA83
 
Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Bahzad5
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
CSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptxCSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptxssusera0771e
 
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....santhyamuthu1
 
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratoryدليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide LaboratoryBahzad5
 
cme397 surface engineering unit 5 part A questions and answers
cme397 surface engineering unit 5 part A questions and answerscme397 surface engineering unit 5 part A questions and answers
cme397 surface engineering unit 5 part A questions and answerskarthi keyan
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxrealme6igamerr
 
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docxSUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docxNaveenVerma126
 
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.pptOracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.pptDheerajKashnyal
 
specification estimation and valuation of a building
specification estimation and valuation of a buildingspecification estimation and valuation of a building
specification estimation and valuation of a buildingswethasekhar5
 
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfsdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfJulia Kaye
 
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...Amil baba
 
Technical Management of cement industry.pdf
Technical Management of cement industry.pdfTechnical Management of cement industry.pdf
Technical Management of cement industry.pdfMadan Karki
 

Recently uploaded (20)

cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabus
 
Power System electrical and electronics .pptx
Power System electrical and electronics .pptxPower System electrical and electronics .pptx
Power System electrical and electronics .pptx
 
nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptx
 
Graphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesGraphics Primitives and CG Display Devices
Graphics Primitives and CG Display Devices
 
Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
CSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptxCSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptx
 
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
 
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratoryدليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
 
cme397 surface engineering unit 5 part A questions and answers
cme397 surface engineering unit 5 part A questions and answerscme397 surface engineering unit 5 part A questions and answers
cme397 surface engineering unit 5 part A questions and answers
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
 
Litature Review: Research Paper work for Engineering
Litature Review: Research Paper work for EngineeringLitature Review: Research Paper work for Engineering
Litature Review: Research Paper work for Engineering
 
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docxSUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
 
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.pptOracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
 
Lecture 4 .pdf
Lecture 4                              .pdfLecture 4                              .pdf
Lecture 4 .pdf
 
specification estimation and valuation of a building
specification estimation and valuation of a buildingspecification estimation and valuation of a building
specification estimation and valuation of a building
 
Présentation IIRB 2024 Chloe Dufrane.pdf
Présentation IIRB 2024 Chloe Dufrane.pdfPrésentation IIRB 2024 Chloe Dufrane.pdf
Présentation IIRB 2024 Chloe Dufrane.pdf
 
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfsdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
 
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
 
Technical Management of cement industry.pdf
Technical Management of cement industry.pdfTechnical Management of cement industry.pdf
Technical Management of cement industry.pdf
 

Graph

  • 1. Dr.M.Usha Assistant Professor, Department of CSE Velammal engineering College Velammal Engineering College (An Autonomous Institution, Affiliated to Anna University, Chennai) (Accredited by NAAC & NBA) INTRODUCTION TO GRAPH
  • 2. GRAPH  A Graph is a non-linear data structure consisting of nodes and edges.  The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph.  Generally, a graph G is represented as G=(V,E) where V is set of vertices and E is set of edges.  In this above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34, 04, 14, 13}. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 2
  • 4. APPLICATIONS  Graphs are used to solve many real-life problems.  Graphs are used to represent networks.  The networks may include paths in a city or telephone network or circuit network.  Graphs are also used in social networks like linkedIn, Facebook.  For example, in Facebook, each person is represented with a vertex(or node).  Each node is a structure and contains information like person id, name, gender, locale etc. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 4
  • 9. GRAPH TERMINOLOGIES  A path is a sequence of vertices such that there is an edge from each vertex to its successor.  A path is simple if each vertex is distinct/A path with no repeated vertices is called a simple path.  A circuit is a path in which the terminal vertex coincides with the initial vertex.(Vertices may repeat but edges are not allowed to repeat.)  Cycle: A circuit that doesn't repeat vertices is called a cycle.(Neither vertices (except possibly the starting and ending vertices) are allowed to repeat, Nor edges are allowed to repeat. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 9 0-1-2-3-0 - CYCLE 0-1-2-4-2-3-0(CIRCUIT)
  • 10.  Self loop: If there is an edge whose starting and end vertices are same, that is, (vi, vj) is an edge, then it is called a self loop.  Adjacent Vertices Two vertices are said to be adjacent if there is an edge (arc) connecting them.  Adjacent Edges Adjacent edges are edges that share a common vertex. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 10
  • 11. Degree of the Node  A degree of a node is the number of edges that are connected with that node. A node with degree 0 is called as isolated node.  In degree: Number of edges entering a node  • Out degree: Number of edges leaving a node  • Degree = Indegree + Outdegree 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 11
  • 12. TYPES OF GRAPH  Directed Graph (or) Digraph  Directed graph is a graph which consists of directed edges, where each edge in E is unidirectional.  It is also referred as Digraph. If (v,w) is a directed edge then (v,w) # (w,v)  Undirected Graph  An undirected graph is a graph, which consists of undirected edges. If (v,w) is an undirected edge, then (v,w)=(w,v) 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 12
  • 13. Connected and Disconnected  A graph G is said to be connected if there exists a path between every pair of vertices.  A connected graph is the one in which some path exists between every two vertices (u, v) in V.  There are no isolated nodes in connected graph.  UNCONNECTED/DisConnected GRAPH: A graph is said as unconnected graph if there exist any 2 unconnected components.  Example: • H1 and H2 are connected • H3 is disconnected 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 13
  • 14. CONT..  Weighted Graph  A graph is said to be weighted graph if every edge in the graph is assigned a weight or value. It can be directed or undirected graph.  Complete Graph  A complete graph is a graph in which there is an direct edge between every pair of vertices.  A complete graph with n vertices will have n(n-1)/2 edges.  There is a path from every vertex to every other vertex.  All complete graphs are connected graphs, but not all connected graphs are complete graphs.  A complete digraph is a strongly connected graph. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 14
  • 16. CONT..  Strongly Connected Graph  If there is a path from every vertex to every other vertex in a directed graph then it is said to be strongly connected graph.  Weakly Connected Graph:  If there does not exist a path from one vertex to another vertex then it is said to be a weakly connected graph. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 16
  • 17.  Regular Graph  A graph G is said to be regular, if all its vertices have the same degree. In a graph, if the degree of each vertex is ‘k’, then the graph is called a ‘k-regular graph’.  In the following graphs, all the vertices have the same degree. So these graphs are called regular graphs.  In both the graphs, all the vertices have degree 2. They are called 2-Regular Graphs. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 17
  • 18. CYCLIC AND ACYCLIC GRPH  Cyclic Graph  A graph with at least one cycle is called a cyclic graph.  Example  In the above example graph, we have two cycles a-b-c-d-a and c-f-g-e-c. Hence it is called a cyclic graph  Acyclic Graph  A graph with no cycles is called an acyclic graph. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 18
  • 19. CYCLIC AND ACYCLIC GRPH  Acyclic Graph  A directional graph which has no cycles is referred to as acyclic graph. It is abbreviated as DAG (Directioinal Acyclic Graph).  If there is a path containing one or more edges which starts from a vertex vi and terminates into the same vertex then the path is known as a cycle.  If a graph(digraph) does not have any cycle then it is called acyclic graph. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 19
  • 20. GRAPH REPRESENTATION  Graph data structure is represented using following representations...  Adjacency Matrix  Incidence Matrix  Adjacency List 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 20
  • 21. ADJACENCY MATRIX  The adjacency matrix A for a graph G = (V,E) with n vertices, is an n* n matrix of bits ,  such that A ij = 1 , if there is an edge from vi to vj and  Aij = 0, if there is no such edge 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 21
  • 23. ADJACENCY LIST  A graph containing m vertices and n edges can be represented using a linked list, referred to as adjacency list.  The number of vertices in a graph forms a singly linked list.  Each vertex have a separate linked list, with nodes equal to the number of edges connected from the corresponding vertex..  Each nodes has at least 2 fields: VERTEX and LINK.  The VERTEX fields contain the indices of the vertices adjacent to vertex i. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 23
  • 25. Incidence Matrix  A graph containing m vertices and n edges can be represented by a matrix with m rows and n columns.  The matrix formed by storing 1 in the ith row and jth column corresponding to the matrix, if there exists a ith vertex, connected to one end of the jth edge, and 0, if there is no ith vertex, connected to any end of the jth edge of the graph, such a matrix is referred as an incidence matrix.  IncMat [i] [j] = 1,if there is an edge Ejfrom vertex Vi  = 0, otherwise 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 25
  • 27. Graph Traversals  A graph traversal is a systematic way of visiting the nodes in a specific order.  There are 2 types of graph traversals namely,  Breadth First Search(BFS)  Depth First Search(DFS) 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 27
  • 29. Depth first search  Visit the first node initially, and then find the unvisited node which is adjacent to the first node, is visited and a DFS is initiated from the adjacent node (considering it as the first node).  If all the adjacent nodes have been visited, backtrack to the last node visited, and find another adjacent node and again initiate the DFS from adjacent node.  This traversal continues until all nodes have been visited once. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 29
  • 30. Steps to Implement DFS  1. Select the start vertex  2. Visit the vertex ( place 1)  3. Push the vertex on the stack  4. pop the vertex  5. find the adjacent vertices and find any one of the unvisited adjacent vertex  6. repeat from step 2 to 5 until the stack becomes empty 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 30
  • 31. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 31 Mark the vertex A as visited. push the vertex A into the stack and pop it out.
  • 33.  Step 5  Since all the vertices of D are visited, and all the vertices in the graph are visited, DFS ends.  The output is the order in which the vertices are popped out. Output: A C B D. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 33
  • 34. EXAMPLE 2: 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 34
  • 41. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 41 A-B-C-E-D-F-G
  • 42. Applications of DFS  To check whether the undirected graph is connected or not  To check if the connected undirected graph is bi-connected or not  To check whether the directed graph is a-cyclic or not 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 42
  • 43. BFS (Breadth First Search)  Breadth First Search (BFS) of a graph G starts from an unvisited vertex u.  Then all unvisited vertices vi adjacent to u are visited and then all unvisited vertices wj adjacent to vi are visited and so on.  The traversal terminates when there are no more nodes to visit.  BFS uses a queue data structure to keep track of the order of the nodes whose adjacent nodes are to be visited. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 43
  • 44. Steps to Implement BFS  1. Select the start vertex and mark it as visited (i.e) place the value 1  2. Enqueue the START vertex.  3. Dequeue the vertex.  4. Find all adjacent unvisited vertices of the dequeued vertex.  5. Mark all unvisited adjacent vertices as visited.  6. Enqueue all adjacent vertices.  7. Repeat from step 3 to step 6 until the queue becomes empty 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 44
  • 48. Pseudo Code For BFS 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 48
  • 49. Applications of BFS  1. To find the shortest path from a vertex s to a vertex v in an unweighted graph  2. To find the length of such a path  3. To find out if a graph contains cycles  4. To construct a BFS tree/forest from a graph 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 49
  • 50. EXAMPLE 2 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 50 Front, rear
  • 54. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 54 A-D-B-E-C-F-G
  • 55. Comparison between DFS and BFS DFS BFS DFS visit nodes of graph depth wise. It visits nodes until reach a leaf or a node which doesn’t have non-visited nodes. BFS visit nodes level by level in Graph. Usually implemented using a stack data structure. Usually implemented using a queue data structure. Generally requires less memory than BFS. Generally requires more memory than DFS. Not Optimal for finding the shortest distance. optimal for finding the shortest distance. DFS is better when target is far from source. BFS is better when target is closer to source. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 55
  • 56. CONT.. DFS BFS DFS is more suitable for game or puzzle problems. We make a decision, then explore all paths through this decision. And if this decision leads to win situation, we stop. BFS considers all neighbors first and therefore not suitable for decision making trees used in games or puzzles. Time Complexity of BFS = O(V+E) where V is vertices and E is edges. Time Complexity of DFS is also O(V+E) where V is vertices and E is edges. Some Applications: Finding all connected components in a graph. Finding the shortest path between two nodes. Finding all nodes within one connected component. Testing a graph for bipartiteness. Some Applications: Topological Sorting. Finding connected components. Solving puzzles such as maze. Finding strongly connected components. Finding articulation points (cut vertices) of the graph. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 56
  • 57. TOPOLOGICAL SORTING  It is a linear ordering of vertices in a directed a-cyclic graph, such that if there is a path from Vi to Vj, then Vj appears after Vi in the linear ordering.  Topological sort is not possible if the graph has a cycle.  Procedure  1. Find the indegree for every vertex  2. Place the vertices whose indegree is zero on the empty queue  3. Dequeue one vertex at a time from the queue and decrement the indegree of all its adjacent vertices  4. Enqueue a vertex to the queue if its indegree falls to zero  5. Repeat from step step 3 unitl the queue becomes empty  The topological ordering is the order in which the vertices are dequeued. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 57
  • 58. EXAMPLE  Find the number of different topological orderings possible for the given graph. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 58
  • 59.  Step-02:  Vertex-1 has the least in-degree.  So, remove vertex-1 and its associated edges.  Now, update the in-degree of other vertices.  Step-03:  There are two vertices with the least in-degree. So, following 2 cases are possible-  In case-01,  Remove vertex-2 and its associated edges.  Then, update the in-degree of other vertices.  In case-02,  Remove vertex-3 and its associated edges.  Then, update the in-degree of other vertices. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 59
  • 60.  Step-04:  Now, the above two cases are continued separately in the similar manner.  In case-01,  Remove vertex-3 since it has the least in-degree.  Then, update the in-degree of other vertices.  In case-02,  Remove vertex-2 since it has the least in-degree.  Then, update the in-degree of other vertices. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 60
  • 61.  Step-05:  In case-01,  Remove vertex-4 since it has the least in-degree.  Then, update the in-degree of other vertices.  In case-02,  Remove vertex-4 since it has the least in-degree.  Then, update the in-degree of other vertices. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 61
  • 62. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 62  Step-06:  In case-01,There are 2 vertices with the least in-degree.  So, 2 cases are possible.  Any of the two vertices may be taken first.  Same is with case-02.
  • 63. Applications of Topological Sort  Few important applications of topological sort are-  Scheduling jobs from the given dependencies among jobs  Instruction Scheduling  Determining the order of compilation tasks to perform in makefiles  Data Serialization 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 63
  • 64. Pseudo Code For Topological Sort 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 64
  • 65. EXAMPLE  Step 1  Find the Indegree of vertices 1,2,3,4,5,6.  Indegree of a vertex is the number of edges entering into the vertex. Indegree of vertex 1 is 0, vertex 2 is 0, vertex 3 is 1, vertex 4 is 3, vertex 5 is 1, vertex 6 is 3.  Step 2  enqueue() the vertices with Indegree 0. Therefore enqueue() vertices 1 and 2. 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 65
  • 71. Applications:  Topological Sorting is mainly used for scheduling jobs from the given dependencies among jobs.  The jobs are represented by vertices, and there is an edge from x to y if job x must be completed before job y can be started  For example,in constructing a building,the basement must be completed before the first floor,which must be completed before the second floor and so on.  A topological sort gives an order in which we should perform the jobs.  In computer science, applications of this type arise in instruction scheduling, ordering of formula cell evaluation when recomputing formula values in spreadsheets,  Determining the order of compilation tasks to perform in makefiles  Data Serialization 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 71
  • 72. THANK YOU 9/8/2021 VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 72