当前位置: 首页 > news >正文

可达矩阵-邻接矩阵-以及有向图的python绘制

参考1
自定义输入矩阵来绘制

根据参考代码,
自定义
代码如下:

# 编程实现有向图连通性的判断
from pylab import mplmpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import pylab#定义x三阶矩阵
x = np.array([[1, 0, 0], [1, 1, 0], [1, 1, 1]])#随机生成x为五阶矩阵
# x = np.random.randint(0, 2, (5, 5))
n = len(x)value_1 = value_2 = sum_1 = sum_2 = sum_3 = sum_4 = y = final = x
y = x + x.T# 计算可达矩阵
for i in range(1, n):value_1 = np.matmul(value_1, x)sum_1 = sum_1 + value_1
sum_2 = sum_1 + np.identity(n)reachability_matrix = sum_2 > 0.5print("此有向图的可达矩阵为:")
print(reachability_matrix.astype(int))final = reachability_matrix + reachability_matrix.Tfor i in range(1, n):value_2 = np.matmul(value_2, y)sum_3 = sum_3 + value_2
sum_4 = sum_3 + np.identity(n)
reachability_matrix_1 = sum_4 > 0.5# 给出判断结果
if ((reachability_matrix.astype(int) == np.ones((n, n)).astype(int)).all()):print("此有向线图G为强连通图或其为无向连通图")
elif ((final.astype(int) == np.ones((n, n)).astype(int)).all()):print("此有向线图G是单向连通图")
elif ((reachability_matrix_1.astype(int) == np.ones((n, n)).astype(int)).all()):print("此有向线图G是弱连通图")
else:print("此有向图不连通")# 下面展示图形化输出有向图G
G = nx.DiGraph()
for i in range(0, n):j=i+1G.add_node(i, desc='p' + str(j))for p in range(0, n):for q in range(0, n):if x[p, q] == 1:G.add_edges_from([(p, q)], weight='1')edge_labels = dict([((u, v), d['weight']) for u, v, d in G.edges(data=True)])
edge_colors = ['black']
pos = nx.spring_layout(G)
node_labels = nx.get_node_attributes(G, 'desc')
nx.draw_networkx_labels(G, pos, labels=node_labels)
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
nx.draw(G, pos, node_size=1500, edge_color=edge_colors, edge_cmap=plt.cm.Reds)
plt.title('Directed Graph', fontsize=10)
pylab.show()

第二版

增大了字体
可以自定义字体大小

# 编程实现有向图连通性的判断
from pylab import mplmpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import pylab#定义x三阶矩阵
x = np.array([[1, 0, 0], [1, 1, 0], [1, 1, 1]])#随机生成x为五阶矩阵
# x = np.random.randint(0, 2, (5, 5))
n = len(x)value_1 = value_2 = sum_1 = sum_2 = sum_3 = sum_4 = y = final = x
y = x + x.T# 计算可达矩阵
for i in range(1, n):value_1 = np.matmul(value_1, x)sum_1 = sum_1 + value_1
sum_2 = sum_1 + np.identity(n)reachability_matrix = sum_2 > 0.5print("此有向图的可达矩阵为:")
print(reachability_matrix.astype(int))final = reachability_matrix + reachability_matrix.Tfor i in range(1, n):value_2 = np.matmul(value_2, y)sum_3 = sum_3 + value_2
sum_4 = sum_3 + np.identity(n)
reachability_matrix_1 = sum_4 > 0.5# 给出判断结果
if ((reachability_matrix.astype(int) == np.ones((n, n)).astype(int)).all()):print("此有向线图G为强连通图或其为无向连通图")
elif ((final.astype(int) == np.ones((n, n)).astype(int)).all()):print("此有向线图G是单向连通图")
elif ((reachability_matrix_1.astype(int) == np.ones((n, n)).astype(int)).all()):print("此有向线图G是弱连通图")
else:print("此有向图不连通")# 下面展示图形化输出有向图G
G = nx.DiGraph()
for i in range(0, n):j = i + 1G.add_node(i, desc='p' + str(j))for p in range(0, n):for q in range(0, n):if x[p, q] == 1:G.add_edges_from([(p, q)], weight='1')edge_labels = dict([((u, v), d['weight']) for u, v, d in G.edges(data=True)])
edge_colors = ['black']
pos = nx.spring_layout(G)
node_labels = nx.get_node_attributes(G, 'desc')
nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=16)  # 设置字体大小为16nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)nx.draw(G, pos, node_size=1500, edge_color=edge_colors, edge_cmap=plt.cm.Reds)
plt.title('Directed Graph', fontsize=10)
pylab.show()
http://www.lryc.cn/news/219727.html

相关文章:

  • react typescript @别名的使用
  • C++性能优化笔记-6-C++元素的效率差异-7-类型转换
  • c#中switch常用模式
  • Flink SQL 常用作业sql
  • nodejs国内镜像及切换版本工具nvm
  • 用Rust和Scraper库编写图像爬虫的建议
  • Java 语言环境搭建
  • 酷开科技 | 酷开系统里萌萌哒小维在等你!
  • Bash 4关联数组:错误“声明:-A:无效选项”
  • 干货|AI辅助完成论文的正确打开方式!
  • SpringBoot--Web开发篇:含enjoy模板引擎整合,SpringBoot整合springMVC;及上传文件至七牛云;restFul
  • 线上JAVA应用平稳运行一段时间后出现JVM崩溃问题 | 京东云技术团队
  • 进口跨境商城源码:高效、安全、可扩展的电商平台解决方案
  • GEE数据集——2019、2020、2021、2022和2023年全球固定宽带和移动(蜂窝)网络性能Shapefile 格式数据集
  • 什么是防火墙?详解三种常见的防火墙及各自的优缺点
  • 动态规划算法实现0-1背包问题Java语言实现
  • linux查看系统版本
  • pg14-sql基础(四)-多表联查
  • el-date-picker 日期时间选择器 限时时间范围 精确到时分秒
  • 轮廓线dp:GYM103446C
  • 羊驼免疫制备纳米抗体
  • 【AI好好玩02】利用Lama Cleaner本地实现AIGC试玩:擦除对象、替换对象、更换风格等等
  • SQL FULL OUTER JOIN 关键字(完整外部连接)||SQL自连接 Self JOIN
  • 专科医院污水处理设备构造解析及工艺流程
  • 【RabbitMQ】RabbitMQ 消息的可靠性 —— 生产者和消费者消息的确认,消息的持久化以及消费失败的重试机制
  • 百万套行泊一体量产定点,中国市场「开启」智驾高低速集成
  • Gopro hero5运动相机格式化后恢复案例
  • Microsoft Dynamics 365 CE 扩展定制 - 6. 增强代码
  • 基于libopenh264 codec的svc分层流实现方案
  • 为机器学习算法准备数据(Machine Learning 研习之八)