0% found this document useful (0 votes)
13 views5 pages

BFS+DFS+Astar Program

The document describes different graph traversal algorithms including breadth-first search, depth-first search, and A* search. Python code examples are provided for implementing BFS, DFS, and A* on graphs represented as dictionaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

BFS+DFS+Astar Program

The document describes different graph traversal algorithms including breadth-first search, depth-first search, and A* search. Python code examples are provided for implementing BFS, DFS, and A* on graphs represented as dictionaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# BFS algorithm in Python

import collections

# BFS algorithm
def bfs(graph, root):

visited, queue = set(), collections.deque([root])


visited.add(root)

while queue:

# Dequeue a vertex from queue


vertex = queue.popleft()
print(str(vertex) + " ", end="")

# If not visited, mark it as visited, and # enqueue it


for neighbour in graph[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)

if __name__ == '__main__':
graph = {0: [1, 2], 1: [2], 2: [3], 3: [1, 2]}
print("Following is Breadth First Traversal: ")
bfs(graph, 0)

output
Following is Breadth First Traversal:
0 1 2 3

DFS

graph = {

'0':['1', '2'], '1':['2'],'2':['3'],'3':['1', '2']

visited =set()

def dfs(visited,graph,root):

if root not in visited:

print(root)

visited.add(root)
for neighbour in graph[root]:

dfs(visited,graph,neighbour)

dfs(visited,graph,'0')

OUTPUT :
0
1
3
4
2

A* Program
Graph_nodes = {

'A': [('B', 6), ('F', 3)],

'B': [('C', 3), ('D', 2)],

'C': [('D', 1), ('E', 5)],

'D': [('C', 1), ('E', 8)],

'E': [('I', 5), ('J', 5)],

'F': [('G', 1),('H', 7)] ,

'G': [('I', 3)],

'H': [('I', 2)],

'I': [('E', 5), ('J', 3)],

def get_neighbors(v):

if v in Graph_nodes:

return Graph_nodes[v]

else:
return None

def h(n):

H_dist = {

'A': 10,

'B': 8,

'C': 5,

'D': 7,

'E': 3,

'F': 6,

'G': 5,

'H': 3,

'I': 1,

'J': 0

return H_dist[n]

def aStarAlgo(start_node, stop_node):

open_set = set(start_node)

closed_set = set()

g = {}

parents = {}

g[start_node] = 0

parents[start_node] = start_node

while len(open_set) > 0:

n = None
for v in open_set:

if n == None or g[v] + h(v) < g[n] + h(n):

n = v

if n == stop_node or Graph_nodes[n] == None:

pass

else:

for (m, weight) in get_neighbors(n):

if m not in open_set and m not in closed_set:

open_set.add(m)

parents[m] = n

g[m] = g[n] + weight

else:

if g[m] > g[n] + weight:

g[m] = g[n] + weight

parents[m] = n

if m in closed_set:

closed_set.remove(m)

open_set.add(m)

if n == None:

print('Path does not exist!')

return None

if n == stop_node:

path = []

while parents[n] != n:

path.append(n)
n = parents[n]

path.append(start_node)

path.reverse()

print('Path found: {}'.format(path))

return path

open_set.remove(n)

closed_set.add(n)

print('Path does not exist!')

return None

aStarAlgo('A', 'J')

Output
Path found: ['A', 'F', 'G', 'I', 'J']

['A', 'F', 'G', 'I', 'J']

You might also like