def question4_plot(): """ Loads the three graphs in the problem, calculates a sequence of targeted attacks using fast_targeted_attack and calculates the resilience of each graph based on this attack. Plots the results. """ # Loading the provided computer network example example_graph = graph_provided.load_graph(NETWORK_URL) # Creating a random ER graph with 1239 nodes and p = .004 er_graph = make_random_ugraph(1239, .004) # Creating an UPA graph, m=12 upa_graph = mod_upa.create_UPA_graph(1239, 2) # Calculating attack sequences with the provided targeted_order #example_attack = graph_provided.targeted_order(example_graph) #er_attack = graph_provided.targeted_order(er_graph) #upa_attack = graph_provided.targeted_order(upa_graph) # Calculating attack sequences with fast_targeted_order example_attack = fast_targeted_order(example_graph) er_attack = fast_targeted_order(er_graph) upa_attack = fast_targeted_order(upa_graph) # Calculating resilience example_resilience = mod_resilience.compute_resilience(example_graph, example_attack) er_resilience = mod_resilience.compute_resilience(er_graph, er_attack) upa_resilience = mod_resilience.compute_resilience(upa_graph, upa_attack) # Plotting the outcome xvals = range(1240) plt.plot(xvals, example_resilience, '-c', label='computer network example (provided)') plt.plot(xvals, er_resilience, '-y', label='generated ER graph (p = .004)') plt.plot(xvals, upa_resilience, '-m', label='generated UPA graph (m = 2)') plt.legend(loc='upper right') plt.title('Graphs resilience to targeted attack') plt.xlabel('Nodes removed') plt.ylabel('Biggest connected element size') plt.show()
for v_neighbor in ugraph[u_node]: v_degree = len(ugraph[v_neighbor]) degree_sets[v_degree].remove(v_neighbor) degree_sets[v_degree - 1].add(v_neighbor) graph_provided.delete_node(graph, u_node) index += 1 return attack_list # Tests for fast_targeted_order #my_graph = mod_upa.create_UPA_graph(10, 3) #print 'my graph = ' + str(my_graph) #print 'provided attack order = ' + str(graph_provided.targeted_order(my_graph)) my_graph = graph_provided.load_graph(NETWORK_URL) attack = fast_targeted_order(my_graph) resilience = mod_resilience.compute_resilience(my_graph, attack) def question3_plot(): """ Calculates a series of targeted attacks using both functions (provided and the fast targeted implemented one), computes the running time of these processes and plots the outcome """ # Initializing the data series xvals = range(10 , 1000, 10) targeted_times = [] fast_targeted_times = [] for size in range(10, 1000, 10): upa_graph = mod_upa.create_UPA_graph(size, 5)