Skip to content

Added BFS and DFS algorithms #166

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions Algorithms/BFS_BreadthFirstSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from collections import deque

class Graph:
def __init__(self):
self.graph = {}

def add_edge(self, vertex, neighbor):
if vertex in self.graph:
self.graph[vertex].append(neighbor)
else:
self.graph[vertex] = [neighbor]

def bfs(self, start):
visited = set()
queue = deque()

queue.append(start)
visited.add(start)

while queue:
vertex = queue.popleft()
print(vertex, end=" ")

for neighbor in self.graph.get(vertex, []):
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)

if __name__ == "__main__":
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)
graph.add_edge(4, 5)
graph.add_edge(5, 6)
graph.add_edge(6, 7)
graph.add_edge(7, 4)
graph.add_edge(8, 9)
graph.add_edge(9, 10)
graph.add_edge(10, 11)
graph.add_edge(11, 8)
graph.add_edge(12, 13)
graph.add_edge(13, 14)
graph.add_edge(14, 15)
graph.add_edge(15, 12)
graph.add_edge(16, 17)
graph.add_edge(17, 18)
graph.add_edge(18, 19)
graph.add_edge(19, 16)

print("Breadth-First Traversal (starting from vertex 2):")
graph.bfs(2)
print("\n")

print("Breadth-First Traversal (starting from vertex 9):")
graph.bfs(9)
28 changes: 28 additions & 0 deletions Algorithms/DFS_DepthFirstSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from collections import defaultdict

class Graph:
def __init__(self):
self.graph = defaultdict(list)

def add_edge(self, u, v):
self.graph[u].append(v)

def dfs(self, v, visited):
visited[v] = True
print(v, end=' ')

for i in self.graph[v]:
if not visited[i]:
self.dfs(i, visited)

g = Graph()

edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6), (3, 7), (3, 8), (4, 9), (4, 10), (5, 11), (5, 12),
(6, 3), (6, 4), (6, 13), (6, 14), (7, 8), (7, 9), (8, 9)]

for edge in edges:
g.add_edge(edge[0], edge[1])

print("Depth-First Traversal (starting from vertex 0):")
visited = [False] * 15
g.dfs(0, visited)
53 changes: 53 additions & 0 deletions Algorithms/HammingCode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
def hamming_encode(data):
r = 1
while 2**r < len(data) + r + 1:
r += 1

encoded_data = [0] * (len(data) + r)

j = 0
for i in range(1, len(encoded_data) + 1):
if i & (i - 1) != 0:
encoded_data[i - 1] = int(data[j])
j += 1

for i in range(r):
mask = 2**i
for j in range(1, len(encoded_data) + 1):
if j & mask == mask:
encoded_data[j - 1] ^= encoded_data[i]

return encoded_data

def hamming_decode(encoded_data):
r = 1
while 2**r < len(encoded_data):
r += 1

syndrome = [0] * r
for i in range(r):
mask = 2**i
for j in range(len(encoded_data)):
if j & mask == mask:
syndrome[i] ^= encoded_data[j]

error_position = sum([2**i * bit for i, bit in enumerate(syndrome)])

if error_position != 0:
encoded_data[error_position - 1] ^= 1

decoded_data = []
j = 0
for i in range(1, len(encoded_data) + 1):
if i & (i - 1) != 0:
decoded_data.append(encoded_data[i - 1])

return decoded_data

data = input("Enter binary data: ")

encoded_data = hamming_encode(data)
print("Encoded:", ''.join(map(str, encoded_data)))

decoded_data = hamming_decode(encoded_data)
print("Decoded:", ''.join(map(str, decoded_data)))