SlideShare a Scribd company logo
1 of 27
By:
A.Bhuvaneshwari,M.SC(cs)
• 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. More formally a Graph can be
defined as, In the above graph,
• V = {a, b, c, d, e}
• E = {ab, ac, bd, cd, de}
• Depth First Traversal (or Search) for a graph is similar
to Depth First Traversal of a tree. The only catch here is,
unlike trees, graphs may contain cycles, a node may be
visited twice. To avoid processing a node more than
once, use a boolean visited array.
• Depth First Search (DFS) algorithm traverses a graph in
a depthward motion and uses a stack to remember to get
the next vertex to start a search, when a dead end occurs
in any iteration. (It will pop up all the vertices from the
stack, which do not have adjacent vertices.
• Undirected graph
• Biconnectivity
• Euler circuits
• Directed graph
• Findind strong components
• Depth First Search (DFS) algorithm traverses a graph in
a depthward motion and uses a stack to remember to get
the next vertex to start a search, when a dead end occurs
in any iteration.
• As in the example given above, DFS algorithm traverses
from S to A to D to G to E to B first, then to F and lastly to
C. It employs the following rules.
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as
visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a vertex
from the stack. (It will pop up all the vertices from the
stack, which do not have adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the stack is
empty.
• Initialize the stack.
• Mark S as visited and put it onto the stack.Explore any
unvisited adjacent node from S. We have three nodes
and we can pick any of them. For this example, we shall
take the node in an alphabetical order.
• Mark A as visited and put it onto the stack. Explore any
unvisited adjacent node from A. Both S and D are
adjacent to A but we are concerned for unvisited nodes
only.
• Visit D and mark it as visited and put onto the stack.
Here, we have B and C nodes, which are adjacent
to D and both are unvisited. However, we shall again
choose in an alphabetical order.
• We choose B, mark it as visited and put onto the stack.
Here B does not have any unvisited adjacent node. So,
we pop B from the stack.
• We check the stack top for return to the previous node
and check if it has any unvisited nodes. Here, we
find D to be on the top of the stack.
• Only unvisited adjacent node is from D is C now. So we
visit C, mark it as visited and put it onto the stack.
As C does not have any unvisited adjacent node so we
keep popping the stack until we find a node that has an
unvisited adjacent node. In this case, there's none and
we keep popping until the stack is empty.
• Depth-first search, or DFS, is a way to traverse the
graph. Initially it allows visiting vertices of the graph only,
but there are hundreds of algorithms for graphs, which
are based on DFS. Therefore, understanding the
principles of depth-first search is quite important to move
ahead into the graph theory. The principle of the
algorithm is quite simple: to go forward (in depth) while
there is such possibility, otherwise to backtrack.
• For most algorithms boolean classification unvisited /
visited is quite enough, but we show general case here.
• Initially all vertices are white (unvisited). DFS starts in
arbitrary vertex and runs as follows:
• Mark vertex u as gray (visited).
• For each edge (u, v), where u is white, run depth-first
search for u recursively.
• Mark vertex u as black and backtrack to the parent.
• Example. Traverse a graph shown below, using DFS.
Start from a vertex with number 1.
• Source graph.
• Mark a vertex 1 as gray.
• There is an edge (1, 4) and a vertex 4 is unvisited. Go
there.
• Mark the vertex 4 as gray.
• There is an edge (4, 2) and vertex a 2 is unvisited. Go
there.
• Mark the vertex 2 as gray
• There is an edge (2, 5) and a vertex 5 is unvisited. Go
there.
• Mark the vertex 5 as gray.
• There is an edge (5, 3) and a vertex 3 is unvisited. Go
there.
• Mark the vertex 3 as gray.
• There are no ways to go from the vertex 3. Mark it as
• There are no ways to go from the vertex 5. Mark it as black
and backtrack to the vertex 2.
• There are no more edges, adjacent to vertex 2. Mark it as
black and backtrack to the vertex 4.
• There is an edge (4, 5), but the vertex 5 is black.
• There are no more edges, adjacent to the vertex 4. Mark it as
black and backtrack to the vertex 1.
• There are no more edges, adjacent to the vertex 1. Mark it as
black. DFS is over.
• As you can see from the example, DFS doesn't go through all
edges. The vertices and edges, which depth-first search has
visited is a tree. This tree contains all vertices of the graph (if it
is connected) and is called graph spanning tree. This tree
exactly corresponds to the recursive calls of DFS.
• If a graph is disconnected, DFS won't visit all of its vertices.
For details, see finding connected components algorithm.
• An undirected graph is said to be a biconnected graph, if
there are two vertex-disjoint paths between any two
vertices are present. In other words, we can say that
there is a cycle between any two vertices.
• We can say that a graph G is a bi-connected graph if it is
connected, and there are no articulation points or cut
vertex are present in the graph.
• To solve this problem, we will use the DFS traversal.
Using DFS, we will try to find if there is any articulation
point is present or not. We also check whether all
vertices are visited by the DFS or not, if not we can say
that the graph is not connected.
• Input and Output
• Input: The adjacency matrix of the graph.
• 0 1 1 1 0
• 1 0 1 0 0
• 1 1 0 0 1
• 1 0 0 0 1
• 0 0 1 1 0
• Output: The Graph is a biconnected graph.
• Euler Graph - A connected graph G is called an Euler
graph, if there is a closed trail which includes every edge
of the graph G. Euler Path - An Euler path is a path that
uses every edge of a graph exactly once. An Euler
path starts and ends at different vertices.
• Euler Graph - A connected graph G is called an Euler
graph, if there is a closed trail which includes every edge
of the graph G.
• Euler Path - An Euler path is a path that uses every edge
of a graph exactly once. An Euler path starts and ends at
different vertices.
• Euler Circuit - An Euler circuit is a circuit that uses every
edge of a graph exactly once. An Euler circuit always
starts and ends at the same vertex. A connected graph G
is an Euler graph if and only if all vertices of G are of
even degree, and a connected graph G is Eulerian if and
only if its edge set can be decomposed into cycles.
• The above graph is an Euler graph as a 1 b 2 c 3 d 4 e 5
c 6 f 7 g covers all the edges of the graph.
• Non-Euler Graph
• Here degree of vertex b and d is 3, an odd degree and
violating the euler graph condition.
• A directed graph is graph, i.e., a set of objects (called
vertices or nodes) that are connected together, where all
the edges are directed from one vertex to another.
A directed graph is sometimes called a digraph or
a directed network.
• Connectivity in an undirected graph means that every
vertex can reach every other vertex via any path. If the
graph is not connected the graph can be broken down
into Connected Components.
• Strong Connectivity applies only to directed graphs. A
directed graph is strongly connected if there is a directed
path from any vertex to every other vertex. This is same
as connectivity in an undirected graph, the only
difference being strong connectivity applies to directed
graphs and there should be directed paths instead of just
paths. Similar to connected components, a directed
graph can be broken down into Strongly Connected
Components.
• 1) Create an empty stack 'S' and do DFS traversal of a
graph. In DFS traversal, after calling recursive DFS for
adjacent vertices of a vertex, push the vertex to stack. ...
• 2) Reverse directions of all arcs to obtain the transpose
graph.
• 3) One by one pop a vertex from S while S is not empty.
Let the popped vertex be 'v'.
• We can find all strongly connected components in
O(V+E) time using Kosaraju's algorithm. Following is
detailed Kosaraju's algorithm. 1) Create an empty stack
'S' and do DFS traversal of a graph. In DFS traversal,
after calling recursive DFS for adjacent vertices of a
vertex, push the vertex to stack.
Depth first traversal(data structure algorithms)

More Related Content

What's hot

Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphssana younas
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searchingKawsar Hamid Sumon
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics TreeMasud Parvaze
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Trees data structure
Trees data structureTrees data structure
Trees data structureSumit Gupta
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theoryChuckie Balbuena
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 

What's hot (20)

Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphs
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
BFS
BFSBFS
BFS
 
Graph theory
Graph theory Graph theory
Graph theory
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
graph theory
graph theory graph theory
graph theory
 
Dfs
DfsDfs
Dfs
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 

Similar to Depth first traversal(data structure algorithms)

Similar to Depth first traversal(data structure algorithms) (20)

Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Daa chpater 12
Daa chpater 12Daa chpater 12
Daa chpater 12
 
Graphs
GraphsGraphs
Graphs
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graphs (1)
Graphs (1)Graphs (1)
Graphs (1)
 
Data structure note
Data structure noteData structure note
Data structure note
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
 
B tree ,B plus and graph
B tree ,B plus and graph B tree ,B plus and graph
B tree ,B plus and graph
 
Graphs
GraphsGraphs
Graphs
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFS
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
Graph 1
Graph 1Graph 1
Graph 1
 

Recently uploaded

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

Depth first traversal(data structure algorithms)

  • 2. • 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. More formally a Graph can be defined as, In the above graph, • V = {a, b, c, d, e} • E = {ab, ac, bd, cd, de}
  • 3. • Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, a node may be visited twice. To avoid processing a node more than once, use a boolean visited array. • Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration. (It will pop up all the vertices from the stack, which do not have adjacent vertices.
  • 4. • Undirected graph • Biconnectivity • Euler circuits • Directed graph • Findind strong components
  • 5. • Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.
  • 6. • As in the example given above, DFS algorithm traverses from S to A to D to G to E to B first, then to F and lastly to C. It employs the following rules. • Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack. • Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.) • Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
  • 8. • Mark S as visited and put it onto the stack.Explore any unvisited adjacent node from S. We have three nodes and we can pick any of them. For this example, we shall take the node in an alphabetical order.
  • 9. • Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both S and D are adjacent to A but we are concerned for unvisited nodes only.
  • 10. • Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes, which are adjacent to D and both are unvisited. However, we shall again choose in an alphabetical order.
  • 11. • We choose B, mark it as visited and put onto the stack. Here B does not have any unvisited adjacent node. So, we pop B from the stack.
  • 12. • We check the stack top for return to the previous node and check if it has any unvisited nodes. Here, we find D to be on the top of the stack.
  • 13. • Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited and put it onto the stack. As C does not have any unvisited adjacent node so we keep popping the stack until we find a node that has an unvisited adjacent node. In this case, there's none and we keep popping until the stack is empty.
  • 14. • Depth-first search, or DFS, is a way to traverse the graph. Initially it allows visiting vertices of the graph only, but there are hundreds of algorithms for graphs, which are based on DFS. Therefore, understanding the principles of depth-first search is quite important to move ahead into the graph theory. The principle of the algorithm is quite simple: to go forward (in depth) while there is such possibility, otherwise to backtrack. • For most algorithms boolean classification unvisited / visited is quite enough, but we show general case here.
  • 15. • Initially all vertices are white (unvisited). DFS starts in arbitrary vertex and runs as follows: • Mark vertex u as gray (visited). • For each edge (u, v), where u is white, run depth-first search for u recursively. • Mark vertex u as black and backtrack to the parent. • Example. Traverse a graph shown below, using DFS. Start from a vertex with number 1.
  • 16. • Source graph. • Mark a vertex 1 as gray. • There is an edge (1, 4) and a vertex 4 is unvisited. Go there. • Mark the vertex 4 as gray. • There is an edge (4, 2) and vertex a 2 is unvisited. Go there. • Mark the vertex 2 as gray • There is an edge (2, 5) and a vertex 5 is unvisited. Go there. • Mark the vertex 5 as gray. • There is an edge (5, 3) and a vertex 3 is unvisited. Go there. • Mark the vertex 3 as gray. • There are no ways to go from the vertex 3. Mark it as
  • 17. • There are no ways to go from the vertex 5. Mark it as black and backtrack to the vertex 2. • There are no more edges, adjacent to vertex 2. Mark it as black and backtrack to the vertex 4. • There is an edge (4, 5), but the vertex 5 is black. • There are no more edges, adjacent to the vertex 4. Mark it as black and backtrack to the vertex 1. • There are no more edges, adjacent to the vertex 1. Mark it as black. DFS is over. • As you can see from the example, DFS doesn't go through all edges. The vertices and edges, which depth-first search has visited is a tree. This tree contains all vertices of the graph (if it is connected) and is called graph spanning tree. This tree exactly corresponds to the recursive calls of DFS. • If a graph is disconnected, DFS won't visit all of its vertices. For details, see finding connected components algorithm.
  • 18. • An undirected graph is said to be a biconnected graph, if there are two vertex-disjoint paths between any two vertices are present. In other words, we can say that there is a cycle between any two vertices. • We can say that a graph G is a bi-connected graph if it is connected, and there are no articulation points or cut vertex are present in the graph.
  • 19. • To solve this problem, we will use the DFS traversal. Using DFS, we will try to find if there is any articulation point is present or not. We also check whether all vertices are visited by the DFS or not, if not we can say that the graph is not connected. • Input and Output • Input: The adjacency matrix of the graph. • 0 1 1 1 0 • 1 0 1 0 0 • 1 1 0 0 1 • 1 0 0 0 1 • 0 0 1 1 0 • Output: The Graph is a biconnected graph.
  • 20. • Euler Graph - A connected graph G is called an Euler graph, if there is a closed trail which includes every edge of the graph G. Euler Path - An Euler path is a path that uses every edge of a graph exactly once. An Euler path starts and ends at different vertices. • Euler Graph - A connected graph G is called an Euler graph, if there is a closed trail which includes every edge of the graph G. • Euler Path - An Euler path is a path that uses every edge of a graph exactly once. An Euler path starts and ends at different vertices.
  • 21. • Euler Circuit - An Euler circuit is a circuit that uses every edge of a graph exactly once. An Euler circuit always starts and ends at the same vertex. A connected graph G is an Euler graph if and only if all vertices of G are of even degree, and a connected graph G is Eulerian if and only if its edge set can be decomposed into cycles.
  • 22. • The above graph is an Euler graph as a 1 b 2 c 3 d 4 e 5 c 6 f 7 g covers all the edges of the graph. • Non-Euler Graph • Here degree of vertex b and d is 3, an odd degree and violating the euler graph condition.
  • 23. • A directed graph is graph, i.e., a set of objects (called vertices or nodes) that are connected together, where all the edges are directed from one vertex to another. A directed graph is sometimes called a digraph or a directed network.
  • 24. • Connectivity in an undirected graph means that every vertex can reach every other vertex via any path. If the graph is not connected the graph can be broken down into Connected Components. • Strong Connectivity applies only to directed graphs. A directed graph is strongly connected if there is a directed path from any vertex to every other vertex. This is same as connectivity in an undirected graph, the only difference being strong connectivity applies to directed graphs and there should be directed paths instead of just paths. Similar to connected components, a directed graph can be broken down into Strongly Connected Components.
  • 25. • 1) Create an empty stack 'S' and do DFS traversal of a graph. In DFS traversal, after calling recursive DFS for adjacent vertices of a vertex, push the vertex to stack. ... • 2) Reverse directions of all arcs to obtain the transpose graph. • 3) One by one pop a vertex from S while S is not empty. Let the popped vertex be 'v'.
  • 26. • We can find all strongly connected components in O(V+E) time using Kosaraju's algorithm. Following is detailed Kosaraju's algorithm. 1) Create an empty stack 'S' and do DFS traversal of a graph. In DFS traversal, after calling recursive DFS for adjacent vertices of a vertex, push the vertex to stack.