Skip to content

Commit 7a760b8

Browse files
authored
Update readme.md
1 parent b1fb2f7 commit 7a760b8

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

Graphs/DFS/readme.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
# Hey
1+
## Overview:
2+
Two techniques are frequently used for graph traversal: These techniques are called **depth-first search (DFS)** and **breadth-first search (BFS)**, although we will just talk about DFS. DFS involves diving deep into the graph and then backtrack when it reaches the bottom.
3+
4+
## What is Depth First Search in Python?
5+
In DFS, *we continue to traverse downwards through linked nodes until we reach the end*, then retrace our steps to check which connected nodes we haven't visited and repeat the process. In depth-first search, we dive deep into the graph and then backtrack when we reach the bottom.
6+
7+
## Algorithm of DFS in Python
8+
Every time we reach a new node, we will take the following steps:
9+
1. We add the node to the top of the stack.
10+
2. Marked it as visited.
11+
3. We check if this node has any adjacent nodes:
12+
1. If it has adjacent nodes, then we ensure that they have not been visited already, and then visited it.
13+
2. We removed it from the stack if it had no adjacent nodes.
14+
15+
## Time & Space Complexity
16+
* **Time Complexity:**
17+
Time complexity of DFS is `O(V+|E|)`, where V is the number of vertices and E is the number of edges.
18+
* **Space Complexity:**
19+
The space complexity of the DFS algorithm is `O(V)`, where V is the number of vertices.
20+
21+
## Input & Output:
22+
### Input:
23+
24+
```python
25+
graph = {
26+
0: [2],
27+
1: [2, 3],
28+
2: [0, 1, 4],
29+
3: [1, 4],
30+
4: [2, 3]
31+
}
32+
```
33+
34+
### Output:
35+
```python
36+
Depth-first search: [0, 2, 1, 3, 4]
37+
```

0 commit comments

Comments
 (0)