0% found this document useful (0 votes)
5 views2 pages

AI Lab4

This document describes the implementation of a uniform cost search (UCS) algorithm. It includes a flow chart and code to perform UCS on a graph by maintaining a priority queue of nodes and tracking explored nodes with costs.

Uploaded by

umerali.dev
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)
5 views2 pages

AI Lab4

This document describes the implementation of a uniform cost search (UCS) algorithm. It includes a flow chart and code to perform UCS on a graph by maintaining a priority queue of nodes and tracking explored nodes with costs.

Uploaded by

umerali.dev
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/ 2

Implementation of UCS

Flow-Chart:
Code:

def ucs(initial, goal, graph):


# Creating Priority Queue Object
frontier = PriorityQueue()
# Creating Node Object
inode = Node(initial, None, 0)
frontier.push(inode)
explored = {initial: 0}

while len(frontier._container) != 0:
# print(frontier)
# pop node from frontier
node = frontier.pop()

# test goal state


if (goal_test(node, goal)):
return node_to_path(node)

# if goal not reached, push the ancestors in the priority queue


for successor, cost in graph[node.state]:
# check for the explored nodes
if successor not in explored:
new_cost = explored[node.state] + cost
frontier.push(Node(successor, node, new_cost))
explored[successor] = new_cost
return None

# Applying UCS on a graph


print(ucs('Arad', 'Bucharest', romania))

Output:

You might also like