求解最大连通子图

 2023-09-15 阅读 20 评论 0

摘要:使用networkx里面的函数来求解最大连通子图 # -*- coding: utf-8 -*- """ Created on Wed Mar 11 21:38:53 2020@author: Administrator """import matplotlib.pyplot as plt import networkx as nxdef get_connected_components():G = nx.Gr

使用networkx里面的函数来求解最大连通子图

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 11 21:38:53 2020@author: Administrator
"""import matplotlib.pyplot as plt
import networkx as nxdef get_connected_components():G = nx.Graph()filestr = ""with open("matrix.txt") as files:for line in files:filestr += line.strip()#将字符串转换成列表matrix = eval(filestr)nodes = range(len(matrix))G.add_nodes_from(nodes)for i in range(len(matrix)):for j in range(len(matrix)):if(matrix[i][j] == 1):G.add_edge(i, j)#计算一个图的最大连通子图 max_cc =max(nx.algorithms.components.connected_components(G),key = len)print(max_cc)print("-----------------该网络的节点总数为----------------------")print(len(matrix))print("----------------最大连通分支包含的节点个数----------------")ncc = float(len(max_cc))print(ncc)print("---------------------最大连通分支比例--------------------")print(ncc/len(matrix))get_connected_components()

在这里插入图片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/62727.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息