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
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
def compute_resilience(ugraph, attack_order):
    '''
    Graph resilience
    In the Application component of this Module, we will study the connectivity of computer networks.
    In particular, we will subject a model of one particular network to random and targeted "attacks".
    These attacks correspond to disabling a sequence of servers in the network and will be simulated by removing a sequence of nodes in the graph that corresponds to these servers.

    For this part of the Project, your task is to implement a function that takes an undirected graph and a list of nodes that will be attacked.
    You will remove these nodes (and their edges) from the graph one at a time and then measure the "resilience" of the graph at each
    removal by computing the size of its largest remaining connected component.
    In particular, your task is to implement the function:

    compute_resilience(ugraph, attack_order) - Takes the undirected graph ugraph, a list of nodes attack_order and iterates through the nodes in attack_order.
    For each node in the list, the function removes the given node and its edges from the graph and then computes the size of the largest connected component for the resulting graph.
    The function should return a list whose k+1th entry is the size of the largest connected component in the graph after the removal of the first k nodes in attack_order.
    The first entry (indexed by zero) is the size of the largest connected component in the original graph.

    The easiest method for implementing compute_resilience is to remove one node at a time and use largest_cc_size
    to compute the size of the largest connected component in the resulting graphs.
    This implementation has a running time of O(n(n+m)) where n is the number of nodes and m is the number of edges in the graph.
    '''
    
    graph_copy = dict(ugraph)
    largest_node = [largest_cc_size(graph_copy)]
    for attack_node in attack_order:
        app2_prov.delete_node(graph_copy, attack_node)
        largest_node.append(largest_cc_size(graph_copy))

    return largest_node
Beispiel #4
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
def fast_targeted_order(ugraph):
    """
    Computes an attack order for calculating the resilience of a graph where, 
    in every iteration, a node of the maximum degree is targeted
    
    ugraph = an undirected graph
    
    Returns attack_order = an ordered list of nodes
    """
    # Initializing the list of sets of nodes by degree
    degree_sets = [set() for dummy_idx in ugraph.keys()]
    
    # Populating the list of sets
    for i_node in ugraph.keys():
        i_degree = len(ugraph[i_node])
        degree_sets[i_degree].add(i_node)
    
    # Initializing other variables
    graph = dict(ugraph)
    attack_list = []
    index = 0
    
    # Iterates through degree_sets in decreasing degree order, adds one of the 
    # top degree nodes left to the list and updates the list and the ugraph
    # before proceeding to the next iteration
    for k_degree in range(len(graph) - 1, -1, -1):
        while degree_sets[k_degree]:
            u_node = random.choice(list(degree_sets[k_degree]))
            attack_list.append(u_node)
            degree_sets[k_degree].remove(u_node)
            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