From f2d85a3e7ddab738d3aa7441758c7bb4e52e4b07 Mon Sep 17 00:00:00 2001 From: Krystian Wasilewski Date: Mon, 20 Jun 2022 19:18:56 +0200 Subject: [PATCH] avg rank calculation for all topologies --- network_attack_propagation.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/network_attack_propagation.py b/network_attack_propagation.py index 4410634..2cd0055 100644 --- a/network_attack_propagation.py +++ b/network_attack_propagation.py @@ -146,16 +146,6 @@ def do_graph_animation(output_file_name: str, in_graph: Graph, frame_count: int, plt.show() -def bus_network(n=30, infected_idx=0) -> Graph: - network = Graph() - nodes = [Node() for _ in range(n)] - nodes[infected_idx].is_infected = True - 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: @@ -164,6 +154,16 @@ def rank_avg(edges, digits=2): return round(mean(ranks.values()), digits) +def bus_network(n=30, infected_idx=0) -> tuple[Graph, float]: + network = Graph() + nodes = [Node() for _ in range(n)] + nodes[infected_idx].is_infected = True + edges = [Edge(nodes[i], nodes[i + 1], 1.0) for i in range(n - 1)] + + network.add_edges(edges) + return network, rank_avg(edges) + + 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)] @@ -180,7 +180,7 @@ def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float]: return network, rank_avg(edges) -def ring_network(n=30) -> Graph: +def ring_network(n=30) -> tuple[Graph, float]: network = Graph() nodes = [Node() for _ in range(n)] nodes[0].is_infected = True @@ -189,14 +189,14 @@ def ring_network(n=30) -> Graph: edges.append(end_edge) network.add_edges(edges) - return network + return network, rank_avg(edges) def main(): - bus = bus_network(36) + bus, star_avg_rank = bus_network(36) do_graph_animation('bus.gif', bus, 90, nx.spiral_layout) - ring = ring_network(36) + ring, star_avg_rank = ring_network(36) do_graph_animation('ring.gif', ring, 90, nx.circular_layout) star, star_avg_rank = star_network()