Merge remote-tracking branch 'origin/stars'

# Conflicts:
#	network_attack_propagation.py
This commit is contained in:
Marcin Kostrzewski 2022-06-16 22:06:23 +02:00
commit 78e3b287fb

View File

@ -1,6 +1,8 @@
import random
import networkx as nx
from statistics import mean
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib import animation
@ -126,6 +128,30 @@ def bus_network(n=30, infected_idx=0) -> Graph:
return network
def rank_avg(edges, digits=2):
ranks = {}
for e in edges:
ranks[e.node_a] = ranks.get(e.node_a, 0) + 1
ranks[e.node_b] = ranks.get(e.node_b, 0) + 1
return round(mean(ranks.values()), digits)
def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float]:
node_count = cluster_count + cluster_count * starsize + 1
nodes = [Node() for _ in range(node_count)]
edges = []
for x in range(cluster_count):
center_node = x * starsize + x
edges += [Edge(nodes[center_node], nodes[i], 1.0) for i in range(center_node + 1, center_node + starsize + 1)]
edges.append(Edge(nodes[-1], nodes[center_node], 1.0))
network = Graph()
network.add_edges(edges)
return network, rank_avg(edges)
def ring_network(n=30) -> Graph:
network = Graph()
nodes = [Node() for _ in range(n)]
@ -161,6 +187,9 @@ def main():
ring = ring_network()
do_graph_animation('ring.gif', ring, 20, nx.circular_layout)
star, star_avg_rank = star_network()
do_graph_animation('star.gif', star, 5, nx.spring_layout)
if __name__ == "__main__":
main()