From 81ef4b724d463ce0a3620c35070cf71bb87dfd43 Mon Sep 17 00:00:00 2001 From: test Date: Thu, 16 Jun 2022 21:44:51 +0200 Subject: [PATCH] star_topology --- network_attack_propagation.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/network_attack_propagation.py b/network_attack_propagation.py index 41a9fcd..8001521 100644 --- a/network_attack_propagation.py +++ b/network_attack_propagation.py @@ -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 @@ -73,12 +75,36 @@ def do_graph_animation(output_file_name: str, in_graph: Graph, frame_count: int) def bus_network(n=30) -> Graph: network = Graph() nodes = [Node() for _ in range(n)] - edges = [Edge(nodes[i], nodes[i+1], 1.0) for i in range(n-1)] + edges = [Edge(nodes[i], nodes[i + 1], 1.0) for i in range(n - 1)] network.add_edges(edges) 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 main(): network = Graph() nodes = [Node(True), Node(), Node(), Node(True), Node()] @@ -97,6 +123,9 @@ def main(): bus = bus_network() do_graph_animation('bus.gif', bus, 5) + star, star_avg_rank = star_network() + do_graph_animation('star.gif', star, 5) + if __name__ == "__main__": main()