Exemplo n.º 1
0
def Question3():
	wynik = list()
	for step in range(10, 1000, 10):
		start = time.clock()
		UPA_graph = UPA_generator(5,step)
		UPA_fast_order = alg_application2_provided.fast_targeted_order(UPA_graph)
		stop = time.clock()
		wynik.append(stop-start)
	xvals = range(10, 1000, 10)
	yvals1 = wynik
	wynik = list()
	for step in range(10, 1000, 10):
		start = time.clock()
		UPA_graph = UPA_generator(5,step)
		UPA_fast_order = alg_application2_provided.targeted_order(UPA_graph)
		stop = time.clock()
		wynik.append(stop-start)
	xvals = range(10, 1000, 10)
	yvals2 = wynik
	#yvals = [1000*item for item in wynik]
	plt.plot(xvals, yvals1, '-b', label='fast_targeted_order')
	plt.plot(xvals, yvals2, '-r', label='targeted_order')
	plt.legend(loc='upper right')
	plt.title('desktop Python time plotting')  
	plt.xlabel('number of nodes')  
	plt.ylabel('time')  
	plt.show()
Exemplo n.º 2
0
def Question3():
    wynik = list()
    for step in range(10, 1000, 10):
        start = time.clock()
        UPA_graph = UPA_generator(5, step)
        UPA_fast_order = alg_application2_provided.fast_targeted_order(
            UPA_graph)
        stop = time.clock()
        wynik.append(stop - start)
    xvals = range(10, 1000, 10)
    yvals1 = wynik
    wynik = list()
    for step in range(10, 1000, 10):
        start = time.clock()
        UPA_graph = UPA_generator(5, step)
        UPA_fast_order = alg_application2_provided.targeted_order(UPA_graph)
        stop = time.clock()
        wynik.append(stop - start)
    xvals = range(10, 1000, 10)
    yvals2 = wynik
    #yvals = [1000*item for item in wynik]
    plt.plot(xvals, yvals1, '-b', label='fast_targeted_order')
    plt.plot(xvals, yvals2, '-r', label='targeted_order')
    plt.legend(loc='upper right')
    plt.title('desktop Python time plotting')
    plt.xlabel('number of nodes')
    plt.ylabel('time')
    plt.show()
Exemplo n.º 3
0
def Question4():
	n = 1239
	xvals = range(n+1)
	UPA_graph = UPA_generator(2,n)
	ER_graph = make_ER_graph_2(n,.0034)
	CN_graph = alg_application2_provided.load_graph(NETWORK_URL)
	UPA_fast_order = alg_application2_provided.fast_targeted_order(UPA_graph)
	ER_fast_order = alg_application2_provided.fast_targeted_order(ER_graph)
	CN_fast_order = alg_application2_provided.fast_targeted_order(CN_graph)
	UPA_resilience = module2_project.compute_resilience(UPA_graph, UPA_fast_order)
	ER_resilience = module2_project.compute_resilience(ER_graph, ER_fast_order)
	CN_resilience = module2_project.compute_resilience(CN_graph, CN_fast_order)
	yvals1 = UPA_resilience
	yvals2 = ER_resilience
	yvals3 = CN_resilience
	plt.plot(xvals, yvals1, '-b', label='UPA_resilience m = 2')
	plt.plot(xvals, yvals2, '-r', label='ER_resilience, p = .0034')
	plt.plot(xvals, yvals3, '-g', label='ComputerNetworkGraph_resilience')
	plt.legend(loc='upper right')
	plt.title('plot of resilience of three graphs ')  
	plt.xlabel('number of removed nodes')  
	plt.ylabel('size of the largest connected component')  
	plt.show()
Exemplo n.º 4
0
def Question4():
    n = 1239
    xvals = range(n + 1)
    UPA_graph = UPA_generator(2, n)
    ER_graph = make_ER_graph_2(n, .0034)
    CN_graph = alg_application2_provided.load_graph(NETWORK_URL)
    UPA_fast_order = alg_application2_provided.fast_targeted_order(UPA_graph)
    ER_fast_order = alg_application2_provided.fast_targeted_order(ER_graph)
    CN_fast_order = alg_application2_provided.fast_targeted_order(CN_graph)
    UPA_resilience = module2_project.compute_resilience(
        UPA_graph, UPA_fast_order)
    ER_resilience = module2_project.compute_resilience(ER_graph, ER_fast_order)
    CN_resilience = module2_project.compute_resilience(CN_graph, CN_fast_order)
    yvals1 = UPA_resilience
    yvals2 = ER_resilience
    yvals3 = CN_resilience
    plt.plot(xvals, yvals1, '-b', label='UPA_resilience m = 2')
    plt.plot(xvals, yvals2, '-r', label='ER_resilience, p = .0034')
    plt.plot(xvals, yvals3, '-g', label='ComputerNetworkGraph_resilience')
    plt.legend(loc='upper right')
    plt.title('plot of resilience of three graphs ')
    plt.xlabel('number of removed nodes')
    plt.ylabel('size of the largest connected component')
    plt.show()
Exemplo n.º 5
0
#graph of computer network
cn_graph = aa2p.load_graph(aa2p.NETWORK_URL)
print edges_in_undirected_graph(cn_graph)

#graph of ER
er_graph = er_algorithm(len(cn_graph.keys()), 0.002)
#print er_graph
print edges_in_undirected_graph(er_graph)

#graph of UPA
upa_graph = get_upa_graph(len(cn_graph.keys()), 2)
print edges_in_undirected_graph(upa_graph)

#random attack
cn_attack_order = aa2p.fast_targeted_order(cn_graph)
er_attack_order = aa2p.fast_targeted_order(er_graph)
upa_attack_order = aa2p.fast_targeted_order(upa_graph)

#resilience for different graphs
cn_resilience = pj2.compute_resilience(aa2p.copy_graph(cn_graph), cn_attack_order)
er_resilience = pj2.compute_resilience(aa2p.copy_graph(er_graph), er_attack_order)
upa_resilience = pj2.compute_resilience(aa2p.copy_graph(upa_graph), upa_attack_order)

print len(cn_resilience)
print len(er_resilience)
print len(upa_resilience)

#making comparison plots
fig, ax = plt.subplots()
def question_four():
    '''
    Question 4 (5 pts)
    To continue our analysis of the computer network, we will examine its resilience under an attack in which servers are chosen based on their connectivity.
    We will again compare the resilience of the network to the resilience of ER and UPA graphs of similar size.

    Using targeted_order (or fast_targeted_order), your task is to compute a targeted attack order for each of the three graphs (computer network, ER, UPA) from Question 1.
    Then, for each of these three graphs, compute the resilience of the graph using compute_resilience.
    Finally, plot the computed resiliences as three curves (line plots) in a single standard plot.
    As in Question 1, please include a legend in your plot that distinguishes the three plots.
    The text labels in this legend should include the values for p and m that you used in computing the ER and UPA graphs, respectively.

    Your plot will be assessed based on the answers to the following three questions:

    Does the plot follow the formatting guidelines for plots?
    Does the plot include a legend? Does this legend indicate the values for p and m used in ER and UPA, respectively?
    Do the three curves in the plot have the correct shape?
    '''

    ###
    computer_network_nodes_length = len(computer_network_dict)
    computer_network_edges = sum(len(val) for val in computer_network_dict.itervalues()) / 2

    er_prob = (computer_network_edges / float(computer_network_nodes_length)) / (float(computer_network_nodes_length) - 1) * 2

    print '\n', 'Example network total nodes: ', len(computer_network_dict)
    print 'Computer network undirected edges: ', sum(len(val) for val in computer_network_dict.itervalues()) / 2, '\n'
    print 'Probability for er_graph',  er_prob, '\n'

    er_graph = deggraph.make_ugraph_prob(len(computer_network_dict), er_prob)

    print 'ER network total nodes: ', len(er_graph)
    print 'ER network undirected edges: ', sum(len(val) for val in er_graph.itervalues()) / 2, '\n'

    m_value_upa = int(math.ceil(computer_network_edges / float(computer_network_nodes_length)))

    print 'M value for UPA graph (total nodes divided by edges): ', m_value_upa, '\n'

    upa_graph = create_UPA_graph(len(computer_network_dict), m_value_upa)

    print 'UPA network total nodes: ', len(upa_graph)
    print 'UPA network undirected edges: ', sum(len(val) for val in upa_graph.itervalues()) / 2, '\n'
    ###

    computer_network_target = app2_prov.fast_targeted_order(computer_network_dict)
    er_target = app2_prov.fast_targeted_order(er_graph)
    upa_target = app2_prov.fast_targeted_order(upa_graph)

    computer_network = project2.compute_resilience(computer_network_dict, computer_network_target)
    er_network = project2.compute_resilience(er_graph, er_target)
    upa_network = project2.compute_resilience(upa_graph, upa_target)

    # see question4.png for graph results

    plt.plot(computer_network, lw=2, label='Example computer network')
    plt.plot(er_network, lw=2, label='ER network - p = 0.00397')
    plt.plot(upa_network, lw=2, label='UPA network - m = 3')
    plt.legend(loc='upper right')
    plt.title('Different networks resilience versus targeted attack')
    plt.xlabel('Number of nodes removed')
    plt.ylabel('Largest connected component size')

    plt.show()

    pass
def question_three():
    '''
    Question 3 (3 pts)
    In the next three problems, we will consider attack orders in which the nodes being removed are chosen based on the structure of the graph.
    A simple rule for these targeted attacks is to always remove a node of maximum (highest) degree from the graph.
    The function targeted_order(ugraph) in the provided code takes an undirected graph ugraph and iteratively does the following:

    Computes a node of the maximum degree in ugraph.
    If multiple nodes have the maximum degree, it chooses any of them (arbitrarily).
    Removes that node (and its incident edges) from ugraph.

    Observe that targeted_order continuously updates ugraph and always computes a node of maximum degree with respect to this updated graph.
    The output of targeted_order is a sequence of nodes that can be used as input to compute_resilience.

    As you examine the code for targeted_order, you feel that the provided implementation of targeted_order is not as efficient as possible.
    In particular, much work is being repeated during the location of nodes with the maximum degree.
    In this question, we will consider an alternative method (which we will refer to as fast_targeted_order) for computing the same targeted attack order.
    Here is a pseudo-code description of the method:

    In Python, this method creates a list degree_sets whose kth element is the set of nodes of degree k.
    The method then iterates through the list degree_sets in order of decreasing degree. When it encounter a non-empty set, the nodes in this set must be of maximum degree.
    The method then repeatedly chooses a node from this set, deletes that node from the graph, and updates degree_sets appropriately.

    For this question, your task is to implement fast_targeted_order and then analyze the running time of these two methods on UPA graphs of size n with m=5.
    Your analysis should be both mathematical and empirical and include the following:

    Determine big-O bounds of the worst-case running times of targeted_order and fast_targeted_order as a function of the number of nodes n in the UPA graph.
    Compute a plot comparing the running times of these methods on UPA graphs of increasing size.

    Since the number of edges in these UPA graphs is always less than 5n (due to the choice of m=5), your big-O bounds for both functions should be expressions in n.
    You should also assume that the all of the set operations used in fast_targeted_order are O(1).

    Next, run these two functions on a sequence of UPA graphs with n in range(10, 1000, 10) and m=5 and use the time module (or your favorite Python timing utility)
    to compute the running times of these functions.
    Then, plot these running times (vertical axis) as a function of the number of nodes n (horizontal axis) using a standard plot (not log/log).
    Your plot should consist of two curves showing the results of your timings.
    Remember to format your plot appropriately and include a legend.
    The title of your plot should indicate the implementation of Python (desktop Python vs. CodeSkulptor) used to generate the timing results.

    Your answer to this question will be assessed according to the following three items:

    What are tight upper bounds on the worst-case running times of targeted_order and fast_targeted_order? Use big-O notation to express your answers (which should be very simple).
    Does the plot follow the formatting guidelines for plots? Does the plot include a legend? Does the title include the implementation of Python used to compute the timings?
    Are the shapes of the timing curves in the plot correct?
    '''

    # see FastTargetedOrderFig.png for pseudo-code
    # see alg_application2_provided for provided targeted_order code
    # and my implementation of fast_targeted_order

    # see question3.png for graph results

    '''
    Answer to upper bounds on the worst-case running times:
    Fast_targeted_order : O(n)
    Targeted_order: O(n^2)
    '''

    m = 5 # given by question

    # produce timing for Fast_targeted_order
    x_values = []
    y_values = []

    for n in xrange(10, 1000, 10):
        graph = create_UPA_graph(n, m)
        start_time = time.clock()
        app2_prov.fast_targeted_order(graph)
        finish_time = time.clock()
        run_time = (finish_time - start_time) * 1000 # convert to ms
        x_values.append(n)
        y_values.append(run_time)

    plt.plot(x_values, y_values, lw=2, label='fast_targeted_order (UPA)')

    # produce timing for targeted_order
    x_values = []
    y_values = []

    for n in xrange(10, 1000, 10):
        graph = create_UPA_graph(n, m)
        start_time = time.clock()
        app2_prov.targeted_order(graph)
        finish_time = time.clock()
        run_time = (finish_time - start_time) * 1000 # convert to ms
        x_values.append(n)
        y_values.append(run_time)

    plt.plot(x_values, y_values, lw=2, label='targeted_order (UPA)')

    plt.legend(loc='upper left')
    plt.title('Execution time (ms) for different UPA graph sizes (m = 5) (Desktop Python implementation)')
    plt.xlabel('Number of nodes')
    plt.ylabel('Running time (ms)')

    plt.show()

    pass
Exemplo n.º 8
0
#print time.time() - start_time
#attack_order_v2 = aa2p.fast_targeted_order(upa_graph)

#running time
running_time_v1 = []
for num_nodes in range(10, 1000, 10):
    upa_graph = get_upa_graph(num_nodes, 5)
    start_time = time.time()
    attack_order_v1 = aa2p.targeted_order(upa_graph)
    running_time_v1.append(time.time() - start_time)

running_time_v2 = []
for num_nodes in range(10, 1000, 10):
    upa_graph = get_upa_graph(num_nodes, 5)
    start_time = time.time()
    attack_order_v2 = aa2p.fast_targeted_order(upa_graph)
    running_time_v2.append(time.time() - start_time)
    
#making comparison plots
fig, ax = plt.subplots()
x_vals = [idx for idx in range(10, 1000, 10)]
#print len(x_vals)

ax.plot(x_vals, running_time_v1, '-b', label = 'targeted order')
ax.plot(x_vals, running_time_v2, '-r', label = 'fast targeted order')

ax.legend(loc = 'upper left')
ax.set_title('Timing results (in desktop Python)')
ax.set_xlabel('number of total nodes')
ax.set_ylabel('running time [second]')