Skip to content

Update queue implementation #5388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 6 additions & 4 deletions graphs/breadth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
""" Author: OMKAR PATHAK """
from __future__ import annotations

from queue import Queue


class Graph:
def __init__(self) -> None:
Expand Down Expand Up @@ -51,19 +53,19 @@ def bfs(self, start_vertex: int) -> set[int]:
visited = set()

# create a first in first out queue to store all the vertices for BFS
queue = []
queue = Queue()

# mark the source node as visited and enqueue it
visited.add(start_vertex)
queue.append(start_vertex)
queue.put(start_vertex)

while queue:
vertex = queue.pop(0)
vertex = queue.get()

# loop through all adjacent vertex and enqueue it if not yet visited
for adjacent_vertex in self.vertices[vertex]:
if adjacent_vertex not in visited:
queue.append(adjacent_vertex)
queue.put(adjacent_vertex)
visited.add(adjacent_vertex)
return visited

Expand Down
11 changes: 7 additions & 4 deletions graphs/breadth_first_search_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"""
from __future__ import annotations

from queue import Queue

G = {
"A": ["B", "C"],
"B": ["A", "D", "E"],
Expand All @@ -30,13 +32,14 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
'ABCDEF'
"""
explored = {start}
queue = [start]
while queue:
v = queue.pop(0) # queue.popleft()
queue = Queue()
queue.put(start)
while not queue.empty():
v = queue.get()
for w in graph[v]:
if w not in explored:
explored.add(w)
queue.append(w)
queue.put(w)
return explored


Expand Down
13 changes: 8 additions & 5 deletions graphs/check_bipartite_graph_bfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.
from queue import Queue


def checkBipartite(graph):
queue = []
queue = Queue()
visited = [False] * len(graph)
color = [-1] * len(graph)

def bfs():
while queue:
u = queue.pop(0)
while not queue.empty():
u = queue.get()
visited[u] = True

for neighbour in graph[u]:
Expand All @@ -23,7 +26,7 @@ def bfs():

if color[neighbour] == -1:
color[neighbour] = 1 - color[u]
queue.append(neighbour)
queue.put(neighbour)

elif color[neighbour] == color[u]:
return False
Expand All @@ -32,7 +35,7 @@ def bfs():

for i in range(len(graph)):
if not visited[i]:
queue.append(i)
queue.put(i)
color[i] = 0
if bfs() is False:
return False
Expand Down