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]DEEPIKA T
 
bfs and dfs (data structures).pptx
bfs and dfs (data structures).pptxbfs and dfs (data structures).pptx
bfs and dfs (data structures).pptxssuser55cbdb
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure Ankit Kumar Singh
 
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
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6DrkhanchanaR
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 

What's hot (20)

Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
bfs and dfs (data structures).pptx
bfs and dfs (data structures).pptxbfs and dfs (data structures).pptx
bfs and dfs (data structures).pptx
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
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
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
BFS
BFSBFS
BFS
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Recursion
RecursionRecursion
Recursion
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Binary tree
Binary tree Binary tree
Binary tree
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 

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

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

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.