Skip to content
Binary file added Graphs/DFS/Images/dfs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graphs/DFS/Images/output.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions Graphs/DFS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Overview:
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.

## What is Depth First Search in Python?
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.

## Algorithm of DFS in Python
Every time we reach a new node, we will take the following steps:
1. We add the node to the top of the stack.
2. Marked it as visited.
3. We check if this node has any adjacent nodes:
1. If it has adjacent nodes, then we ensure that they have not been visited already, and then visited it.
2. We removed it from the stack if it had no adjacent nodes.

## Time & Space Complexity
* **Time Complexity:**
Time complexity of DFS is `O(V+|E|)`, where V is the number of vertices and E is the number of edges.
* **Space Complexity:**
The space complexity of the DFS algorithm is `O(V)`, where V is the number of vertices.

## Input & Output:
<img src="../DFS/Images/dfs.jpg">

### Input:

```python
graph = {
0: [2],
1: [2, 3],
2: [0, 1, 4],
3: [1, 4],
4: [2, 3]
}
```

### Output:
```python
Depth-first search: [0, 2, 1, 3, 4]
```
<img width=50% src="../DFS/Images/output.jpg">
25 changes: 25 additions & 0 deletions Graphs/DFS/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def dfs(node, graph, visited, component):
component.append(node) # Store answer
visited[node] = True # Mark visited

# Traverse to each adjacent node of a node
for child in graph[node]:
if not visited[child]: # Check whether the node is visited or not
dfs(child, graph, visited, component)


if __name__ == "__main__":

# Graph of nodes
graph = {
0: [2],
1: [2, 3],
2: [0, 1, 4],
3: [1, 4],
4: [2, 3]
}
node = 0 # Starting node
visited = [False]*len(graph)
component = []
dfs(node, graph, visited, component) # Traverse to each node of a graph
print(f"Following is the Depth-first search: {component}") # Print the answer