DAA Record
DAA Record
import numpy as np
def strassen_matrix_multiply(A, B):
# Check if matrices are compatible for multiplication
if A.shape[1] != B.shape[0]:
raise ValueError("Matrices are not compatible for multiplication")
# Recursive steps
P1 = strassen_matrix_multiply(A11 + A22, B11 + B22)
P2 = strassen_matrix_multiply(A21 + A22, B11)
P3 = strassen_matrix_multiply(A11, B12 - B22)
P4 = strassen_matrix_multiply(A22, B21 - B11)
P5 = strassen_matrix_multiply(A11 + A12, B22)
P6 = strassen_matrix_multiply(A21 - A11, B11 + B12)
P7 = strassen_matrix_multiply(A12 - A22, B21 + B22)
# Example usage
A = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]])
B = np.array([[17, 18, 19, 20],[21, 22, 23, 24],[25, 26, 27, 28],[29, 30, 31, 32]])
product = strassen_matrix_multiply(A, B)
print("Product of A and B:")
print(product)
Program:
# Example usage
graph = defaultdict(list)
graph[1] = [2, 3]
graph[2] = [4]
graph[3] = [4, 5]
graph[4] = [6]
graph[5] = []
graph[6] = []
sorted_order = topological_sort(graph)
print("Topological Sort Order:")
print(sorted_order)
Program:
def heap_sort(arr):
n = len(arr)
# Build max heap
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
# Perform sorting
for i in range(n - 1, 0, -1):
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0)
# Example usage
arr = [12, 11, 13, 5, 6, 7]
heap_sort(arr)
print("Sorted array:")
print(arr)
Program :
# Example usage
coins = [1, 2, 5]
target = 11
min_coins, coin_combination = coin_change(coins, target)
print("Minimum number of coins needed:", min_coins)
print("Coin combination:", coin_combination)
Program:
def warshall(adj_matrix):
num_vertices = len(adj_matrix)
tc = [row[:] for row in adj_matrix] # Create a copy of the adjacency matrix
for k in range(num_vertices):
for i in range(num_vertices):
for j in range(num_vertices):
tc[i][j] = tc[i][j] or (tc[i][k] and tc[k][j])
return tc
# Example usage
adj_matrix = [[0, 1, 0, 0],[0, 0, 0, 1],[0, 0, 0, 0],[1, 0, 1, 0]]
transitive_closure = warshall(adj_matrix)
print("Transitive Closure Matrix:")
for row in transitive_closure:
print(row)
def floyd(adj_matrix):
num_vertices = len(adj_matrix)
dist = [row[:] for row in adj_matrix] # Create a copy of the adjacency matrix
for k in range(num_vertices):
for i in range(num_vertices):
for j in range(num_vertices):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
return dist
# Example usage
adj_matrix = [[0, 1, float('inf'), 3],[2, 0, 4, float('inf')],[float('inf'), float('inf'), 0, 2],
[float('inf'), float('inf'), 1, 0]]
shortest_distances = floyd(adj_matrix)
print("Shortest Distance Matrix:")
for row in shortest_distances:
print(row)
Program :
# Example usage
items = ['Item1', 'Item2', 'Item3', 'Item4', 'Item5']
weights = [2, 3, 4, 5, 6]
values = [5, 7, 9, 11, 13]
capacity = 10
Program:
import heapq
# Example usage
graph = [[(1, 4), (2, 2)],[(2, 5), (3, 2)],[(1, 1), (3, 1)],[(4, 3)], []]
source_vertex = 0
Program:
class Node:
def __init__(self, char, frequency):
self.char = char
self.frequency = frequency
self.left = None
self.right = None
def build_huffman_tree(characters, frequencies):
pq = PriorityQueue()
for i in range(len(characters)):
node = Node(characters[i], frequencies[i])
pq.put((node.frequency, node))
while pq.qsize() > 1:
freq1, node1 = pq.get()
freq2, node2 = pq.get()
new_node = Node(None, freq1 + freq2)
new_node.left = node1
new_node.right = node2
pq.put((new_node.frequency, new_node))
root = pq.get()[1]
return root
def generate_huffman_codes(root):
codes = {}
# Example usage
characters = ['A', 'B', 'C', 'D', 'E']
frequencies = [10, 7, 15, 4, 12]
print("Huffman Codes:")
for char, code in huffman_codes.items():
print(char, ":", code)
Program:
import numpy as np
Program:
def solve_n_queen(N):
board = [[0] * N for _ in range(N)]
solutions = []
place_queens(0, board, N, solutions)
return solutions
# Example usage
N=4
solutions = solve_n_queen(N)
# Example usage
nums = [2, 4, 6, 8]
target_sum = 8
result = subset_sum(nums, target_sum)
print("Subsets with a sum of", target_sum)
for subset in result:
print(subset)
Program:
import numpy as np
def branch_and_bound(cost_matrix):
N = cost_matrix.shape[0]
assignment_matrix = np.zeros((N, N), dtype=int)
selected_tasks = []
min_cost = float('inf')
# Example usage
cost_matrix = np.array([[5, 7, 3, 8],[9, 2, 6, 4],[1, 3, 8, 6],[7, 6, 4, 2]])
assignment, min_cost = branch_and_bound(cost_matrix)
print("Assignment Matrix:")
print(assignment)
print("Minimum Cost:", min_cost)
Program:
import numpy as np
# Example usage
distances = np.array([[0, 2, 9, 10],[1, 0, 6, 4],[15, 7, 0, 8],[6, 3, 12, 0]])
N = distances.shape[0]
min_distance = float('inf')
best_path = []
branch_and_bound(0, {0}, [0], 0)
print("Best Path:", best_path)
print("Minimum Distance:", min_distance)