This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MonkeyBananaProblem: | |
def __init__(self): | |
self.monkey_position = "door" | |
self.box_position = "corner" | |
self.banana_position = "middle" | |
self.monkey_on_box = False | |
def move_monkey(self, to): | |
print(f"Monkey moves from {self.monkey_position} to {to}") | |
self.monkey_position = to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a program to implement Traveling salesman problem | |
distanceMatrix = [ | |
[0, 10, 15, 20], | |
[10, 0, 35, 25], | |
[15, 35, 0, 30], | |
[20, 25, 30, 0] | |
] | |
def tsp(graph, visited, cur_node, count, cost, ans): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a program to implement Traveling salesman problem | |
def tsp_dp(graph, start): | |
N = len(graph) | |
VISITED_ALL = (1 << N) - 1 | |
dp = [[-1] * N for _ in range(1 << N)] | |
def tsp(mask, pos): | |
if mask==VISITED_ALL: | |
return graph[pos][start] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isSafe(row, col, board, n): | |
x = row | |
y = col | |
while y>=0: | |
if board[x][y]=='Q': | |
return False | |
y -= 1 | |
x = row | |
y = col |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def floyd_warshall(G): | |
distance = [row[:] for row in G] | |
nV = len(G) | |
for k in range(nV): | |
for i in range(nV): | |
for j in range(nV): | |
distance[i][j] = min(distance[i][j], distance[i][k]+distance[k][j]) | |
return distance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
def initialize_population(): | |
population = [] | |
for i in range(POP_SIZE): | |
chromosome = ''.join(random.choices(GENES, k=CHROMOSOME_LENGTH)) | |
population.append(chromosome) | |
return population |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# write a program to find solution for knapsack problem using greedy method | |
def getMaxProfit(M, n, W, P): | |
# sort PWRatio with item number | |
items = sorted(enumerate(zip(W, P)), key=lambda x: x[1][1]/x[1][0], reverse = True) | |
totalProfit = 0 | |
selectedItems = [] | |
for index, (w, p) in items: | |
if M<=0: | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
distanceMatrix = [ | |
[0, 10, 15, 20], | |
[10, 0, 35, 25], | |
[15, 35, 0, 30], | |
[20, 25, 30, 0] | |
] | |
def totalDistance(path): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
def bfs(graph, start, goal): | |
queue = deque([(start, [start])]) | |
visited = set() | |
visited.add(start) | |
while queue: | |
node, path = queue.popleft() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfs(graph, start, goal): | |
stack = [(start, [start])] | |
visited = set() | |
visited.add(start) | |
while stack: | |
node, path = stack.pop() | |
if node==goal: | |
return path |
NewerOlder