Depth First Search (DFS) Graph Traversal in Data Structures

CodeWithHarry
6 Nov 202110:51

Summary

TLDRThis video introduces Depth First Search (DFS), a method for graph traversal. It contrasts DFS with Breadth First Search (BFS), explaining how DFS explores nodes deeply before backtracking. The speaker outlines the procedure, starting from a node and using a stack to keep track of visited nodes. As the traversal progresses, the video illustrates how to add connected vertices to the stack and visit them sequentially. The speaker emphasizes the simplicity of DFS while hinting at a forthcoming detailed coding tutorial in C. Overall, the video provides a clear and engaging explanation of DFS.

Takeaways

  • 😀 Depth First Search (DFS) is a method for traversing graphs, just like Breadth First Search (BFS).
  • 🌐 In DFS, you start at a node and explore its connected vertices, going as deep as possible before backtracking.
  • 🔄 If you reach a dead end while traversing, you backtrack to the last visited node to explore other paths.
  • 📊 DFS and BFS are two major graph traversal techniques, each with different approaches.
  • 📚 The procedure for DFS involves using a stack to keep track of the nodes to visit.
  • ✅ When implementing DFS, nodes are marked as visited to avoid revisiting them.
  • 🗂️ The stack in DFS is crucial, as it follows the Last In, First Out (LIFO) principle.
  • 💻 The programming implementation of DFS can be done in languages like C, utilizing function call stacks.
  • 🔍 The traversal order in DFS can vary based on the order nodes are pushed onto the stack.
  • 📝 Pseudocode for DFS is provided to assist with coding the algorithm in the next video.

Q & A

  • What is Depth First Search (DFS) and how is it different from Breadth First Search (BFS)?

    -Depth First Search (DFS) is a graph traversal method where you start at a node and explore as deeply as possible along each branch before backtracking. In contrast, Breadth First Search (BFS) explores all neighboring nodes at the current level before moving on to nodes at the next level.

  • How does DFS explore a graph?

    -In DFS, the exploration starts from a node, and the algorithm moves to its connected vertices, going deeper into the graph. When a dead end is reached, it backtracks and explores other suspended paths. This continues until all nodes are visited.

  • What is the role of a stack in DFS?

    -A stack is used in DFS to manage the nodes to be visited. It follows the Last In, First Out (LIFO) principle, where the last node added to the stack is the first one to be visited. This structure helps DFS explore deeper nodes before backtracking.

  • Can DFS be implemented without a stack?

    -While DFS can be conceptually described without a stack, in computer programming, a stack is essential to manage the traversal process efficiently. The stack ensures that DFS explores nodes correctly and backtracks when necessary.

  • What happens when DFS encounters a dead end?

    -When DFS encounters a dead end, meaning a node has no unvisited neighbors, it backtracks by popping the last node from the stack and continues exploring from there.

  • How does DFS work programmatically?

    -Programmatically, DFS begins by pushing the starting node onto a stack. Then, nodes are popped from the stack, visited, and any unvisited connected nodes are pushed onto the stack. This process continues until all nodes are visited.

  • Why does the speaker mention that DFS is simple in layman terms?

    -The speaker mentions that DFS is simple in layman terms to emphasize the straightforward nature of the algorithm. It involves going deep into a graph and backtracking when necessary, which is easy to grasp without technical details.

  • What is the key difference between how DFS is described informally and how it is implemented in programming?

    -Informally, DFS is described as a process of exploring a graph deeply before backtracking. In programming, DFS is implemented using a stack to track nodes to be visited and manage backtracking efficiently.

  • What does the pseudocode provided in the video illustrate?

    -The pseudocode provided in the video illustrates the step-by-step procedure for implementing DFS in a programming language, showing how nodes are added to the stack, popped from it, and visited until all nodes are explored.

  • How does DFS compare to BFS in terms of traversal order?

    -DFS explores a graph by going deep along one branch before backtracking, whereas BFS explores nodes level by level, visiting all neighboring nodes before moving deeper. DFS can lead to a different traversal order compared to BFS, depending on the graph structure and node connections.

Outlines

00:00

🧩 Understanding Depth First Search (DFS)

In this part, the speaker introduces Depth First Search (DFS), explaining it as a method for traversing graphs, akin to Breadth First Search (BFS). DFS begins with a starting node, exploring its connected vertices deeply before backtracking. The speaker illustrates the procedure of DFS, noting that it involves placing the starting vertex on a stack and visiting connected nodes. When a dead end is reached, the algorithm backtracks to previously visited nodes, continuing until all nodes are traversed. A clear comparison is made between DFS and BFS, emphasizing their different approaches to graph traversal. The speaker outlines the algorithm's steps and mentions that pseudocode for DFS will be provided in future content, preparing viewers for coding in C language.

Mindmap

Keywords

💡Depth First Search (DFS)

Depth First Search (DFS) is an algorithm used for traversing or searching tree or graph data structures. It starts at a given node and explores as far down a branch as possible before backtracking. The video illustrates DFS by explaining that it can go deep into a graph until a dead end is reached, at which point it backtracks to explore other unvisited paths.

💡Breadth First Search (BFS)

Breadth First Search (BFS) is another graph traversal method, which explores all the neighbor nodes at the present depth before moving on to nodes at the next depth level. In the video, BFS is compared with DFS to highlight their differences, with BFS visiting all connected nodes of a starting point before going deeper, making it useful for finding the shortest path in unweighted graphs.

💡Graph

A graph is a collection of nodes (or vertices) connected by edges. In the context of the video, graphs are the primary data structure used for implementing DFS and BFS algorithms. The narrator illustrates graph traversal by visualizing connections between nodes, which serve as the basis for both DFS and BFS.

💡Node

A node is an individual element within a graph that can represent a data point or object. In the video, the narrator refers to nodes when explaining how DFS and BFS traverse through the graph, emphasizing that traversal begins at a specific node and explores its connections.

💡Stack

A stack is a data structure that follows the Last In, First Out (LIFO) principle, meaning that the last element added to the stack is the first one to be removed. The video discusses how a stack is essential for implementing DFS programmatically, allowing the algorithm to backtrack effectively when reaching dead ends in the graph.

💡Visited List

A visited list is an array or data structure used to keep track of which nodes have been explored during the traversal process. In the video, the narrator mentions creating a visited list to ensure that nodes are not revisited, thus preventing infinite loops and improving the efficiency of the traversal.

💡Backtracking

Backtracking refers to the process of returning to a previous node after reaching a dead end in a graph traversal. In the context of the video, backtracking is a critical aspect of DFS, allowing the algorithm to explore all possible paths in the graph by returning to earlier nodes when no further moves are possible.

💡Dead End

A dead end in graph traversal is a situation where a node has no unvisited connected vertices. The video explains that when DFS reaches a dead end, it must backtrack to explore other paths. This concept is essential for understanding how DFS efficiently covers all nodes in a graph.

💡Traversal

Traversal refers to the process of visiting all the nodes in a graph or tree structure systematically. The video focuses on how both DFS and BFS serve as methods for traversal, each with distinct strategies for exploring nodes, thereby highlighting the importance of understanding different traversal techniques in computer science.

💡Pseudocode

Pseudocode is a simplified, informal way of describing an algorithm using structured but human-readable notation. The video concludes by mentioning that pseudocode for DFS will be provided in the next installment, indicating that it will help viewers translate the DFS concept into actual programming code more easily.

Highlights

Depth First Search (DFS) is a method of traversing a graph, akin to Breadth First Search (BFS).

DFS involves starting at a node and exploring its connected vertices deeply.

When a dead end is reached in DFS, the algorithm backtracks to explore other paths.

DFS and BFS are the two primary ways to perform graph traversal.

DFS uses a stack to manage the nodes that need to be visited.

The traversal process in DFS involves visiting nodes and marking them as explored.

DFS is particularly useful in scenarios requiring exploration of all possible paths.

The traversal order in DFS can vary based on the graph's structure and the exploration order.

DFS can be implemented programmatically, taking advantage of the call stack.

An initial node is placed on top of a stack to begin the DFS process.

After visiting a node, its unvisited neighbors are pushed onto the stack.

Pseudocode for DFS outlines the recursive nature of the algorithm.

The algorithm backtracks through the stack when no unvisited neighbors remain.

DFS is effective for searching paths in complex graphs, including trees.

The next session will cover coding DFS in the C programming language.

Understanding DFS enhances problem-solving skills in graph-related challenges.

Transcripts

play00:00

Today we'll see about Depth First Search.

play00:03

In previous video we saw about breadth first search.

play00:05

Firstly I'll talk about what is Depth First Search.

play00:08

So Depth First Search is not a big thing.

play00:11

It is just a way to traverse a graph.

play00:14

As we saw that Breadth First Search is a way to traverse graph,

play00:18

in the same way this is also a way to traverse graph, nothing else.

play00:22

So when we do graph traversal,

play00:24

we have Depth First Search and Breadth First Search,

play00:28

which are the two major ways to do it,

play00:30

to do traversal.

play00:32

So here we have already seen BFS.

play00:35

Now we'll see DFS.

play00:36

So these things you already know, I know that.

play00:39

So in DFS what we do ? In DFS we start with,

play00:43

a node and explore its connected vertices.

play00:47

And try to go deep in it.

play00:49

So we go deep and deep and deep,

play00:51

and finally a time comes when we,

play00:53

get a node which is already explored.

play00:56

So we come back and,

play00:58

the traversals which we have suspended,

play01:00

we do them again.

play01:01

Now many people will think what am I talking about.

play01:05

But, let me tell you here,

play01:08

I'll compare DFS and BFS.

play01:13

Firstly I'll compare this with BFS.

play01:16

BFS. Suppose this is a graph.

play01:20

I am making some nodes.

play01:21

It is not necessary to mark these nodes which I am making,

play01:26

or to write something in it.

play01:27

But let us assume that this a graph.

play01:31

So if I am doing BFS, than what I'll do is,

play01:34

if I start with this node.

play01:37

So firstly I'll visit its all collected vertex, which means

play01:41

I'll visit this, and this, and this.

play01:45

But if I am doing DFS than what I'll do is,

play01:48

I'll start from here and than I'll go there, there and there.

play01:52

So I came to the dead end, now I'll go back again and again.

play01:56

And I'll go here, and here, and here and likewise whole graph will be traverse.

play02:00

I know at this point, some people must get this confusing.

play02:03

Lets go further, and I guarantee you will get this very clearly.

play02:09

So lets understand the procedure first.

play02:11

I'll directly come to the point, as it's not a big thing.

play02:14

It is a way to do traverse.

play02:16

I'll talk to the point, let me directly come to the point.

play02:18

Let's see what we have to do.

play02:20

So we have to start by putting any one of the graph's vertices

play02:24

on top of a stack.

play02:26

Before we talk about stack and programmatic procedure,

play02:29

let me tell you a very simple procedure

play02:31

that how DFS works.

play02:33

Suppose I start here from 0.

play02:37

I'll make a visited array here, so I am writing visited.

play02:40

So in this visited list or say array

play02:43

I'll start with a node, suppose I am starting with 0.

play02:47

So I am starting from 0, so I am writing here 0.

play02:51

Starting from 0, I'll go towards 1.

play02:55

And I'll write 1.

play02:57

Than from 1 I'll go towards 2 and I'll write 2.

play03:01

From 2 I can go to 3 or 4, its up to me. But I'll go to 3.

play03:07

So I'll go towards 3.

play03:09

From 3 where I can go ? I can go towards 4. So I'll go to 4.

play03:14

From 4 I can go towards 5, so I'll go to 5.

play03:17

But when I'll go to 5, I'll realise that I can't go ahead.

play03:21

So I'll go back one step in the same direction from where I came.

play03:25

I'll go back in that same direction.

play03:27

And I'll come back to 4.

play03:30

On 4 I realise that I can go on 6, so I'll go to 6.

play03:34

Like this I'll go to 6.

play03:36

But when I am on 6, I'll realise that I can't go ahead.

play03:39

As this is a type of dead end.

play03:41

So I'll go back in the same direction from which I came.

play03:46

Not like you'll go to 0 from 6.

play03:47

So I will go back in same direction to 4.

play03:51

In the same direction I came to 4.

play03:53

When I came to 4 I realised that I can go to 2.

play03:58

And when I came to 2,

play04:01

than I realised that I can't go anywhere, its a dead end.

play04:05

So I'll come back again and again from the path.

play04:09

And I"ll come back here and DFS will be completed.

play04:13

As we have visited all the nodes.

play04:15

So this is our layman terms procedure of how DFS works.

play04:20

But when you give command to a computer,

play04:22

if you want computer to do DFS than this will not be the approch.

play04:26

Than what will be the approch there ?

play04:27

If you want that computer do DFS than there you will use stack.

play04:32

And the good thing is that, when in computer

play04:35

we write code in C language for DFS,

play04:38

than by default we get stack there, and its really amazing.

play04:42

And you must be also enjoying this,

play04:45

that wow we already get stack in DFS.

play04:48

And how all this happens that I'll tell you.

play04:50

Functions already uses stack, this I have already told you.

play04:55

By using that only we will write the procedure of DFS.

play04:57

But now lets understand the programmatic procedure of DFS.

play05:00

So the 1st point is,

play05:02

start by putting any one of the graph's vertices on top of a stack.

play05:07

Procedure says that take a stack.

play05:08

And what is a stack ? It is a bucket.

play05:11

And start keeping things in the bucket.

play05:13

The thing which is kept at last, will come out first.

play05:17

After this, take the top item of stack and add it to visited list.

play05:21

Firstly we have to put a node, so I kept 0.

play05:24

So I kept 0.

play05:26

I can put any node say 1 or 2.

play05:29

Now it says that take out what you kept inside.

play05:31

So lets take out what we kept.

play05:33

And we took out 0.

play05:35

So 0 came out of stack now.

play05:38

We took 0 out and,

play05:39

put all the vertices in the stack which are connected to 0.

play05:43

In any order I'll put 1, 2 and 3.

play05:48

Now 1, 2, 3 are inside our stack.

play05:51

I can also put like 3, 2, 1 .

play05:53

I can also put like this,

play05:55

that at the end of stack its 3 then 2 and then 1.

play05:59

In any order you can keep it, there is no hard and fast rule here.

play06:03

But If you kept it once,

play06:05

than you'll first remove the one which you kept at last.

play06:08

So you'll remove 1 first.

play06:10

And what will come in visited, so first 0 came.

play06:14

Than which ever we are removing from stack, will come to visited.

play06:16

Once you removed 1, it came to visited.

play06:20

Now you removed 1, so you have to do this procedure again.

play06:23

That you have to remove the top item of the stack

play06:26

and add it to the visited list.

play06:29

After that all the items which are connect to this,

play06:31

and not visited, that you'll keep in stack.

play06:33

So if we talk about 1,

play06:35

0 and 2 are connected to 1.

play06:39

So what we'll do here, as 0 is already in the visited list

play06:42

but 2 is not in visited list, so we'll put it in the stack.

play06:45

As 2 is already in the stack, we will pop it out.

play06:49

And after that we will visit 2.

play06:51

We will do same thing with 2.

play06:52

Which are the connected vertices with 2,

play06:56

for that we will use this same procedure.

play07:00

And after that, If I

play07:02

put the 1st step on the graph which you can see on right hand side.

play07:06

So what I did is, I started with 0.

play07:08

I kept 0 in stack.

play07:10

As you can see 0 came in the stack.

play07:13

Now I'll start putting any one of the graph vertices.

play07:17

And after that my 2nd step is,

play07:18

take the top item of the stack and add it to the visited list.

play07:21

As you can see, 0 is also visited now.

play07:25

This is done, now I'll put items in stack which are connected to 0.

play07:29

And that are 1, 2 and 3.

play07:30

As you see 1, 2 and 3 will go in the stack in any order.

play07:34

Once I kept 1, 2 and 3 in stack in any order,

play07:38

I'll pop out the top most element.

play07:41

I'll pop out the top most element and put it in visited.

play07:45

And I'll keep nodes in the stack, which are connected to it.

play07:50

Also it must not be visited, as 0 is visited I'll not put it.

play07:53

I'll keep 2 in stack.

play07:55

So again I'll put 2 in the stack.

play07:59

So after keeping it inside I'll pop out 2.

play08:03

So this and this are visited.

play08:06

2 will also be visited as I popped it out.

play08:08

But which all should come in stack ?

play08:10

0, 1, 3 and 4 .

play08:12

As 0 and 1 are visited, 3 and 4 will come.

play08:15

So I'll keep 3 and 4.

play08:17

I'll pop out 4 from stack and put it to visited.

play08:21

So I kept 4 in visited.

play08:24

4 is visited now. What I'll keep in stack now ?

play08:27

I should put 3, 5 and 6 as they are not yet visited.

play08:30

So I'll keep 3, 5 and 6.

play08:34

Now I'll pop out 6.

play08:36

And I'll write 6 here, it is also visited now.

play08:40

Is there any element connected to 6 but unvisited ? No.

play08:42

There is no node connected to 6 which is unvisited.

play08:46

So thats why what I'll do is,

play08:48

I'll pop out the next one, which is 5.

play08:50

Which one will I pop out ? The next one which is 5.

play08:54

And I'll put it in the visited list.

play08:57

And there is no node connect to it,

play09:00

which is unvisited.

play09:03

That's why now I'll pop out 3.

play09:05

I'll put 3 in the visited list.

play09:08

And there is no node connected to 3 which is unvisited.

play09:12

Similarly, 3 and 3 is done.

play09:15

As well as 2 is also done.

play09:19

So I'll remove 2 also.

play09:21

Finally our stack is empty.

play09:23

So as you can see that,

play09:25

our DFS traversal has been came.

play09:30

Can this DFS traversal be done with the way which I showed you first ?

play09:33

Yes.

play09:35

So you start with 0.

play09:38

Then go to 1 and then go towards 2.

play09:41

From 2 go to 4.

play09:43

From 4 you go to 6.

play09:45

At 6 you got dead end, so come back.

play09:48

So from 4 you have gone to 6,

play09:52

and came back to 4.

play09:53

After this you can go to 5.

play09:56

From 5 you have to come back, and than you'll go to 3.

play10:00

So a valid DFS traversal has been made.

play10:04

I hope it is understood as its a really simple thing.

play10:05

And to write this programmatically with stack makes it more easy.

play10:10

On you screen,

play10:11

It is Pseudocode of DFS which you can use,

play10:15

if you want to code DFS in C language.

play10:17

Or I am going to explain this in the next video.

play10:20

We will see code for DFS in C language in detail.

play10:24

That how we can code the DFS.

play10:26

So for now thats it for this video.

play10:28

Thank you so much for watching this video.

play10:30

And I will see you next time.

Rate This

5.0 / 5 (0 votes)

Related Tags
Graph TheoryDFS AlgorithmBFS ComparisonComputer ScienceProgramming BasicsAlgorithm TutorialData StructuresGraph TraversalCoding EducationSoftware Development