Skip to content

Instantly share code, notes, and snippets.

View hisuperaman's full-sized avatar
🎯
Focusing

Aman hisuperaman

🎯
Focusing
View GitHub Profile
@hisuperaman
hisuperaman / monkey-banana-problem.py
Created April 16, 2025 17:38
Monkey Banana Problem
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
@hisuperaman
hisuperaman / tsp-backtracking.py
Created April 16, 2025 17:16
TSP using Backtracking
# 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):
@hisuperaman
hisuperaman / tsp-dp.py
Created April 16, 2025 17:16
TSP using DP
# 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]
@hisuperaman
hisuperaman / n-queens-problem.py
Created April 1, 2025 16:57
N-Queens problem solution using backtracking
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
@hisuperaman
hisuperaman / warshall_algorithm.py
Created March 31, 2025 17:50
All pairs shortest distance Warshall Algorithm
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
@hisuperaman
hisuperaman / genetic_algorithm.py
Created March 26, 2025 02:54
Genetic algorithm to find target string from a population of random strings
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
@hisuperaman
hisuperaman / fractional-knapsack.py
Last active March 28, 2025 16:08
Fractional Knapsack Problem solution in Python
# 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
@hisuperaman
hisuperaman / hillClimbingTsp.py
Created February 28, 2025 08:44
Hill Climbing Algorithm in AI for solving Travelling Salesman Problem
import random
distanceMatrix = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]
def totalDistance(path):
from collections import deque
def bfs(graph, start, goal):
queue = deque([(start, [start])])
visited = set()
visited.add(start)
while queue:
node, path = queue.popleft()
def dfs(graph, start, goal):
stack = [(start, [start])]
visited = set()
visited.add(start)
while stack:
node, path = stack.pop()
if node==goal:
return path