Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Graphs/Breadth First Search/Images/input.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/Breadth First Search/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/Breadth First Search/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 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.

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

## Algorithm of DFS in Python
The algorithm works as follows:

* Create a queue and insert any one vertex of the graph at the back of the queue.
* Initialize a visited array and mark that vertex as visited.
* Follow the below process till the queue becomes empty:
* Remove a front vertex from the queue.
* Get all the adjacent vertices of the removed vertex.
* If the adjacent vertex has not been visited, mark it as visited and insert it at the back of the queue.

## Time & Space Complexity
* Time complexity is `O(V+E)`, where V denotes the number of vertices and E denotes the number of edges.
* Space complexity is `O(V)`.

## Input & Output
### Input:

<img width=50% src="../Breadth First Search/Images/input.jpg">

```python
graph = {
'A': ['B', 'C', 'D'],
'B': ['A'],
'C': ['A', 'D'],
'D': ['A', 'C', 'E'],
'E': ['D']
}
```
### Output:
<img width=50% src="../Breadth First Search/Images/output.jpg">

```python
Breadth-first Search: A B C D E
```
36 changes: 36 additions & 0 deletions Graphs/Breadth First Search/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Create a graph given in the above diagram.
graph = {
'A': ['B', 'C', 'D'],
'B': ['A'],
'C': ['A', 'D'],
'D': ['A', 'C', 'E'],
'E': ['D']
}

# to print a BFS of a graph
def bfs(node):

# mark vertices as False means not visited
visited = [False] * (len(graph))

# make a empty queue for bfs
queue = []

# mark given node as visited and add it to the queue
visited.append(node)
queue.append(node)

while queue:
# Remove the front vertex or the vertex at 0th index from the queue and print that vertex.
v = queue.pop(0)
print(v, end=" ")

for neigh in graph[v]:
if neigh not in visited:
visited.append(neigh)
queue.append(neigh)


# Driver Code
if __name__ == "__main__":
bfs('A')