Skip to content
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
47 changes: 47 additions & 0 deletions BFS_and_DFS_al.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Normal BFS Traversal

# model
model = {"S": ["A", "B", "C"],
"A": ["S", "D"],
"B": ["S", "E", "D", "G"],
"C": ["S", "E"],
"D": ["A", "F", "B"],
"F": ["D", "G", "E"],
"E": ["C", "F", "H", "B"],
"H": ["E", "G"],
"G": ["F", "B", "H"]
}

initial = "S"
goal = "G"
frontier = [initial]
explored = []

count = 1
while frontier:
value = frontier.pop(0)
if value not in explored:
explored.append(value)
if goal == value:
break
for val in model[value]:
if val not in explored and val not in frontier:
frontier.append(val)
count+=1
print("BFS: ")
print(explored)

# Normal DFS Traversal without Goal

initial_dfs = "S"
frontier_dfs = [initial_dfs]
explored_dfs = []
while frontier_dfs:
value = frontier_dfs.pop()
if value not in explored_dfs:
explored_dfs.append(value)
new_frontier_dfs = [val for val in model[value]
if val not in explored_dfs and val not in frontier_dfs][::-1]
frontier_dfs.extend(new_frontier_dfs)
print("DFS: ")
print(explored_dfs)