使用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()
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态