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

Notebook

The document contains implementations of various graph search algorithms including Depth-First Search (DFS), Breadth-First Search (BFS), A* Search, and AO* Search. Each algorithm is defined with its respective function and example graph structure, demonstrating how to find a path from a start node to a goal node. The document provides code snippets for each algorithm, showcasing their functionality and usage.
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)
1 views6 pages

Notebook

The document contains implementations of various graph search algorithms including Depth-First Search (DFS), Breadth-First Search (BFS), A* Search, and AO* Search. Each algorithm is defined with its respective function and example graph structure, demonstrating how to find a path from a start node to a goal node. The document provides code snippets for each algorithm, showcasing their functionality and usage.
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/ 6

…………………………………………..

Dfs code…………………………………………………………………………………………

def dfs(node, goal, visited):

print("visiting:", node)

if node == goal:

return "Goal found", goal

visited.add(node)

for neighbor in graph[node]:

if neighbor not in visited:

result = dfs(neighbor, goal, visited)

if result:

return result

return None

graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': [],

'F': []

visited = set()

result = dfs('A', 'E', visited)

print(result if result else "goal not found")


--------------------Bfs code------------------------------------------------------------------------------------------------------

from collections import deque

def bfs(graph, start):

visited = set()

queue = deque([start])

visited.add(start)

while queue:

node = queue.popleft()

print(node, end=" ")

for neighbor in graph[node]:

if neighbor not in visited:

visited.add(neighbor)

queue.append(neighbor)

# Define the graph

graph = {

'A': ['B', 'C'],

'B': ['A', 'D', 'E'],

'C': ['A', 'F'],

'D': ['B'],

'E': ['B', 'F'],

'F': ['C', 'E']

# Execute BFS

bfs(graph, 'A')
-------------------------------------------------------------A* Search------------------------------------------------------

import heapq

from typing import Dict, List, Tuple, Callable, Any

def a_star_search(

start: Any,

goal: Any,

get_neighbors: Callable[[Any], List[Tuple[Any, int]]],

h: Callable[[Any, Any], int]

) -> List[Any]:

priority_queue = []

heapq.heappush(priority_queue, (h(start, goal), start))

g_costs = {start: 0}

came_from = {start: None}

while priority_queue:

_, current_node = heapq.heappop(priority_queue)

if current_node == goal:

path = []

while current_node is not None:

path.append(current_node)

current_node = came_from[current_node]

return path[::-1]

for neighbor, cost in get_neighbors(current_node):

new_g_cost = g_costs[current_node] + cost

if neighbor not in g_costs or new_g_cost < g_costs[neighbor]:

g_costs[neighbor] = new_g_cost

f_cost = new_g_cost + h(neighbor, goal)

heapq.heappush(priority_queue, (f_cost, neighbor))


came_from[neighbor] = current_node

return None

graph = {

'A': [('B', 1), ('C', 3)],

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

'C': [('F', 2)],

'D': [],

'E': [],

'F': []

# Function to get neighbors of a node

def get_neighbors(node: Any) -> List[Tuple[Any, int]]:

return graph.get(node, [])

def h(node: Any, goal: Any) -> int:

heuristics = {'A': 6, 'B': 4, 'C': 4, 'D': 3, 'E': 1, 'F': 2}

return heuristics.get(node, float('inf'))

# Execute A* search

result = a_star_search('A', 'E', get_neighbors, h)

print(result if result else 'No path found')


----------------------------------------------------AO*-----------------------------------------

from collections import defaultdict

from typing import Any, Callable, List, Tuple

def ao_star_search(start: Any, goal: Any, get_children: Callable[[Any], List[Tuple[Any, float]]]) ->
List[Any]:

open_set = {start}

costs = {start: 0}

parents = defaultdict(list)

while open_set:

current = min(open_set, key=costs.get)

if current == goal:

path = []

while current:

path.append(current)

current = parents[current][0] if parents[current] else None

return path[::-1]

open_set.remove(current)

for child, cost in get_children(current):

new_cost = costs[current] + cost

if child not in costs or new_cost < costs[child]:

costs[child] = new_cost

parents[child] = [current]

open_set.add(child)

return None

# Example graph structure


graph = {

'A': [('B', 1), ('C', 2)],

'B': [('D', 1), ('E', 3)],

'C': [('F', 2)],

'D': [], 'E': [], 'F': []

def get_children(node: Any) -> List[Tuple[Any, float]]:

return graph.get(node, [])

# Run AO* search

result = ao_star_search('A', 'E', get_children)

print(result if result else 'No path found')

You might also like