0% found this document useful (0 votes)
1 views4 pages

DFS BFS Graph python

The document provides implementations of various graph traversal algorithms including Depth-First Search (DFS), Breadth-First Search (BFS), Best-First Search, and A* Search. Each algorithm is demonstrated using a simple directed graph and includes example usage for traversing the graph. The Best-First Search utilizes a heuristic for prioritizing nodes, while A* Search combines path cost with heuristic estimates.

Uploaded by

dosojac340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

DFS BFS Graph python

The document provides implementations of various graph traversal algorithms including Depth-First Search (DFS), Breadth-First Search (BFS), Best-First Search, and A* Search. Each algorithm is demonstrated using a simple directed graph and includes example usage for traversing the graph. The Best-First Search utilizes a heuristic for prioritizing nodes, while A* Search combines path cost with heuristic estimates.

Uploaded by

dosojac340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Graph

graph = {
'A': ['B', 'C'],
'B': ['D'],
'C': ['E'],
'D': [],
'E': []
}

DFS
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start, end=' ')

for neighbor in graph[start]:


if neighbor not in visited:
dfs(graph, neighbor, visited)

# Usage
print("DFS traversal:")
dfs(graph, 'A')

BFS(breadth)
from collections import deque

def bfs(graph, start):


visited = set()
queue = deque([start])

while queue:
node = queue.popleft()
if node not in visited:
print(node, end=' ')
visited.add(node)
queue.extend(graph[node])

# Usage
print("\nBFS traversal:")
bfs(graph, 'A')

Best first
import heapq

# Simple heuristic (for example purposes)


heuristic = {
'A': 3,
'B': 2,
'C': 4,
'D': 1,
'E': 0
}

def best_first_search(graph, start, goal):


visited = set()
pq = [(heuristic[start], start)]

while pq:
_, node = heapq.heappop(pq)
if node not in visited:
print(node, end=' ')
visited.add(node)
if node == goal:
break
for neighbor in graph[node]:
if neighbor not in visited:
heapq.heappush(pq, (heuristic[neighbor], neighbor))

# Usage
print("\nBest-First Search (Greedy):")
best_first_search(graph, 'A', 'E')

A*
def a_star(graph, start, goal, heuristic):
open_set = [(heuristic[start], 0, start)]
visited = set()

while open_set:
f, g, current = heapq.heappop(open_set)
if current in visited:
continue
print(current, end=' ')
visited.add(current)

if current == goal:
break

for neighbor in graph[current]:


if neighbor not in visited:
new_g = g + 1 # assume cost=1 for each edge
f = new_g + heuristic[neighbor]
heapq.heappush(open_set, (f, new_g, neighbor))

# Usage
print("\nA* Search:")
a_star(graph, 'A', 'E', heuristic)

You might also like