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 (20)

Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Graph theory
Graph  theoryGraph  theory
Graph theory
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Hashing
HashingHashing
Hashing
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
DES Simplified
DES SimplifiedDES Simplified
DES Simplified
 
Block Cipher
Block CipherBlock Cipher
Block Cipher
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Stack, Queue, Linked List.pptx
Stack, Queue, Linked List.pptxStack, Queue, Linked List.pptx
Stack, Queue, Linked List.pptx
 
Adjacency list
Adjacency listAdjacency list
Adjacency list
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 

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
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph 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
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
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
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
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
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

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.