def fastTargetedOrder(graph):
    ''' input : graph -- a graph
        output: a list, of nodes in decreasing order of their degrees   
    ''' 
    new_graph = provided.copy_graph(graph)
    number_nodes = len(new_graph.keys())
    degree_sets = []
    for dummy in range(number_nodes):
        degree_sets.append(set([]))
    for node in new_graph.keys():
        #print node, new_graph[node]
        degree = len(new_graph[node])
        #print degree
        degree_sets[degree].update([node])
        #print  '*****'
    #for item in degree_sets:
        #print item
    order = []
    for degree in range(number_nodes -1, -1, -1):
        #print 'Degree: ', degree
        while degree_sets[degree]:
            #print new_graph
            node = degree_sets[degree].pop()
            #print type(new_graph[node])
            #print new_graph#[node]
            for neighbor in new_graph[node]:
                neighbor_degree = len(new_graph[neighbor])
                degree_sets[neighbor_degree].remove(neighbor)
                degree_sets[neighbor_degree - 1].update([neighbor])
            order.append(node)
            provided.delete_node(new_graph,node)
    return order
예제 #2
0
def fast_targeted_order(ugraph):
    """
    Compute a targeted attack order consisting of nodes of maximal degree.
    """
    n = len(ugraph)
    new_graph = app2.copy_graph(ugraph)
    degree_sets = {}
    for node, neighbors in ugraph.iteritems():
        degree_sets.setdefault(len(neighbors), set()).add(node)
    order = []
    i = 0
    for k in range(n-1, 0, -1):
        if degree_sets.has_key(k):
            while len(degree_sets[k]) != 0:
                max_degree_node = degree_sets[k].pop()
                for neighbor in new_graph[max_degree_node]:
                    d = len(new_graph[neighbor])
                    degree_sets[d].remove(neighbor)
                    degree_sets.setdefault(d-1, set()).add(neighbor)
                order.append(max_degree_node)
                prj2.delete_node(new_graph, max_degree_node)
                i += 1
    if i < n:
        order.extend(new_graph.keys())
    return order
예제 #3
0
def fast_targeted_order(ugraph):
    """
    Compute a targeted attack order consisting of nodes of maximal degree.
    """
    n = len(ugraph)
    new_graph = app2.copy_graph(ugraph)
    degree_sets = {}
    for node, neighbors in ugraph.iteritems():
        degree_sets.setdefault(len(neighbors), set()).add(node)
    order = []
    i = 0
    for k in range(n - 1, 0, -1):
        if degree_sets.has_key(k):
            while len(degree_sets[k]) != 0:
                max_degree_node = degree_sets[k].pop()
                for neighbor in new_graph[max_degree_node]:
                    d = len(new_graph[neighbor])
                    degree_sets[d].remove(neighbor)
                    degree_sets.setdefault(d - 1, set()).add(neighbor)
                order.append(max_degree_node)
                prj2.delete_node(new_graph, max_degree_node)
                i += 1
    if i < n:
        order.extend(new_graph.keys())
    return order
def fast_targeted_order(ugraph):

	ugraph = provided.copy_graph(ugraph)

	n = len(ugraph)

	degree_sets = dict()

	for k in range(n):
		degree_sets[k] = set([])

	for node in range(n):
		node_degree = len(ugraph[node])
		degree_sets[node_degree].add(node)

	L = list()

	for k in range(n - 1, -1, -1):
		while len(degree_sets[k]) > 0:
			u = degree_sets[k].pop()

			for neighbor in ugraph[u]:
				degree = len(ugraph[neighbor])  
				degree_sets[degree].remove(neighbor)
				degree_sets[degree-1].add(neighbor)

			L.append(u)
			provided.delete_node(ugraph, u)

	return L
예제 #5
0
def compute_resilience(ugraph, attack_order):
    '''Compute graph resilience (size of the largest connected component)
    after removing each of provided nodes.'''
    ugraph = alg_application2_provided.copy_graph(ugraph)
    resilience = [largest_cc_size(ugraph)]
    for node in attack_order:
        remove_node(ugraph, node)
        resilience.append(largest_cc_size(ugraph))
    return resilience
예제 #6
0
def compute_resilience(ugraph, attack_order):
    '''Compute graph resilience (size of the largest connected component)
    after removing each of provided nodes.'''
    ugraph = alg_application2_provided.copy_graph(ugraph)
    resilience = [largest_cc_size(ugraph)]
    for node in attack_order:
        remove_node(ugraph, node)
        resilience.append(largest_cc_size(ugraph))
    return resilience
def attack(graph, attack_type):
    new_graph = provided.copy_graph(graph)
    if attack_type == 'normal':
        order = provided.targeted_order(new_graph)
    elif attack_type == 'fast':
        order = fastTargetedOrder(new_graph)
    resilience = pa2.compute_resilience(new_graph, order)
    return resilience
    
#t0 = time.time()
#normal_resilience = attack(upa, 'normal') 
#normal_time = time.time() - t0
#
#t0 = time.time()
#fast_resilience = attack(upa, 'fast')
#fast_time = time.time() - t0   
##
#print 'Normal attack time: ', normal_time
#print 'Fast attack time: ', fast_time
    
#print fastTargetedOrder(create_UPA(25,3))
def fast_targeted_order(ugraph):
    ugraph = alg_application2_provided.copy_graph(ugraph)
    N = len(ugraph)
    degree_sets = [set()] * N
    for node, neighbors in ugraph.iteritems():
        degree = len(neighbors)
        degree_sets[degree].add(node)
    order = []

    for k in range(N - 1, -1, -1):
        while degree_sets[k]:
            u = degree_sets[k].pop()
            for neighbor in ugraph[u]:
                d = len(ugraph[neighbor])
                degree_sets[d].remove(neighbor)
                degree_sets[d - 1].add(neighbor)

            order.append(u)
            project2.remove_node(ugraph, u)

    return order
예제 #9
0
def fast_targeted_order(ugraph):
    ugraph = alg_application2_provided.copy_graph(ugraph)
    N = len(ugraph)
    degree_sets = [set()] * N
    for node, neighbors in ugraph.iteritems():
        degree = len(neighbors)
        degree_sets[degree].add(node)
    order = []

    for k in range(N - 1, -1, -1):
        while degree_sets[k]:
            u = degree_sets[k].pop()
            for neighbor in ugraph[u]:
                d = len(ugraph[neighbor])
                degree_sets[d].remove(neighbor)
                degree_sets[d - 1].add(neighbor)

            order.append(u)
            module2.remove_node(ugraph, u)

    return order
예제 #10
0
def fast_targeted_order(ugraph):
    new_graph = alg_application2_provided.copy_graph(ugraph)
    DegreeSets = degreesets(new_graph)
    #print DegreeSets
    #print new_graph
    L = list()
    i = 0
    for k in range(len(new_graph)-1,-1,-1):
        while DegreeSets[k] != set():
            u = DegreeSets[k].pop()     
            #print u       
            #print new_graph[u]
            for neighbor in new_graph[u]:                
                d = len(new_graph[neighbor])               
                DegreeSets[d].remove(neighbor)
                DegreeSets[d-1].add(neighbor)   
                #print "DegreeSets", DegreeSets
            L.insert(i,u)            
            i = i + 1
            #if new_graph.get(u): 
            alg_application2_provided.delete_node(new_graph,u)
    return L
예제 #11
0
#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 = random_order(cn_graph)
er_attack_order = random_order(er_graph)
upa_attack_order = random_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)

#making comparison plots
fig, ax = plt.subplots()

print len(cn_resilience)
x_vals = [idx for idx in range(len(cn_graph.keys()) + 1)]
print len(x_vals)

ax.plot(x_vals, cn_resilience, '-b', label = 'computer network')
ax.plot(x_vals, er_resilience, '-r', label = 'ER graph (p = 0.002)')
ax.plot(x_vals, upa_resilience, '-g', label = 'UPA graph (m = 2)')
ax.legend(loc = 'upper right')
ax.set_title('Resiliences of different models ')
import algo_app2_1 as app2
import alg_application2_provided as provided
import algo_pa2 as pa2
import pickle

create_upas = []
normal_time = []
fast_time = []

for n in range(10, 1000, 10):
    print n
    t0 = time.time()
    upa = app2.create_UPA(n, 5)
    create_upas.append(time.time() - t0)
    #print upa
    use_graph = provided.copy_graph(upa)

    t0 = time.time()
    order = provided.targeted_order(use_graph)
    #normal_resilience = pa2.compute_resilience(use_graph, order)
    #normal_resilience = app2.attack(upa, 'normal')
    normal_time.append(time.time() - t0)

    use_graph = provided.copy_graph(upa)

    t0 = time.time()
    order = app2.fastTargetedOrder(use_graph)
    #fast_resilience = pa2.compute_resilience(use_graph, order)
    #fast_resilience = app2.attack(upa, 'fast')
    fast_time.append(time.time() - t0)