python 在图形上实现广度优先搜索以查找连接的组件以及通过删除节点来测试图形弹性的各种方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在图形上实现广度优先搜索以查找连接的组件以及通过删除节点来测试图形弹性的各种方法相关的知识,希望对你有一定的参考价值。
"""
Implements BFS
Updated to use python3
Imports physics citation graph as sample data
"""
# general imports
import requests
from collections import deque
# import matplotlib.pyplot as plt
import math
import random
###################################
# Code for loading citation graph
# comment out for owltest
# CITATION_URL = "http://storage.googleapis.com/codeskulptor-alg/alg_phys-cite.txt"
# NETWORK_URL = "http://storage.googleapis.com/codeskulptor-alg/alg_rf7.txt"
# def load_graph(graph_url):
# """
# Function that loads a graph given the URL
# for a text representation of the graph
#
# Returns a dictionary that models a graph
# """
# graph_file = requests.get(graph_url)
# graph_text = graph_file.text
# graph_lines = graph_text.split('\n')
# graph_lines = graph_lines[ : -1]
#
# print ("Loaded graph with", len(graph_lines), "nodes")
#
# answer_graph = {}
# for line in graph_lines:
# neighbors = line.split(' ')
# node = int(neighbors[0])
# answer_graph[node] = set([])
# for neighbor in neighbors[1 : -1]:
# answer_graph[node].add(int(neighbor))
#
# return answer_graph
def bfs_visited(ugraph,start_node):
"""
Function that loads a undirected graph ugraph,
a starting node, and returns a set of all visited nodes
based on a breadth first search
"""
bfs_queue = deque()
visited = set()
visited.add(start_node)
bfs_queue.append(start_node)
while len(bfs_queue) > 0:
node_to_process = bfs_queue.popleft()
for neighbour in ugraph[node_to_process]:
if neighbour not in visited:
visited.add(neighbour)
bfs_queue.append(neighbour)
return visited
def cc_visited(ugraph):
"""
Function that loads a undirected graph ugraph,
returns a set of all connected components
based on a breadth first search
"""
remaining_nodes=set(ugraph.keys())
connected_components=[]
while len(remaining_nodes) > 0:
chosen_node=random.choice(list(remaining_nodes))
chosen_node_connected = bfs_visited(ugraph,chosen_node)
connected_components.append(chosen_node_connected)
remaining_nodes=remaining_nodes - chosen_node_connected
print(len(remaining_nodes))
return connected_components
def largest_cc_size(ugraph):
"""
Function that loads a undirected graph ugraph,
returns a list of largest connected components
"""
largest_cc_set=cc_visited(ugraph)
max_cc = 0
for connected_component in largest_cc_set:
if len(connected_component) > max_cc:
max_cc = len(connected_component)
return max_cc
def compute_resilience(ugraph,attack_order):
"""
Function that loads a undirected graph ugraph,
removes one node in turn, and then
returns a list of largest connected component size
"""
resilience_list=[]
resilience_list.append(largest_cc_size(ugraph))
for node in attack_order:
neighbors = ugraph[node]
ugraph.pop(node)
for neighbor in neighbors:
ugraph[neighbor].remove(node)
resilience_list.append(largest_cc_size(ugraph))
return resilience_list
# Graph portions (comment out for owl_test)
# citation_graph = load_graph(CITATION_URL)
# network_graph=load_graph(NETWORK_URL)
# # # # bfs_visited(citation_graph,9306141)
# # # cc_visited(citation_graph)
# print(compute_resilience(network_graph,[1, 2, 3, 4, 5, 6, 7, 8]))
以上是关于python 在图形上实现广度优先搜索以查找连接的组件以及通过删除节点来测试图形弹性的各种方法的主要内容,如果未能解决你的问题,请参考以下文章