DAA Record END
DAA Record END
ALGORITHM:
Step 1: Start the program.
Step 2: Import required modules: time, numpy, and ThreadPoolExecutor for parallelism.
Step 3: Define a function to perform the Floyd-Warshall algorithm sequentially.
Step 4: Define a function to perform the Floyd-Warshall algorithm in parallel using threads.
Step 5: Generate a random weighted graph with a given number of vertices.
Step 6: Run the sequential Floyd-Warshall function and record execution time.
Step 7: Run the parallel Floyd-Warshall function and record execution time.
Step 8: Calculate the speed-up achieved by parallel execution over sequential.
Step 9: Display the sequential time, parallel time, and speed-up.
Step 10: End the program.
PROGRAM:
import time
import numpy as np
from multiprocessing import Pool, cpu_count, Manager
INF = float('inf')
# Floyd-Warshall Sequential
def floyd_warshall_seq(graph):
n = len(graph)
dist = [row[:] for row in graph]
for k in range(n):
for i in range(n):
for j in range(n):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
return dist
for k in range(n):
args = [(i, k, shared_dist, n) for i in range(n)]
with Pool(cpu_count()) as pool:
pool.map(update_row, args)
# Main Execution
if __name__ == "__main__":
N = 50
graph = generate_graph(N)
start_seq = time.time()
floyd_warshall_seq(graph)
end_seq = time.time()
start_par = time.time()
floyd_warshall_parallel(graph)
end_par = time.time()
OUTPUT:
RESULT:
Thus, the Python program that implements Floyd’s algorithm to solve the all-pairs shortest paths
problem has been successfully executed.
Ex.no: 5
Date: N Queen's problem using Back Tracking
AIM:
To write a Python program that implements N Queen’s problem using Back Tracking.
ALGORITHM:
Step 1: Start the program
Step 2: Read the input value N, representing the size of the chessboard.
Step 3: Initialize a board representation to track queen positions column-wise.
Step 4: Create a function to check if placing a queen at a given row and column is safe.
Step 5: Use a recursive function to attempt placing a queen in each row of the current column.
Step 6: If a safe position is found, place the queen and recursively try to solve for the next column.
Step 7: If all columns are filled, record the current configuration as a valid solution.
Step 8: After exploring all options, display the total number of valid solutions and print them.
Step 9: End the Program.
PROGRAM:
def print_solution(board, N):
for row in board:
line = ""
for col in range(N):
line += "Q " if row == col else ". "
print(line)
print()
return
def solve_n_queens(N):
board = [-1] * N
solutions = []
solve_n_queens_util(board, 0, N, solutions)
# Driver
if __name__ == "__main__":
N = int(input("Enter value of N: "))
solve_n_queens(N)
OUTPUT:
RESULT:
Thus, the Python program that implements N Queen’s problem using Back Tracking has been
successfully executed.
Ex.no: 6
Date: AVL Tree Construction
AIM:
To construct an AVL tree using Python, demonstrating self-balancing through LL, RR, LR, and RL
rotations
ALGORITHM:
Step 1: Start the program
Step 2: Define an empty AVL tree.
Step 3: Read the number of elements and the list of input values.
Step 4: For each element, insert it into the AVL tree.
Step 5: During each insertion, update the height of affected nodes.
Step 6: Calculate the balance factor of each node after insertion.
Step 7: Identify the type of imbalance (LL, RR, LR, or RL).
Step 8: Perform appropriate rotations to restore AVL balance.
Step 9: After all insertions, perform an inorder traversal to print elements.
Step 10: Display the total time taken for all insertions.
Step 11: End the Program.
PROGRAM:
import time
class Node:
def __init__(self, k):
self.k = k; self.l = self.r = None; self.h = 1
def r_rot(y):
print(f"Right Rotation on {y.k}")
x, T = y.l, y.l.r
x.r, y.l = y, T
y.h = 1 + max(ht(y.l), ht(y.r))
x.h = 1 + max(ht(x.l), ht(x.r))
return x
def l_rot(x):
print(f"Left Rotation on {x.k}")
y, T = x.r, x.r.l
y.l, x.r = x, T
x.h = 1 + max(ht(x.l), ht(x.r))
y.h = 1 + max(ht(y.l), ht(y.r))
return y
return n
def inorder(n):
if n:
inorder(n.l)
print(n.k, end=' ')
inorder(n.r)
# Main
n = int(input("Enter number of elements: "))
a = list(map(int, input("Enter elements: ").split()))
start = time.perf_counter()
root = None
for x in a:
root = insert(root, x)
end = time.perf_counter()
OUTPUT:
RESULT:
Thus, the Python program that implements AVL tree construction and the speed-up time has been
successfully executed.