Professional Documents
Culture Documents
BFS
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
queue = []
queue.append(s)
visited[s] = True
while queue:
s = queue.pop(0)
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
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)
DFS
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def DFSUtil(self,v,visited):
visited[v]= True
print (v)
for i in self.graph[v]:
if visited[i] == False:
self.DFSUtil(i, visited)
def DFS(self,v):
visited = [False]*(len(self.graph))
self.DFSUtil(v,visited)
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.DFS(2)
Hanoi
if n == 1:
return
# Driver code
n=4
TowerOfHanoi(n , "A","B","C")
N_Queen Problem
class QueenChessBoard:
self.size = size
self.columns = []
self.columns.append(column)
def remove_in_current_row(self):
return self.columns.pop()
row = len(self.columns)
# check column
if column == queen_column:
return False
# check diagonal
return False
return False
return True
def display(self):
if column == self.columns[row]:
else:
print()
def solve_queen(size):
board = QueenChessBoard(size)
number_of_solutions = 0
row = 0
column = 0
while True:
if board.is_this_column_safe_in_next_row(column):
board.place_in_next_row(column)
row += 1
column = 0
break
else:
column += 1
if row == size:
board.display()
print()
number_of_solutions += 1
# small optimization:
# the last row where a queen can be placed. In this case, there
board.remove_in_current_row()
row -= 1
# now backtrack
try:
prev_column = board.remove_in_current_row()
except IndexError:
break
row -= 1
column = 1 + prev_column
n = int(input('Enter n: '))
solve_queen(n)
alpha_beta
tree = [[[5, 1, 2], [8, -8, -9]], [[9, 4, 5], [-3, 4, 3]]]
root = 0
pruned = 0
global tree
global root
global pruned
i=0
if type(child) is list:
if depth % 2 == 1:
else:
i += 1
else:
alpha = child
beta = child
pruned += 1
break
if depth == root:
global tree
global pruned
global root
if __name__ == "__main__":
if __name__ == "__main__":
alphabeta()
if jug2 is fill:
return
pour(0, jug1)
pour(0, jug1)
pour(jug1, 0)
pour(max1, jug2)
pour(0, (jug1+jug2))
else:
pour(jug1-(max2-jug2), (max2-jug2)+jug2)
print("JUG1\tJUG2")
pour(0, 0)
choices = []
choices.append(str(x + 1))
playerOneTurn = True
winner = False
def printBoard() :
printBoard()
if playerOneTurn :
else :
try:
except:
continue
continue
if playerOneTurn :
choices[choice - 1] = 'X'
else :
choices[choice - 1] = 'O'
y=x*3
winner = True
printBoard()
winner = True
printBoard()
winner = True
printBoard()
deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))
random.shuffle(deck)
print("You got:")
for i in range(5):
World problem
constraints = [
print(backtrack(my_problem))
print(backtrack(my_problem,
variable_heuristic=MOST_CONSTRAINED_VARIABLE))
print(backtrack(my_problem,
variable_heuristic=HIGHEST_DEGREE_VARIABLE))
print(backtrack(my_problem,
value_heuristic=LEAST_CONSTRAINING_VALUE))
print(backtrack(my_problem,
variable_heuristic=MOST_CONSTRAINED_VARIABLE,
value_heuristic=LEAST_CONSTRAINING_VALUE)s)
print(backtrack(my_problem,
variable_heuristic=HIGHEST_DEGREE_VARIABLE,
value_heuristic=LEAST_CONSTRAINING_VALUE))
print(min_conflicts(my_problem))
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
break
return best
else:
best = MAX
# right children
break
return best
# Driver Code
if _name_ == "_main_":
#include <bits/stdc++.h>
#define V 4
vector<int> vertex;
if (i != s)
vertex.push_back(i);
do {
int current_pathweight = 0;
int k = s;
current_pathweight += graph[k][vertex[i]];
k = vertex[i];
current_pathweight += graph[k][s];
// update minimum
return min_path;
int main()
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
int s = 0;
return 0;