《大学离散数学》图的矩阵表示,无向图关联矩阵,有向图关联矩阵
无向图关联矩阵相关计算
mport numpy as np
ramdom_matrix = np.array([[1,1,1,0,0,0],
[0,1,1,0,1,0],
[0,0,0,1,1,0],
[1,0,0,1,0,2]])#输入你想算的矩阵
print("每条边关联顶点个数")
print(sum(ramdom_matrix))
a = 0
for i in range (0,4):
print("第",i,"行元素的度数为")
print(sum(ramdom_matrix[i]))
a = a + sum(ramdom_matrix[i])
print("根据握手定律,可知顶点度数总和为:")
print(a)
有向图邻接矩阵
import numpy as np
ramdom_matrix = np.array([[1,2,1,0],
[0,0,1,0],
[0,0,0,1],
[0,0,1,0]])#输入需要算的矩阵
print("每列元素的入度为{}".format(sum(ramdom_matrix)))
a = 0
for i in range (0,4):
print("第",i,"行元素的出度为{}".format(sum(ramdom_matrix[i])))
a = a + sum(ramdom_matrix[i])
'''矩阵乘法'''
print("\n")
A_square = np.dot(ramdom_matrix,ramdom_matrix)
print("A²为\n",A_square)
A_cube = np.dot(ramdom_matrix,A_square)
print("A³为\n",A_cube)
A_quartic = np.dot (A_square,A_square)
print("A四次方为\n",A_quartic)
print("\n从vi到vj长度小于等于l的通路数或回路数为")
B = ramdom_matrix + A_square + A_cube + A_quartic
print(B)
虽说是无聊写的代码,但这个代码我感觉把我带向了喜欢编程的行列中。本人学生党,第一次发博客,很多不足,望读者可以体谅,希望以后可以做个真正的程序员!RushB!!!
来源:CSDN
作者:繁星和孤岛的故事
链接:https://blog.csdn.net/weixin_43818177/article/details/103570111