Design and Analysis Algorithm
Assignment Number 3
March 11, 2025
Nguyen Viet Dung
20001534
DAA/HW3/Recursion
1. Convex Hull Problem
Ans: The Convex Hull of a set of points in a 2D plane is the smallest convex
polygon that encloses all given points. It can be visualized as a rubber band
stretched around the outermost points.
Algorithms to Compute Convex Hull:
• Graham’s Scan - O(n log n) complexity, uses sorting and a stack to construct the hull.
• Jarvis March (Gift Wrapping) - O(nh) complexity, iteratively selects
the next hull point.
• QuickHull - Similar to QuickSort, with an average case of O(n log n).
• Andrew’s Algorithm (Monotone Chain) - A modified Graham’s scan
with O(n log n) complexity.
• Chan’s Algorithm - O(n log h) complexity, combines Graham’s scan and
Jarvis March.
Graham’s Scan Algorithm:
1. Find the point with the lowest y-coordinate (leftmost if tie) as the pivot.
2. Sort all points based on the polar angle with the pivot.
3. Use a stack to build the convex hull by checking left-turns.
Python Implementation:
DAA
Homework
def graham_scan(points):
points.sort(key=lambda p: (p[1], p[0])) # Sort by y, then x
def cross(o, a, b):
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
hull = []
for p in points:
while len(hull) >= 2 and cross(hull[-2], hull[-1], p) <= 0:
hull.pop()
hull.append(p)
return hull
2. Gaussian Elimination
Ans: The Gaussian Elimination method is used to solve systems of linear
equations, compute determinants, and find matrix inverses.
Steps:
1. Convert the system of equations into an augmented matrix.
2. Perform row operations to transform the matrix into an upper triangular
form.
3. Use back-substitution to find the solution.
Applications:
• Solving Systems of Linear Equations: Transforming the coefficient
matrix into row echelon form and using back-substitution.
• Computing the Determinant: If the matrix is transformed into an upper triangular form, the determinant is the product of its diagonal elements.
• Finding the Inverse of a Matrix: Augmenting the matrix with the
identity matrix and applying row operations to transform it into the inverse.
Python Implementation:
Page 2
DAA
Homework
import numpy as np
def gaussian_elimination(A, b):
n = len(A)
M = np.hstack((A, b.reshape(-1,1)))
# Augmented matrix
for i in range(n):
M[i] = M[i] / M[i,i] # Make pivot 1
for j in range(i+1, n):
M[j] -= M[i] * M[j,i] # Eliminate column
x = np.zeros(n)
for i in range(n-1, -1, -1):
x[i] = M[i,-1] - np.dot(M[i,i+1:n], x[i+1:n])
return x
3. Tiling a 2n × 2n Floor with L-shaped Tiles Problem Description:
Given a square floor of size 2n × 2n , where one specific square is reserved for
drainage, we need to tile the entire floor (except the drainage square) using
L-shaped tiles. Each L-shaped tile covers exactly 3 squares. We will use a
divide-and-conquer approach to solve this problem. Solution Approach: The
problem can be solved using a recursive divide-and-conquer strategy. The key
idea is to:Divide the 2n ×2n grid into four quadrants of size 2n−1 ×2n−1 . Identify
the quadrant containing the drainage square. Place one L-shaped tile at the
center to handle the special cases, covering three of the four quadrants’ inner
corners. Recursively tile each quadrant, treating the three squares covered by
the central L-tile as ”blocked”.
Algorithm:
1.• Base Case: If n = 0 (grid size 1 × 1), no tiling is needed.
2. Recursive Case:
(a) Divide the grid into four quadrants.
(b) Identify which quadrant contains the drainage square.
(c) Place an L-tile at the center, covering three of the four inner corners.
(d) Recursively solve each of the four quadrants.
3. Termination: Recursion stops when the base case is reached.
Page 3
DAA
Homework
This approach ensures complete tiling of all squares except the drainage square. Since
the total number of squares is 4n , and 4n − 1 is always divisible by 3, the tiling is
always feasible.
Python Implementation: In tile floor.py
Time Complexity: The recurrence relation for this problem is T (n) = 4T (n−1)+O(1),
leading to a total complexity of O(4n ). However, since each tile covers 3 squares, the
n
number of tiles required is 4 3−1 , ensuring feasibility.
Page 4