Ai Lab PDF
Ai Lab PDF
class Graph:
# Constructor
def __init__(self):
self.graph = defaultdict(list)
self.graph[u].append(v)
queue = []
queue.append(s)
visited[s] = True
while queue:
s = queue.pop(0)
# dequeued vertex s.
for i in self.graph[s]:
if not visited[i]:
queue.append(i)
visited[i] = True
# Driver code
if __name__ == '__main__':
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
g.BFS(2)
2]DFS
visited[s] = True
for i in adj[s]:
if not visited[i]:
dfs_rec(adj, visited, i)
dfs_rec(adj, visited, s)
adj[s].append(t)
adj[t].append(s)
if __name__ == "__main__":
V=5
edges = [[1, 2], [1, 0], [2, 0], [2, 3], [2, 4]]
for e in edges:
source = 1
dfs(adj, source)
3]TTT
visited[s] = True
# Print the current vertex
for i in adj[s]:
if not visited[i]:
dfs_rec(adj, visited, i)
dfs_rec(adj, visited, s)
adj[s].append(t)
adj[t].append(s)
if __name__ == "__main__":
V=5
edges = [[1, 2], [1, 0], [2, 0], [2, 3], [2, 4]]
for e in edges:
source = 1
dfs(adj, source)
4] 8 puzzle
import copy
# Importing the heap methods from the python
n=3
rows = [ 1, 0, -1, 0 ]
cols = [ 0, -1, 0, 1 ]
class priorityQueue:
# Priority Queue
def __init__(self):
self.heap = []
heappush(self.heap, key)
# funct to remove the element that is minimum,
def pop(self):
return heappop(self.heap)
def empty(self):
if not self.heap:
return True
else:
return False
class nodes:
costs, levels):
self.parent = parent
self.mats = mats
# useful for Storing the position where the
self.empty_tile_posi = empty_tile_posi
self.costs = costs
self.levels = levels
count = 0
for i in range(n):
for j in range(n):
if ((mats[i][j]) and
(mats[i][j] != final[i][j])):
count += 1
return count
new_mats = copy.deepcopy(mats)
x1 = empty_tile_posi[0]
y1 = empty_tile_posi[1]
x2 = new_empty_tile_posi[0]
y2 = new_empty_tile_posi[1]
costs, levels)
return new_nodes
# func to print the N by N matrix
def printMatsrix(mats):
for i in range(n):
for j in range(n):
print()
# matrix coordinates
# Printing the path from the root node to the final node
def printPath(root):
if root == None:
return
printPath(root.parent)
printMatsrix(root.mats)
print()
# method for solving N*N - 1 puzzle algo
pq = priorityQueue()
empty_tile_posi, costs, 0)
pq.push(root)
# the list.
# live nodes
minimum = pq.pop()
if minimum.costs == 0:
# destination;
printPath(minimum)
return
for i in range(n):
new_tile_posi = [
minimum.empty_tile_posi[0] + rows[i],
minimum.empty_tile_posi[1] + cols[i], ]
if isSafe(new_tile_posi[0], new_tile_posi[1]):
child = newNodes(minimum.mats,
minimum.empty_tile_posi,
new_tile_posi,
minimum.levels + 1,
minimum, final,)
pq.push(child)
# Main Code
# Initial configuration
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]
final = [ [ 1, 2, 3 ],
[ 5, 8, 6 ],
[ 0, 7, 4 ] ]
# initial configuration
empty_tile_posi = [ 1, 2 ]
m = {}
isSolvable = False
path = []
q = deque()
q.append((0, 0))
# Current state
u = q.popleft()
continue
continue
path.append([u[0], u[1]])
m[(u[0], u[1])] = 1
isSolvable = True
if (u[0] == target):
if (u[1] != 0):
path.append([u[0], 0])
else:
if (u[0] != 0):
path.append([0, u[1]])
sz = len(path)
for i in range(sz):
path[i][1], ")")
break
d = u[1] - ap
q.append([c, d])
c = u[0] - ap
d = u[1] + ap
q.append([c, d])
q.append([a, 0])
q.append([0, b])
if (not isSolvable):
if __name__ == '__main__':
def tsp(cost):
# Number of nodes
numNodes = len(cost)
minCost = float('inf')
# remaining nodes
currCost = 0
currNode = 0
currCost += cost[currNode][node]
currNode = node
# Add the cost to return to the starting node
currCost += cost[currNode][0]
# is lower
return minCost
if __name__ == "__main__":
cost = [
res = tsp(cost)
print(res)
7]TOH
if n==1:
return
# Driver code
n=4
TowerOfHanoi(n,'A','B','C')
9.AIPHA-BETA
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
# Recur for left and right children
break
return best
else:
best = MAX
# right children
break
return best
# Driver Code
if __name__ == "__main__":
import copy
n=3
rows = [ 1, 0, -1, 0 ]
cols = [ 0, -1, 0, 1 ]
class priorityQueue:
# Constructor for initializing a
# Priority Queue
def __init__(self):
self.heap = []
heappush(self.heap, key)
def pop(self):
return heappop(self.heap)
def empty(self):
if not self.heap:
return True
else:
return False
class nodes:
def __init__(self, parent, mats, empty_tile_posi,
costs, levels):
self.parent = parent
self.mats = mats
self.empty_tile_posi = empty_tile_posi
self.costs = costs
self.levels = levels
count = 0
for i in range(n):
for j in range(n):
if ((mats[i][j]) and
(mats[i][j] != final[i][j])):
count += 1
return count
new_mats = copy.deepcopy(mats)
x1 = empty_tile_posi[0]
y1 = empty_tile_posi[1]
x2 = new_empty_tile_posi[0]
y2 = new_empty_tile_posi[1]
costs, levels)
return new_nodes
def printMatsrix(mats):
for i in range(n):
for j in range(n):
print()
# matrix coordinates
def printPath(root):
if root == None:
return
printPath(root.parent)
printMatsrix(root.mats)
print()
pq = priorityQueue()
empty_tile_posi, costs, 0)
# Adding root to the list of live nodes
pq.push(root)
# the list.
# live nodes
minimum = pq.pop()
if minimum.costs == 0:
# destination;
printPath(minimum)
return
for i in range(n):
new_tile_posi = [
minimum.empty_tile_posi[0] + rows[i],
minimum.empty_tile_posi[1] + cols[i], ]
if isSafe(new_tile_posi[0], new_tile_posi[1]):
child = newNodes(minimum.mats,
minimum.empty_tile_posi,
new_tile_posi,
minimum.levels + 1,
minimum, final,)
pq.push(child)
# Main Code
# Initial configuration
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]
[ 5, 8, 6 ],
[ 0, 7, 4 ] ]
# initial configuration
empty_tile_posi = [ 1, 2 ]