예제 #1
0
def run_question_5():
    plt.title(
        "Graph resilience for random attack order of the first 20% nodes")
    computer_network = (load_graph(NETWORK_URL))
    total_nodes = len(computer_network)  # 1239
    proc20 = total_nodes / 5  # 247
    nodes_remaining = total_nodes - proc20
    plt.axis([0, 250, 0, 1400])
    p = 0.004
    m = 2

    highest_cc = []
    plots = [(computer_network, "Computer network"),
             (random_ugraph(total_nodes, p), "ER graph, p = " + str(p)),
             (UPA(total_nodes, m), "UPA graph, m = " + str(m))]

    for graph, label_on_plot in plots:
        data = (
            pro2.compute_resilience(graph, targeted_order_fast(graph))
        )  # since the 'compute_resilience' procedure already outputs the correct order we don't really have to do anything extra
        highest_cc.append(data[proc20])
        plt.plot(data, linewidth=1, label=label_on_plot)

    print "The biggest connected components after removal of 20% of nodes, with", nodes_remaining, "nodes remaining:", highest_cc
    print "The % of connected components removed after removal of 20% of nodes:", [
        "{0:.0%}".format(cc / float(nodes_remaining)) for cc in highest_cc
    ]  # values must be ~equal to the number of nodes remaining
    plt.legend()
    plt.show()
예제 #2
0
def run_question_4():
    computer_network = (load_graph(NETWORK_URL))
    total_nodes = len(computer_network)  # 1239
    total_edges = compute_edges(computer_network)  # 3047
    p = 0.004
    m = 2
    plots = [(computer_network, "Computer network"),
             (random_ugraph(total_nodes, p), "ER graph, p = " + str(p)),
             (UPA(total_nodes, m), "UPA graph, m = " + str(m))]

    for graph, label_on_plot in plots:
        y = pro2.compute_resilience(
            graph, targeted_order_fast(graph)
        )  # since the 'compute_resilience' procedure already outputs the correct order we don't really have to do anything extra
        plt.plot(y, linewidth=1, label=label_on_plot)

    plt.title("Graph resilience comparison for targeted attack order")
    plt.xlabel("Number of nodes removed")
    plt.ylabel("Largest component size")
    plt.axis([0, 1400, 0, 1400])
    plt.legend()
    plt.show(
    )  # this plot will show us that for three similar graphs (same no. of nodes and edges) the resilience of the graph is alike
예제 #3
0
def run_question_1():
    computer_network = (load_graph(NETWORK_URL))
    total_nodes = len(computer_network)  # 1239
    total_edges = compute_edges(computer_network)  # 3047

    # 1.1 To begin, you should determine the probability 'p' such that the ER graph computed using this edge probability has approximately the same number of edges as the computer network.
    # (Your choice for 'p' should be consistent with considering each edge in the undirected graph exactly once, not twice.)
    plt.title("Number of edges distribution for various probabilities")
    plt.xlabel("Probability")
    plt.ylabel("Number of edges created")
    x = []
    y = []

    for probability in np.arange(0, 0.01, 0.001):
        x.append(probability)
        y.append(
            compute_edges(random_ugraph(total_nodes, probability))
        )  # here we check the number of edges we can get for a given probability

    plt.plot(
        x,
        y,
        color='blue',
        marker='.',
        linestyle='-',
        linewidth=1,
        markersize=2
    )  # a non-logarithmic version where we can see how the number of edges changes as we change probability
    plt.show(
    )  # this plot will show us that the closes to 3047 edges is probability equal to 0.004

    # 1.2 Likewise, you should compute an integer 'm' such that the number of edges in the UPA graph is close to the number of edges in the computer network. Remember that all three graphs being analyzed in this
    # Application should have the same number of nodes and approximately the same number of edges.
    plt.title("Number of edges distribution for various values of m")
    plt.xlabel("m of UPA function")
    plt.ylabel("Number of edges created")
    x = []
    y = []

    for m in range(1, 5):
        x.append(m)
        y.append(
            compute_edges(UPA(total_nodes, m))
        )  # here we check the number of edges we can get for a given 'm'

    plt.plot(
        x,
        y,
        color='blue',
        marker='.',
        linestyle='-',
        linewidth=1,
        markersize=2
    )  # a non-logarithmic version where we can see how the number of edges changes as we change probability
    plt.show(
    )  # this plot will show us that the closes to 3047 edges is m equal to either 2 or 3

    # 1.3 Once you have computed the resilience for all three graphs, plot the results as three curves combined in a single standard plot (not log/log). Use a line plot for each curve. The horizontal axis for your single plot
    #  be the the number of nodes removed (ranging from zero to the number of nodes in the graph) while the vertical axis should be the size of the largest connect component in the graphs resulting from the node removal.
    plt.title("Graph resilience comparison for random attack order")
    plt.xlabel("Number of nodes removed")
    plt.ylabel("Largest component size")
    plt.axis([0, 1400, 0, 1400])
    p = 0.004
    m = 2
    plots = [(computer_network, "Computer network"),
             (random_ugraph(total_nodes, p), "ER graph, p = " + str(p)),
             (UPA(total_nodes, m), "UPA graph, m = " + str(m))]

    for graph, label_on_plot in plots:
        y = pro2.compute_resilience(
            graph, random_order(graph)
        )  # since the 'compute_resilience' procedure already outputs the correct order we don't really have to do anything extra
        plt.plot(y, linewidth=1, label=label_on_plot)

    plt.legend()
    plt.show(
    )  # this plot will show us that for three similar graphs (same no. of nodes and edges) the resilience of the graph is alike
예제 #4
0
def run_suite():
    """ Some informal testing code """
    print("\nSTARTING TESTS:")
    suite = poc_simpletest.TestSuite()  # create a TestSuite object

    # 1. check the basic methods of the program
    suite.run_test(
        pro.bfs_visited(
            {
                0: set([1, 2, 3]),
                1: set([0, 2, 3]),
                2: set([0, 1, 3]),
                3: set([0, 1, 2])
            }, 0), set([0, 1, 2, 3]), "Test #1a: 'bfs_visited' method")
    suite.run_test(
        pro.bfs_visited({
            0: set([]),
            1: set([2]),
            2: set([1]),
            3: set([])
        }, 0), set([0]), "Test #1b: 'bfs_visited' method")
    suite.run_test(
        pro.bfs_visited({
            0: set([]),
            1: set([2]),
            2: set([1]),
            3: set([])
        }, 2), set([1, 2]), "Test #1b: 'bfs_visited' method")

    # 2. check the basic methods of the program
    suite.run_test(
        pro.cc_visited({
            0: set([1, 2, 3]),
            1: set([0, 2, 3]),
            2: set([0, 1, 3]),
            3: set([0, 1, 2])
        }), [set([0, 1, 2, 3])], "Test #2a: 'cc_visited' method")
    suite.run_test(
        pro.cc_visited({
            0: set([]),
            1: set([2]),
            2: set([1]),
            3: set([]),
            4: set([])
        }), [set([0]), set([1, 2]), set([3]),
             set([4])], "Test #2b: 'cc_visited' method")
    suite.run_test(
        pro.cc_visited({
            0: set([]),
            1: set([]),
            2: set([]),
            3: set([])
        }),
        [set([0]), set([1]), set([2]), set([3])],
        "Test #2c: 'cc_visited' method")

    # 3. check the basic methods of the program
    suite.run_test(
        pro.largest_cc_size({
            0: set([1, 2, 3]),
            1: set([0, 2, 3]),
            2: set([0, 1, 3]),
            3: set([0, 1, 2])
        }), 4, "Test #3a: 'largest_cc_size' method")
    suite.run_test(
        pro.largest_cc_size({
            0: set([]),
            1: set([2, 3]),
            2: set([1]),
            3: set([1]),
            4: set([])
        }), 3, "Test #3b: 'largest_cc_size' method")
    suite.run_test(
        pro.largest_cc_size({
            0: set([]),
            1: set([]),
            2: set([]),
            3: set([])
        }), 1, "Test #3c: 'largest_cc_size' method"
    )  # if there are no edges, then the answer is '1' as there is one node at the time

    # 4. check the basic methods of the program
    suite.run_test(
        pro.compute_resilience(
            {
                0: set([]),
                1: set([2, 3]),
                2: set([1]),
                3: set([1]),
                4: set([])
            },
            [2, 3, 1]), [3, 2, 1, 1], "Test #4a: 'compute_resilience' method"
    )  # first element in the list is the initial state

    # 6. report number of tests and failures
    suite.report_results()