You are on page 1of 7

ASSIGNMENT -1

CODE:-     for neighbour in graph[m]:

BFS  

graph = {     if neighbour not in visited:

'5' : ['3','7'],         visited.append(neighbour)

  '3' : ['2', '4'],         queue.append(neighbour)

  '7' : ['8'],

  '2' : [], print("Following is the Breadth-First


Search")
  '4' : ['8'],
bfs(visited, graph, '5')  
  '8' : []

visited = [].
OUTPUT:-
queue = []     

def bfs(visited, graph, node):

  visited.append(node)

  queue.append(node)

  while queue:         

    m = queue.pop(0) 

    print (m, end = " ") 


CODE:-

 DFS

graph = { OUTPUT:-

  '5' : ['3','7'],

  '3' : ['2', '4'],

  '7' : ['8'],

  '2' : [],

  '4' : ['8'],

  '8' : []

visited = set() # Set to keep track of visited


nodes of graph.

def dfs(visited, graph, node): 

    if node not in visited:

        print (node)

        visited.add(node)

        for neighbour in graph[node]:

            dfs(visited, graph, neighbour)

print("Following is the Depth-First


Search")

dfs(visited, graph, '5')


ASSIGNMENT - 3 print("The optimal value is :",
minimax(0, 0, True, values, MIN, MAX))

CODE:-
MAX, MIN = 1000, -1000
OUTPUT:-
def minimax(depth, nodeIndex,
maximizingPlayer,values, alpha, beta):
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
for i in range(0, 2):
val = minimax(depth + 1,
nodeIndex * 2 + i,False, values, alpha,
beta)
best = max(best, val)
alpha = max(alpha, best)
if beta <= alpha:
break
return best
else:
best = MAX
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex *
2 + i,True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)
if beta <= alpha:
break

return best
if __name__ == "__main__":
values = [3, 5, 6, 9, 1, 2, 0, -1] ASSIGNMENT - 2
g[m] = g[n] + weight

CODE:- #change parent of m to n

def aStarAlgo(start_node, stop_node): parents[m] = n

open_set = set(start_node) #if m in closed set,remove and


add to open
closed_set = set()
if m in closed_set:
g = {}
closed_set.remove(m)
parents = {}
open_set.add(m)
g[start_node] = 0
if n == None:
parents[start_node] = start_node
print('Path does not exist!')
while len(open_set) > 0:
return None
n = None
for v in open_set:
# if the current node is the stop_node
if n == None or g[v] + heuristic(v) <
g[n] + heuristic(n): # then we begin reconstructin the path
from it to the start_node
n=v
if n == stop_node:
if n == stop_node or Graph_nodes[n] ==
None: path = []

pass while parents[n] != n:

else: path.append(n)

for (m, weight) in get_neighbors(n): n = parents[n]

#nodes 'm' not in first and last set are path.append(start_node)


added to first
path.reverse()
#n is set its parent
print('Path found: {}'.format(path))
if m not in open_set and m not in
return path
closed_set:
# remove n from the open_list, and add it
open_set.add(m)
to closed_list
parents[m] = n
# because all of his neighbors were
g[m] = g[n] + weight inspected

#for each node m,compare its open_set.remove(n)


distance from start i.e g(m) to the
closed_set.add(n)
#from start through n node
print('Path does not exist!')
else:
return None
if g[m] > g[n] + weight:
#update g(m)
#define fuction to return neighbor and its 'I': [('E', 5), ('G', 3), ('H', 2), ('J', 3)],
distance
}
#from the passed node
def get_neighbors(v):
aStarAlgo('A', 'J')
if v in Graph_nodes:
return Graph_nodes[v]
OUTPUT:-
else:
return None
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 5,
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
}
return H_dist[n]

#Describe your graph here


Graph_nodes = {
'A': [('B', 6), ('F', 3)],
'B': [('A', 6), ('C', 3), ('D', 2)],
'C': [('B', 3), ('D', 1), ('E', 5)],
'D': [('B', 2), ('C', 1), ('E', 8)],
'E': [('C', 5), ('D', 8), ('I', 5), ('J', 5)],
'F': [('A', 3), ('G', 1), ('H', 7)],
'G': [('F', 1), ('I', 3)],
'H': [('F', 7), ('I', 2)],
ASSIGNMENT 4

CODE:- OUTPUT:-
def printjobschedule(array, t):
m = len(array)
for j in range(m):
for q in range(m - 1 - j):
if array[q][2] < array[q + 1][2]:
array[q], array[q + 1] = array[q + 1],
array[q]
res = [False] * t
# To store result
job = ['-1'] * t
for q in range(len(array)):
# Find a free slot
for q in range(min(t - 1, array[q][1] - 1), -
1, -1):
if res[q] is False:
res[q] = True
job[q] = array[q][0]
break
print(job)
array = [['a', 7, 202],
['b', 5, 29],
['c', 6, 84],
['d', 1, 75],
['e', 2, 43]]
print("Maximum profit sequence of jobs is- ")
printjobschedule(array, 3)
ASSIGNMENT 5:-

CODE:-

print("Simple Question and Answering OUTPUT:-


Program")print("========================
=============")

print(" You may ask any one of these


questions")

print("Hi")

print("How are you?")

print("Are you working?")

print("What is your name?")

print("what did you do yesterday?")

print("Quit")while True:question =
input("Enter one question from above
list:")question = question.lower()

if question in ['hi']:

print("Hello")

elif question in ['how are you?','how do you


do?']:

print("I am fine")

elif question in ['are you working?','are you


doing any job?']:

print("yes. I'am working in KLU")

elif question in ['what is your name?']:

print("My name is JK")

name=input("Enter your name?")  

print("Nice name and Nice meeting


you",name)

elif question in ['what did you do yesterday?']:

print("I saw Bahubali 5 times")

elif question in ['quit']:

break

else:print("I don't understand what you said")

You might also like