极⼤连通⼦图原理介绍及代码实现Python3版
原理介绍
图分为⽆向图和有向图。
⽆向图中的极⼤连通⼦图也叫连通分量。⽆向图可以分成两种类型:连通的⽆向图、不连通的⽆向图。连通的⽆向图只有⼀个极⼤连通⼦图,即它本⾝,因为不存在另⼀个连通的⼦图包含的点和边⽐它本⾝还要多,所以叫作极⼤连通⼦图。不连通的⽆向图可以拆分为若⼲个连通的⽆向图,如果我们在拆分时注意把能连通的点边都放在⼀个连通⼦图中,使这个连通⼦图⾜够⼤,以⾄于再多包含⼀个点或边它就变成不连通的了,我们称这个连通⼦图为极⼤连通⼦图,也叫连通分量。
有向图可以分为强连通图、弱连通图、单向连通图、不连通图。极⼤连通⼦图⼀般只在强连通图中讨论,即强连通分量。
代码实现
# -*- coding:utf-8 -*-
#====#====#====#====
# __author__ = "liubc"
#FileName: *.py
#Version:1.0.0
#CreateTime:xxxx-xx-xx
#====#====#====#====
from collections import deque
import sys
import matplotlib.pyplot as plt
import networkx as nx
import time
import openpyxl
'''求解极⼤连通⼦图'''
# 定义graph
G = nx.Graph()
class ConnectedSubgraph:
'''
function:
读⼊Excel⽂档(默认带权重)
input:
file:输⼊表格(xlsx格式):第⼀列为源项⽬,第⼆列为引⽤项⽬,第三列为权重(可选)
output:
nodes:节点(项⽬)
edges:边(依赖关系)
'''
@classmethod
def readExcel(cls, file):
nodes = []
edges = []
# 输⼊的表格必须整⾏整列(即m*n矩阵中的每个单元格不能为空)
wb = openpyxl.load_workbook(file)
mySheet = wb['Sheet1']
m_rows = mySheet.max_row
n_columns = mySheet.max_column
for i in range(1, m_rows + 1):
mylist = []
for j in range(1, n_columns + 1):
key = ll(i, j).value
mylist.append(key)
if (j < 3) and (key not in nodes):
nodes.append(key)
tup = tuple(mylist)
edges.append(tup)
wb.save(file)
return cls(nodes, edges)
def __init__(self, nodes, edges):
self.edges = edges
dict = {}
mylist1 = []
bootstrap使用类实现标签mylist2 = []
for edge in edges:
node1 = edge[0]
node2 = edge[1]
mylist1.append(node1)
mylist2.append(node2)
for key in nodes:
num1 = unt(key)
num2 = unt(key)
num = num1 + num2
dict[key] = num
'''
function:
读⼊节点和边
input:
nodes:节点(项⽬)
edges:边(依赖关系)
weight_b:是否带权重(默认为true)
'''
def addNodesEdges(self, weight_b):
# 定义graph
self.weight_b = weight_b
G.add_nodes_des)
if self.weight_b:
G.add_weighted_edges_from(self.edges)
else:
G.add_edges_from(self.edges)
return
'''
function:
将极⼤连通⼦图写⼊Excel⽂档(默认带权重)
input:
mylist:连通⼦图
关于sqlyog使用正确的是output:
file:输出表格(xlsx格式):第⼀列为源项⽬,第⼆列为引⽤项⽬,第三列为权重(可选)
'''
def writeGraph(self, file):
# 写⽂件
wb = openpyxl.Workbook() # 创建新的excel⽂件,⼀个⼯作簿(workbook)在创建的时候同时⾄少也新建了⼀张⼯作表(worksheet) # wb.save(file)
sheet_names = wb.sheetnames # 获取所有表名
sheet1 = wb[sheet_names[0]] # 打开第⼀个 sheet ⼯作表
i = 1
print("写⼊极⼤连通⼦图")
for sublist ted_components(G):
j = 1
for key in sublist:
j += 1
i += 1
#print("写⼊极⼤连通⼦图数:", i)
wb.save(file)
return
'''
计算极⼤连通⼦图中不同图的数量及节点个数
统计极⼤连通⼦图
input:
mylist:连通⼦图
'''
数据库设计入门经典def count(self):
dict = {}
for tmp_list ted_components(G):
length = len(tmp_list)
ret = (length)
if ret == None:
dict[length] = 1
else:
dict[length] = ret + 1
items = sorted(dict.items(), key=lambda item: item[0]) for key in items:
print(key)
教程自学# print('\n')
return
'''
过滤社区中节点数⼩于threshold的极⼤连通⼦图
input:
mylist:极⼤连通⼦图 nx.connected_components(G) threshold:节点阈值
output:
节点列表
'''
def filtercommunity(self, threshold):
dataset = []
for tmplist ted_components(G):
if len(tmplist) > threshold:
for key in tmplist:
if key not in dataset:
dataset.append(key)
self.dataset = py()
return
'''
求最⼤的极⼤连通⼦图
'''
def maxSubGraph(self):
maxsubgraph = []
max = 0
for tmplist ted_components(G):python在线编辑器python3
if max < len(tmplist):
max = len(tmplist)
maxsubgraph.clear()
maxsubgraph = py()
self.max = max
self.maxsubgraph = maxsubgraph
dataset = []
for key in maxsubgraph:
if key not in dataset:
dataset.append(key)
self.dataset = dataset
print('最⼤连通⼦图的项⽬数:', max)
return
'''
function:
显⽰图形
input:
mylist:连通⼦图
'''
def show(self):
nx.draw(G, with_labels=True, font_weight='bold') plt.axis('on')
plt.show()
return
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论