0% found this document useful (0 votes)
78 views7 pages

Jug Problem and Game Implementations

The document describes implementations of depth first search (DFS) and breadth first search (BFS) algorithms on graphs. For DFS, it defines a graph as a dictionary and performs a recursive DFS function that prints each visited node. For BFS, it defines a Graph class with edge addition and performs a BFS function using a queue that prints visited nodes level-by-level. Both algorithms are demonstrated on sample graph data with output showing the traversal order.

Uploaded by

Arpit Tyagi
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)
78 views7 pages

Jug Problem and Game Implementations

The document describes implementations of depth first search (DFS) and breadth first search (BFS) algorithms on graphs. For DFS, it defines a graph as a dictionary and performs a recursive DFS function that prints each visited node. For BFS, it defines a Graph class with edge addition and performs a BFS function using a queue that prints visited nodes level-by-level. Both algorithms are demonstrated on sample graph data with output showing the traversal order.

Uploaded by

Arpit Tyagi
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

Assignment 5(a)

NAME – Shivam Kumar GROUP NO. –G2


ROLL NO. - 2100270140052
5(a). Solve two jug Problem.
from collections import deque
def BFS(a, b, target):
m = {}
isSolvable = False
path = []
q = deque()
[Link]((0, 0))
while (len(q) > 0):
u = [Link]()
if ((u[0], u[1]) in m):
continue
if ((u[0] > a or u[1] > b or
u[0] < 0 or u[1] < 0)):
continue
[Link]([u[0], u[1]])
m[(u[0], u[1])] = 1
if (u[0] == target or u[1] == target):
isSolvable = True
if (u[0] == target):
if (u[1] != 0):
[Link]([u[0], 0])
else:
if (u[0] != 0):
[Link]([0, u[1]])
sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break
[Link]([u[0], b])
[Link]([a, u[1]])
for ap in range(max(a, b) + 1):
c = u[0] + ap
d = u[1] - ap
if (c == a or (d == 0 and d >= 0)):
[Link]([c, d])
c = u[0] - ap
d = u[1] + ap
if ((c == 0 and c >= 0) or d == b):
[Link]([c, d])
[Link]([a, 0])
[Link]([0, b])
if (not isSolvable):
print("No solution");
if __name__ == '__main__':
Jug1=int(input("Enter the cpacity of jug1 : "));
Jug2=int(input("Enter the capacity of jug2 : "));
target=int(input("Enter the target you want to achive : "));
BFS(Jug1, Jug2, target)

OUTPUT
Enter the cpacity of jug1 : 4
Enter the capacity of jug2 : 6
Enter the target you want to achive : 2
(0,0)
(0,6)
(4,0)
(4,6)
(4,2)
(0,2)
Assignment 5(b)

NAME – Shivam Kumar GROUP NO. – G2


ROLL NO. - 2100270140052
5(b). Implement Tic- Tac-Toe.
import random
class TicTacToe:
def __init__(self):
[Link] = []
def create_board(self):
for i in range(3):
row = []
for j in range(3):
[Link]('-')
[Link](row)
def get_random_first_player(self):
return [Link](0, 1)
def fix_spot(self, row, col, player):
[Link][row][col] = player
def is_player_win(self, player):
win = None
n = len([Link])
for i in range(n):
win = True
for j in range(n):
if [Link][i][j] != player:
win = False
break
if win:
return win
for i in range(n):
win = True
for j in range(n):
if [Link][j][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if [Link][i][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if [Link][i][n - 1 - i] != player:
win = False
break
if win:
return win
return False
for row in [Link]:
for item in row:
if item == '-':
return False
return True
def is_board_filled(self):
for row in [Link]:
for item in row:
if item == '-':
return False
return True
def swap_player_turn(self, player):
return 'X' if player == 'O' else 'O'
def show_board(self):
for row in [Link]:
for item in row:
print(item, end=" ")
print()
def start(self):
self.create_board()
player = 'X' if self.get_random_first_player() == 1 else 'O'
while True:
print(f"Player {player} turn")
self.show_board()
row, col = list(
map(int, input("Enter row and column numbers to fix spot: ").split()))
print()
self.fix_spot(row - 1, col - 1, player)
if self.is_player_win(player):
print(f"Player {player} wins the game!")
break
if self.is_board_filled():
print("Match Draw!")
break
player = self.swap_player_turn(player)
print()
self.show_board()
tic_tac_toe = TicTacToe()
tic_tac_toe.start()

OUTPUT
Player X turn
---
---
---
Enter row and column numbers to fix spot: 3 3

Player O turn
---
---
--X
Enter row and column numbers to fix spot: 1 1

Player X turn
O--
---
--X
Enter row and column numbers to fix spot: 1 3

Player O turn
O-X
---
--X
Enter row and column numbers to fix spot: 2 3

Player X turn
O-X
--O
--X
Enter row and column numbers to fix spot: 3 1

Player O turn
O-X
--O
X-X
Enter row and column numbers to fix spot: 2 2

Player X turn
O-X
-OO
X-X
Enter row and column numbers to fix spot: 3 2

Player X wins the game!

O-X
-OO
XXX
Assignment 6(a)

NAME – Shivam Kumar GROUP NO. – G2


ROLL NO. - 2100270140052
6(a). Implement Depth First Search.
graph = {
'6' : ['5','4'],
'4' : ['3', '5'],
'5' : ['7'],
'3' : [],
'7' : ['6'],
'2' : ['5'],
'1' : ['2','3']
}

visited = set()

def dfs(visited, graph, node):


if node not in visited:
print (node)
[Link](node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

print("The Depth-First Search traversal is: ")


dfs(visited, graph, '1')

OUTPUT

The Depth-First Search traversal is:


1
2
5
7
6
4
3
Assignment 6(b)

NAME – Shivam Kumar GROUP NO. – G2


ROLL NO. - 2100270140052

6(b). Implement Breadth First Search.


from collections import defaultdict as dd
class Graph:
def __init__(self):
[Link] = dd(list)
def addEdgetoGraph(self, x, y):
[Link][x].append(y)
def BFSearch(self, n):
visited_vertices = ( len([Link] ))*[False]
queue = []
visited_vertices[n] = True
[Link](n)
while queue:
n = [Link](0)
print (n)
for v in [Link][ n ]:
if visited_vertices[v] == False:
[Link](v)
visited_vertices[v] = True
graph = Graph()
[Link](0, 1)
[Link](1, 3)
[Link](4, 5)
[Link](2, 4)
[Link](3, 2)
[Link](5, 4)
[Link](6, 5)
print ( " The Breadth First Search Traversal is : " )
[Link](1)

OUTPUT
The Breadth First Search Traversal is :
1
3
2
4
5

You might also like