End of DAA
End of DAA
AIM:
To develop a Python program that implements Floyd’s algorithm for solving the All-Pairs Shortest
Paths problem and measures its execution time.
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')
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()
if board[i] == row or \
abs(board[i] - row) == abs(i - col):
return False
return True
def solve_n_queens(N):
board = [-1] * N solutions
= []
solve_n_queens_util(board, 0, N, solutions)
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
def inorder(n):
if n:
inorder(n.l)
print(n.k, end=' ')
inorder(n.r)
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.