Professional Documents
Culture Documents
# Singular-value decomposition
from numpy import array
from scipy.linalg import svd
# define a matrix
A = array([[5,7,-4], [-3,4,6]])
print(A)
# SVD
U, s, VT = svd(A)
print(U)
print(s)
print(VT)
Output
[[ 5 7 -4]
[-3 4 6]]
[[-0.94781097 0.31883282]
[ 0.31883282 0.94781097]]
[9.67989022 7.5696582 ]
[[-0.58839028 -0.55365768 0.58928775]
[-0.16503636 0.79568634 0.58279178]
[ 0.79155536 -0.24565511 0.55954775]]
2. Gram Schmidt
import numpy as np
def gram_schmidt(A):
n = A.shape[1]
for j in range(n):
for k in range(j):
A[:, j] == np.dot(A[:, k], A[:, j]) * A[:, k]
A[:, j] = A[:, j] / np.linalg.norm(A[:, j])
return A
if _name_ == '_main_':
A = np.array([[1.0, 1.0, 1.0], [1.0, 3.0, 1.0], [2.0, -1.0, 1.0]])
print(gram_schmidt(A))
Output
import numpy as np
Output
987321456
The second matrix is:
[[9 8 7]
[3 2 1]
[4 5 6]]
The result matrix is:
[[ 27 27 27]
[123 117 111]
[ 75 72 69]]
import numpy as np
from numpy.linalg import eig
n = int(input("Enter the number of rows & columns:"))
R = n #number of rows
C = n #number of columns
print("Enter the entries in single line (separated by space): ")
w,v=eig(matrix)
print('E-value:', w)
print('E-vector:', v)
Output
3 4 6 7 8 3 -8 -2 6
[[ 3 4 6]
[ 7 8 3]
[-8 -2 6]]
E-value: [2.47250657+0.j 7.26374672+5.22672562j 7.26374672-5.22672562j]
E-vector: [[ 0.4065112 +0.j 0.02454257-0.35884786j
0.02454257+0.35884786j]
[-0.77629128+0.j -0.52995846-0.35043978j -0.52995846+0.35043978j]
[ 0.48178885+0.j 0.68334608+0.j 0.68334608-0.j ]]
5. Matrix Multiplication
import numpy as np
A = np.array([[2, 4, 1], [8, 3, 5], [9, 0, 7]])
print(A)
B = np.array([[5, 2, 1], [0, 4, 9], [2, 1, 3]])
print(B)
X = A*B
print(X)
Output
[[2 4 1]
[8 3 5]
[9 0 7]]
[[5 2 1]
[0 4 9]
[2 1 3]]
[[10 8 1]
[ 0 12 45]
[18 0 21]]
6.
#Calculation of AXI where A and I are 3X3 matrix
import numpy as np
X = A*I
print(X)
Output
[[2 4 1]
[8 3 5]
[9 0 7]]
[[1 0 0]
[0 1 0]
[0 0 1]]
[[2 0 0]
[0 3 0]
[0 0 7]]
7.
import numpy as np
a = np.array([[3,3,3,3,3],[3,3,3,3,3],[3,3,3,3,3],[3,3,3,3,3]])
print (a)
b = np.array([[5],[7],[3],[8],[9]])
print (b)
x = np.dot(a, b)
print (x)
Output
[[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]]
[[5]
[7]
[3]
[8]
[9]]
[[96]
[96]
[96]
[96]]
8.
import numpy as np
A = np.array([[1,2,3],[2,3,4],[3,4,5]])
print (A)
I = np.array([[1,0,0],[0,1,0],[0,0,1]])
print (I)
X = np.dot(A, I)
print (X)
Output
[[1 2 3]
[2 3 4]
[3 4 5]]
[[1 0 0]
[0 1 0]
[0 0 1]]
[[1 2 3]
[2 3 4]
[3 4 5]]
9.
from numpy import array
from numpy import diag
from numpy import dot
from numpy import zeros
from scipy.linalg import svd
# define a matrix
A = array([[1, 2], [3, 4], [5, 6]])
print(A)
# Singular-value decomposition
U, s, VT = svd(A)
# create m x n Sigma matrix
Sigma = zeros((A.shape[0], A.shape[1]))
# populate Sigma with n x n diagonal matrix
Sigma[:A.shape[1], :A.shape[1]] = diag(s)
# reconstruct matrix
B = U.dot(Sigma.dot(VT))
print(B)
Output
[[1 2]
[3 4]
[5 6]]
[[1. 2.]
[3. 4.]
[5. 6.]]
10.
#Initializing activities with start and finish times
activities = [["A1", 0, 6],
["A2", 3, 4],
["A3", 1, 2],
["A4", 5, 8],
["A5", 5, 7],
["A6", 8, 9]
]
printMaxActivities(activities)
Output
A3
A2
A5
A6
Output
Edge : Weight
0-2:5
2-3:1
3-4:1
4-1:2
class Graph:
parent = []
rank = []
# Create V subsets with single elements
for node in range(self.V):
parent.append(node)
rank.append(0)
minimumCost = 0
print ("Edges in the constructed MST")
for u, v, weight in result:
minimumCost += weight
print("%d -- %d == %d" % (u, v, weight))
print("Minimum Spanning Tree" , minimumCost)
# Driver code
g = Graph(4)
g.addEdge(0, 1, 10)
g.addEdge(0, 2, 6)
g.addEdge(0, 3, 5)
g.addEdge(1, 3, 15)
g.addEdge(2, 3, 4)
# Function call
g.KruskalMST()
Output
return min_index
self.printSolution(dist)
# Driver program
g = Graph(9)
g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0, 11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9, 14, 0, 0, 0],
[0, 0, 0, 9, 0, 10, 0, 0, 0],
[0, 0, 4, 14, 10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8, 11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]
]
g.dijkstra(0)
Output
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
adj[v].append(w)
result = [-1] * V
cr += 1
# Driver Code
if __name__ == '__main__':
output
Coloring of graph 1
Vertex 0 ---> Color 0
Vertex 1 ---> Color 1
Vertex 2 ---> Color 2
Vertex 3 ---> Color 0
Vertex 4 ---> Color 1
Coloring of graph 2
Vertex 0 ---> Color 0
Vertex 1 ---> Color 1
Vertex 2 ---> Color 2
Vertex 3 ---> Color 0
Vertex 4 ---> Color 3
# A Tree node
class Node:
def _init_(self, key):
self.key = key
self.left = None
self.right = None
current = root
pre = Node(0)
current = current.right
else:
current = current.right
# End of while
# Driver Code
if _name_ == "_main_":
# /* 15
#/\
# 19 11
# /\
# 25 5
#/\/\
# 17 3 23 24
root = Node(15)
root.left = Node(19)
root.right = Node(11)
root.right.left = Node(25)
root.right.right = Node(5)
root.right.left.left = Node(17)
root.right.left.right = Node(3)
root.right.right.left = Node(23)
root.right.right.right = Node(24)
Max Value is : 25
Min Value is : 3
# traveling salesman
from sys import maxsize
from itertools import permutations
V=4
# update minimum
min_path = min(min_path, current_pathweight)
return min_path
# Driver Code
if _name_ == "_main_":
output
80