0% found this document useful (0 votes)
50 views47 pages

DAA - LAB Manual-Anna-University-Regulation-2021

Uploaded by

archana
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)
50 views47 pages

DAA - LAB Manual-Anna-University-Regulation-2021

Uploaded by

archana
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
You are on page 1/ 47

DESIGN AND ANALYSIS OF AN ALGORITHM

LAB MANUAL
AD3351
EX.No:1A IMPLEMENT RECURSIVE AND NON-RECURSIVE
ALGORITHMSANDSTUDYTHEORDEROFGROWTH
FROMlog2nto n!

AIM:
Toimplementrecursiveandnon-recursivealgorithmsforvariousfunctions and
study their order of growth from log ton!.

ALGORITHM:
1. RecursiveAlgorithm:
 BaseCase:Ifnis0or1,return 1.
 RecursiveCase:Returnn multiplied bythefactorialof(n-1).
The algorithm is linear, takes the running time O(n).
2. Non-RecursiveAlgorithm:
 Initializeavariableto1.
 Iterate from1 ton,multiplyingthecurrentvaluebytheiterator.
Iterator takes the running time O(mn)

PROGRAM:
import time
importmath

# Recursive Algorithm
defrecursive_factorial(n):
ifn==0orn==1: return 1
returnn*recursive_factorial(n -1)

# Non-Recursive Algorithm
defnon_recursive_factorial(n):
result= 1
foriinrange(1,n+1):
result *= i
returnresult

#Measuretimecomplexity
defmeasure_time_complexity(algorithm,n): start_time
= time.time()
algorithm(n)
end_time = time.time()
returnend_time-start_time

# Main program
foriinrange(1,6):
n=int(math.pow(2,i))
recursive_time = measure_time_complexity(recursive_factorial, n)
non_recursive_time=measure_time_complexity(non_recursive_factorial,
n)
print(f"n=2^{i}")
print(f"RecursiveTime:{recursive_time}")
print(f"Non-RecursiveTime:{non_recursive_time}")
print("------")

OUTPUT:

RESULT:
Thus,therecursiveandnon-recursivealgorithmsforvariousfunctionsand study their
order of growth was implemented successfully.
EX.No:2 DIVIDEANDCONQUER–STRASSEN’sMATRIX
MULTIPLICATION

AIM:
To implement Strassen’s MatrixMultiplication using the Divide and Conquer
approach and demonstrate its application on directed acyclic graphs (DAGs).

ALGORITHM:
Strassen'salgorithmisanefficientmethodformultiplyingtwomatricesusing
adivideandconquerstrategy.Giventwosquarematrices A and B oforder
n×n, thegoal istocomputetheirproductC=A×B.
1.Strassen'sMatrixMultiplication
 BreakdownthematrixmultiplicationintosubproblemsusingStrassen's
approach
 Utilizerecursivecallstosolvethesesubproblems.
 Combinetheresultstoobtainthefinalproduct.

PROGRAM:
importnumpyasnp

defstrassen_multiply(A,B): n
= len(A)

#Basecase: Ifthematricesare1x1,performstandardmultiplication if n
== 1:
returnnp.array([[A[0][0]*B[0][0]]])
#Splitmatricesintofourequal-sized submatrices
a11, a12, a21, a22 = A[:n//2, :n//2], A[:n//2, n//2:], A[n//2:, :n//2], A[n//2:,
n//2:]
b11, b12, b21, b22 = B[:n//2, :n//2], B[:n//2, n//2:], B[n//2:, :n//2], B[n//2:,
n//2:]

# Recursive computation of seven products


p1=strassen_multiply(a11+a22,b11+b22) p2
= strassen_multiply(a21 + a22, b11)
p3= strassen_multiply(a11, b12 -b22)
p4= strassen_multiply(a22, b21 -b11)
p5=strassen_multiply(a11+a12, b22)
p6=strassen_multiply(a21 -a11,b11+b12)
p7=strassen_multiply(a12-a22,b21+ b22)

#Combinetheproductstogettheresultmatrix c11 =
p1 + p4 - p5 + p7
c12=p3+p5
c21=p2+p4
c22=p1-p2+p3+p6

#Combinethesubmatricesintotheresult matrix
result=np.vstack((np.hstack((c11,c12)),np.hstack((c21,c22))))
returnresult

#Main program
if name ==" main ":
#ExamplematricesAandB
A=np.array([[1, 2],[3,4]])
B=np.array([[5,6],[7,8]])

#PerformStrassen'sMatrixMultiplication
result = strassen_multiply(A, B)

# Display the result


print("Matrix A:")
print(A) print("\
nMatrixB:")
print(B)
print("\nResultantMatrixC(Strassen'sMultiplication):")
print(result)
OUTPUT:
Matrix A:
[[12]
[34]]

Matrix B:
[[56]
[78]]

ResultantMatrixC(Strassen'sMultiplication):
[[1922]
[43 50]]

RESULT:
Thus,theStrassen’sMatrixMultiplicationusingtheDivideandConquer
approachwasimplementedsuccessfully.
EX.No:3 DECREASEANDCONQUER– TOPOLOGICAL
SORTING

AIM:
ToimplementTopologicalSortingusingtheDecreaseand Conquerapproach in
Python and analyze its performance

ALGORITHM:
Topological Sorting is an ordering of vertices in a directed acyclic graph
(DAG)such that foreverydirected edge(u, v), vertexu comes before vin the
ordering. The objective is to find a topological ordering of the vertices.
1. DecreaseandConquer-TopologicalSorting:

 Findavertexwithin-degree0(avertexwithnoincomingedges).
 Removethevertexanditsoutgoingedgesfromthegraph.
 Repeattheprocessuntilallverticesareprocessed

PROGRAM:
fromcollectionsimportdefaultdict

classGraph:
def init (self, vertices):
self.vertices = vertices
self.graph=defaultdict(list)

def add_edge(self, u, v):


self.graph[u].append(v)
deftopological_sort_util(self,v,visited,stack):
visited[v] = True
forneighborinself.graph[v]: if
not visited[neighbor]:
self.topological_sort_util(neighbor,visited,stack)
stack.append(v)

deftopological_sort(self):
visited=[False]*self.vertices stack
= []

foriinrange(self.vertices): if
not visited[i]:
self.topological_sort_util(i,visited,stack)
return stack[::-1]

#Main program
if name == " main ":
# Create a graph
g = Graph(6)
g.add_edge(5,2)
g.add_edge(5,0)
g.add_edge(4,0)
g.add_edge(4,1)
g.add_edge(2,3)
g.add_edge(3,1)

#PerformTopologicalSorting
result = g.topological_sort()

# Display the result


print("TopologicalSortingOrder:")
print(result)

OUTPUT:
TopologicalSortingOrder:
[5,4,2,3,1,0]

RESULT:
Thus, the Topological Sorting using the Decrease and Conquer approach was
implemented successfully.
EX.No:4 TRANSFORMANDCONQUER–HEAP SORT

AIM:
To implement Heap Sort using the Transform and Conquer approach in
Python and analyze performance.

ALGORITHM:
Heap Sort is a comparison-based sorting algorithm that uses a binary heap
data structure to build a max-heap (or min-heap) and then perform a heap-
based sorting. The objective is to sort an array in ascending (or descending)
order.
Transformand Conquer-HeapSort:
 Transformtheinput arrayintoamax-heap.
 Repeatedlyextractthe maximumelement(root oftheheap)and swapit with
the last element of the heap.
 Reducetheheapsizeby1andheapifytheremainingelements.
 Repeattheprocessuntiltheheapis empty.

PROGRAM:
defheapify(arr,n,i): largest
=i
left_child = 2 * i + 1
right_child=2*i+2

ifleft_child<nandarr[i]<arr[left_child]:
largest = left_child
ifright_child<nandarr[largest]<arr[right_child]:
largest = right_child

iflargest!=i:
arr[i],arr[largest]=arr[largest],arr[i]
heapify(arr, n, largest)

defheap_sort(arr):
n = len(arr)

#Buildmaxheap
foriinrange(n//2-1,-1,-1):
heapify(arr, n, i)

#Extractelementsonebyone
for i in range(n - 1, 0, -1):
arr[i],arr[0]=arr[0],arr[i]#swap heapify(arr,
i, 0)

#Main program
ifname== "main":
#Examplearrayfortesting
arr = [12, 11, 13, 5, 6, 7]
#PerformHeapSort
heap_sort(arr)

#Displaytheresult
print("SortedarrayusingHeapSort:") print(arr)

OUTPUT:
Originalarray:[12,11,13,5,6,7]
SortedarrayusingHeapSort:[5,6,7,11,12,13]

RESULT:
Thus,theHeapSortusingtheTransformandConquerapproachwasimplemented
successfully.
EX.No:5a DYNAMICPROGRAMMING–
COINCHANGE PROBLEM

AIM:
ToimplementtheCoinChangeProblemusingdynamicprogrammingapproach in
Python.

ALGORITHM:
Givena set of coins anda target sum, the objective is tofindthe numberof ways
to make the target sum using anycombination of the given coins.
ProgramLogic:
1. Createatabletostorethesolutionstosubproblems.
2. Initializethetablewithbasecases.
3. Fillinthetableusingtherecurrencerelation.
4. Thefinalvalueinthetablerepresentsthesolutiontotheoriginalproblem

PROGRAM:
defcount_ways_to_make_change(coins,target): n
= len(coins)
table=[0]*(target+1)
table[0]=1#Thereisonewaytomakeachangeof0

forcoinin coins:
foriinrange(coin,target+1): table[i]
+= table[i - coin]
returntable[target]

#Main program
if name == " main ":
coins = [1, 2, 5]
target= 5
ways = count_ways_to_make_change(coins, target)
print(f"Numberofwaystomakechangefor{target}is:{ways}")

OUTPUT:
Numberofwaystomakechange for5is:4

RESULT:
Thus, the Coin Change Problem using dynamic programming approach
wasimplemented successfully.
EX.No:5b DYNAMICPROGRAMMING–
WARSHALL’SANDFLOYD’SALGORITHM

AIM:
To implement the Warshall’s and Floyd’s Algorithm using dynamic
programmingapproachinPython.

ALGORITHM:
 Initialize the solution matrix same as the input graph matrix as a first
step.
 Then update the solution matrix by considering all vertices as an
intermediate vertex.
 The idea is to pick all vertices one by one and updates all shortest paths
which include the picked vertex as an intermediate vertex in the shortest
path.
 When we pick vertex number k as an intermediate vertex, we already
have considered vertices {0, 1, 2, .. k-1} as intermediate vertices.
 For every pair (i, j) of the source and destination vertices respectively,
there are two possible cases.
 k is not an intermediate vertex in shortest path fromi to j. We keep
the value of dist[i][j] as it is.
 k is an intermediate vertex in shortest path from i to j. We update
the value of dist[i][j] as dist[i][k] + dist[k][j], if dist[i][j] > dist[i]
[k] + dist[k][j]

PROGRAM:
# Number ofverticesin the graph V
=4
#Defineinfinityasthelarge
#enoughvalue.Thisvaluewillbe
# used for vertices not connected toeach other
INF = 99999

#Solvesallpairshortestpath
#viaFloydWarshallAlgorithm

deffloydWarshall(graph):
"""dist[][] will be the output matrix that will finally have the shortest
distances between every pair of vertices """
""" initializing the solution matrix same as input graph matrix OR we can
say that the initial values of shortest distances are based on shortest paths
considering no intermediate vertices """
dist=list(map(lambdai:list(map(lambdaj:j,i)),graph))
"""Addallverticesoneby onetothesetofintermediatevertices.
--->Beforestartofaniteration,
we have shortest distances between all pairs of vertices such that the
shortest distances consider onlythe vertices in the set {0, 1, 2, .. k-1} as
intermediate vertices.
----> After the end ofa iteration, vertex no. k is added to the set of
intermediate vertices and the set becomes {0, 1, 2, .. k}"""
forkinrange(V):

# pickall vertices as source one by one


for i in range(V):
# Pickall vertices as destination for the
# above picked source
forjinrange(V):

# If vertex k is on the shortest path from


# i toj, then update the value ofdist[i][j]
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j] )
printSolution(dist)

# A utility function toprint the solution


def printSolution(dist):
print("Following matrix shows the shortest distances\
between every pair of vertices")
for i in range(V):
forjinrange(V):
if(dist[i][j]==INF):
print("%7s" %("INF"), end="")
else:
print("%7d\t" % (dist[i][j]), end='')
if j == V-1:
print()

#Driver'scode
if name == " main ":
"""
10
(0)------->(3)
| /|\
5| |
| |1
\|/ |
(1)------->(2)
3 """
graph=[[0,5,INF,10],
[INF,0,3,INF],
[INF,INF,0, 1],
[INF,INF,INF,0]
]

# Function call
floydWarshall(graph)
OUTPUT:
The following matrix shows the shortest distances between every pair of
vertices
0 5 8 9
INF 0 3 4
INF INF 0 1
INF INF INF 0

RESULT:
Thus,theWarshall’sandFloyd’sAlgorithmusingdynamicprogramming
approachwasimplementedsuccessfully.
EX.No:5c DYNAMICPROGRAMMING–
KNAPSACKPROBLEM

AIM:
To implement the Knapsack Problem using dynamic programming approach
in Python.

ALGORITHM:
 Knapsackdynamicprogrammingistostoretheanswerstosolvedsubproble
ms in a table.
 Allpotentialweightsfrom‘1’to‘W’arethecolumnsinthetable,and
weightsaretherows.
 The state DP[i][j] reflects the greatest value of ‘j-weight’ considering
all values from ‘1 to ith’. So, if we consider ‘wi’ (weight in ‘ith’ row),it
is added to all columns with ‘weight values > wi’.
 Therearetwooptions:fillorleave‘wi’blankinthatparticularcolumn.
 Ifwedonotenterthe‘ith’weightinthe‘jth’column,theDP[i][j]will
besameasDP[i-1][j].
 However,if wefillthe weight,DP[i][j]equals the value of ‘wi’+the value
of the column weighing ‘j-wi’ on the former row.
 As a result, we choose the best of these two options to fill the
presentcondition.

PROGRAM:
defknapSack(W,wt,val,n):
K=[[0 fоrxin rаnge(W+1)]fоrxin rаnge(n+1)] #
Build tаble K[][] in bоttоm uр mаnner
foriinrange(n+1):
forwin range(W+1):
ifi==0orw==0:
K[i][w]=0
elif wt[i-1] <= w:K[i]
[w]=max(val[i-1]
+K[i-1][w-wt[i-1]],
K[i-1][w])
else:
K[i][w]=K[i-1][w]
returnK[n][W]
# Driver code
val=[60,100,120]
wt = [10,20, 30]
W=50
n = len(val)
print(knapSack(W,wt,val,n))

OUTPUT:
220

RESULT:
Thus,theKnapsackProblemusingdynamicprogrammingapproachwasimplement
ed successfully.
EX.No:6a GREEDYTECHNIQUE–DIJKSTRA‘SALGORITHM
HUFFMANTREEANDCODES

AIM:
ToimplementDijkstra'sAlgorithmusingGreedyTechniqueinPythonfor finding
the shortest path in a weighted graph.

ALGORITHM:
Givenaweightedgraphandasourcevertex,theobjectiveistofindtheshortest path from
the source to all other vertices.
ProgramLogic:
1. Initializethedistanceofallverticesfromthesourceasinfinity,andthe distance of
the source vertex to itself as 0.
2. Createapriorityqueuetostoreverticesand theirdistances.
3. While thepriorityqueue isnot empty, extract the vertex with the minimum
distance.
4. Updatethedistancesofadjacentverticesifashorterpathisfound.
5. Repeatuntilallverticesareprocessed.

PROGRAM:
importheapq

defdijkstra(graph,source):
distances={vertex:float('infinity')forvertexingraph} distances[source]
=0
priority_queue=[(0,source)]
whilepriority_queue:
current_distance,current_vertex=heapq.heappop(priority_queue) if
current_distance > distances[current_vertex]:
continue
forneighbor,weightingraph[current_vertex].items():
distance = current_distance + weight
ifdistance<distances[neighbor]:
distances[neighbor]=distance
heapq.heappush(priority_queue,(distance,neighbor)) return
distances

#Main program
if name ==" main ":
#Examplegraphrepresentedasanadjacencylist graph =
{
'A':{'B':1,'C':4},
'B':{'A':1,'C':2, 'D':5},
'C':{'A':4,'B':2, 'D':1},
'D':{'B':5,'C':1}
}
source_vertex='A'
shortest_distances=dijkstra(graph,source_vertex)
print(f"Shortestdistancesfrom{source_vertex}:{shortest_distances}")
OUTPUT:
ShortestdistancesfromA:{'A':0,'B':1,'C':3,'D':4}

RESULT:
Thus,theDijkstra'sAlgorithmusingGreedyTechniquewasimplemented successfully.
EX.No:6b GREEDYTECHNIQUE–HUFFMANTREEANDCODES

AIM:
To implement Huffman tree and codes using GreedyTechnique in Python for
finding the shortest path in a weighted graph.

ALGORITHM:
Input is an array ofunique characters along with their frequency of
occurrences and output is Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all
leafnodes (Min Heap is used as a priority queue. The value of frequency
field is used to compare two nodes in min heap. Initially, the least
frequent character is at root)
2. Extracttwonodeswiththeminimumfrequencyfromtheminheap.
3. Create a new internal node with a frequency equal to the sum of the two
nodes frequencies. Make the first extracted node as its left child and the
other extracted node as its right child. Add this node to the min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The
remaining node is the root node and the tree is complete.

PROGRAM:
#AHuffman TreeNode
import heapq

classnode:
def init (self,freq,symbol,left=None,right=None):
# frequencyofsymbol
self.freq = freq

# symbol name(character)
self.symbol = symbol

# nodeleft ofcurrent node


self.left = left

# noderight ofcurrent node


self.right = right

# treedirection (0/1)
self.huff = ''

def lt (self,nxt):
returnself.freq<nxt.freq

#utility functiontoprinthuffman
# codes for all symbols in the newly
# created Huffman tree
defprintNodes(node,val=''):
# huffman code for current node
newVal = val + str(node.huff)

# ifnodeisnot an edgenode #
then traverse inside it
if(node.left):
printNodes(node.left, newVal)
if(node.right):
printNodes(node.right,newVal)

# ifnodeis edgenodethen #
display its huffman code
if(not node.left and not node.right):
print(f"{node.symbol} -> {newVal}")

# characters forhuffman tree


chars = ['a', 'b', 'c', 'd', 'e', 'f']

# frequency of characters
freq = [5, 9, 12, 13, 16, 45]

# list containingunused nodes


nodes = []
# converting characters and frequencies
# into huffman tree nodes
forxinrange(len(chars)):
heapq.heappush(nodes, node(freq[x], chars[x]))
while len(nodes) > 1:

# sort all thenodesin ascending order #


based on their frequency
left = heapq.heappop(nodes)
right = heapq.heappop(nodes)

# assign directional value tothese nodes


left.huff = 0
right.huff=1

# combine the 2 smallest nodes to create


# new node as their parent
newNode = node(left.freq+right.freq, left.symbol+right.symbol, left,
right)
heapq.heappush(nodes,newNode)

# Huffman Treeisready!
printNodes(nodes[0])
OUTPUT:
f:0
c:100
d:101
a:1100
b: 1101
e:111

RESULT:
Thus, the Huffman tree and codes using Greedy Technique was implemented
successfully.
EX.No:7 ITREATIVEIMPROVEMENT–SIMPLEXMETHOD

AIM:
Toimplement theSimplexMethod usingIterative Improvement inPython for
solving linear programming problems.

ALGORITHM:
1. Formulatetheinitialtableau.
2. Iteratethroughthetableauuntilanoptimalsolutionisfoundoritis determined
that the solution is unbounded.
3. Chooseapivotcolumnandpivotrow.
4. Updatethetableauusingpivotoperations.
5. Repeatuntilanoptimalsolutionisachieved.

PROGRAM:
importnumpyasnp

defsimplex_method(c,A,b): m,
n = A.shape
tableau=np.hstack((A,np.eye(m))) c
= np.hstack((c, np.zeros(m)))
basic_vars = np.arange(n, n + m)

whileTrue:
#Checkifthecurrentsolution
#reaches the optimal solution
ifnp.all(c<=0):
optimal_solution=tableau[:,-1]
optimal_value= -tableau[-1, -1]
returnoptimal_solution[:n],optimal_value

#Choosethepivotcolumn(minimumcoefficientintheobjective function)
pivot_col=np.argmin(c)

#Checkforunbounded solution
ifnp.all(tableau[:,pivot_col]<=0):
raiseException("Thesolutionisunbounded.")

#Choosethepivotrow(minimumratiotest)
pivot_row=np.argmin(tableau[:-1,-1]/tableau[:-1,pivot_col])

#Updatethetableauusingpivotoperations pivot
= tableau[pivot_row, pivot_col]
tableau[pivot_row, :] /= pivot
foriinrange(m+1): if i
!= pivot_row:
tableau[i,:]-=tableau[i,pivot_col]*tableau[pivot_row,:]
# Update basic variables
basic_vars[pivot_row]=pivot_col

#Main program
if name ==" main ":
#Examplelinearprogrammingproblem c
= np.array([-2, -3, 0, 0])
A=np.array([[1,-1, 1,0],
[3,1,0,1]])
b=np.array([2,5])
optimal_solution,optimal_value=simplex_method(c,A,b)
print("Optimal Solution:", optimal_solution)
print("OptimalValue:",optimal_value)

OUTPUT:
OptimalSolution:[3.2.0. 0.]
OptimalValue:-13.0

RESULT:
Thus,theSimplexMethodusingIterativeImprovementwasimplemented successfully.
EX.No:8a BACKTRACKING–N-QUEENPROBLEM

AIM:
ToimplementtheN-QueenProblemusingtheBacktrackingalgorithmin Python.

ALGORITHM:
TheN-QueenproblemistoplaceNchessqueensonan\(N\timesN\) chessboard in such
a waythat no two queens threaten each other.
1. Startwithanemptychessboard.
2. Place queensone by one in differentcolumns,starting from the
leftmostcolumn.
3. Checkifthecurrentplacementissafe.Ifnot,backtrackandtry thenext position.
4. Repeattheprocessuntilallqueensareplacedorit'sdeterminedthatno solution
exists.

PROGRAM:
defis_safe(board,row,col,n):
#Checkifthereisaqueeninthesamerow if
any(board[row]):
returnFalse

#Checkifthereisaqueeninthesamecolumn if
any(board[i][col] for i in range(n)):
returnFalse

# Checkifthereisaqueen in thesamediagonal(left-toptoright-bottom) if
any(board[i][j] for i, j in zip(range(row, -1, -1), range(col, -1, -1))):
returnFalse

# Checkifthereisaqueen in thesamediagonal(right-toptoleft-bottom) if
any(board[i][j] for i, j in zip(range(row, -1, -1), range(col, n))):
returnFalse

returnTrue

defsolve_n_queens_util(board,row,n,solutions): if
row == n:
# Found a solution, add it to the list
solutions.append([''.join(row)forrowinboard])
return

forcolin range(n):
ifis_safe(board,row,col,n): board[row][col]
= 'Q'
solve_n_queens_util(board,row+1,n,solutions)
board[row][col] = '.'# Backtrack
defsolve_n_queens(n):
board=[['.'for_inrange(n)]for_inrange(n)] solutions =
[]
solve_n_queens_util(board,0,n,solutions)
return solutions

#Main program
if name == " main ":
n=4
solutions=solve_n_queens(n)

print(f"Numberofsolutionsfor{n}-Queensproblem:{len(solutions)}") for i,
solution in enumerate(solutions):
print(f"\nSolution{i+1}:")
for row in solution:
print(row)
OUTPUT:
Number ofsolutionsfor4-Queensproblem:2

Solution1:
. Q . .
. . . Q
Q . . .
. . Q .

Solution2:
. . Q .
Q . . .
. . . Q
. Q . .

RESULT:
Thus, the N-Queen Problem using the Backtracking algorithm was
implemented successfully.
EX.No:8b BACKTRACKING–SUBSETSUMPROBLEM

AIM:
To implement the Subset Sum Problem using the Backtracking algorithm
inPython.

ALGORITHM:
Given aset ofpositive integersand atarget sum, determineifthereisasubset of
the set that adds up to the target sum.
1. Startwithanemptysubset.
2. Includeanelementinthesubsetandrecursivelycheckiftheremaining sum can
be obtained.
3. Exclude the element from the subset and recursively check if the sum can
be obtained without the element.
4. Repeattheprocessforeachelementintheset.

PROGRAM:
defsubset_sum_util(nums,target,current_sum,start,path,result): if
current_sum == target:
result.append(path[:])
return

foriinrange(start,len(nums)):
ifcurrent_sum+nums[i]<=target:
path.append(nums[i])
subset_sum_util(nums,target,current_sum+nums[i],i+1,path,path.pop()
result)

defsubset_sum(nums,target):
result = []
subset_sum_util(nums,target,0,0,[],result)
return result

#Main program
if name == " main ":
nums = [1, 2, 3, 4, 5]
target= 7
subsets = subset_sum(nums, target)
print(f"Subsetswithsumequalto{target}:") for
subset in subsets:
print(subset)
OUTPUT:
Subsetswithsumequalto7:
[1,2,4]
[2,5]
[3,4]

RESULT:
Thus,theSubsetSumProblemusingtheBacktrackingalgorithmwasimplemented
successfully.
EX.No:9a BRANCHANDBOUND–ASSIGNMENTPROBLEM

AIM:
To implement the Assignment Problem using the Branch and Bound
algorithm in Python.

ALGORITHM:
Given a set of cities and the distances between each pair of cities, find the
shortest possible tour that visits each city exactly once and returns to the
starting city
1. Createacostmatrixfortheassignmentproblem.
2. ImplementtheBranchandBoundalgorithmtofindtheoptimalassignment.

PROGRAM:
importnumpyasnp
fromitertoolsimportpermutations

defassignment_cost(assignment,cost_matrix):
total_cost = 0
forworker,taskinenumerate(assignment):
total_cost += cost_matrix[worker, task]
returntotal_cost

defbranch_and_bound_assignment(cost_matrix):
n = len(cost_matrix)
min_cost = float('inf')
optimal_assignment=None

forassignmentinpermutations(range(n)):
current_cost=assignment_cost(assignment,cost_matrix) if
current_cost < min_cost:
min_cost = current_cost
optimal_assignment=assignment
returnoptimal_assignment,min_cost

#Main program
if name ==" main ":
#Examplecost matrixfortheassignmentproblem
cost_matrix = np.array([
[9,2,7,8],
[6,4,3,7],
[5,8,1,8],
[7,6,9,4] ])

optimal_assignment,min_cost=branch_and_bound_assignment(cost_matrix)
print("Optimal Assignment:", optimal_assignment)
print("MinimumCost:",min_cost)
OUTPUT:
OptimalAssignment:(2,3,0,1)
MinimumCost:13

RESULT:
Thus,theAssignmentProblem using theBranchandBoundalgorithm was
implemented successfully.
EX.No:9b BRANCHANDBOUND–TRAVELINGSALESMAN
PROBLEM

AIM:
To implement the Traveling Salesman Problem using the Branch and Bound
algorithm in Python.

ALGORITHM:
Given a set of cities and the distances between each pair of cities, find the
shortest possible tour that visits each city exactly once and returns to the
starting city.
1. Createadistancematrixforthe TSP.
2. ImplementtheBranchandBoundalgorithmtofindtheoptimaltour.

PROGRAM:
importnumpyasnp

deftsp_cost(tour,distance_matrix):
total_cost = 0
foriinrange(len(tour)-1):
total_cost+=distance_matrix[tour[i],tour[i+1]]
total_cost += distance_matrix[tour[-1], tour[0]]
#Returntothestartingcity return
total_cost
defbranch_and_bound_tsp(distance_matrix):
n = len(distance_matrix)
optimal_tour = None
min_cost = float('inf')
initial_tour=list(range(n))

def tsp_recursive(tour, cost):


nonlocaloptimal_tour,min_cost

iflen(tour)==n:
#Completedafulltour,updateoptimalsolutionifneeded if
cost < min_cost:
min_cost = cost
optimal_tour=tour[:]
return

for cityin range(n):


ifcitynotintour:
tsp_recursive(tour+[city],cost+distance_matrix[tour[-1],city])
tsp_recursive(initial_tour, 0)
returnoptimal_tour,min_cost
# Main program
if name ==" main ":
#ExampledistancematrixfortheTSP distance_matrix =
np.array([
[0,10,15, 20],
[10,0,35, 25],
[15,35,0, 30],
[20,25,30,0]])
optimal_tour,min_cost=branch_and_bound_tsp(distance_matrix)
print("Optimal Tour:", optimal_tour)
print("MinimumCost:",min_cost)

OUTPUT:
OptimalTour:[0, 1,3,2]
MinimumCost:80

RESULT:
Thus,theTravelingSalesman ProblemusingtheBranch and Bound algorithm was
implemented successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy