網站首頁 編程語言 正文
networkx返回圖的鄰接矩陣
最近有用到關于鄰接矩陣的一些問題,平時不太常用所以一直沒注意到networkx里面的函數,權當筆記了,有興趣查看官方文檔
這里只舉個小例子
import networkx as nx # 導入必要的函數包
import scipy as sp
import operator
import matplotlib.pyplot as plp
h = nx.Graph() # 我這里需要用到的圖
h.add_nodes_from(list(range(1, 9)))
h.add_edges_from([(1, 2), (1, 3), (1, 5), (1, 4), (2, 8), (2, 6), (3, 6), (4, 7)])
nx.draw_networkx(h) # draw
plp.show()
print(nx.adjacency_matrix(h).todense()) # 返回圖的鄰接矩陣
圖長這樣:
輸出的鄰接矩陣:
ok,一個簡簡單單的鄰接矩陣解決完畢。
NetworkX學習筆記之鄰接矩陣
鄰接矩陣
As = nx.adjacency_matrix(G)
print(As)
# 轉化成二維數組形式的矩陣
A = As.todense()
print(A)
已知圖的鄰接矩陣,創建圖
import numpy as np
A = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]])
G = nx.from_numpy_matrix(A)
nx.draw(G, node_size=800, with_labels=True)
總結
原文鏈接:https://blog.csdn.net/qq_37730871/article/details/111561699
相關推薦
- 2022-06-30 Python利用shutil模塊實現文件夾的復制刪除與裁剪_python
- 2022-06-08 nacos項目啟動報錯:Connection refused: no further informa
- 2022-09-01 一文搞懂Scrapy與MongoDB交互過程_MongoDB
- 2023-07-26 webpakc原理之開發一個清除console.log(xxx)的loader
- 2022-09-15 Python中eval()函數的詳細使用教程_python
- 2022-12-24 Docker網絡模型以及容器通信詳解續篇_docker
- 2022-07-07 go語言心跳超時的實現示例_Golang
- 2023-05-06 react中axios結合后端實現GET和POST請求方式_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支