Professional Documents
Culture Documents
BACKTRACKING
ALGORITHM
INTRODUCTION TO BACTRACKING
ALGORITHM
CPET5 LECTURE
Presented By:
Sarceno,Angeline V.
Romero, Marielle
Leonida, Joseph
De Jose, Jm
TABLE OF CONTENT
01 BACKTRACKING ALGRORITHM
02 BACTRACKING ALGORITHM
04 PROBLEMS
APPLICATION OF BACTRACKING
06 ALGORITHM
SAMPLE CODES
USER INTERFACE
BACKTRACKING ALGORITHM
WHAT IS BACKTRACKING?
BACKTRACKING ALGORITHM
BACKTRACKING PROBLEM
used to search or to find a used to find the best used to find the set of all
feasible solution of the solution that can be feasible solutions of the
problem applied problem
TYPES OF BACKTRACKING
ALGORITHM
E
APPLICATION
SOURCE
DESTINATION
APPLICATION
Code:
Finding all possible order of
def permute(list, s):
arrangements of a given set
if list == 1:
of letters. When we choose a
return s
pair we apply backtracking
else:
to verify if that exact pair
return [
has already been created or
y+x
not. If not already created,
for y in permute(1, s)
the pair is added to the
for x in permute(list - 1, s)
answer list else it is ignored.
]
print(permute(1, ["a","b","c"]))
print(permute(2, ["a","b","c"]))
Output:
['a', 'b', 'c']
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
EXAMPLE CODE # 2
Code:
Getting multiples of 2 from def multiple_of_2(nums, multiple_list):
set of numbers 1-10 if not nums:
return multiple_list
else:
num = nums.pop()
if num % 2 == 0:
multiple_list.append(num)
return multiple_of_2(nums, multiple_list)
Output:
['10','8','6','4','2']
USER INTERFACE
n = int (input("enter the value of n:")) #Backtracking
def nqn(board,row):
board= [[0 for i in range(n)] for i in range(n)] if row==n:
return True
def check_column(board, row,column):
for i in range(row,-1,-1): for i in range(n):
if board[i][column]==1: if (check_column(board,row,i)==True and
return false cheack_diagonal(board,row,i)==True):
return True board[row][i]=1
if nqn(board, row=1):
return True
def cheack_diagonal(board, row, column):
board[row][i]=0
for i, j in zip(range(row,-1,-1), range(column,-1,-1)):
return False
return False
for i,j in zip(range(row, -1,-1),range(column,n)):
if board [i][j]==1:
nqn(board,0)
return Fase
return True
for row in board:
print(row)
USER INTERFACE
Output:
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
USER INTERFACE
zodiac_signs = { def get_zodiac_sign(day, month):
"capricorn": (12, 22, -1, 19), for sign, (start_month, start_day, end_month, end_day)
"aquarius": (1, 20, -2, 18), in zodiac_signs.items():
"pisces": (2, 19, -3, 20), if (month == start_month and day >= start_day) or
"aries": (3, 21, -4, 19), (month == end_month and day <= end_day) or
"taurus": (4, 20, -5, 20), (start_month < month < end_month):
"gemini": (5, 21, -6, 20), return sign
"cancer": (6, 21, -7, 22), return None
"leo": (7, 23, -8, 22),
"virgo": (8, 23, -9, 22), print("Enter your birthday (DD MM):")
day, month = map(int, input().split())
"libra": (9, 23, -10, 22),
"scorpio": (10, 23, -11, 21),
sign = get_zodiac_sign(day, month)
"sagittarius": (11, 22, -12, 21)
if sign:
}
print("Your zodiac sign is", sign)
else:
print("Invalid date")
USER INTERFACE
Output:
5. It is tries out all the possible solutions and chooses the desired/best solutions.
This approach is used to solve problems that have multiple solutions.
8-10. Debuging
Fill in the
USERblanks:
INTERFACE
8. n = ________________________
board= [[0 for i in range(n)] for i in range(n)]
#Backtracking
def nqn(board,row):
if row==n:
def check_column(board, row,column): return True
for i in range(row,-1,-1):
if board[i][column]==1: for i in range(n):
return false if (check_column(board,row,i)==True and
return True cheack_diagonal(board,row,i)==True):
board[row][i]=1
def cheack_diagonal(board, row, column): if nqn(board, row=1):
return True
for i, j in zip(range(row,-1,-1), range(column,-1,-1)):
board[row][i]=0
return False
for i,j in zip(range(row, -1,-1),range(column,n)):
return False
if board [i][j]==1:
return Fase
nqn(board,0)
return True
Output:
_______________
10. CGKBINACKTRA
Answers:
1. Enumeration Problem