Skip to content

Commit 5e963f2

Browse files
Merge pull request #210 from Iamtripathisatyam/main
Breadth First Search
2 parents 446eac3 + a0f6c5c commit 5e963f2

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
16.9 KB
Loading
23.1 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 BFS. Breadth-first search (BFS) is used to solve many problems, including finding the shortest path in a graph or solving puzzle games such as Rubik’s Cubes.
3+
4+
## What is Breadth First Search in Python?
5+
Breadth-first search (BFS) is an algorithm for traversing graphs or trees. Breath-first search for trees and graphs are almost the same. BFS uses a queue data structure for finding the shortest path in a graph.
6+
7+
## Algorithm of DFS in Python
8+
The algorithm works as follows:
9+
10+
* Create a queue and insert any one vertex of the graph at the back of the queue.
11+
* Initialize a visited array and mark that vertex as visited.
12+
* Follow the below process till the queue becomes empty:
13+
* Remove a front vertex from the queue.
14+
* Get all the adjacent vertices of the removed vertex.
15+
* If the adjacent vertex has not been visited, mark it as visited and insert it at the back of the queue.
16+
17+
## Time & Space Complexity
18+
* Time complexity is `O(V+E)`, where V denotes the number of vertices and E denotes the number of edges.
19+
* Space complexity is `O(V)`.
20+
21+
## Input & Output
22+
### Input:
23+
24+
<img width=50% src="../Breadth First Search/Images/input.jpg">
25+
26+
```python
27+
graph = {
28+
'A': ['B', 'C', 'D'],
29+
'B': ['A'],
30+
'C': ['A', 'D'],
31+
'D': ['A', 'C', 'E'],
32+
'E': ['D']
33+
}
34+
```
35+
### Output:
36+
<img width=50% src="../Breadth First Search/Images/output.jpg">
37+
38+
```python
39+
Breadth-first Search: A B C D E
40+
```

Graphs/Breadth First Search/bfs.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Create a graph given in the above diagram.
2+
graph = {
3+
'A': ['B', 'C', 'D'],
4+
'B': ['A'],
5+
'C': ['A', 'D'],
6+
'D': ['A', 'C', 'E'],
7+
'E': ['D']
8+
}
9+
10+
# to print a BFS of a graph
11+
def bfs(node):
12+
13+
# mark vertices as False means not visited
14+
visited = [False] * (len(graph))
15+
16+
# make a empty queue for bfs
17+
queue = []
18+
19+
# mark given node as visited and add it to the queue
20+
visited.append(node)
21+
queue.append(node)
22+
23+
while queue:
24+
# Remove the front vertex or the vertex at 0th index from the queue and print that vertex.
25+
v = queue.pop(0)
26+
print(v, end=" ")
27+
28+
for neigh in graph[v]:
29+
if neigh not in visited:
30+
visited.append(neigh)
31+
queue.append(neigh)
32+
33+
34+
# Driver Code
35+
if __name__ == "__main__":
36+
bfs('A')

0 commit comments

Comments
 (0)