HoangGiaUy 20020014 VNUK en - 2024
HoangGiaUy 20020014 VNUK en - 2024
import numpy as np
def cholesky_decomposition(A):
"""
Performs Cholesky decomposition on a symmetric positive-definite matrix A.
Args:
A: A symmetric positive-definite matrix.
Returns:
L: The lower triangular matrix.
"""
n = A.shape[0]
L = np.zeros_like(A)
for i in range(n):
for j in range(i+1):
s = sum(L[i, k] * L[j, k] for k in range(j))
if i == j:
L[i, j] = np.sqrt(A[i, i] - s)
else:
L[i, j] = (1.0 / L[j, j]) * (A[i, j] - s)
return L
# Example usage:
A = np.array([[1, 2, 0, 0, 0, 0, 0, 0, 0],
[2, 5, 2, 0, 0, 0, 0, 0, 0],
[0, 2, 5, 2, 0, 0, 0, 0, 0],
[0, 0, 2, 5, 2, 0, 0, 0, 0],
[0, 0, 0, 2, 5, 2, 0, 0, 0],
[0, 0, 0, 0, 2, 5, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 5, 2, 0],
[0, 0, 0, 0, 0, 0, 2, 5, 2],
[0, 0, 0, 0, 0, 0, 0, 2, 5]])
L = cholesky_decomposition(A)
print(L)
U, S, V = np.linalg.svd(A)
print(U, S, V)
# Answer: Paste the results of the SVD decomposition of the 9x9 matrix below.
U:
[[-0.05163034 0.10139725 0.14734538 -0.18724436 0.21811848 -0.2349018
0.22623037 -0.16221427 0.86604481]
[-0.200725 0.36129247 0.45110258 -0.45642105 0.38377818 -0.2589122
0.12232696 -0.02394212 -0.43301869]
[-0.32728485 0.46335307 0.33151438 -0.01247133 -0.31041938 0.46734938
-0.40473983 0.20656476 0.21650005]
[-0.41710142 0.36299273 -0.0991895 0.45096398 -0.3091205 -0.16066724
0.46830237 -0.35869933 -0.10823051]
[-0.46009127 0.10405499 -0.4368072 0.20979917 0.38476467 -0.32310469
-0.27864513 0.45789144 0.05407577]
[-0.45142805 -0.20034033 -0.36449526 -0.35916237 0.21658234 0.4507459
-0.0616807 -0.48950077 -0.02695867]
[-0.39208436 -0.41721476 0.04988386 -0.36695748 -0.43685399 -0.08156838
0.36865463 0.44886191 0.01332079]
[-0.28872254 -0.45182428 0.41744857 0.19859317 -0.1115164 -0.377515
-0.47629016 -0.34197299 -0.00634325]
[-0.1529467 -0.28904935 0.3932511 0.45385569 0.46367434 0.42049582
0.32638677 0.18461033 0.0025373 ]]
S:
[[8.77546601e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 8.12627779e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 7.12306373e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 0.00000000e+00 5.87513794e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
4.51898828e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 3.20442927e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 2.08143710e+00 0.00000000e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 1.29519131e+00
0.00000000e+00]
[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
8.58349452e-06]]
V^T:
[[-0.05163034 -0.200725 -0.32728485 -0.41710142 -0.46009127 -0.45142805
-0.39208436 -0.28872254 -0.1529467 ]
[ 0.10139725 0.36129247 0.46335307 0.36299273 0.10405499 -0.20034033
-0.41721476 -0.45182428 -0.28904935]
[ 0.14734538 0.45110258 0.33151438 -0.0991895 -0.4368072 -0.36449526
0.04988386 0.41744857 0.3932511 ]
[-0.18724436 -0.45642105 -0.01247133 0.45096398 0.20979917 -0.35916237
-0.36695748 0.19859317 0.45385569]
[ 0.21811848 0.38377818 -0.31041938 -0.3091205 0.38476467 0.21658234
-0.43685399 -0.1115164 0.46367434]
[-0.2349018 -0.2589122 0.46734938 -0.16066724 -0.32310469 0.4507459
-0.08156838 -0.377515 0.42049582]
[ 0.22623037 0.12232696 -0.40473983 0.46830237 -0.27864513 -0.0616807
0.36865463 -0.47629016 0.32638677]
[-0.16221427 -0.02394212 0.20656476 -0.35869933 0.45789144 -0.48950077
0.44886191 -0.34197299 0.18461033]
[ 0.86604481 -0.43301869 0.21650005 -0.10823051 0.05407577 -0.02695867
0.01332079 -0.00634325 0.0025373 ]]
Question 2 (3 points): Given a binary string of length n ( n > 1). Write a program to generate a
binary string of length n using the Backtracking method
a) (1.0 point) Description of the Backtracking algorithm
# Answer: Paste the block diagram below:
b) (2.0 points) Write a program
# Answer: Paste the code below:
def generate_binary_strings(n):
def backtrack(s):
if len(s) == n:
print(s)
else:
backtrack(s + '0')
backtrack(s + '1')
backtrack("")
000000
000001
000010
000011
000100
000101
000110
000111
001000
001001
001010
001011
001100
001101
001110
001111
010000
010001
010010
010011
010100
010101
010110
010111
011000
011001
011010
011011
011100
011101
011110
011111
100000
100001
100010
100011
100100
100101
100110
100111
101000
101001
101010
101011
101100
101101
101110
101111
110000
110001
110010
110011
110100
110101
110110
110111
111000
111001
111010
111011
111100
111101
111110
111111
b) (1 point) Write a program (using a function) to calculate the minimum value of f(x) using
gradient descent with the learning rate, number of iterations N and error :
Args:
f: Hàm số cần tìm cực tiểu.
df: Đạo hàm của hàm số f.
x0: Điểm bắt đầu.
learning_rate: Tốc độ học.
num_iterations: Số lần lặp tối đa.
epsilon: Độ chính xác.
Returns:
Giá trị x tại điểm cực tiểu.
"""
x = x0
for i in range(num_iterations):
gradient = df(x)
x = x - learning_rate * gradient
if abs(gradient) < epsilon:
break
return x
# Định nghĩa hàm và đạo hàm
def f(x):
return math.exp(2*x**2+1) - 18*x + 2*x**3 - 5
def df(x):
return 4*x*math.exp(2*x**2+1) - 18 + 6*x**2
# Answer: Paste the execution result with the starting point,x = 5, learning rate = 0.001,
number of iterations N = 10000 and error :
Traceback (most recent call last):
File "c:\New folder\math1.py", line 41, in <module>
result = gradient_descent(f, df, x0, learning_rate, num_iterations, epsilon)
File "c:\New folder\math1.py", line 21, in gradient_descent
gradient = df(x)
File "c:\New folder\math1.py", line 32, in df
return 4*x*math.exp(2*x**2+1) - 18 + 6*x**2
OverflowError: math range error
while min_heap:
weight, current_node, previous_node = heapq.heappop(min_heap)
if current_node not in visited:
visited.add(current_node)
if previous_node is not None:
mst.append((previous_node, current_node, weight))
b) (1 points) Build the minimum spanning tree of the graph using Kruscal’s algorithm.
# Answer: Paste the code of Kruscal algorithm below:
class DisjointSet:
def __init__(self, vertices):
self.parent = {v: v for v in vertices}
self.rank = {v: 0 for v in vertices}
def kruskal(graph):
edges = []
for node in graph:
for neighbor, weight in graph[node]:
edges.append((weight, node, neighbor))
edges.sort() # Sort edges by weight
mst = []
disjoint_set = DisjointSet(graph.keys())
return mst
c) (1 điểm) Distinguish the similarities and differences between Prim's algorithm and Kruskal's
algorithm
# Answer:
Similarities:
Goal: Both algorithms aim to find a minimum spanning tree of a given graph.
Data Structure: Both algorithms often use a priority queue to efficiently select edges.
Output: Both algorithms produce a minimum spanning tree.
Differences:
Approach:
o Prim's Algorithm: It starts from an arbitrary vertex and grows the MST by adding the
minimum-weight edge connected to the current tree.
o Kruskal's Algorithm: It sorts all the edges by weight and adds the smallest-weight edge
that doesn't form a cycle.
Time Complexity:
o Prim's Algorithm:
Using a binary heap: O(E log V)
Using a Fibonacci heap: O(E + V log V)
o Kruskal's Algorithm: O(E log E)
Space Complexity:
o Both algorithms generally have a space complexity of O(V + E).
-THE END-
Reviewed and Approved by Head of Computer Science Date:
and Engineering Department Signed by Lecturer: