Exemple #1
0
def run_example():
    network_graph = load_network_graph()
    num_nodes = len(network_graph)
    print "Total number of nodes in Network Graph : ", num_nodes
    total_edges = 0
    for node in network_graph:
        edge = network_graph[node]
        total_edges += len(edge)
    total_edges /= 2
    print "Total number of edges in Network Graph : ", total_edges

    prob_edge = float((total_edges * 2)) / (num_nodes * (num_nodes - 1))
    er_graph = load_er_graph(num_nodes, prob_edge)
    print "Total number of nodes in ER Graph : ", num_nodes
    total_edges = 0
    for node in er_graph:
        edge = er_graph[node]
        total_edges += len(edge)
    total_edges /= 2
    print "Total number of edges in ER Graph : ", total_edges, " with probability : ", prob_edge

    approx_m = total_edges / num_nodes
    exact_m = int(
        math.ceil(0.5 *
                  (-math.sqrt(((2 * num_nodes - 1)**2) - 8 * total_edges) +
                   (2 * num_nodes - 1))))
    upa_graph = load_upa_graph(num_nodes, approx_m)
    print "Total number of nodes in UPA Graph : ", num_nodes
    total_edges = 0
    for node in upa_graph:
        edge = upa_graph[node]
        total_edges += len(edge)
    total_edges /= 2
    print "Total number of edges in UPA Graph : ", total_edges, " with m : ", approx_m

    attack_network = fast_targeted_order(network_graph)
    resilience_network = cc_gr.compute_resilience(
        network_graph, attack_network[:len(attack_network) / 5])

    attack_er = fast_targeted_order(er_graph)
    resilience_er = cc_gr.compute_resilience(er_graph,
                                             attack_er[:len(attack_er) / 5])

    attack_upa = fast_targeted_order(upa_graph)
    resilience_upa = cc_gr.compute_resilience(upa_graph,
                                              attack_upa[:len(attack_upa) / 5])

    print resilience_network[-1], len(network_graph)
    print resilience_er[-1], len(er_graph)
    print resilience_upa[-1], len(upa_graph)
def run_example():
    network_graph = load_network_graph()
    num_nodes = len(network_graph)
    print "Total number of nodes in Network Graph : ", num_nodes
    total_edges = 0
    for node in network_graph:
        edge = network_graph[node]
        total_edges += len(edge)
    total_edges /= 2
    print "Total number of edges in Network Graph : ", total_edges

    prob_edge = float((total_edges * 2)) / (num_nodes * (num_nodes - 1))
    er_graph = load_er_graph(num_nodes, prob_edge)
    print "Total number of nodes in ER Graph : ", num_nodes
    total_edges = 0
    for node in er_graph:
        edge = er_graph[node]
        total_edges += len(edge)
    total_edges /= 2
    print "Total number of edges in ER Graph : ", total_edges, " with probability : ", prob_edge

    approx_m = total_edges / num_nodes
    exact_m = int(math.ceil(0.5 * (-math.sqrt(((2 * num_nodes - 1) ** 2) - 8 * total_edges) + (2 * num_nodes - 1))))
    upa_graph = load_upa_graph(num_nodes, approx_m)
    print "Total number of nodes in UPA Graph : ", num_nodes
    total_edges = 0
    for node in upa_graph:
        edge = upa_graph[node]
        total_edges += len(edge)
    total_edges /= 2
    print "Total number of edges in UPA Graph : ", total_edges, " with m : ", approx_m

    attack_network = random_order(network_graph)
    resilience_network = cc_gr.compute_resilience(network_graph, attack_network[:len(attack_network)/5])

    attack_er = random_order(er_graph)
    resilience_er = cc_gr.compute_resilience(er_graph, attack_er[:len(attack_er)/5])

    attack_upa = random_order(upa_graph)
    resilience_upa = cc_gr.compute_resilience(upa_graph, attack_upa[:len(attack_upa)/5])

    print resilience_network[-1], len(network_graph)
    print resilience_er[-1], len(er_graph)
    print resilience_upa[-1], len(upa_graph)
def question1():
    """
    In question 1, we will examine the resilience of the computer network
    under an attack in which servers are chosen at random. We will then compare
    the resilience of the network to the resilience of ER and UPA graphs of
    similar size.
    """
    # Determine the probability such that the ER graph has approx. the same
    # number of edges as the computer network. Likewise, compute an integer such 
    # that the number of edges in the UPA graph is close to the number of edges
    # in the computer network. Each graph should have the same number of nodes, 1239.
    ER_prob =  .004
    UPA_num_existing_nodes = 3
    
    ER_graph = make_ER_graph(1239, ER_prob)
    UPA_graph = make_UPA_graph(1239, UPA_num_existing_nodes)
    network_graph = make_computer_network_graph()

    # Compute random attack order for each graph.
    ER_random = random_order(ER_graph)
    UPA_random = random_order(UPA_graph)
    network_random = random_order(network_graph)

    # Compute the resilience of each graph.
    ER_resilience = project2.compute_resilience(ER_graph, ER_random)
    UPA_resilience = project2.compute_resilience(UPA_graph, UPA_random)
    network_resilience = project2.compute_resilience(network_graph, network_random)

    # Plot the results
    xvals = range(len(network_resilience))
    ER_yvals = ER_resilience
    UPA_yvals = UPA_resilience
    network_yvals = network_resilience

    plt.title("Question 1: Resilience of Computer Network, ER Graph, and UPA Graph")
    plt.xlabel("Number of Nodes Removed")
    plt.ylabel("Size of Largest Connected Component")

    plt.plot(xvals, ER_yvals, '-b', label='ER Graph (p=.004)')
    plt.plot(xvals, UPA_yvals, '-r', label='UPA Graph (m=3)')
    plt.plot(xvals, network_yvals, '-g', label='Computer Network')
    plt.grid(which='major', axis='both')
    plt.legend(loc='upper right')
    plt.show()
def question4():
    """
    This function:
    1) Uses fast_targeted_order to compute a targeted attack order for 
    each of the three graphs (computer network, ER, UPA) from Question 1.
    2) Uses compute_resilience to compute the resilience of each graph.
    3) Plots the computed resiliences as three curves in a single standard plot.
    """
    # Three graphs. The probability argument (.004) for make_ER_graph and 
    # num_existing_nodes argument (3) for make_UPA_graph are same as in question1. 
    network_graph = make_computer_network_graph()
    ER_graph = make_ER_graph(1239, .004)
    UPA_graph = make_UPA_graph(1239, 3)

    # Computes a targeted attack order for each of the three graphs.
    network_order = fast_targeted_order(network_graph)
    ER_order = fast_targeted_order(ER_graph)
    UPA_order = fast_targeted_order(UPA_graph)

    # Computes graph resilience for each of the three graphs.
    network_resilience = project2.compute_resilience(network_graph, network_order) 
    ER_resilience = project2.compute_resilience(ER_graph, ER_order)
    UPA_resilience = project2.compute_resilience(UPA_graph, UPA_order)

    # Plot the computed resiliences.
    xvals = range(len(network_resilience))
    ER_yvals = ER_resilience
    UPA_yvals = UPA_resilience
    network_yvals = network_resilience

    plt.title("Question 4: Graph and Network Resilience Under Connectivity-Based Attack")
    plt.xlabel("Number of Nodes Removed")
    plt.ylabel("Size of Largest Connected Component")

    plt.plot(xvals, ER_yvals, '-b', label='ER Graph (p=.004)')
    plt.plot(xvals, UPA_yvals, '-r', label='UPA Graph (m=3)')
    plt.plot(xvals, network_yvals, '-g', label='Computer Network')
    plt.grid(which='major', axis='both')
    plt.legend(loc='upper right')
    plt.show()
def test_compute_resilience():
    resilience = cc.compute_resilience(graphs.GRAPH2, [1, 3, 5, 7, 2, 4, 6, 8])
    print resilience
def test_compute_resilience():
    resilience = cc.compute_resilience(graphs.GRAPH2, [1, 3, 5, 7, 2, 4, 6, 8])
    print resilience